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/Module.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Analysis/DebugInfo.h"
30 #include "llvm/ValueSymbolTable.h"
31 #include "llvm/TypeSymbolTable.h"
32 #include "llvm/Transforms/Utils/Local.h"
33 #include "llvm/ADT/SmallPtrSet.h"
37 class StripSymbols : public ModulePass {
40 static char ID; // Pass identification, replacement for typeid
41 explicit StripSymbols(bool ODI = false)
42 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
44 virtual bool runOnModule(Module &M);
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51 class StripNonDebugSymbols : public ModulePass {
53 static char ID; // Pass identification, replacement for typeid
54 explicit StripNonDebugSymbols()
57 virtual bool runOnModule(Module &M);
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 class StripDebugDeclare : public ModulePass {
66 static char ID; // Pass identification, replacement for typeid
67 explicit StripDebugDeclare()
70 virtual bool runOnModule(Module &M);
72 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
78 char StripSymbols::ID = 0;
79 static RegisterPass<StripSymbols>
80 X("strip", "Strip all symbols from a module");
82 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
83 return new StripSymbols(OnlyDebugInfo);
86 char StripNonDebugSymbols::ID = 0;
87 static RegisterPass<StripNonDebugSymbols>
88 Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
90 ModulePass *llvm::createStripNonDebugSymbolsPass() {
91 return new StripNonDebugSymbols();
94 char StripDebugDeclare::ID = 0;
95 static RegisterPass<StripDebugDeclare>
96 Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
98 ModulePass *llvm::createStripDebugDeclarePass() {
99 return new StripDebugDeclare();
102 /// OnlyUsedBy - Return true if V is only used by Usr.
103 static bool OnlyUsedBy(Value *V, Value *Usr) {
104 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
112 static void RemoveDeadConstant(Constant *C) {
113 assert(C->use_empty() && "Constant is not dead!");
114 SmallPtrSet<Constant*, 4> Operands;
115 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
116 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
117 OnlyUsedBy(C->getOperand(i), C))
118 Operands.insert(cast<Constant>(C->getOperand(i)));
119 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
120 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
121 GV->eraseFromParent();
123 else if (!isa<Function>(C))
124 if (isa<CompositeType>(C->getType()))
125 C->destroyConstant();
127 // If the constant referenced anything, see if we can delete it as well.
128 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
129 OE = Operands.end(); OI != OE; ++OI)
130 RemoveDeadConstant(*OI);
133 // Strip the symbol table of its names.
135 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
136 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
137 Value *V = VI->getValue();
139 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
140 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
141 // Set name to "", removing from symbol table!
147 // Strip the symbol table of its names.
148 static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
149 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
150 if (PreserveDbgInfo && StringRef(TI->first).startswith("llvm.dbg"))
157 /// Find values that are marked as llvm.used.
158 static void findUsedValues(GlobalVariable *LLVMUsed,
159 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
160 if (LLVMUsed == 0) return;
161 UsedValues.insert(LLVMUsed);
163 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
164 if (Inits == 0) return;
166 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
167 if (GlobalValue *GV =
168 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
169 UsedValues.insert(GV);
172 /// StripSymbolNames - Strip symbol names.
173 static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
175 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
176 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
177 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
179 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
181 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
182 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
183 I->setName(""); // Internal symbols can't participate in linkage
186 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
187 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
188 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
189 I->setName(""); // Internal symbols can't participate in linkage
190 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
193 // Remove all names from types.
194 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
199 // StripDebugInfo - Strip debug info in the module if it exists.
200 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
201 // llvm.dbg.region.end calls, and any globals they point to if now dead.
202 static bool StripDebugInfo(Module &M) {
204 bool Changed = false;
206 // Remove all of the calls to the debugger intrinsics, and remove them from
208 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
209 while (!Declare->use_empty()) {
210 CallInst *CI = cast<CallInst>(Declare->use_back());
211 CI->eraseFromParent();
213 Declare->eraseFromParent();
217 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
218 while (!DbgVal->use_empty()) {
219 CallInst *CI = cast<CallInst>(DbgVal->use_back());
220 CI->eraseFromParent();
222 DbgVal->eraseFromParent();
226 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
229 NMD->eraseFromParent();
232 unsigned MDDbgKind = M.getMDKindID("dbg");
233 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
234 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
236 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
238 BI->setMetadata(MDDbgKind, 0);
243 bool StripSymbols::runOnModule(Module &M) {
244 bool Changed = false;
245 Changed |= StripDebugInfo(M);
247 Changed |= StripSymbolNames(M, false);
251 bool StripNonDebugSymbols::runOnModule(Module &M) {
252 return StripSymbolNames(M, true);
255 bool StripDebugDeclare::runOnModule(Module &M) {
257 Function *Declare = M.getFunction("llvm.dbg.declare");
258 std::vector<Constant*> DeadConstants;
261 while (!Declare->use_empty()) {
262 CallInst *CI = cast<CallInst>(Declare->use_back());
263 Value *Arg1 = CI->getOperand(1);
264 Value *Arg2 = CI->getOperand(2);
265 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
266 CI->eraseFromParent();
267 if (Arg1->use_empty()) {
268 if (Constant *C = dyn_cast<Constant>(Arg1))
269 DeadConstants.push_back(C);
271 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
273 if (Arg2->use_empty())
274 if (Constant *C = dyn_cast<Constant>(Arg2))
275 DeadConstants.push_back(C);
277 Declare->eraseFromParent();
280 while (!DeadConstants.empty()) {
281 Constant *C = DeadConstants.back();
282 DeadConstants.pop_back();
283 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
284 if (GV->hasLocalLinkage())
285 RemoveDeadConstant(GV);
287 RemoveDeadConstant(C);