blob: d25e7580883accee486cbf51e40e8544fdb72849 [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"
Reid Spencer9133fe22007-02-05 23:32:05 +000031#include "llvm/Support/Compiler.h"
Devang Patel8c231e52008-01-16 03:33:05 +000032#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000033using namespace llvm;
34
35namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000036 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
Chris Lattnere3ad43c2004-12-02 21:25:03 +000037 bool OnlyDebugInfo;
38 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000040 explicit StripSymbols(bool ODI = false)
Dan Gohmanae73dc12008-09-04 17:05:41 +000041 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000042
Devang Patel229de952008-11-14 22:49:37 +000043 /// StripSymbolNames - Strip symbol names.
44 bool StripSymbolNames(Module &M);
45
46 // StripDebugInfo - Strip debug info in the module if it exists.
47 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
48 // llvm.dbg.region.end calls, and any globals they point to if now dead.
49 bool StripDebugInfo(Module &M);
50
Chris Lattnere3ad43c2004-12-02 21:25:03 +000051 virtual bool runOnModule(Module &M);
52
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.setPreservesAll();
55 }
56 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000057}
58
Dan Gohman844731a2008-05-13 00:00:25 +000059char StripSymbols::ID = 0;
60static RegisterPass<StripSymbols>
61X("strip", "Strip all symbols from a module");
62
Chris Lattnere3ad43c2004-12-02 21:25:03 +000063ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
64 return new StripSymbols(OnlyDebugInfo);
65}
66
Devang Patelbf5db812008-11-13 01:28:40 +000067/// OnlyUsedBy - Return true if V is only used by Usr.
68static bool OnlyUsedBy(Value *V, Value *Usr) {
69 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
70 User *U = *I;
71 if (U != Usr)
72 return false;
73 }
74 return true;
75}
76
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000077static void RemoveDeadConstant(Constant *C) {
78 assert(C->use_empty() && "Constant is not dead!");
Devang Patelbf5db812008-11-13 01:28:40 +000079 SmallPtrSet<Constant *, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000080 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
81 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patelbf5db812008-11-13 01:28:40 +000082 OnlyUsedBy(C->getOperand(i), C))
83 Operands.insert(C->getOperand(i));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000084 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
85 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
86 GV->eraseFromParent();
87 }
88 else if (!isa<Function>(C))
89 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +000090
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000091 // If the constant referenced anything, see if we can delete it as well.
Devang Patelbf5db812008-11-13 01:28:40 +000092 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
93 OE = Operands.end(); OI != OE; ++OI)
94 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000095}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000096
Chris Lattner7f1444b2007-02-07 06:22:45 +000097// Strip the symbol table of its names.
98//
99static void StripSymtab(ValueSymbolTable &ST) {
100 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000101 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +0000102 ++VI;
103 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
104 // Set name to "", removing from symbol table!
105 V->setName("");
106 }
107 }
108}
109
Devang Patel229de952008-11-14 22:49:37 +0000110bool StripSymbols::runOnModule(Module &M) {
111 bool Changed = false;
112 Changed |= StripDebugInfo(M);
113 Changed |= StripSymbolNames(M);
114 return Changed;
115}
116
Chris Lattner7f1444b2007-02-07 06:22:45 +0000117// Strip the symbol table of its names.
118static void StripTypeSymtab(TypeSymbolTable &ST) {
119 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
120 ST.remove(TI++);
121}
122
Devang Patel4460a7e2008-11-18 21:13:41 +0000123/// Find values that are marked as llvm.used.
124void findUsedValues(Module &M,
125 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
Devang Patel229de952008-11-14 22:49:37 +0000126 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
127 llvmUsedValues.insert(LLVMUsed);
128 // Collect values that are preserved as per explicit request.
129 // llvm.used is used to list these values.
130 if (ConstantArray *Inits =
131 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
132 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
133 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
134 llvmUsedValues.insert(GV);
135 else if (ConstantExpr *CE =
136 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
137 if (CE->getOpcode() == Instruction::BitCast)
138 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
139 llvmUsedValues.insert(GV);
Devang Patel8c231e52008-01-16 03:33:05 +0000140 }
141 }
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000142 }
Devang Patel4460a7e2008-11-18 21:13:41 +0000143}
144
145/// StripSymbolNames - Strip symbol names.
146bool StripSymbols::StripSymbolNames(Module &M) {
147
148 if (OnlyDebugInfo)
149 return false;
150
151 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
152 findUsedValues(M, llvmUsedValues);
153
Devang Patel229de952008-11-14 22:49:37 +0000154 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
155 I != E; ++I) {
156 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
157 I->setName(""); // Internal symbols can't participate in linkage
158 }
159
160 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
161 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
162 I->setName(""); // Internal symbols can't participate in linkage
163 StripSymtab(I->getValueSymbolTable());
164 }
165
166 // Remove all names from types.
167 StripTypeSymtab(M.getTypeSymbolTable());
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000168
Devang Patel229de952008-11-14 22:49:37 +0000169 return true;
170}
171
172// StripDebugInfo - Strip debug info in the module if it exists.
173// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
174// llvm.dbg.region.end calls, and any globals they point to if now dead.
175bool StripSymbols::StripDebugInfo(Module &M) {
176
Reid Spencer688b0492007-02-05 21:19:13 +0000177 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
178 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
179 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
180 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
181 Function *Declare = M.getFunction("llvm.dbg.declare");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000182
Devang Patel4460a7e2008-11-18 21:13:41 +0000183 std::vector<Constant*> DeadConstants;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000184
185 // Remove all of the calls to the debugger intrinsics, and remove them from
186 // the module.
187 if (FuncStart) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000188 while (!FuncStart->use_empty()) {
189 CallInst *CI = cast<CallInst>(FuncStart->use_back());
190 Value *Arg = CI->getOperand(1);
Jim Laskey4ca97572006-03-23 18:11:33 +0000191 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000192 CI->eraseFromParent();
193 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000194 if (Constant *C = dyn_cast<Constant>(Arg))
195 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000196 }
197 FuncStart->eraseFromParent();
198 }
199 if (StopPoint) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000200 while (!StopPoint->use_empty()) {
201 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyf4321a32006-03-13 13:07:37 +0000202 Value *Arg = CI->getOperand(3);
Jim Laskey4ca97572006-03-23 18:11:33 +0000203 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000204 CI->eraseFromParent();
205 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000206 if (Constant *C = dyn_cast<Constant>(Arg))
207 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000208 }
209 StopPoint->eraseFromParent();
210 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000211 if (RegionStart) {
212 while (!RegionStart->use_empty()) {
213 CallInst *CI = cast<CallInst>(RegionStart->use_back());
214 Value *Arg = CI->getOperand(1);
215 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
216 CI->eraseFromParent();
217 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000218 if (Constant *C = dyn_cast<Constant>(Arg))
219 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000220 }
221 RegionStart->eraseFromParent();
222 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000223 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000224 while (!RegionEnd->use_empty()) {
225 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000226 Value *Arg = CI->getOperand(1);
227 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000228 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000229 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000230 if (Constant *C = dyn_cast<Constant>(Arg))
231 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000232 }
233 RegionEnd->eraseFromParent();
234 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000235 if (Declare) {
236 while (!Declare->use_empty()) {
237 CallInst *CI = cast<CallInst>(Declare->use_back());
238 Value *Arg = CI->getOperand(2);
239 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
240 CI->eraseFromParent();
241 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000242 if (Constant *C = dyn_cast<GlobalVariable>(Arg))
243 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000244 }
245 Declare->eraseFromParent();
246 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000247
Devang Patel4460a7e2008-11-18 21:13:41 +0000248 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
249 findUsedValues(M, llvmUsedValues);
250
Devang Patelbf5db812008-11-13 01:28:40 +0000251 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
252 // but since we are removing all debug information, make them internal now.
253 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
254 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
255 GV->setLinkage(GlobalValue::InternalLinkage);
256
257 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
258 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
259 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patel4460a7e2008-11-18 21:13:41 +0000260
261 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
262 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
263 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patelbf5db812008-11-13 01:28:40 +0000264
265 // Delete all dbg variables.
266 const Type *DbgVTy = M.getTypeByName("llvm.dbg.variable.type");
267 const Type *DbgGVTy = M.getTypeByName("llvm.dbg.global_variable.type");
268 if (DbgVTy || DbgGVTy)
269 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Devang Patel4460a7e2008-11-18 21:13:41 +0000270 I != E; ++I) {
271 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
272 if (!GV) continue;
273 if (GV->use_empty() && llvmUsedValues.count(I) == 0
274 && (!GV->hasSection()
275 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
276 DeadConstants.push_back(GV);
277 }
Devang Patelbf5db812008-11-13 01:28:40 +0000278
Devang Patel4460a7e2008-11-18 21:13:41 +0000279 if (DeadConstants.empty())
Devang Patel229de952008-11-14 22:49:37 +0000280 return false;
281
Devang Patelbf5db812008-11-13 01:28:40 +0000282 // Delete any internal globals that were only used by the debugger intrinsics.
Devang Patel4460a7e2008-11-18 21:13:41 +0000283 while (!DeadConstants.empty()) {
284 Constant *C = DeadConstants.back();
285 DeadConstants.pop_back();
286 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
287 if (GV->hasInternalLinkage())
288 RemoveDeadConstant(GV);
289 }
290 else
291 RemoveDeadConstant(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000292 }
293
Devang Patelbf5db812008-11-13 01:28:40 +0000294 // Remove all llvm.dbg types.
295 TypeSymbolTable &ST = M.getTypeSymbolTable();
Chris Lattner3f914f02008-11-16 06:35:18 +0000296 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
297 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
Devang Patelbf5db812008-11-13 01:28:40 +0000298 ST.remove(TI++);
299 else
300 ++TI;
301 }
302
Misha Brukmanfd939082005-04-21 23:48:37 +0000303 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000304}