From 4460a7e90c318857a37be386a7ebccff3020a795 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Tue, 18 Nov 2008 21:13:41 +0000 Subject: [PATCH] Remove even more llvm.dbg variables. Remove all dead globals from llvm.metadata. Ignore linkonce linkage for selected llvm.dbg values. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@59547 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/IPO/StripSymbols.cpp | 83 +++++++++++++++++------------ 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/lib/Transforms/IPO/StripSymbols.cpp b/lib/Transforms/IPO/StripSymbols.cpp index a073ccea959..d25e7580883 100644 --- a/lib/Transforms/IPO/StripSymbols.cpp +++ b/lib/Transforms/IPO/StripSymbols.cpp @@ -114,20 +114,15 @@ bool StripSymbols::runOnModule(Module &M) { return Changed; } - // Strip the symbol table of its names. static void StripTypeSymtab(TypeSymbolTable &ST) { for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) ST.remove(TI++); } -/// StripSymbolNames - Strip symbol names. -bool StripSymbols::StripSymbolNames(Module &M) { - - if (OnlyDebugInfo) - return false; - - SmallPtrSet llvmUsedValues; +/// Find values that are marked as llvm.used. +void findUsedValues(Module &M, + SmallPtrSet& llvmUsedValues) { if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) { llvmUsedValues.insert(LLVMUsed); // Collect values that are preserved as per explicit request. @@ -145,7 +140,17 @@ bool StripSymbols::StripSymbolNames(Module &M) { } } } - +} + +/// StripSymbolNames - Strip symbol names. +bool StripSymbols::StripSymbolNames(Module &M) { + + if (OnlyDebugInfo) + return false; + + SmallPtrSet llvmUsedValues; + findUsedValues(M, llvmUsedValues); + for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) { if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0) @@ -175,7 +180,7 @@ bool StripSymbols::StripDebugInfo(Module &M) { Function *RegionEnd = M.getFunction("llvm.dbg.region.end"); Function *Declare = M.getFunction("llvm.dbg.declare"); - std::vector DeadGlobals; + std::vector DeadConstants; // Remove all of the calls to the debugger intrinsics, and remove them from // the module. @@ -186,8 +191,8 @@ bool StripSymbols::StripDebugInfo(Module &M) { assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); CI->eraseFromParent(); if (Arg->use_empty()) - if (GlobalVariable *GV = dyn_cast(Arg)) - DeadGlobals.push_back(GV); + if (Constant *C = dyn_cast(Arg)) + DeadConstants.push_back(C); } FuncStart->eraseFromParent(); } @@ -198,8 +203,8 @@ bool StripSymbols::StripDebugInfo(Module &M) { assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); CI->eraseFromParent(); if (Arg->use_empty()) - if (GlobalVariable *GV = dyn_cast(Arg)) - DeadGlobals.push_back(GV); + if (Constant *C = dyn_cast(Arg)) + DeadConstants.push_back(C); } StopPoint->eraseFromParent(); } @@ -210,8 +215,8 @@ bool StripSymbols::StripDebugInfo(Module &M) { assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); CI->eraseFromParent(); if (Arg->use_empty()) - if (GlobalVariable *GV = dyn_cast(Arg)) - DeadGlobals.push_back(GV); + if (Constant *C = dyn_cast(Arg)) + DeadConstants.push_back(C); } RegionStart->eraseFromParent(); } @@ -222,8 +227,8 @@ bool StripSymbols::StripDebugInfo(Module &M) { assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); CI->eraseFromParent(); if (Arg->use_empty()) - if (GlobalVariable *GV = dyn_cast(Arg)) - DeadGlobals.push_back(GV); + if (Constant *C = dyn_cast(Arg)) + DeadConstants.push_back(C); } RegionEnd->eraseFromParent(); } @@ -234,12 +239,15 @@ bool StripSymbols::StripDebugInfo(Module &M) { assert(CI->use_empty() && "llvm.dbg intrinsic should have void result"); CI->eraseFromParent(); if (Arg->use_empty()) - if (GlobalVariable *GV = dyn_cast(Arg)) - DeadGlobals.push_back(GV); + if (Constant *C = dyn_cast(Arg)) + DeadConstants.push_back(C); } Declare->eraseFromParent(); } + SmallPtrSet llvmUsedValues; + findUsedValues(M, llvmUsedValues); + // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce // but since we are removing all debug information, make them internal now. if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units")) @@ -249,29 +257,38 @@ bool StripSymbols::StripDebugInfo(Module &M) { if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms")) if (GlobalVariable *GV = dyn_cast(C)) GV->setLinkage(GlobalValue::InternalLinkage); + + if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables")) + if (GlobalVariable *GV = dyn_cast(C)) + GV->setLinkage(GlobalValue::InternalLinkage); // Delete all dbg variables. const Type *DbgVTy = M.getTypeByName("llvm.dbg.variable.type"); const Type *DbgGVTy = M.getTypeByName("llvm.dbg.global_variable.type"); if (DbgVTy || DbgGVTy) for (Module::global_iterator I = M.global_begin(), E = M.global_end(); - I != E; ++I) - if (GlobalVariable *GV = dyn_cast(I)) - if (GV->hasName() && GV->use_empty() - && !strncmp(GV->getNameStart(), "llvm.dbg", 8) - && (GV->getType()->getElementType() == DbgVTy - || GV->getType()->getElementType() == DbgGVTy)) - DeadGlobals.push_back(GV); + I != E; ++I) { + GlobalVariable *GV = dyn_cast(I); + if (!GV) continue; + if (GV->use_empty() && llvmUsedValues.count(I) == 0 + && (!GV->hasSection() + || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0)) + DeadConstants.push_back(GV); + } - if (DeadGlobals.empty()) + if (DeadConstants.empty()) return false; // Delete any internal globals that were only used by the debugger intrinsics. - while (!DeadGlobals.empty()) { - GlobalVariable *GV = DeadGlobals.back(); - DeadGlobals.pop_back(); - if (GV->hasInternalLinkage()) - RemoveDeadConstant(GV); + while (!DeadConstants.empty()) { + Constant *C = DeadConstants.back(); + DeadConstants.pop_back(); + if (GlobalVariable *GV = dyn_cast(C)) { + if (GV->hasInternalLinkage()) + RemoveDeadConstant(GV); + } + else + RemoveDeadConstant(C); } // Remove all llvm.dbg types.