blob: c74bad00b5542a7ab2c47119fe33d622e4df3230 [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 Gohmane336cbc2010-07-20 23:10:36 +0000262 Changed |= !BI->getDebugLoc().isUnknown();
Dan Gohman7bc230e2010-07-20 23:09:34 +0000263 BI->setDebugLoc(DebugLoc());
Duncan Sandsa7065b12010-06-29 14:52:10 +0000264 }
Devang Patelbf5db812008-11-13 01:28:40 +0000265
Duncan Sandsa7065b12010-06-29 14:52:10 +0000266 return Changed;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000267}
Devang Patelf17fc462008-11-18 21:34:39 +0000268
269bool StripSymbols::runOnModule(Module &M) {
270 bool Changed = false;
271 Changed |= StripDebugInfo(M);
272 if (!OnlyDebugInfo)
273 Changed |= StripSymbolNames(M, false);
274 return Changed;
275}
276
277bool StripNonDebugSymbols::runOnModule(Module &M) {
278 return StripSymbolNames(M, true);
279}
Devang Patel23e528b2009-03-09 20:49:37 +0000280
281bool StripDebugDeclare::runOnModule(Module &M) {
282
283 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel23e528b2009-03-09 20:49:37 +0000284 std::vector<Constant*> DeadConstants;
285
Dale Johannesen44252402009-03-13 22:59:47 +0000286 if (Declare) {
287 while (!Declare->use_empty()) {
288 CallInst *CI = cast<CallInst>(Declare->use_back());
Gabor Greife9af3522010-06-30 12:40:35 +0000289 Value *Arg1 = CI->getArgOperand(0);
290 Value *Arg2 = CI->getArgOperand(1);
Dale Johannesen44252402009-03-13 22:59:47 +0000291 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
292 CI->eraseFromParent();
293 if (Arg1->use_empty()) {
294 if (Constant *C = dyn_cast<Constant>(Arg1))
295 DeadConstants.push_back(C);
296 else
Dan Gohmane66f6f12009-05-02 20:22:10 +0000297 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesen44252402009-03-13 22:59:47 +0000298 }
299 if (Arg2->use_empty())
300 if (Constant *C = dyn_cast<Constant>(Arg2))
301 DeadConstants.push_back(C);
Devang Patel23e528b2009-03-09 20:49:37 +0000302 }
Dale Johannesen44252402009-03-13 22:59:47 +0000303 Declare->eraseFromParent();
Devang Patel23e528b2009-03-09 20:49:37 +0000304 }
Devang Patel23e528b2009-03-09 20:49:37 +0000305
306 while (!DeadConstants.empty()) {
307 Constant *C = DeadConstants.back();
308 DeadConstants.pop_back();
309 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
310 if (GV->hasLocalLinkage())
311 RemoveDeadConstant(GV);
Chris Lattner0eeb9132009-10-28 05:14:34 +0000312 } else
Devang Patel23e528b2009-03-09 20:49:37 +0000313 RemoveDeadConstant(C);
314 }
315
316 return true;
317}
Devang Patel26d14292010-07-01 19:49:20 +0000318
319/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
320/// printer to not emit usual symbol prefix before the symbol name is used then
321/// return linkage name after skipping this special LLVM prefix.
322static StringRef getRealLinkageName(StringRef LinkageName) {
323 char One = '\1';
324 if (LinkageName.startswith(StringRef(&One, 1)))
325 return LinkageName.substr(1);
326 return LinkageName;
327}
328
329bool StripDeadDebugInfo::runOnModule(Module &M) {
330 bool Changed = false;
331
332 // Debugging infomration is encoded in llvm IR using metadata. This is designed
333 // such a way that debug info for symbols preserved even if symbols are
334 // optimized away by the optimizer. This special pass removes debug info for
335 // such symbols.
336
337 // llvm.dbg.gv keeps track of debug info for global variables.
338 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
339 SmallVector<MDNode *, 8> MDs;
340 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
341 if (DIGlobalVariable(NMD->getOperand(i)).Verify())
342 MDs.push_back(NMD->getOperand(i));
343 else
344 Changed = true;
345 NMD->eraseFromParent();
346 NMD = NULL;
347
348 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
349 E = MDs.end(); I != E; ++I) {
350 if (M.getGlobalVariable(DIGlobalVariable(*I).getGlobal()->getName(),
351 true)) {
352 if (!NMD)
353 NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
354 NMD->addOperand(*I);
355 }
356 else
357 Changed = true;
358 }
359 }
360
361 // llvm.dbg.sp keeps track of debug info for subprograms.
362 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
363 SmallVector<MDNode *, 8> MDs;
364 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
365 if (DISubprogram(NMD->getOperand(i)).Verify())
366 MDs.push_back(NMD->getOperand(i));
367 else
368 Changed = true;
369 NMD->eraseFromParent();
370 NMD = NULL;
371
372 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
373 E = MDs.end(); I != E; ++I) {
374 bool FnIsLive = false;
375 if (Function *F = DISubprogram(*I).getFunction())
376 if (M.getFunction(F->getName()))
377 FnIsLive = true;
378 if (FnIsLive) {
379 if (!NMD)
380 NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
381 NMD->addOperand(*I);
382 } else {
383 // Remove llvm.dbg.lv.fnname named mdnode which may have been used
384 // to hold debug info for dead function's local variables.
385 StringRef FName = DISubprogram(*I).getLinkageName();
386 if (FName.empty())
387 FName = DISubprogram(*I).getName();
388 if (NamedMDNode *LVNMD =
389 M.getNamedMetadata(Twine("llvm.dbg.lv.",
390 getRealLinkageName(FName))))
391 LVNMD->eraseFromParent();
392 }
393 }
394 }
395
396 return Changed;
397}