1 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The StripSymbols transformation implements code stripping. Specifically, it
13 // * names for virtual registers
14 // * symbols for internal globals and functions
15 // * debug information
17 // Note that this transformation makes code much less readable, so it should
18 // only be used in situations where the 'strip' utility would be used, such as
19 // reducing code size or making it harder to reverse engineer code.
21 //===----------------------------------------------------------------------===//
23 #include "llvm/Transforms/IPO.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/LLVMContext.h"
28 #include "llvm/Module.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Analysis/DebugInfo.h"
31 #include "llvm/ValueSymbolTable.h"
32 #include "llvm/TypeSymbolTable.h"
33 #include "llvm/Transforms/Utils/Local.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/ADT/SmallPtrSet.h"
39 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
42 static char ID; // Pass identification, replacement for typeid
43 explicit StripSymbols(bool ODI = false)
44 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
46 virtual bool runOnModule(Module &M);
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
55 static char ID; // Pass identification, replacement for typeid
56 explicit StripNonDebugSymbols()
59 virtual bool runOnModule(Module &M);
61 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
66 class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
68 static char ID; // Pass identification, replacement for typeid
69 explicit StripDebugDeclare()
72 virtual bool runOnModule(Module &M);
74 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
80 char StripSymbols::ID = 0;
81 static RegisterPass<StripSymbols>
82 X("strip", "Strip all symbols from a module");
84 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
85 return new StripSymbols(OnlyDebugInfo);
88 char StripNonDebugSymbols::ID = 0;
89 static RegisterPass<StripNonDebugSymbols>
90 Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
92 ModulePass *llvm::createStripNonDebugSymbolsPass() {
93 return new StripNonDebugSymbols();
96 char StripDebugDeclare::ID = 0;
97 static RegisterPass<StripDebugDeclare>
98 Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
100 ModulePass *llvm::createStripDebugDeclarePass() {
101 return new StripDebugDeclare();
104 /// OnlyUsedBy - Return true if V is only used by Usr.
105 static bool OnlyUsedBy(Value *V, Value *Usr) {
106 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
114 static void RemoveDeadConstant(Constant *C) {
115 assert(C->use_empty() && "Constant is not dead!");
116 SmallPtrSet<Constant *, 4> Operands;
117 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
118 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
119 OnlyUsedBy(C->getOperand(i), C))
120 Operands.insert(C->getOperand(i));
121 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
122 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
123 GV->eraseFromParent();
125 else if (!isa<Function>(C))
126 if (isa<CompositeType>(C->getType()))
127 C->destroyConstant();
129 // If the constant referenced anything, see if we can delete it as well.
130 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
131 OE = Operands.end(); OI != OE; ++OI)
132 RemoveDeadConstant(*OI);
135 // Strip the symbol table of its names.
137 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
138 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
139 Value *V = VI->getValue();
141 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
142 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
143 // Set name to "", removing from symbol table!
149 // Strip the symbol table of its names.
150 static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
151 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
152 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
159 /// Find values that are marked as llvm.used.
160 static void findUsedValues(GlobalVariable *LLVMUsed,
161 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
162 if (LLVMUsed == 0) return;
163 UsedValues.insert(LLVMUsed);
165 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
166 if (Inits == 0) return;
168 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
169 if (GlobalValue *GV =
170 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
171 UsedValues.insert(GV);
174 /// StripSymbolNames - Strip symbol names.
175 static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
177 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
178 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
179 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
181 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
183 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
184 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
185 I->setName(""); // Internal symbols can't participate in linkage
188 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
189 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
190 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
191 I->setName(""); // Internal symbols can't participate in linkage
192 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
195 // Remove all names from types.
196 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
201 // StripDebugInfo - Strip debug info in the module if it exists.
202 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
203 // llvm.dbg.region.end calls, and any globals they point to if now dead.
204 static bool StripDebugInfo(Module &M) {
206 // Remove all of the calls to the debugger intrinsics, and remove them from
208 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
209 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
210 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
211 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
212 Function *Declare = M.getFunction("llvm.dbg.declare");
215 while (!FuncStart->use_empty()) {
216 CallInst *CI = cast<CallInst>(FuncStart->use_back());
217 CI->eraseFromParent();
219 FuncStart->eraseFromParent();
222 while (!StopPoint->use_empty()) {
223 CallInst *CI = cast<CallInst>(StopPoint->use_back());
224 CI->eraseFromParent();
226 StopPoint->eraseFromParent();
229 while (!RegionStart->use_empty()) {
230 CallInst *CI = cast<CallInst>(RegionStart->use_back());
231 CI->eraseFromParent();
233 RegionStart->eraseFromParent();
236 while (!RegionEnd->use_empty()) {
237 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
238 CI->eraseFromParent();
240 RegionEnd->eraseFromParent();
243 while (!Declare->use_empty()) {
244 CallInst *CI = cast<CallInst>(Declare->use_back());
245 CI->eraseFromParent();
247 Declare->eraseFromParent();
250 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
252 NMD->eraseFromParent();
254 // Remove dead metadata.
255 M.getContext().RemoveDeadMetadata();
259 bool StripSymbols::runOnModule(Module &M) {
260 bool Changed = false;
261 Changed |= StripDebugInfo(M);
263 Changed |= StripSymbolNames(M, false);
267 bool StripNonDebugSymbols::runOnModule(Module &M) {
268 return StripSymbolNames(M, true);
271 bool StripDebugDeclare::runOnModule(Module &M) {
273 Function *Declare = M.getFunction("llvm.dbg.declare");
274 std::vector<Constant*> DeadConstants;
277 while (!Declare->use_empty()) {
278 CallInst *CI = cast<CallInst>(Declare->use_back());
279 Value *Arg1 = CI->getOperand(1);
280 Value *Arg2 = CI->getOperand(2);
281 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
282 CI->eraseFromParent();
283 if (Arg1->use_empty()) {
284 if (Constant *C = dyn_cast<Constant>(Arg1))
285 DeadConstants.push_back(C);
287 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
289 if (Arg2->use_empty())
290 if (Constant *C = dyn_cast<Constant>(Arg2))
291 DeadConstants.push_back(C);
293 Declare->eraseFromParent();
296 // Delete all llvm.dbg.global_variables.
297 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
299 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
301 if (GV->use_empty() && GV->getName().startswith("llvm.dbg.global_variable"))
302 DeadConstants.push_back(GV);
305 while (!DeadConstants.empty()) {
306 Constant *C = DeadConstants.back();
307 DeadConstants.pop_back();
308 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
309 if (GV->hasLocalLinkage())
310 RemoveDeadConstant(GV);
313 RemoveDeadConstant(C);