1 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The StripSymbols transformation implements code stripping. Specifically, it
13 // * names for virtual registers
14 // * symbols for internal globals and functions
15 // * debug information
17 // 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.
21 //===----------------------------------------------------------------------===//
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/Analysis/DebugInfo.h"
30 #include "llvm/ValueSymbolTable.h"
31 #include "llvm/TypeSymbolTable.h"
32 #include "llvm/Transforms/Utils/Local.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/ADT/SmallPtrSet.h"
38 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
41 static char ID; // Pass identification, replacement for typeid
42 explicit StripSymbols(bool ODI = false)
43 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
45 virtual bool runOnModule(Module &M);
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
54 static char ID; // Pass identification, replacement for typeid
55 explicit StripNonDebugSymbols()
58 virtual bool runOnModule(Module &M);
60 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
65 class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
67 static char ID; // Pass identification, replacement for typeid
68 explicit StripDebugDeclare()
71 virtual bool runOnModule(Module &M);
73 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
79 char StripSymbols::ID = 0;
80 static RegisterPass<StripSymbols>
81 X("strip", "Strip all symbols from a module");
83 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
84 return new StripSymbols(OnlyDebugInfo);
87 char StripNonDebugSymbols::ID = 0;
88 static RegisterPass<StripNonDebugSymbols>
89 Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
91 ModulePass *llvm::createStripNonDebugSymbolsPass() {
92 return new StripNonDebugSymbols();
95 char StripDebugDeclare::ID = 0;
96 static RegisterPass<StripDebugDeclare>
97 Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
99 ModulePass *llvm::createStripDebugDeclarePass() {
100 return new StripDebugDeclare();
103 /// OnlyUsedBy - Return true if V is only used by Usr.
104 static bool OnlyUsedBy(Value *V, Value *Usr) {
105 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
113 static void RemoveDeadConstant(Constant *C) {
114 assert(C->use_empty() && "Constant is not dead!");
115 SmallPtrSet<Constant *, 4> Operands;
116 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
117 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
118 OnlyUsedBy(C->getOperand(i), C))
119 Operands.insert(C->getOperand(i));
120 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
121 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
122 GV->eraseFromParent();
124 else if (!isa<Function>(C))
125 if (isa<CompositeType>(C->getType()))
126 C->destroyConstant();
128 // If the constant referenced anything, see if we can delete it as well.
129 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
130 OE = Operands.end(); OI != OE; ++OI)
131 RemoveDeadConstant(*OI);
134 // Strip the symbol table of its names.
136 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
137 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
138 Value *V = VI->getValue();
140 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
141 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
142 // Set name to "", removing from symbol table!
148 // Strip the symbol table of its names.
149 static 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)
158 /// Find values that are marked as llvm.used.
159 void findUsedValues(Module &M,
160 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
161 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
162 llvmUsedValues.insert(LLVMUsed);
163 // Collect values that are preserved as per explicit request.
164 // llvm.used is used to list these values.
165 if (ConstantArray *Inits =
166 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
167 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
168 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
169 llvmUsedValues.insert(GV);
170 else if (ConstantExpr *CE =
171 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
172 if (CE->getOpcode() == Instruction::BitCast)
173 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
174 llvmUsedValues.insert(GV);
180 /// StripSymbolNames - Strip symbol names.
181 bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
183 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
184 findUsedValues(M, llvmUsedValues);
186 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
188 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
189 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
190 I->setName(""); // Internal symbols can't participate in linkage
193 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
194 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
195 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
196 I->setName(""); // Internal symbols can't participate in linkage
197 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
200 // Remove all names from types.
201 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
206 // StripDebugInfo - Strip debug info in the module if it exists.
207 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
208 // llvm.dbg.region.end calls, and any globals they point to if now dead.
209 bool StripDebugInfo(Module &M) {
211 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
212 findUsedValues(M, llvmUsedValues);
214 SmallVector<GlobalVariable *, 2> CUs;
215 SmallVector<GlobalVariable *, 4> GVs;
216 SmallVector<GlobalVariable *, 4> SPs;
217 CollectDebugInfoAnchors(M, CUs, GVs, SPs);
218 // These anchors use LinkOnce linkage so that the optimizer does not
219 // remove them accidently. Set InternalLinkage for all these debug
221 for (SmallVector<GlobalVariable *, 2>::iterator I = CUs.begin(),
222 E = CUs.end(); I != E; ++I)
223 (*I)->setLinkage(GlobalValue::InternalLinkage);
224 for (SmallVector<GlobalVariable *, 4>::iterator I = GVs.begin(),
225 E = GVs.end(); I != E; ++I)
226 (*I)->setLinkage(GlobalValue::InternalLinkage);
227 for (SmallVector<GlobalVariable *, 4>::iterator I = SPs.begin(),
228 E = SPs.end(); I != E; ++I)
229 (*I)->setLinkage(GlobalValue::InternalLinkage);
232 // Delete all dbg variables.
233 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
235 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
237 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
238 if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) {
239 GV->replaceAllUsesWith(UndefValue::get(GV->getType()));
244 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
245 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
246 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
247 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
248 Function *Declare = M.getFunction("llvm.dbg.declare");
250 std::vector<Constant*> DeadConstants;
252 // Remove all of the calls to the debugger intrinsics, and remove them from
255 while (!FuncStart->use_empty()) {
256 CallInst *CI = cast<CallInst>(FuncStart->use_back());
257 Value *Arg = CI->getOperand(1);
258 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
259 CI->eraseFromParent();
260 if (Arg->use_empty())
261 if (Constant *C = dyn_cast<Constant>(Arg))
262 DeadConstants.push_back(C);
264 FuncStart->eraseFromParent();
267 while (!StopPoint->use_empty()) {
268 CallInst *CI = cast<CallInst>(StopPoint->use_back());
269 Value *Arg = CI->getOperand(3);
270 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
271 CI->eraseFromParent();
272 if (Arg->use_empty())
273 if (Constant *C = dyn_cast<Constant>(Arg))
274 DeadConstants.push_back(C);
276 StopPoint->eraseFromParent();
279 while (!RegionStart->use_empty()) {
280 CallInst *CI = cast<CallInst>(RegionStart->use_back());
281 Value *Arg = CI->getOperand(1);
282 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
283 CI->eraseFromParent();
284 if (Arg->use_empty())
285 if (Constant *C = dyn_cast<Constant>(Arg))
286 DeadConstants.push_back(C);
288 RegionStart->eraseFromParent();
291 while (!RegionEnd->use_empty()) {
292 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
293 Value *Arg = CI->getOperand(1);
294 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
295 CI->eraseFromParent();
296 if (Arg->use_empty())
297 if (Constant *C = dyn_cast<Constant>(Arg))
298 DeadConstants.push_back(C);
300 RegionEnd->eraseFromParent();
303 while (!Declare->use_empty()) {
304 CallInst *CI = cast<CallInst>(Declare->use_back());
305 Value *Arg1 = CI->getOperand(1);
306 Value *Arg2 = CI->getOperand(2);
307 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
308 CI->eraseFromParent();
309 if (Arg1->use_empty()) {
310 if (Constant *C = dyn_cast<Constant>(Arg1))
311 DeadConstants.push_back(C);
313 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
315 if (Arg2->use_empty())
316 if (Constant *C = dyn_cast<Constant>(Arg2))
317 DeadConstants.push_back(C);
319 Declare->eraseFromParent();
322 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
323 // but since we are removing all debug information, make them internal now.
324 // FIXME: Use private linkage maybe?
325 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
326 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
327 GV->setLinkage(GlobalValue::InternalLinkage);
329 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
330 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
331 GV->setLinkage(GlobalValue::InternalLinkage);
333 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
334 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
335 GV->setLinkage(GlobalValue::InternalLinkage);
337 // Delete all dbg variables.
338 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
340 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
342 if (GV->use_empty() && llvmUsedValues.count(I) == 0
343 && (!GV->hasSection()
344 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
345 DeadConstants.push_back(GV);
348 if (DeadConstants.empty())
351 // Delete any internal globals that were only used by the debugger intrinsics.
352 while (!DeadConstants.empty()) {
353 Constant *C = DeadConstants.back();
354 DeadConstants.pop_back();
355 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
356 if (GV->hasLocalLinkage())
357 RemoveDeadConstant(GV);
360 RemoveDeadConstant(C);
363 // Remove all llvm.dbg types.
364 TypeSymbolTable &ST = M.getTypeSymbolTable();
365 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
366 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
375 bool StripSymbols::runOnModule(Module &M) {
376 bool Changed = false;
377 Changed |= StripDebugInfo(M);
379 Changed |= StripSymbolNames(M, false);
383 bool StripNonDebugSymbols::runOnModule(Module &M) {
384 return StripSymbolNames(M, true);
387 bool StripDebugDeclare::runOnModule(Module &M) {
389 Function *Declare = M.getFunction("llvm.dbg.declare");
390 std::vector<Constant*> DeadConstants;
393 while (!Declare->use_empty()) {
394 CallInst *CI = cast<CallInst>(Declare->use_back());
395 Value *Arg1 = CI->getOperand(1);
396 Value *Arg2 = CI->getOperand(2);
397 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
398 CI->eraseFromParent();
399 if (Arg1->use_empty()) {
400 if (Constant *C = dyn_cast<Constant>(Arg1))
401 DeadConstants.push_back(C);
403 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
405 if (Arg2->use_empty())
406 if (Constant *C = dyn_cast<Constant>(Arg2))
407 DeadConstants.push_back(C);
409 Declare->eraseFromParent();
412 // Delete all llvm.dbg.global_variables.
413 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
415 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
417 if (GV->use_empty() && GV->hasName()
418 && strncmp(GV->getNameStart(), "llvm.dbg.global_variable", 24) == 0)
419 DeadConstants.push_back(GV);
422 while (!DeadConstants.empty()) {
423 Constant *C = DeadConstants.back();
424 DeadConstants.pop_back();
425 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
426 if (GV->hasLocalLinkage())
427 RemoveDeadConstant(GV);
430 RemoveDeadConstant(C);