blob: fbb2075cb4607917bbd4acc03c99e6ad08ff4945 [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"
Reid Spenceref9b9a72007-02-05 20:47:22 +000029#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000030#include "llvm/TypeSymbolTable.h"
Devang Patel9adb01c2009-03-03 21:31:02 +000031#include "llvm/Transforms/Utils/Local.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000032#include "llvm/Support/Compiler.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 {
Reid Spencer9133fe22007-02-05 23:32:05 +000037 class VISIBILITY_HIDDEN 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
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 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 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000063}
64
Dan Gohman844731a2008-05-13 00:00:25 +000065char StripSymbols::ID = 0;
66static RegisterPass<StripSymbols>
67X("strip", "Strip all symbols from a module");
68
Chris Lattnere3ad43c2004-12-02 21:25:03 +000069ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
70 return new StripSymbols(OnlyDebugInfo);
71}
72
Devang Patelf17fc462008-11-18 21:34:39 +000073char StripNonDebugSymbols::ID = 0;
74static RegisterPass<StripNonDebugSymbols>
75Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
76
77ModulePass *llvm::createStripNonDebugSymbolsPass() {
78 return new StripNonDebugSymbols();
79}
80
Devang Patelbf5db812008-11-13 01:28:40 +000081/// OnlyUsedBy - Return true if V is only used by Usr.
82static bool OnlyUsedBy(Value *V, Value *Usr) {
83 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
84 User *U = *I;
85 if (U != Usr)
86 return false;
87 }
88 return true;
89}
90
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000091static void RemoveDeadConstant(Constant *C) {
92 assert(C->use_empty() && "Constant is not dead!");
Devang Patelbf5db812008-11-13 01:28:40 +000093 SmallPtrSet<Constant *, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000094 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
95 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patelbf5db812008-11-13 01:28:40 +000096 OnlyUsedBy(C->getOperand(i), C))
97 Operands.insert(C->getOperand(i));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000098 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +000099 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000100 GV->eraseFromParent();
101 }
102 else if (!isa<Function>(C))
Devang Patelf23de862008-11-20 01:20:42 +0000103 if (isa<CompositeType>(C->getType()))
104 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +0000105
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000106 // If the constant referenced anything, see if we can delete it as well.
Devang Patelbf5db812008-11-13 01:28:40 +0000107 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
108 OE = Operands.end(); OI != OE; ++OI)
109 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000110}
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000111
Chris Lattner7f1444b2007-02-07 06:22:45 +0000112// Strip the symbol table of its names.
113//
Devang Patelf17fc462008-11-18 21:34:39 +0000114static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner7f1444b2007-02-07 06:22:45 +0000115 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000116 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +0000117 ++VI;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000118 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Devang Patelf17fc462008-11-18 21:34:39 +0000119 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
120 // Set name to "", removing from symbol table!
121 V->setName("");
Chris Lattner7f1444b2007-02-07 06:22:45 +0000122 }
123 }
124}
125
126// Strip the symbol table of its names.
Devang Patelf17fc462008-11-18 21:34:39 +0000127static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
128 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
129 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
130 ++TI;
131 else
132 ST.remove(TI++);
133 }
Chris Lattner7f1444b2007-02-07 06:22:45 +0000134}
135
Devang Patel4460a7e2008-11-18 21:13:41 +0000136/// Find values that are marked as llvm.used.
137void findUsedValues(Module &M,
138 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
Devang Patel229de952008-11-14 22:49:37 +0000139 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
140 llvmUsedValues.insert(LLVMUsed);
141 // Collect values that are preserved as per explicit request.
142 // llvm.used is used to list these values.
143 if (ConstantArray *Inits =
144 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
145 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
146 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
147 llvmUsedValues.insert(GV);
148 else if (ConstantExpr *CE =
149 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
150 if (CE->getOpcode() == Instruction::BitCast)
151 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
152 llvmUsedValues.insert(GV);
Devang Patel8c231e52008-01-16 03:33:05 +0000153 }
154 }
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000155 }
Devang Patel4460a7e2008-11-18 21:13:41 +0000156}
157
158/// StripSymbolNames - Strip symbol names.
Devang Patelf17fc462008-11-18 21:34:39 +0000159bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel4460a7e2008-11-18 21:13:41 +0000160
161 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
162 findUsedValues(M, llvmUsedValues);
163
Devang Patel229de952008-11-14 22:49:37 +0000164 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
165 I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000166 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patelf17fc462008-11-18 21:34:39 +0000167 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
168 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel229de952008-11-14 22:49:37 +0000169 }
170
171 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000172 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patelf17fc462008-11-18 21:34:39 +0000173 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
174 I->setName(""); // Internal symbols can't participate in linkage
175 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel229de952008-11-14 22:49:37 +0000176 }
177
178 // Remove all names from types.
Devang Patelf17fc462008-11-18 21:34:39 +0000179 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000180
Devang Patel229de952008-11-14 22:49:37 +0000181 return true;
182}
183
184// StripDebugInfo - Strip debug info in the module if it exists.
185// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
186// llvm.dbg.region.end calls, and any globals they point to if now dead.
Devang Patelf17fc462008-11-18 21:34:39 +0000187bool StripDebugInfo(Module &M) {
Devang Patel229de952008-11-14 22:49:37 +0000188
Devang Patel73df3c92009-03-02 22:50:58 +0000189 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
190 findUsedValues(M, llvmUsedValues);
191
192 // Delete all dbg variables.
193 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
194 I != E; ++I) {
195 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
196 if (!GV) continue;
197 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
198 if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) {
199 GV->replaceAllUsesWith(UndefValue::get(GV->getType()));
200 }
201 }
202 }
203
Reid Spencer688b0492007-02-05 21:19:13 +0000204 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
205 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
206 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
207 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
208 Function *Declare = M.getFunction("llvm.dbg.declare");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000209
Devang Patel4460a7e2008-11-18 21:13:41 +0000210 std::vector<Constant*> DeadConstants;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000211
212 // Remove all of the calls to the debugger intrinsics, and remove them from
213 // the module.
214 if (FuncStart) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000215 while (!FuncStart->use_empty()) {
216 CallInst *CI = cast<CallInst>(FuncStart->use_back());
217 Value *Arg = CI->getOperand(1);
Jim Laskey4ca97572006-03-23 18:11:33 +0000218 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000219 CI->eraseFromParent();
220 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000221 if (Constant *C = dyn_cast<Constant>(Arg))
222 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000223 }
224 FuncStart->eraseFromParent();
225 }
226 if (StopPoint) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000227 while (!StopPoint->use_empty()) {
228 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyf4321a32006-03-13 13:07:37 +0000229 Value *Arg = CI->getOperand(3);
Jim Laskey4ca97572006-03-23 18:11:33 +0000230 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000231 CI->eraseFromParent();
232 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000233 if (Constant *C = dyn_cast<Constant>(Arg))
234 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000235 }
236 StopPoint->eraseFromParent();
237 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000238 if (RegionStart) {
239 while (!RegionStart->use_empty()) {
240 CallInst *CI = cast<CallInst>(RegionStart->use_back());
241 Value *Arg = CI->getOperand(1);
242 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
243 CI->eraseFromParent();
244 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000245 if (Constant *C = dyn_cast<Constant>(Arg))
246 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000247 }
248 RegionStart->eraseFromParent();
249 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000250 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000251 while (!RegionEnd->use_empty()) {
252 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000253 Value *Arg = CI->getOperand(1);
254 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000255 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000256 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000257 if (Constant *C = dyn_cast<Constant>(Arg))
258 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000259 }
260 RegionEnd->eraseFromParent();
261 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000262 if (Declare) {
263 while (!Declare->use_empty()) {
264 CallInst *CI = cast<CallInst>(Declare->use_back());
Devang Patelf23de862008-11-20 01:20:42 +0000265 Value *Arg1 = CI->getOperand(1);
266 Value *Arg2 = CI->getOperand(2);
Jim Laskey4ca97572006-03-23 18:11:33 +0000267 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
268 CI->eraseFromParent();
Devang Patelf23de862008-11-20 01:20:42 +0000269 if (Arg1->use_empty()) {
270 if (Constant *C = dyn_cast<Constant>(Arg1))
271 DeadConstants.push_back(C);
Devang Patel9adb01c2009-03-03 21:31:02 +0000272 else
273 RecursivelyDeleteTriviallyDeadInstructions(Arg1, NULL);
Devang Patelf23de862008-11-20 01:20:42 +0000274 }
275 if (Arg2->use_empty())
276 if (Constant *C = dyn_cast<Constant>(Arg2))
Devang Patel4460a7e2008-11-18 21:13:41 +0000277 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000278 }
279 Declare->eraseFromParent();
280 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000281
Devang Patelbf5db812008-11-13 01:28:40 +0000282 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
283 // but since we are removing all debug information, make them internal now.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000284 // FIXME: Use private linkage maybe?
Devang Patelbf5db812008-11-13 01:28:40 +0000285 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
286 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
287 GV->setLinkage(GlobalValue::InternalLinkage);
288
289 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
290 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
291 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patel4460a7e2008-11-18 21:13:41 +0000292
293 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
294 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
295 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patelbf5db812008-11-13 01:28:40 +0000296
297 // Delete all dbg variables.
Devang Patelfb5fd5a2008-11-19 00:22:02 +0000298 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
299 I != E; ++I) {
300 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
301 if (!GV) continue;
302 if (GV->use_empty() && llvmUsedValues.count(I) == 0
303 && (!GV->hasSection()
304 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
305 DeadConstants.push_back(GV);
306 }
Devang Patelbf5db812008-11-13 01:28:40 +0000307
Devang Patel4460a7e2008-11-18 21:13:41 +0000308 if (DeadConstants.empty())
Devang Patel229de952008-11-14 22:49:37 +0000309 return false;
310
Devang Patelbf5db812008-11-13 01:28:40 +0000311 // Delete any internal globals that were only used by the debugger intrinsics.
Devang Patel4460a7e2008-11-18 21:13:41 +0000312 while (!DeadConstants.empty()) {
313 Constant *C = DeadConstants.back();
314 DeadConstants.pop_back();
315 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000316 if (GV->hasLocalLinkage())
Devang Patel4460a7e2008-11-18 21:13:41 +0000317 RemoveDeadConstant(GV);
318 }
319 else
320 RemoveDeadConstant(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000321 }
322
Devang Patelbf5db812008-11-13 01:28:40 +0000323 // Remove all llvm.dbg types.
324 TypeSymbolTable &ST = M.getTypeSymbolTable();
Chris Lattner3f914f02008-11-16 06:35:18 +0000325 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
326 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
Devang Patelbf5db812008-11-13 01:28:40 +0000327 ST.remove(TI++);
328 else
329 ++TI;
330 }
331
Misha Brukmanfd939082005-04-21 23:48:37 +0000332 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000333}
Devang Patelf17fc462008-11-18 21:34:39 +0000334
335bool StripSymbols::runOnModule(Module &M) {
336 bool Changed = false;
337 Changed |= StripDebugInfo(M);
338 if (!OnlyDebugInfo)
339 Changed |= StripSymbolNames(M, false);
340 return Changed;
341}
342
343bool StripNonDebugSymbols::runOnModule(Module &M) {
344 return StripSymbolNames(M, true);
345}