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/LLVMContext.h"
28 #include "llvm/Module.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Analysis/DebugInfo.h"
31 #include "llvm/ValueSymbolTable.h"
32 #include "llvm/TypeSymbolTable.h"
33 #include "llvm/Transforms/Utils/Local.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/ADT/SmallPtrSet.h"
39 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
42 static char ID; // Pass identification, replacement for typeid
43 explicit StripSymbols(bool ODI = false)
44 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
46 virtual bool runOnModule(Module &M);
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
55 static char ID; // Pass identification, replacement for typeid
56 explicit StripNonDebugSymbols()
59 virtual bool runOnModule(Module &M);
61 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
66 class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
68 static char ID; // Pass identification, replacement for typeid
69 explicit StripDebugDeclare()
72 virtual bool runOnModule(Module &M);
74 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
80 char StripSymbols::ID = 0;
81 static RegisterPass<StripSymbols>
82 X("strip", "Strip all symbols from a module");
84 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
85 return new StripSymbols(OnlyDebugInfo);
88 char StripNonDebugSymbols::ID = 0;
89 static RegisterPass<StripNonDebugSymbols>
90 Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
92 ModulePass *llvm::createStripNonDebugSymbolsPass() {
93 return new StripNonDebugSymbols();
96 char StripDebugDeclare::ID = 0;
97 static RegisterPass<StripDebugDeclare>
98 Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
100 ModulePass *llvm::createStripDebugDeclarePass() {
101 return new StripDebugDeclare();
104 /// OnlyUsedBy - Return true if V is only used by Usr.
105 static bool OnlyUsedBy(Value *V, Value *Usr) {
106 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
114 static void RemoveDeadConstant(Constant *C) {
115 assert(C->use_empty() && "Constant is not dead!");
116 SmallPtrSet<Constant *, 4> Operands;
117 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
118 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
119 OnlyUsedBy(C->getOperand(i), C))
120 Operands.insert(C->getOperand(i));
121 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
122 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
123 GV->eraseFromParent();
125 else if (!isa<Function>(C))
126 if (isa<CompositeType>(C->getType()))
127 C->destroyConstant();
129 // If the constant referenced anything, see if we can delete it as well.
130 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
131 OE = Operands.end(); OI != OE; ++OI)
132 RemoveDeadConstant(*OI);
135 // Strip the symbol table of its names.
137 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
138 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
139 Value *V = VI->getValue();
141 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
142 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
143 // Set name to "", removing from symbol table!
149 // Strip the symbol table of its names.
150 static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
151 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
152 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
159 /// Find values that are marked as llvm.used.
160 void findUsedValues(Module &M,
161 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
162 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
163 llvmUsedValues.insert(LLVMUsed);
164 // Collect values that are preserved as per explicit request.
165 // llvm.used is used to list these values.
166 if (ConstantArray *Inits =
167 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
168 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
169 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
170 llvmUsedValues.insert(GV);
171 else if (ConstantExpr *CE =
172 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
173 if (CE->getOpcode() == Instruction::BitCast)
174 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
175 llvmUsedValues.insert(GV);
181 /// StripSymbolNames - Strip symbol names.
182 bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
184 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
185 findUsedValues(M, llvmUsedValues);
187 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
189 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
190 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
191 I->setName(""); // Internal symbols can't participate in linkage
194 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
195 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
196 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
197 I->setName(""); // Internal symbols can't participate in linkage
198 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
201 // Remove all names from types.
202 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
207 // StripDebugInfo - Strip debug info in the module if it exists.
208 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
209 // llvm.dbg.region.end calls, and any globals they point to if now dead.
210 bool StripDebugInfo(Module &M) {
212 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
213 findUsedValues(M, llvmUsedValues);
215 SmallVector<GlobalVariable *, 2> CUs;
216 SmallVector<GlobalVariable *, 4> GVs;
217 SmallVector<GlobalVariable *, 4> SPs;
218 CollectDebugInfoAnchors(M, CUs, GVs, SPs);
219 // These anchors use LinkOnce linkage so that the optimizer does not
220 // remove them accidently. Set InternalLinkage for all these debug
222 for (SmallVector<GlobalVariable *, 2>::iterator I = CUs.begin(),
223 E = CUs.end(); I != E; ++I)
224 (*I)->setLinkage(GlobalValue::InternalLinkage);
225 for (SmallVector<GlobalVariable *, 4>::iterator I = GVs.begin(),
226 E = GVs.end(); I != E; ++I)
227 (*I)->setLinkage(GlobalValue::InternalLinkage);
228 for (SmallVector<GlobalVariable *, 4>::iterator I = SPs.begin(),
229 E = SPs.end(); I != E; ++I)
230 (*I)->setLinkage(GlobalValue::InternalLinkage);
233 // Delete all dbg variables.
234 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
236 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
238 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
239 if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) {
240 GV->replaceAllUsesWith(M.getContext().getUndef(GV->getType()));
245 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
246 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
247 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
248 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
249 Function *Declare = M.getFunction("llvm.dbg.declare");
251 std::vector<Constant*> DeadConstants;
253 // Remove all of the calls to the debugger intrinsics, and remove them from
256 while (!FuncStart->use_empty()) {
257 CallInst *CI = cast<CallInst>(FuncStart->use_back());
258 Value *Arg = CI->getOperand(1);
259 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
260 CI->eraseFromParent();
261 if (Arg->use_empty())
262 if (Constant *C = dyn_cast<Constant>(Arg))
263 DeadConstants.push_back(C);
265 FuncStart->eraseFromParent();
268 while (!StopPoint->use_empty()) {
269 CallInst *CI = cast<CallInst>(StopPoint->use_back());
270 Value *Arg = CI->getOperand(3);
271 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
272 CI->eraseFromParent();
273 if (Arg->use_empty())
274 if (Constant *C = dyn_cast<Constant>(Arg))
275 DeadConstants.push_back(C);
277 StopPoint->eraseFromParent();
280 while (!RegionStart->use_empty()) {
281 CallInst *CI = cast<CallInst>(RegionStart->use_back());
282 Value *Arg = CI->getOperand(1);
283 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
284 CI->eraseFromParent();
285 if (Arg->use_empty())
286 if (Constant *C = dyn_cast<Constant>(Arg))
287 DeadConstants.push_back(C);
289 RegionStart->eraseFromParent();
292 while (!RegionEnd->use_empty()) {
293 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
294 Value *Arg = CI->getOperand(1);
295 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
296 CI->eraseFromParent();
297 if (Arg->use_empty())
298 if (Constant *C = dyn_cast<Constant>(Arg))
299 DeadConstants.push_back(C);
301 RegionEnd->eraseFromParent();
304 while (!Declare->use_empty()) {
305 CallInst *CI = cast<CallInst>(Declare->use_back());
306 Value *Arg1 = CI->getOperand(1);
307 Value *Arg2 = CI->getOperand(2);
308 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
309 CI->eraseFromParent();
310 if (Arg1->use_empty()) {
311 if (Constant *C = dyn_cast<Constant>(Arg1))
312 DeadConstants.push_back(C);
314 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
316 if (Arg2->use_empty())
317 if (Constant *C = dyn_cast<Constant>(Arg2))
318 DeadConstants.push_back(C);
320 Declare->eraseFromParent();
323 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
324 // but since we are removing all debug information, make them internal now.
325 // FIXME: Use private linkage maybe?
326 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
327 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
328 GV->setLinkage(GlobalValue::InternalLinkage);
330 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
331 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
332 GV->setLinkage(GlobalValue::InternalLinkage);
334 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
335 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
336 GV->setLinkage(GlobalValue::InternalLinkage);
338 // Delete all dbg variables.
339 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
341 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
343 if (GV->use_empty() && llvmUsedValues.count(I) == 0
344 && (!GV->hasSection()
345 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
346 DeadConstants.push_back(GV);
349 if (DeadConstants.empty())
352 // Delete any internal globals that were only used by the debugger intrinsics.
353 while (!DeadConstants.empty()) {
354 Constant *C = DeadConstants.back();
355 DeadConstants.pop_back();
356 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
357 if (GV->hasLocalLinkage())
358 RemoveDeadConstant(GV);
361 RemoveDeadConstant(C);
364 // Remove all llvm.dbg types.
365 TypeSymbolTable &ST = M.getTypeSymbolTable();
366 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
367 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
376 bool StripSymbols::runOnModule(Module &M) {
377 Context = &M.getContext();
378 bool Changed = false;
379 Changed |= StripDebugInfo(M);
381 Changed |= StripSymbolNames(M, false);
385 bool StripNonDebugSymbols::runOnModule(Module &M) {
386 return StripSymbolNames(M, true);
389 bool StripDebugDeclare::runOnModule(Module &M) {
391 Function *Declare = M.getFunction("llvm.dbg.declare");
392 std::vector<Constant*> DeadConstants;
395 while (!Declare->use_empty()) {
396 CallInst *CI = cast<CallInst>(Declare->use_back());
397 Value *Arg1 = CI->getOperand(1);
398 Value *Arg2 = CI->getOperand(2);
399 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
400 CI->eraseFromParent();
401 if (Arg1->use_empty()) {
402 if (Constant *C = dyn_cast<Constant>(Arg1))
403 DeadConstants.push_back(C);
405 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
407 if (Arg2->use_empty())
408 if (Constant *C = dyn_cast<Constant>(Arg2))
409 DeadConstants.push_back(C);
411 Declare->eraseFromParent();
414 // Delete all llvm.dbg.global_variables.
415 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
417 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
419 if (GV->use_empty() && GV->hasName()
420 && strncmp(GV->getNameStart(), "llvm.dbg.global_variable", 24) == 0)
421 DeadConstants.push_back(GV);
424 while (!DeadConstants.empty()) {
425 Constant *C = DeadConstants.back();
426 DeadConstants.pop_back();
427 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
428 if (GV->hasLocalLinkage())
429 RemoveDeadConstant(GV);
432 RemoveDeadConstant(C);