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 {
77 class StripDeadDebugInfo : public ModulePass {
79 static char ID; // Pass identification, replacement for typeid
80 explicit StripDeadDebugInfo()
83 virtual bool runOnModule(Module &M);
85 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
91 char StripSymbols::ID = 0;
92 INITIALIZE_PASS(StripSymbols, "strip",
93 "Strip all symbols from a module", false, false)
95 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
96 return new StripSymbols(OnlyDebugInfo);
99 char StripNonDebugSymbols::ID = 0;
100 INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
101 "Strip all symbols, except dbg symbols, from a module",
104 ModulePass *llvm::createStripNonDebugSymbolsPass() {
105 return new StripNonDebugSymbols();
108 char StripDebugDeclare::ID = 0;
109 INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
110 "Strip all llvm.dbg.declare intrinsics", false, false)
112 ModulePass *llvm::createStripDebugDeclarePass() {
113 return new StripDebugDeclare();
116 char StripDeadDebugInfo::ID = 0;
117 INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
118 "Strip debug info for unused symbols", false, false)
120 ModulePass *llvm::createStripDeadDebugInfoPass() {
121 return new StripDeadDebugInfo();
124 /// OnlyUsedBy - Return true if V is only used by Usr.
125 static bool OnlyUsedBy(Value *V, Value *Usr) {
126 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
134 static void RemoveDeadConstant(Constant *C) {
135 assert(C->use_empty() && "Constant is not dead!");
136 SmallPtrSet<Constant*, 4> Operands;
137 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
138 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
139 OnlyUsedBy(C->getOperand(i), C))
140 Operands.insert(cast<Constant>(C->getOperand(i)));
141 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
142 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
143 GV->eraseFromParent();
145 else if (!isa<Function>(C))
146 if (isa<CompositeType>(C->getType()))
147 C->destroyConstant();
149 // If the constant referenced anything, see if we can delete it as well.
150 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
151 OE = Operands.end(); OI != OE; ++OI)
152 RemoveDeadConstant(*OI);
155 // Strip the symbol table of its names.
157 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
158 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
159 Value *V = VI->getValue();
161 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
162 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
163 // Set name to "", removing from symbol table!
169 // Strip the symbol table of its names.
170 static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
171 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
172 if (PreserveDbgInfo && StringRef(TI->first).startswith("llvm.dbg"))
179 /// Find values that are marked as llvm.used.
180 static void findUsedValues(GlobalVariable *LLVMUsed,
181 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
182 if (LLVMUsed == 0) return;
183 UsedValues.insert(LLVMUsed);
185 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
186 if (Inits == 0) return;
188 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
189 if (GlobalValue *GV =
190 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
191 UsedValues.insert(GV);
194 /// StripSymbolNames - Strip symbol names.
195 static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
197 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
198 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
199 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
201 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
203 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
204 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
205 I->setName(""); // Internal symbols can't participate in linkage
208 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
209 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
210 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
211 I->setName(""); // Internal symbols can't participate in linkage
212 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
215 // Remove all names from types.
216 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
221 // StripDebugInfo - Strip debug info in the module if it exists.
222 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
223 // llvm.dbg.region.end calls, and any globals they point to if now dead.
224 static bool StripDebugInfo(Module &M) {
226 bool Changed = false;
228 // Remove all of the calls to the debugger intrinsics, and remove them from
230 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
231 while (!Declare->use_empty()) {
232 CallInst *CI = cast<CallInst>(Declare->use_back());
233 CI->eraseFromParent();
235 Declare->eraseFromParent();
239 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
240 while (!DbgVal->use_empty()) {
241 CallInst *CI = cast<CallInst>(DbgVal->use_back());
242 CI->eraseFromParent();
244 DbgVal->eraseFromParent();
248 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
249 NME = M.named_metadata_end(); NMI != NME;) {
250 NamedMDNode *NMD = NMI;
252 if (NMD->getName().startswith("llvm.dbg.")) {
253 NMD->eraseFromParent();
258 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
259 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
261 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
263 if (!BI->getDebugLoc().isUnknown()) {
265 BI->setDebugLoc(DebugLoc());
272 bool StripSymbols::runOnModule(Module &M) {
273 bool Changed = false;
274 Changed |= StripDebugInfo(M);
276 Changed |= StripSymbolNames(M, false);
280 bool StripNonDebugSymbols::runOnModule(Module &M) {
281 return StripSymbolNames(M, true);
284 bool StripDebugDeclare::runOnModule(Module &M) {
286 Function *Declare = M.getFunction("llvm.dbg.declare");
287 std::vector<Constant*> DeadConstants;
290 while (!Declare->use_empty()) {
291 CallInst *CI = cast<CallInst>(Declare->use_back());
292 Value *Arg1 = CI->getArgOperand(0);
293 Value *Arg2 = CI->getArgOperand(1);
294 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
295 CI->eraseFromParent();
296 if (Arg1->use_empty()) {
297 if (Constant *C = dyn_cast<Constant>(Arg1))
298 DeadConstants.push_back(C);
300 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
302 if (Arg2->use_empty())
303 if (Constant *C = dyn_cast<Constant>(Arg2))
304 DeadConstants.push_back(C);
306 Declare->eraseFromParent();
309 while (!DeadConstants.empty()) {
310 Constant *C = DeadConstants.back();
311 DeadConstants.pop_back();
312 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
313 if (GV->hasLocalLinkage())
314 RemoveDeadConstant(GV);
316 RemoveDeadConstant(C);
322 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
323 /// printer to not emit usual symbol prefix before the symbol name is used then
324 /// return linkage name after skipping this special LLVM prefix.
325 static StringRef getRealLinkageName(StringRef LinkageName) {
327 if (LinkageName.startswith(StringRef(&One, 1)))
328 return LinkageName.substr(1);
332 bool StripDeadDebugInfo::runOnModule(Module &M) {
333 bool Changed = false;
335 // Debugging infomration is encoded in llvm IR using metadata. This is designed
336 // such a way that debug info for symbols preserved even if symbols are
337 // optimized away by the optimizer. This special pass removes debug info for
340 // llvm.dbg.gv keeps track of debug info for global variables.
341 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
342 SmallVector<MDNode *, 8> MDs;
343 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
344 if (DIGlobalVariable(NMD->getOperand(i)).Verify())
345 MDs.push_back(NMD->getOperand(i));
348 NMD->eraseFromParent();
351 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
352 E = MDs.end(); I != E; ++I) {
353 GlobalVariable *GV = DIGlobalVariable(*I).getGlobal();
354 if (GV && M.getGlobalVariable(GV->getName(), true)) {
356 NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
364 // llvm.dbg.sp keeps track of debug info for subprograms.
365 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
366 SmallVector<MDNode *, 8> MDs;
367 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
368 if (DISubprogram(NMD->getOperand(i)).Verify())
369 MDs.push_back(NMD->getOperand(i));
372 NMD->eraseFromParent();
375 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
376 E = MDs.end(); I != E; ++I) {
377 bool FnIsLive = false;
378 if (Function *F = DISubprogram(*I).getFunction())
379 if (M.getFunction(F->getName()))
383 NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
386 // Remove llvm.dbg.lv.fnname named mdnode which may have been used
387 // to hold debug info for dead function's local variables.
388 StringRef FName = DISubprogram(*I).getLinkageName();
390 FName = DISubprogram(*I).getName();
391 if (NamedMDNode *LVNMD =
392 M.getNamedMetadata(Twine("llvm.dbg.lv.",
393 getRealLinkageName(FName))))
394 LVNMD->eraseFromParent();