blob: 57aaf43c65add176123a24091970ba362507bff1 [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"
Owen Anderson14ce9ef2009-07-06 01:34:54 +000027#include "llvm/LLVMContext.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000028#include "llvm/Module.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000029#include "llvm/Pass.h"
Devang Patel13e16b62009-06-26 01:49:18 +000030#include "llvm/Analysis/DebugInfo.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000031#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000032#include "llvm/TypeSymbolTable.h"
Devang Patel9adb01c2009-03-03 21:31:02 +000033#include "llvm/Transforms/Utils/Local.h"
Devang Patel8c231e52008-01-16 03:33:05 +000034#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000035using namespace llvm;
36
37namespace {
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000038 class StripSymbols : public ModulePass {
Chris Lattnere3ad43c2004-12-02 21:25:03 +000039 bool OnlyDebugInfo;
40 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000041 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000042 explicit StripSymbols(bool ODI = false)
Dan Gohmanae73dc12008-09-04 17:05:41 +000043 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000044
Devang Patelf17fc462008-11-18 21:34:39 +000045 virtual bool runOnModule(Module &M);
Devang Patel229de952008-11-14 22:49:37 +000046
Devang Patelf17fc462008-11-18 21:34:39 +000047 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.setPreservesAll();
49 }
50 };
51
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000052 class StripNonDebugSymbols : public ModulePass {
Devang Patelf17fc462008-11-18 21:34:39 +000053 public:
54 static char ID; // Pass identification, replacement for typeid
55 explicit StripNonDebugSymbols()
56 : ModulePass(&ID) {}
Devang Patel229de952008-11-14 22:49:37 +000057
Chris Lattnere3ad43c2004-12-02 21:25:03 +000058 virtual bool runOnModule(Module &M);
59
60 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.setPreservesAll();
62 }
63 };
Devang Patel23e528b2009-03-09 20:49:37 +000064
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000065 class StripDebugDeclare : public ModulePass {
Devang Patel23e528b2009-03-09 20:49:37 +000066 public:
67 static char ID; // Pass identification, replacement for typeid
68 explicit StripDebugDeclare()
69 : ModulePass(&ID) {}
70
71 virtual bool runOnModule(Module &M);
72
73 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74 AU.setPreservesAll();
75 }
76 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000077}
78
Dan Gohman844731a2008-05-13 00:00:25 +000079char StripSymbols::ID = 0;
80static RegisterPass<StripSymbols>
81X("strip", "Strip all symbols from a module");
82
Chris Lattnere3ad43c2004-12-02 21:25:03 +000083ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
84 return new StripSymbols(OnlyDebugInfo);
85}
86
Devang Patelf17fc462008-11-18 21:34:39 +000087char StripNonDebugSymbols::ID = 0;
88static RegisterPass<StripNonDebugSymbols>
89Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
90
91ModulePass *llvm::createStripNonDebugSymbolsPass() {
92 return new StripNonDebugSymbols();
93}
94
Devang Patel23e528b2009-03-09 20:49:37 +000095char StripDebugDeclare::ID = 0;
96static RegisterPass<StripDebugDeclare>
97Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
98
99ModulePass *llvm::createStripDebugDeclarePass() {
100 return new StripDebugDeclare();
101}
102
Devang Patelbf5db812008-11-13 01:28:40 +0000103/// OnlyUsedBy - Return true if V is only used by Usr.
104static bool OnlyUsedBy(Value *V, Value *Usr) {
105 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
106 User *U = *I;
107 if (U != Usr)
108 return false;
109 }
110 return true;
111}
112
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000113static void RemoveDeadConstant(Constant *C) {
114 assert(C->use_empty() && "Constant is not dead!");
Devang Patelbf5db812008-11-13 01:28:40 +0000115 SmallPtrSet<Constant *, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000116 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
117 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patelbf5db812008-11-13 01:28:40 +0000118 OnlyUsedBy(C->getOperand(i), C))
119 Operands.insert(C->getOperand(i));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000120 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000121 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000122 GV->eraseFromParent();
123 }
124 else if (!isa<Function>(C))
Devang Patelf23de862008-11-20 01:20:42 +0000125 if (isa<CompositeType>(C->getType()))
126 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +0000127
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000128 // If the constant referenced anything, see if we can delete it as well.
Devang Patelbf5db812008-11-13 01:28:40 +0000129 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
130 OE = Operands.end(); OI != OE; ++OI)
131 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000132}
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000133
Chris Lattner7f1444b2007-02-07 06:22:45 +0000134// Strip the symbol table of its names.
135//
Devang Patelf17fc462008-11-18 21:34:39 +0000136static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner7f1444b2007-02-07 06:22:45 +0000137 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000138 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +0000139 ++VI;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000140 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar460f6562009-07-26 09:48:23 +0000141 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000142 // Set name to "", removing from symbol table!
143 V->setName("");
Chris Lattner7f1444b2007-02-07 06:22:45 +0000144 }
145 }
146}
147
148// Strip the symbol table of its names.
Devang Patelf17fc462008-11-18 21:34:39 +0000149static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
150 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
151 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
152 ++TI;
153 else
154 ST.remove(TI++);
155 }
Chris Lattner7f1444b2007-02-07 06:22:45 +0000156}
157
Devang Patel4460a7e2008-11-18 21:13:41 +0000158/// Find values that are marked as llvm.used.
Chris Lattner401e10c2009-07-20 06:14:25 +0000159static void findUsedValues(GlobalVariable *LLVMUsed,
160 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
161 if (LLVMUsed == 0) return;
162 UsedValues.insert(LLVMUsed);
163
164 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
165 if (Inits == 0) return;
166
167 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
168 if (GlobalValue *GV =
169 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
170 UsedValues.insert(GV);
Devang Patel4460a7e2008-11-18 21:13:41 +0000171}
172
173/// StripSymbolNames - Strip symbol names.
Dan Gohman7db949d2009-08-07 01:32:21 +0000174static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel4460a7e2008-11-18 21:13:41 +0000175
176 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner401e10c2009-07-20 06:14:25 +0000177 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
178 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel4460a7e2008-11-18 21:13:41 +0000179
Devang Patel229de952008-11-14 22:49:37 +0000180 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
181 I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000182 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000183 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000184 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel229de952008-11-14 22:49:37 +0000185 }
186
187 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000188 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000189 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000190 I->setName(""); // Internal symbols can't participate in linkage
191 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel229de952008-11-14 22:49:37 +0000192 }
193
194 // Remove all names from types.
Devang Patelf17fc462008-11-18 21:34:39 +0000195 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000196
Devang Patel229de952008-11-14 22:49:37 +0000197 return true;
198}
199
200// StripDebugInfo - Strip debug info in the module if it exists.
201// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
202// llvm.dbg.region.end calls, and any globals they point to if now dead.
Dan Gohman7db949d2009-08-07 01:32:21 +0000203static bool StripDebugInfo(Module &M) {
Devang Patel229de952008-11-14 22:49:37 +0000204
Devang Patele4b27562009-08-28 23:24:31 +0000205 // Remove all of the calls to the debugger intrinsics, and remove them from
206 // the module.
Reid Spencer688b0492007-02-05 21:19:13 +0000207 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
208 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
209 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
210 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
211 Function *Declare = M.getFunction("llvm.dbg.declare");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000212
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000213 if (FuncStart) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000214 while (!FuncStart->use_empty()) {
215 CallInst *CI = cast<CallInst>(FuncStart->use_back());
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000216 CI->eraseFromParent();
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000217 }
218 FuncStart->eraseFromParent();
219 }
220 if (StopPoint) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000221 while (!StopPoint->use_empty()) {
222 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000223 CI->eraseFromParent();
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000224 }
225 StopPoint->eraseFromParent();
226 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000227 if (RegionStart) {
228 while (!RegionStart->use_empty()) {
229 CallInst *CI = cast<CallInst>(RegionStart->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000230 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000231 }
232 RegionStart->eraseFromParent();
233 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000234 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000235 while (!RegionEnd->use_empty()) {
236 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000237 CI->eraseFromParent();
238 }
239 RegionEnd->eraseFromParent();
240 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000241 if (Declare) {
242 while (!Declare->use_empty()) {
243 CallInst *CI = cast<CallInst>(Declare->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000244 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000245 }
246 Declare->eraseFromParent();
247 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000248
Devang Patele4b27562009-08-28 23:24:31 +0000249 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
250 if (NMD)
251 NMD->eraseFromParent();
Devang Patelbf5db812008-11-13 01:28:40 +0000252
Misha Brukmanfd939082005-04-21 23:48:37 +0000253 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000254}
Devang Patelf17fc462008-11-18 21:34:39 +0000255
256bool StripSymbols::runOnModule(Module &M) {
257 bool Changed = false;
258 Changed |= StripDebugInfo(M);
259 if (!OnlyDebugInfo)
260 Changed |= StripSymbolNames(M, false);
261 return Changed;
262}
263
264bool StripNonDebugSymbols::runOnModule(Module &M) {
265 return StripSymbolNames(M, true);
266}
Devang Patel23e528b2009-03-09 20:49:37 +0000267
268bool StripDebugDeclare::runOnModule(Module &M) {
269
270 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel23e528b2009-03-09 20:49:37 +0000271 std::vector<Constant*> DeadConstants;
272
Dale Johannesen44252402009-03-13 22:59:47 +0000273 if (Declare) {
274 while (!Declare->use_empty()) {
275 CallInst *CI = cast<CallInst>(Declare->use_back());
276 Value *Arg1 = CI->getOperand(1);
277 Value *Arg2 = CI->getOperand(2);
278 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
279 CI->eraseFromParent();
280 if (Arg1->use_empty()) {
281 if (Constant *C = dyn_cast<Constant>(Arg1))
282 DeadConstants.push_back(C);
283 else
Dan Gohmane66f6f12009-05-02 20:22:10 +0000284 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesen44252402009-03-13 22:59:47 +0000285 }
286 if (Arg2->use_empty())
287 if (Constant *C = dyn_cast<Constant>(Arg2))
288 DeadConstants.push_back(C);
Devang Patel23e528b2009-03-09 20:49:37 +0000289 }
Dale Johannesen44252402009-03-13 22:59:47 +0000290 Declare->eraseFromParent();
Devang Patel23e528b2009-03-09 20:49:37 +0000291 }
Devang Patel23e528b2009-03-09 20:49:37 +0000292
Devang Pateld07128c2009-03-09 21:32:28 +0000293 // Delete all llvm.dbg.global_variables.
294 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
295 I != E; ++I) {
296 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
297 if (!GV) continue;
Daniel Dunbar460f6562009-07-26 09:48:23 +0000298 if (GV->use_empty() && GV->getName().startswith("llvm.dbg.global_variable"))
Devang Pateld07128c2009-03-09 21:32:28 +0000299 DeadConstants.push_back(GV);
300 }
301
Devang Patel23e528b2009-03-09 20:49:37 +0000302 while (!DeadConstants.empty()) {
303 Constant *C = DeadConstants.back();
304 DeadConstants.pop_back();
305 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
306 if (GV->hasLocalLinkage())
307 RemoveDeadConstant(GV);
308 }
309 else
310 RemoveDeadConstant(C);
311 }
312
313 return true;
314}