blob: ee6fbd3cba1064628cac0de449937a1d87bc527f [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"
29#include "llvm/ValueSymbolTable.h"
30#include "llvm/TypeSymbolTable.h"
Devang Patele68804a2009-03-03 21:31:02 +000031#include "llvm/Transforms/Utils/Local.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Support/Compiler.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 {
37 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
38 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
51 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
52 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
64 class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
65 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 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076}
77
Dan Gohman089efff2008-05-13 00:00:25 +000078char StripSymbols::ID = 0;
79static RegisterPass<StripSymbols>
80X("strip", "Strip all symbols from a module");
81
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
83 return new StripSymbols(OnlyDebugInfo);
84}
85
Devang Patel159f7ce2008-11-18 21:34:39 +000086char StripNonDebugSymbols::ID = 0;
87static RegisterPass<StripNonDebugSymbols>
88Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
89
90ModulePass *llvm::createStripNonDebugSymbolsPass() {
91 return new StripNonDebugSymbols();
92}
93
Devang Patel3876c8a2009-03-09 20:49:37 +000094char StripDebugDeclare::ID = 0;
95static RegisterPass<StripDebugDeclare>
96Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
97
98ModulePass *llvm::createStripDebugDeclarePass() {
99 return new StripDebugDeclare();
100}
101
Devang Patel2fa85562008-11-13 01:28:40 +0000102/// OnlyUsedBy - Return true if V is only used by Usr.
103static bool OnlyUsedBy(Value *V, Value *Usr) {
104 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
105 User *U = *I;
106 if (U != Usr)
107 return false;
108 }
109 return true;
110}
111
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112static void RemoveDeadConstant(Constant *C) {
113 assert(C->use_empty() && "Constant is not dead!");
Devang Patel2fa85562008-11-13 01:28:40 +0000114 SmallPtrSet<Constant *, 4> Operands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
116 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patel2fa85562008-11-13 01:28:40 +0000117 OnlyUsedBy(C->getOperand(i), C))
118 Operands.insert(C->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000120 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 GV->eraseFromParent();
122 }
123 else if (!isa<Function>(C))
Devang Patelabd69112008-11-20 01:20:42 +0000124 if (isa<CompositeType>(C->getType()))
125 C->destroyConstant();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126
127 // If the constant referenced anything, see if we can delete it as well.
Devang Patel2fa85562008-11-13 01:28:40 +0000128 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
129 OE = Operands.end(); OI != OE; ++OI)
130 RemoveDeadConstant(*OI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131}
132
133// Strip the symbol table of its names.
134//
Devang Patel159f7ce2008-11-18 21:34:39 +0000135static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
137 Value *V = VI->getValue();
138 ++VI;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000139 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Devang Patel159f7ce2008-11-18 21:34:39 +0000140 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
141 // Set name to "", removing from symbol table!
142 V->setName("");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 }
144 }
145}
146
147// Strip the symbol table of its names.
Devang Patel159f7ce2008-11-18 21:34:39 +0000148static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
149 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
150 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
151 ++TI;
152 else
153 ST.remove(TI++);
154 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155}
156
Devang Patel2d53e2d2008-11-18 21:13:41 +0000157/// Find values that are marked as llvm.used.
158void findUsedValues(Module &M,
159 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
Devang Patel2c98b522008-11-14 22:49:37 +0000160 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
161 llvmUsedValues.insert(LLVMUsed);
162 // Collect values that are preserved as per explicit request.
163 // llvm.used is used to list these values.
164 if (ConstantArray *Inits =
165 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
166 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
167 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
168 llvmUsedValues.insert(GV);
169 else if (ConstantExpr *CE =
170 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
171 if (CE->getOpcode() == Instruction::BitCast)
172 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
173 llvmUsedValues.insert(GV);
Devang Patel5dc8fde2008-01-16 03:33:05 +0000174 }
175 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 }
Devang Patel2d53e2d2008-11-18 21:13:41 +0000177}
178
179/// StripSymbolNames - Strip symbol names.
Devang Patel159f7ce2008-11-18 21:34:39 +0000180bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel2d53e2d2008-11-18 21:13:41 +0000181
182 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
183 findUsedValues(M, llvmUsedValues);
184
Devang Patel2c98b522008-11-14 22:49:37 +0000185 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
186 I != E; ++I) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000187 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patel159f7ce2008-11-18 21:34:39 +0000188 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
189 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel2c98b522008-11-14 22:49:37 +0000190 }
191
192 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000193 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patel159f7ce2008-11-18 21:34:39 +0000194 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
195 I->setName(""); // Internal symbols can't participate in linkage
196 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel2c98b522008-11-14 22:49:37 +0000197 }
198
199 // Remove all names from types.
Devang Patel159f7ce2008-11-18 21:34:39 +0000200 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201
Devang Patel2c98b522008-11-14 22:49:37 +0000202 return true;
203}
204
205// StripDebugInfo - Strip debug info in the module if it exists.
206// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
207// llvm.dbg.region.end calls, and any globals they point to if now dead.
Devang Patel159f7ce2008-11-18 21:34:39 +0000208bool StripDebugInfo(Module &M) {
Devang Patel2c98b522008-11-14 22:49:37 +0000209
Devang Patel4ed9e402009-03-02 22:50:58 +0000210 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
211 findUsedValues(M, llvmUsedValues);
212
213 // Delete all dbg variables.
214 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
215 I != E; ++I) {
216 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
217 if (!GV) continue;
218 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
219 if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) {
220 GV->replaceAllUsesWith(UndefValue::get(GV->getType()));
221 }
222 }
223 }
224
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
226 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
227 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
228 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
229 Function *Declare = M.getFunction("llvm.dbg.declare");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230
Devang Patel2d53e2d2008-11-18 21:13:41 +0000231 std::vector<Constant*> DeadConstants;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232
233 // Remove all of the calls to the debugger intrinsics, and remove them from
234 // the module.
235 if (FuncStart) {
236 while (!FuncStart->use_empty()) {
237 CallInst *CI = cast<CallInst>(FuncStart->use_back());
238 Value *Arg = CI->getOperand(1);
239 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
240 CI->eraseFromParent();
241 if (Arg->use_empty())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000242 if (Constant *C = dyn_cast<Constant>(Arg))
243 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 }
245 FuncStart->eraseFromParent();
246 }
247 if (StopPoint) {
248 while (!StopPoint->use_empty()) {
249 CallInst *CI = cast<CallInst>(StopPoint->use_back());
250 Value *Arg = CI->getOperand(3);
251 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
252 CI->eraseFromParent();
253 if (Arg->use_empty())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000254 if (Constant *C = dyn_cast<Constant>(Arg))
255 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 }
257 StopPoint->eraseFromParent();
258 }
259 if (RegionStart) {
260 while (!RegionStart->use_empty()) {
261 CallInst *CI = cast<CallInst>(RegionStart->use_back());
262 Value *Arg = CI->getOperand(1);
263 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
264 CI->eraseFromParent();
265 if (Arg->use_empty())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000266 if (Constant *C = dyn_cast<Constant>(Arg))
267 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 }
269 RegionStart->eraseFromParent();
270 }
271 if (RegionEnd) {
272 while (!RegionEnd->use_empty()) {
273 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
274 Value *Arg = CI->getOperand(1);
275 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
276 CI->eraseFromParent();
277 if (Arg->use_empty())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000278 if (Constant *C = dyn_cast<Constant>(Arg))
279 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 }
281 RegionEnd->eraseFromParent();
282 }
283 if (Declare) {
284 while (!Declare->use_empty()) {
285 CallInst *CI = cast<CallInst>(Declare->use_back());
Devang Patelabd69112008-11-20 01:20:42 +0000286 Value *Arg1 = CI->getOperand(1);
287 Value *Arg2 = CI->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
289 CI->eraseFromParent();
Devang Patelabd69112008-11-20 01:20:42 +0000290 if (Arg1->use_empty()) {
291 if (Constant *C = dyn_cast<Constant>(Arg1))
292 DeadConstants.push_back(C);
Devang Patele68804a2009-03-03 21:31:02 +0000293 else
294 RecursivelyDeleteTriviallyDeadInstructions(Arg1, NULL);
Devang Patelabd69112008-11-20 01:20:42 +0000295 }
296 if (Arg2->use_empty())
297 if (Constant *C = dyn_cast<Constant>(Arg2))
Devang Patel2d53e2d2008-11-18 21:13:41 +0000298 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 }
300 Declare->eraseFromParent();
301 }
302
Devang Patel2fa85562008-11-13 01:28:40 +0000303 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
304 // but since we are removing all debug information, make them internal now.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000305 // FIXME: Use private linkage maybe?
Devang Patel2fa85562008-11-13 01:28:40 +0000306 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
307 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
308 GV->setLinkage(GlobalValue::InternalLinkage);
309
310 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
311 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
312 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patel2d53e2d2008-11-18 21:13:41 +0000313
314 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
315 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
316 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patel2fa85562008-11-13 01:28:40 +0000317
318 // Delete all dbg variables.
Devang Patel43f586b2008-11-19 00:22:02 +0000319 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
320 I != E; ++I) {
321 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
322 if (!GV) continue;
323 if (GV->use_empty() && llvmUsedValues.count(I) == 0
324 && (!GV->hasSection()
325 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
326 DeadConstants.push_back(GV);
327 }
Devang Patel2fa85562008-11-13 01:28:40 +0000328
Devang Patel2d53e2d2008-11-18 21:13:41 +0000329 if (DeadConstants.empty())
Devang Patel2c98b522008-11-14 22:49:37 +0000330 return false;
331
Devang Patel2fa85562008-11-13 01:28:40 +0000332 // Delete any internal globals that were only used by the debugger intrinsics.
Devang Patel2d53e2d2008-11-18 21:13:41 +0000333 while (!DeadConstants.empty()) {
334 Constant *C = DeadConstants.back();
335 DeadConstants.pop_back();
336 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000337 if (GV->hasLocalLinkage())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000338 RemoveDeadConstant(GV);
339 }
340 else
341 RemoveDeadConstant(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 }
343
Devang Patel2fa85562008-11-13 01:28:40 +0000344 // Remove all llvm.dbg types.
345 TypeSymbolTable &ST = M.getTypeSymbolTable();
Chris Lattnerba58d9c2008-11-16 06:35:18 +0000346 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
347 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
Devang Patel2fa85562008-11-13 01:28:40 +0000348 ST.remove(TI++);
349 else
350 ++TI;
351 }
352
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 return true;
354}
Devang Patel159f7ce2008-11-18 21:34:39 +0000355
356bool StripSymbols::runOnModule(Module &M) {
357 bool Changed = false;
358 Changed |= StripDebugInfo(M);
359 if (!OnlyDebugInfo)
360 Changed |= StripSymbolNames(M, false);
361 return Changed;
362}
363
364bool StripNonDebugSymbols::runOnModule(Module &M) {
365 return StripSymbolNames(M, true);
366}
Devang Patel3876c8a2009-03-09 20:49:37 +0000367
368bool StripDebugDeclare::runOnModule(Module &M) {
369
370 Function *Declare = M.getFunction("llvm.dbg.declare");
371
372 if (!Declare)
373 return false;
374
375 std::vector<Constant*> DeadConstants;
376
377 while (!Declare->use_empty()) {
378 CallInst *CI = cast<CallInst>(Declare->use_back());
379 Value *Arg1 = CI->getOperand(1);
380 Value *Arg2 = CI->getOperand(2);
381 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
382 CI->eraseFromParent();
383 if (Arg1->use_empty()) {
384 if (Constant *C = dyn_cast<Constant>(Arg1))
385 DeadConstants.push_back(C);
386 else
387 RecursivelyDeleteTriviallyDeadInstructions(Arg1, NULL);
388 }
389 if (Arg2->use_empty())
390 if (Constant *C = dyn_cast<Constant>(Arg2))
391 DeadConstants.push_back(C);
392 }
393 Declare->eraseFromParent();
394
Devang Patel3a402b02009-03-09 21:32:28 +0000395 // Delete all llvm.dbg.global_variables.
396 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
397 I != E; ++I) {
398 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
399 if (!GV) continue;
400 if (GV->use_empty() && GV->hasName()
401 && strncmp(GV->getNameStart(), "llvm.dbg.global_variable", 24) == 0)
402 DeadConstants.push_back(GV);
403 }
404
Devang Patel3876c8a2009-03-09 20:49:37 +0000405 while (!DeadConstants.empty()) {
406 Constant *C = DeadConstants.back();
407 DeadConstants.pop_back();
408 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
409 if (GV->hasLocalLinkage())
410 RemoveDeadConstant(GV);
411 }
412 else
413 RemoveDeadConstant(C);
414 }
415
416 return true;
417}