blob: 55674614bce235ba2693d01f59a7d816b29ae126 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gordon Henriksen79ed5872007-11-04 16:15:04 +000010// The StripSymbols transformation implements code stripping. Specifically, it
11// can delete:
12//
13// * names for virtual registers
14// * symbols for internal globals and functions
15// * debug information
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016//
Gordon Henriksen79ed5872007-11-04 16:15:04 +000017// 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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020//
21//===----------------------------------------------------------------------===//
22
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"
Devang Patel166f8432009-06-26 01:49:18 +000029#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/ValueSymbolTable.h"
31#include "llvm/TypeSymbolTable.h"
Devang Patele68804a2009-03-03 21:31:02 +000032#include "llvm/Transforms/Utils/Local.h"
Devang Patel5dc8fde2008-01-16 03:33:05 +000033#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034using namespace llvm;
35
36namespace {
Nick Lewycky04791af2009-09-03 06:43:15 +000037 class StripSymbols : public ModulePass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 bool OnlyDebugInfo;
39 public:
40 static char ID; // Pass identification, replacement for typeid
Dan Gohman34c280e2007-08-01 15:32:29 +000041 explicit StripSymbols(bool ODI = false)
Dan Gohman26f8c272008-09-04 17:05:41 +000042 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043
Devang Patel159f7ce2008-11-18 21:34:39 +000044 virtual bool runOnModule(Module &M);
Devang Patel2c98b522008-11-14 22:49:37 +000045
Devang Patel159f7ce2008-11-18 21:34:39 +000046 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.setPreservesAll();
48 }
49 };
50
Nick Lewycky04791af2009-09-03 06:43:15 +000051 class StripNonDebugSymbols : public ModulePass {
Devang Patel159f7ce2008-11-18 21:34:39 +000052 public:
53 static char ID; // Pass identification, replacement for typeid
54 explicit StripNonDebugSymbols()
55 : ModulePass(&ID) {}
Devang Patel2c98b522008-11-14 22:49:37 +000056
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 virtual bool runOnModule(Module &M);
58
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesAll();
61 }
62 };
Devang Patel3876c8a2009-03-09 20:49:37 +000063
Nick Lewycky04791af2009-09-03 06:43:15 +000064 class StripDebugDeclare : public ModulePass {
Devang Patel3876c8a2009-03-09 20:49:37 +000065 public:
66 static char ID; // Pass identification, replacement for typeid
67 explicit StripDebugDeclare()
68 : ModulePass(&ID) {}
69
70 virtual bool runOnModule(Module &M);
71
72 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.setPreservesAll();
74 }
75 };
Devang Patele524efe2010-07-01 19:49:20 +000076
77 class StripDeadDebugInfo : public ModulePass {
78 public:
79 static char ID; // Pass identification, replacement for typeid
80 explicit StripDeadDebugInfo()
81 : ModulePass(&ID) {}
82
83 virtual bool runOnModule(Module &M);
84
85 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
86 AU.setPreservesAll();
87 }
88 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089}
90
Dan Gohman089efff2008-05-13 00:00:25 +000091char StripSymbols::ID = 0;
Owen Anderson6374c3d2010-07-21 22:09:45 +000092INITIALIZE_PASS(StripSymbols, "strip",
93 "Strip all symbols from a module", false, false);
Dan Gohman089efff2008-05-13 00:00:25 +000094
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
96 return new StripSymbols(OnlyDebugInfo);
97}
98
Devang Patel159f7ce2008-11-18 21:34:39 +000099char StripNonDebugSymbols::ID = 0;
Owen Anderson6374c3d2010-07-21 22:09:45 +0000100INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
101 "Strip all symbols, except dbg symbols, from a module",
102 false, false);
Devang Patel159f7ce2008-11-18 21:34:39 +0000103
104ModulePass *llvm::createStripNonDebugSymbolsPass() {
105 return new StripNonDebugSymbols();
106}
107
Devang Patel3876c8a2009-03-09 20:49:37 +0000108char StripDebugDeclare::ID = 0;
Owen Anderson6374c3d2010-07-21 22:09:45 +0000109INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
110 "Strip all llvm.dbg.declare intrinsics", false, false);
Devang Patel3876c8a2009-03-09 20:49:37 +0000111
112ModulePass *llvm::createStripDebugDeclarePass() {
113 return new StripDebugDeclare();
114}
115
Devang Patele524efe2010-07-01 19:49:20 +0000116char StripDeadDebugInfo::ID = 0;
Owen Anderson6374c3d2010-07-21 22:09:45 +0000117INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
118 "Strip debug info for unused symbols", false, false);
Devang Patele524efe2010-07-01 19:49:20 +0000119
120ModulePass *llvm::createStripDeadDebugInfoPass() {
121 return new StripDeadDebugInfo();
122}
123
Devang Patel2fa85562008-11-13 01:28:40 +0000124/// OnlyUsedBy - Return true if V is only used by Usr.
125static bool OnlyUsedBy(Value *V, Value *Usr) {
126 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
127 User *U = *I;
128 if (U != Usr)
129 return false;
130 }
131 return true;
132}
133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134static void RemoveDeadConstant(Constant *C) {
135 assert(C->use_empty() && "Constant is not dead!");
Chris Lattner67abb532009-10-28 05:14:34 +0000136 SmallPtrSet<Constant*, 4> Operands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
138 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patel2fa85562008-11-13 01:28:40 +0000139 OnlyUsedBy(C->getOperand(i), C))
Chris Lattner67abb532009-10-28 05:14:34 +0000140 Operands.insert(cast<Constant>(C->getOperand(i)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000142 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 GV->eraseFromParent();
144 }
145 else if (!isa<Function>(C))
Devang Patelabd69112008-11-20 01:20:42 +0000146 if (isa<CompositeType>(C->getType()))
147 C->destroyConstant();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148
149 // If the constant referenced anything, see if we can delete it as well.
Chris Lattner67abb532009-10-28 05:14:34 +0000150 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
Devang Patel2fa85562008-11-13 01:28:40 +0000151 OE = Operands.end(); OI != OE; ++OI)
152 RemoveDeadConstant(*OI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153}
154
155// Strip the symbol table of its names.
156//
Devang Patel159f7ce2008-11-18 21:34:39 +0000157static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
159 Value *V = VI->getValue();
160 ++VI;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000161 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000162 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patel159f7ce2008-11-18 21:34:39 +0000163 // Set name to "", removing from symbol table!
164 V->setName("");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 }
166 }
167}
168
169// Strip the symbol table of its names.
Devang Patel159f7ce2008-11-18 21:34:39 +0000170static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
171 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
Benjamin Kramer95ba4162010-01-22 20:00:21 +0000172 if (PreserveDbgInfo && StringRef(TI->first).startswith("llvm.dbg"))
Devang Patel159f7ce2008-11-18 21:34:39 +0000173 ++TI;
174 else
175 ST.remove(TI++);
176 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177}
178
Devang Patel2d53e2d2008-11-18 21:13:41 +0000179/// Find values that are marked as llvm.used.
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000180static void findUsedValues(GlobalVariable *LLVMUsed,
181 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
182 if (LLVMUsed == 0) return;
183 UsedValues.insert(LLVMUsed);
184
185 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
186 if (Inits == 0) return;
187
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);
Devang Patel2d53e2d2008-11-18 21:13:41 +0000192}
193
194/// StripSymbolNames - Strip symbol names.
Dan Gohman6d29b322009-08-07 01:32:21 +0000195static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel2d53e2d2008-11-18 21:13:41 +0000196
197 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000198 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
199 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel2d53e2d2008-11-18 21:13:41 +0000200
Devang Patel2c98b522008-11-14 22:49:37 +0000201 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
202 I != E; ++I) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000203 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000204 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patel159f7ce2008-11-18 21:34:39 +0000205 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel2c98b522008-11-14 22:49:37 +0000206 }
207
208 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000209 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000210 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patel159f7ce2008-11-18 21:34:39 +0000211 I->setName(""); // Internal symbols can't participate in linkage
212 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel2c98b522008-11-14 22:49:37 +0000213 }
214
215 // Remove all names from types.
Devang Patel159f7ce2008-11-18 21:34:39 +0000216 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217
Devang Patel2c98b522008-11-14 22:49:37 +0000218 return true;
219}
220
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.
Dan Gohman6d29b322009-08-07 01:32:21 +0000224static bool StripDebugInfo(Module &M) {
Devang Patel2c98b522008-11-14 22:49:37 +0000225
Devang Pateldadd6cd2009-11-17 00:47:06 +0000226 bool Changed = false;
227
Devang Patel15e723d2009-08-28 23:24:31 +0000228 // Remove all of the calls to the debugger intrinsics, and remove them from
229 // the module.
Devang Pateldadd6cd2009-11-17 00:47:06 +0000230 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 while (!Declare->use_empty()) {
232 CallInst *CI = cast<CallInst>(Declare->use_back());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 CI->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 }
235 Declare->eraseFromParent();
Devang Pateldadd6cd2009-11-17 00:47:06 +0000236 Changed = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 }
238
Devang Patel989853e2010-02-10 21:19:56 +0000239 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
240 while (!DbgVal->use_empty()) {
241 CallInst *CI = cast<CallInst>(DbgVal->use_back());
242 CI->eraseFromParent();
243 }
244 DbgVal->eraseFromParent();
245 Changed = true;
246 }
247
Devang Patelcfb60212010-06-30 21:29:00 +0000248 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
249 NME = M.named_metadata_end(); NMI != NME;) {
250 NamedMDNode *NMD = NMI;
251 ++NMI;
Devang Patel7c4b8e72010-07-01 18:27:46 +0000252 if (NMD->getName().startswith("llvm.dbg.")) {
Devang Patelcfb60212010-06-30 21:29:00 +0000253 NMD->eraseFromParent();
Devang Patel7c4b8e72010-07-01 18:27:46 +0000254 Changed = true;
255 }
Devang Patelba0e38c2010-05-20 16:49:22 +0000256 }
Duncan Sandsac8dc3a2010-06-29 14:52:10 +0000257
Duncan Sandsac8dc3a2010-06-29 14:52:10 +0000258 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
Devang Pateldadd6cd2009-11-17 00:47:06 +0000259 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
260 ++FI)
261 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
Duncan Sandsac8dc3a2010-06-29 14:52:10 +0000262 ++BI) {
Dan Gohman2205f422010-07-20 23:49:44 +0000263 if (!BI->getDebugLoc().isUnknown()) {
264 Changed = true;
265 BI->setDebugLoc(DebugLoc());
266 }
Duncan Sandsac8dc3a2010-06-29 14:52:10 +0000267 }
Devang Patel2fa85562008-11-13 01:28:40 +0000268
Duncan Sandsac8dc3a2010-06-29 14:52:10 +0000269 return Changed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270}
Devang Patel159f7ce2008-11-18 21:34:39 +0000271
272bool StripSymbols::runOnModule(Module &M) {
273 bool Changed = false;
274 Changed |= StripDebugInfo(M);
275 if (!OnlyDebugInfo)
276 Changed |= StripSymbolNames(M, false);
277 return Changed;
278}
279
280bool StripNonDebugSymbols::runOnModule(Module &M) {
281 return StripSymbolNames(M, true);
282}
Devang Patel3876c8a2009-03-09 20:49:37 +0000283
284bool StripDebugDeclare::runOnModule(Module &M) {
285
286 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel3876c8a2009-03-09 20:49:37 +0000287 std::vector<Constant*> DeadConstants;
288
Dale Johannesendd031b72009-03-13 22:59:47 +0000289 if (Declare) {
290 while (!Declare->use_empty()) {
291 CallInst *CI = cast<CallInst>(Declare->use_back());
Gabor Greifca175d62010-06-30 12:40:35 +0000292 Value *Arg1 = CI->getArgOperand(0);
293 Value *Arg2 = CI->getArgOperand(1);
Dale Johannesendd031b72009-03-13 22:59:47 +0000294 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);
299 else
Dan Gohmandb2b9462009-05-02 20:22:10 +0000300 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesendd031b72009-03-13 22:59:47 +0000301 }
302 if (Arg2->use_empty())
303 if (Constant *C = dyn_cast<Constant>(Arg2))
304 DeadConstants.push_back(C);
Devang Patel3876c8a2009-03-09 20:49:37 +0000305 }
Dale Johannesendd031b72009-03-13 22:59:47 +0000306 Declare->eraseFromParent();
Devang Patel3876c8a2009-03-09 20:49:37 +0000307 }
Devang Patel3876c8a2009-03-09 20:49:37 +0000308
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);
Chris Lattner67abb532009-10-28 05:14:34 +0000315 } else
Devang Patel3876c8a2009-03-09 20:49:37 +0000316 RemoveDeadConstant(C);
317 }
318
319 return true;
320}
Devang Patele524efe2010-07-01 19:49:20 +0000321
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.
325static StringRef getRealLinkageName(StringRef LinkageName) {
326 char One = '\1';
327 if (LinkageName.startswith(StringRef(&One, 1)))
328 return LinkageName.substr(1);
329 return LinkageName;
330}
331
332bool StripDeadDebugInfo::runOnModule(Module &M) {
333 bool Changed = false;
334
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
338 // such symbols.
339
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));
346 else
347 Changed = true;
348 NMD->eraseFromParent();
349 NMD = NULL;
350
351 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
352 E = MDs.end(); I != E; ++I) {
353 if (M.getGlobalVariable(DIGlobalVariable(*I).getGlobal()->getName(),
354 true)) {
355 if (!NMD)
356 NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
357 NMD->addOperand(*I);
358 }
359 else
360 Changed = true;
361 }
362 }
363
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));
370 else
371 Changed = true;
372 NMD->eraseFromParent();
373 NMD = NULL;
374
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()))
380 FnIsLive = true;
381 if (FnIsLive) {
382 if (!NMD)
383 NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
384 NMD->addOperand(*I);
385 } else {
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();
389 if (FName.empty())
390 FName = DISubprogram(*I).getName();
391 if (NamedMDNode *LVNMD =
392 M.getNamedMetadata(Twine("llvm.dbg.lv.",
393 getRealLinkageName(FName))))
394 LVNMD->eraseFromParent();
395 }
396 }
397 }
398
399 return Changed;
400}