blob: 3bcaee3437f707141b9f97b1449371218119ca2e [file] [log] [blame]
Chris Lattnere3ad43c2004-12-02 21:25:03 +00001//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattnere3ad43c2004-12-02 21:25:03 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattnere3ad43c2004-12-02 21:25:03 +00008//===----------------------------------------------------------------------===//
9//
Gordon Henriksenc86b6772007-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
Chris Lattnere3ad43c2004-12-02 21:25:03 +000016//
Gordon Henriksenc86b6772007-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.
Chris Lattnere3ad43c2004-12-02 21:25:03 +000020//
21//===----------------------------------------------------------------------===//
22
23#include "llvm/Transforms/IPO.h"
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000024#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Instructions.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000027#include "llvm/Module.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000028#include "llvm/Pass.h"
Devang Patel13e16b62009-06-26 01:49:18 +000029#include "llvm/Analysis/DebugInfo.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000030#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000031#include "llvm/TypeSymbolTable.h"
Devang Patel9adb01c2009-03-03 21:31:02 +000032#include "llvm/Transforms/Utils/Local.h"
Devang Patel8c231e52008-01-16 03:33:05 +000033#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000034using namespace llvm;
35
36namespace {
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000037 class StripSymbols : public ModulePass {
Chris Lattnere3ad43c2004-12-02 21:25:03 +000038 bool OnlyDebugInfo;
39 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000040 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000041 explicit StripSymbols(bool ODI = false)
Dan Gohmanae73dc12008-09-04 17:05:41 +000042 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000043
Devang Patelf17fc462008-11-18 21:34:39 +000044 virtual bool runOnModule(Module &M);
Devang Patel229de952008-11-14 22:49:37 +000045
Devang Patelf17fc462008-11-18 21:34:39 +000046 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.setPreservesAll();
48 }
49 };
50
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000051 class StripNonDebugSymbols : public ModulePass {
Devang Patelf17fc462008-11-18 21:34:39 +000052 public:
53 static char ID; // Pass identification, replacement for typeid
54 explicit StripNonDebugSymbols()
55 : ModulePass(&ID) {}
Devang Patel229de952008-11-14 22:49:37 +000056
Chris Lattnere3ad43c2004-12-02 21:25:03 +000057 virtual bool runOnModule(Module &M);
58
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesAll();
61 }
62 };
Devang Patel23e528b2009-03-09 20:49:37 +000063
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000064 class StripDebugDeclare : public ModulePass {
Devang Patel23e528b2009-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 Patel26d14292010-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 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000089}
90
Dan Gohman844731a2008-05-13 00:00:25 +000091char StripSymbols::ID = 0;
92static RegisterPass<StripSymbols>
93X("strip", "Strip all symbols from a module");
94
Chris Lattnere3ad43c2004-12-02 21:25:03 +000095ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
96 return new StripSymbols(OnlyDebugInfo);
97}
98
Devang Patelf17fc462008-11-18 21:34:39 +000099char StripNonDebugSymbols::ID = 0;
100static RegisterPass<StripNonDebugSymbols>
101Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
102
103ModulePass *llvm::createStripNonDebugSymbolsPass() {
104 return new StripNonDebugSymbols();
105}
106
Devang Patel23e528b2009-03-09 20:49:37 +0000107char StripDebugDeclare::ID = 0;
108static RegisterPass<StripDebugDeclare>
109Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
110
111ModulePass *llvm::createStripDebugDeclarePass() {
112 return new StripDebugDeclare();
113}
114
Devang Patel26d14292010-07-01 19:49:20 +0000115char StripDeadDebugInfo::ID = 0;
116static RegisterPass<StripDeadDebugInfo>
117A("strip-dead-debug-info", "Strip debug info for unused symbols");
118
119ModulePass *llvm::createStripDeadDebugInfoPass() {
120 return new StripDeadDebugInfo();
121}
122
Devang Patelbf5db812008-11-13 01:28:40 +0000123/// OnlyUsedBy - Return true if V is only used by Usr.
124static bool OnlyUsedBy(Value *V, Value *Usr) {
125 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
126 User *U = *I;
127 if (U != Usr)
128 return false;
129 }
130 return true;
131}
132
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000133static void RemoveDeadConstant(Constant *C) {
134 assert(C->use_empty() && "Constant is not dead!");
Chris Lattner0eeb9132009-10-28 05:14:34 +0000135 SmallPtrSet<Constant*, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000136 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
137 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patelbf5db812008-11-13 01:28:40 +0000138 OnlyUsedBy(C->getOperand(i), C))
Chris Lattner0eeb9132009-10-28 05:14:34 +0000139 Operands.insert(cast<Constant>(C->getOperand(i)));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000140 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000141 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000142 GV->eraseFromParent();
143 }
144 else if (!isa<Function>(C))
Devang Patelf23de862008-11-20 01:20:42 +0000145 if (isa<CompositeType>(C->getType()))
146 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +0000147
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000148 // If the constant referenced anything, see if we can delete it as well.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000149 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
Devang Patelbf5db812008-11-13 01:28:40 +0000150 OE = Operands.end(); OI != OE; ++OI)
151 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000152}
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000153
Chris Lattner7f1444b2007-02-07 06:22:45 +0000154// Strip the symbol table of its names.
155//
Devang Patelf17fc462008-11-18 21:34:39 +0000156static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner7f1444b2007-02-07 06:22:45 +0000157 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000158 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +0000159 ++VI;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000160 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar460f6562009-07-26 09:48:23 +0000161 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000162 // Set name to "", removing from symbol table!
163 V->setName("");
Chris Lattner7f1444b2007-02-07 06:22:45 +0000164 }
165 }
166}
167
168// Strip the symbol table of its names.
Devang Patelf17fc462008-11-18 21:34:39 +0000169static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
170 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
Benjamin Kramerb0706d12010-01-22 20:00:21 +0000171 if (PreserveDbgInfo && StringRef(TI->first).startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000172 ++TI;
173 else
174 ST.remove(TI++);
175 }
Chris Lattner7f1444b2007-02-07 06:22:45 +0000176}
177
Devang Patel4460a7e2008-11-18 21:13:41 +0000178/// Find values that are marked as llvm.used.
Chris Lattner401e10c2009-07-20 06:14:25 +0000179static void findUsedValues(GlobalVariable *LLVMUsed,
180 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
181 if (LLVMUsed == 0) return;
182 UsedValues.insert(LLVMUsed);
183
184 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
185 if (Inits == 0) return;
186
187 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
188 if (GlobalValue *GV =
189 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
190 UsedValues.insert(GV);
Devang Patel4460a7e2008-11-18 21:13:41 +0000191}
192
193/// StripSymbolNames - Strip symbol names.
Dan Gohman7db949d2009-08-07 01:32:21 +0000194static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel4460a7e2008-11-18 21:13:41 +0000195
196 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner401e10c2009-07-20 06:14:25 +0000197 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
198 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel4460a7e2008-11-18 21:13:41 +0000199
Devang Patel229de952008-11-14 22:49:37 +0000200 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
201 I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000202 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000203 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000204 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel229de952008-11-14 22:49:37 +0000205 }
206
207 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000208 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000209 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000210 I->setName(""); // Internal symbols can't participate in linkage
211 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel229de952008-11-14 22:49:37 +0000212 }
213
214 // Remove all names from types.
Devang Patelf17fc462008-11-18 21:34:39 +0000215 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000216
Devang Patel229de952008-11-14 22:49:37 +0000217 return true;
218}
219
220// StripDebugInfo - Strip debug info in the module if it exists.
221// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
222// llvm.dbg.region.end calls, and any globals they point to if now dead.
Dan Gohman7db949d2009-08-07 01:32:21 +0000223static bool StripDebugInfo(Module &M) {
Devang Patel229de952008-11-14 22:49:37 +0000224
Devang Patel76e3e502009-11-17 00:47:06 +0000225 bool Changed = false;
226
Devang Patele4b27562009-08-28 23:24:31 +0000227 // Remove all of the calls to the debugger intrinsics, and remove them from
228 // the module.
Devang Patel76e3e502009-11-17 00:47:06 +0000229 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
Jim Laskey4ca97572006-03-23 18:11:33 +0000230 while (!Declare->use_empty()) {
231 CallInst *CI = cast<CallInst>(Declare->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000232 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000233 }
234 Declare->eraseFromParent();
Devang Patel76e3e502009-11-17 00:47:06 +0000235 Changed = true;
Jim Laskey4ca97572006-03-23 18:11:33 +0000236 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000237
Devang Pateldf9292c2010-02-10 21:19:56 +0000238 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
239 while (!DbgVal->use_empty()) {
240 CallInst *CI = cast<CallInst>(DbgVal->use_back());
241 CI->eraseFromParent();
242 }
243 DbgVal->eraseFromParent();
244 Changed = true;
245 }
246
Devang Patel444a08c2010-06-30 21:29:00 +0000247 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
248 NME = M.named_metadata_end(); NMI != NME;) {
249 NamedMDNode *NMD = NMI;
250 ++NMI;
Devang Patele62b2032010-07-01 18:27:46 +0000251 if (NMD->getName().startswith("llvm.dbg.")) {
Devang Patel444a08c2010-06-30 21:29:00 +0000252 NMD->eraseFromParent();
Devang Patele62b2032010-07-01 18:27:46 +0000253 Changed = true;
254 }
Devang Patel69b4d1c2010-05-20 16:49:22 +0000255 }
Duncan Sandsa7065b12010-06-29 14:52:10 +0000256
Duncan Sandsa7065b12010-06-29 14:52:10 +0000257 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
Devang Patel76e3e502009-11-17 00:47:06 +0000258 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
259 ++FI)
260 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
Duncan Sandsa7065b12010-06-29 14:52:10 +0000261 ++BI) {
Dan Gohman549979f2010-07-20 23:49:44 +0000262 if (!BI->getDebugLoc().isUnknown()) {
263 Changed = true;
264 BI->setDebugLoc(DebugLoc());
265 }
Duncan Sandsa7065b12010-06-29 14:52:10 +0000266 }
Devang Patelbf5db812008-11-13 01:28:40 +0000267
Duncan Sandsa7065b12010-06-29 14:52:10 +0000268 return Changed;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000269}
Devang Patelf17fc462008-11-18 21:34:39 +0000270
271bool StripSymbols::runOnModule(Module &M) {
272 bool Changed = false;
273 Changed |= StripDebugInfo(M);
274 if (!OnlyDebugInfo)
275 Changed |= StripSymbolNames(M, false);
276 return Changed;
277}
278
279bool StripNonDebugSymbols::runOnModule(Module &M) {
280 return StripSymbolNames(M, true);
281}
Devang Patel23e528b2009-03-09 20:49:37 +0000282
283bool StripDebugDeclare::runOnModule(Module &M) {
284
285 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel23e528b2009-03-09 20:49:37 +0000286 std::vector<Constant*> DeadConstants;
287
Dale Johannesen44252402009-03-13 22:59:47 +0000288 if (Declare) {
289 while (!Declare->use_empty()) {
290 CallInst *CI = cast<CallInst>(Declare->use_back());
Gabor Greife9af3522010-06-30 12:40:35 +0000291 Value *Arg1 = CI->getArgOperand(0);
292 Value *Arg2 = CI->getArgOperand(1);
Dale Johannesen44252402009-03-13 22:59:47 +0000293 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
294 CI->eraseFromParent();
295 if (Arg1->use_empty()) {
296 if (Constant *C = dyn_cast<Constant>(Arg1))
297 DeadConstants.push_back(C);
298 else
Dan Gohmane66f6f12009-05-02 20:22:10 +0000299 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesen44252402009-03-13 22:59:47 +0000300 }
301 if (Arg2->use_empty())
302 if (Constant *C = dyn_cast<Constant>(Arg2))
303 DeadConstants.push_back(C);
Devang Patel23e528b2009-03-09 20:49:37 +0000304 }
Dale Johannesen44252402009-03-13 22:59:47 +0000305 Declare->eraseFromParent();
Devang Patel23e528b2009-03-09 20:49:37 +0000306 }
Devang Patel23e528b2009-03-09 20:49:37 +0000307
308 while (!DeadConstants.empty()) {
309 Constant *C = DeadConstants.back();
310 DeadConstants.pop_back();
311 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
312 if (GV->hasLocalLinkage())
313 RemoveDeadConstant(GV);
Chris Lattner0eeb9132009-10-28 05:14:34 +0000314 } else
Devang Patel23e528b2009-03-09 20:49:37 +0000315 RemoveDeadConstant(C);
316 }
317
318 return true;
319}
Devang Patel26d14292010-07-01 19:49:20 +0000320
321/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
322/// printer to not emit usual symbol prefix before the symbol name is used then
323/// return linkage name after skipping this special LLVM prefix.
324static StringRef getRealLinkageName(StringRef LinkageName) {
325 char One = '\1';
326 if (LinkageName.startswith(StringRef(&One, 1)))
327 return LinkageName.substr(1);
328 return LinkageName;
329}
330
331bool StripDeadDebugInfo::runOnModule(Module &M) {
332 bool Changed = false;
333
334 // Debugging infomration is encoded in llvm IR using metadata. This is designed
335 // such a way that debug info for symbols preserved even if symbols are
336 // optimized away by the optimizer. This special pass removes debug info for
337 // such symbols.
338
339 // llvm.dbg.gv keeps track of debug info for global variables.
340 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
341 SmallVector<MDNode *, 8> MDs;
342 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
343 if (DIGlobalVariable(NMD->getOperand(i)).Verify())
344 MDs.push_back(NMD->getOperand(i));
345 else
346 Changed = true;
347 NMD->eraseFromParent();
348 NMD = NULL;
349
350 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
351 E = MDs.end(); I != E; ++I) {
352 if (M.getGlobalVariable(DIGlobalVariable(*I).getGlobal()->getName(),
353 true)) {
354 if (!NMD)
355 NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
356 NMD->addOperand(*I);
357 }
358 else
359 Changed = true;
360 }
361 }
362
363 // llvm.dbg.sp keeps track of debug info for subprograms.
364 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
365 SmallVector<MDNode *, 8> MDs;
366 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
367 if (DISubprogram(NMD->getOperand(i)).Verify())
368 MDs.push_back(NMD->getOperand(i));
369 else
370 Changed = true;
371 NMD->eraseFromParent();
372 NMD = NULL;
373
374 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
375 E = MDs.end(); I != E; ++I) {
376 bool FnIsLive = false;
377 if (Function *F = DISubprogram(*I).getFunction())
378 if (M.getFunction(F->getName()))
379 FnIsLive = true;
380 if (FnIsLive) {
381 if (!NMD)
382 NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
383 NMD->addOperand(*I);
384 } else {
385 // Remove llvm.dbg.lv.fnname named mdnode which may have been used
386 // to hold debug info for dead function's local variables.
387 StringRef FName = DISubprogram(*I).getLinkageName();
388 if (FName.empty())
389 FName = DISubprogram(*I).getName();
390 if (NamedMDNode *LVNMD =
391 M.getNamedMetadata(Twine("llvm.dbg.lv.",
392 getRealLinkageName(FName))))
393 LVNMD->eraseFromParent();
394 }
395 }
396 }
397
398 return Changed;
399}