blob: 0bd1696f7f8d18d63d7d6f3e48bd9f5ced9149c2 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gordon Henriksen79ed5872007-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016//
Gordon Henriksen79ed5872007-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020//
21//===----------------------------------------------------------------------===//
22
23#include "llvm/Transforms/IPO.h"
24#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Instructions.h"
Owen Anderson086ea052009-07-06 01:34:54 +000027#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Module.h"
29#include "llvm/Pass.h"
Devang Patel166f8432009-06-26 01:49:18 +000030#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/ValueSymbolTable.h"
32#include "llvm/TypeSymbolTable.h"
Devang Patele68804a2009-03-03 21:31:02 +000033#include "llvm/Transforms/Utils/Local.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Support/Compiler.h"
Devang Patel5dc8fde2008-01-16 03:33:05 +000035#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036using namespace llvm;
37
38namespace {
39 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
40 bool OnlyDebugInfo;
41 public:
42 static char ID; // Pass identification, replacement for typeid
Dan Gohman34c280e2007-08-01 15:32:29 +000043 explicit StripSymbols(bool ODI = false)
Dan Gohman26f8c272008-09-04 17:05:41 +000044 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045
Devang Patel159f7ce2008-11-18 21:34:39 +000046 virtual bool runOnModule(Module &M);
Devang Patel2c98b522008-11-14 22:49:37 +000047
Devang Patel159f7ce2008-11-18 21:34:39 +000048 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.setPreservesAll();
50 }
51 };
52
53 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
54 public:
55 static char ID; // Pass identification, replacement for typeid
56 explicit StripNonDebugSymbols()
57 : ModulePass(&ID) {}
Devang Patel2c98b522008-11-14 22:49:37 +000058
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 virtual bool runOnModule(Module &M);
60
61 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
62 AU.setPreservesAll();
63 }
64 };
Devang Patel3876c8a2009-03-09 20:49:37 +000065
66 class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
67 public:
68 static char ID; // Pass identification, replacement for typeid
69 explicit StripDebugDeclare()
70 : ModulePass(&ID) {}
71
72 virtual bool runOnModule(Module &M);
73
74 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
75 AU.setPreservesAll();
76 }
77 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078}
79
Dan Gohman089efff2008-05-13 00:00:25 +000080char StripSymbols::ID = 0;
81static RegisterPass<StripSymbols>
82X("strip", "Strip all symbols from a module");
83
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
85 return new StripSymbols(OnlyDebugInfo);
86}
87
Devang Patel159f7ce2008-11-18 21:34:39 +000088char StripNonDebugSymbols::ID = 0;
89static RegisterPass<StripNonDebugSymbols>
90Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
91
92ModulePass *llvm::createStripNonDebugSymbolsPass() {
93 return new StripNonDebugSymbols();
94}
95
Devang Patel3876c8a2009-03-09 20:49:37 +000096char StripDebugDeclare::ID = 0;
97static RegisterPass<StripDebugDeclare>
98Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
99
100ModulePass *llvm::createStripDebugDeclarePass() {
101 return new StripDebugDeclare();
102}
103
Devang Patel2fa85562008-11-13 01:28:40 +0000104/// OnlyUsedBy - Return true if V is only used by Usr.
105static bool OnlyUsedBy(Value *V, Value *Usr) {
106 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
107 User *U = *I;
108 if (U != Usr)
109 return false;
110 }
111 return true;
112}
113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114static void RemoveDeadConstant(Constant *C) {
115 assert(C->use_empty() && "Constant is not dead!");
Devang Patel2fa85562008-11-13 01:28:40 +0000116 SmallPtrSet<Constant *, 4> Operands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
118 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patel2fa85562008-11-13 01:28:40 +0000119 OnlyUsedBy(C->getOperand(i), C))
120 Operands.insert(C->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000122 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 GV->eraseFromParent();
124 }
125 else if (!isa<Function>(C))
Devang Patelabd69112008-11-20 01:20:42 +0000126 if (isa<CompositeType>(C->getType()))
127 C->destroyConstant();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128
129 // If the constant referenced anything, see if we can delete it as well.
Devang Patel2fa85562008-11-13 01:28:40 +0000130 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
131 OE = Operands.end(); OI != OE; ++OI)
132 RemoveDeadConstant(*OI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133}
134
135// Strip the symbol table of its names.
136//
Devang Patel159f7ce2008-11-18 21:34:39 +0000137static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
139 Value *V = VI->getValue();
140 ++VI;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000141 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000142 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patel159f7ce2008-11-18 21:34:39 +0000143 // Set name to "", removing from symbol table!
144 V->setName("");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 }
146 }
147}
148
149// Strip the symbol table of its names.
Devang Patel159f7ce2008-11-18 21:34:39 +0000150static 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)
153 ++TI;
154 else
155 ST.remove(TI++);
156 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157}
158
Devang Patel2d53e2d2008-11-18 21:13:41 +0000159/// Find values that are marked as llvm.used.
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000160static void findUsedValues(GlobalVariable *LLVMUsed,
161 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
162 if (LLVMUsed == 0) return;
163 UsedValues.insert(LLVMUsed);
164
165 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
166 if (Inits == 0) return;
167
168 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
169 if (GlobalValue *GV =
170 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
171 UsedValues.insert(GV);
Devang Patel2d53e2d2008-11-18 21:13:41 +0000172}
173
174/// StripSymbolNames - Strip symbol names.
Dan Gohman6d29b322009-08-07 01:32:21 +0000175static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel2d53e2d2008-11-18 21:13:41 +0000176
177 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000178 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
179 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel2d53e2d2008-11-18 21:13:41 +0000180
Devang Patel2c98b522008-11-14 22:49:37 +0000181 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
182 I != E; ++I) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000183 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000184 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patel159f7ce2008-11-18 21:34:39 +0000185 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel2c98b522008-11-14 22:49:37 +0000186 }
187
188 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000189 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000190 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patel159f7ce2008-11-18 21:34:39 +0000191 I->setName(""); // Internal symbols can't participate in linkage
192 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel2c98b522008-11-14 22:49:37 +0000193 }
194
195 // Remove all names from types.
Devang Patel159f7ce2008-11-18 21:34:39 +0000196 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
Devang Patel2c98b522008-11-14 22:49:37 +0000198 return true;
199}
200
201// StripDebugInfo - Strip debug info in the module if it exists.
202// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
203// llvm.dbg.region.end calls, and any globals they point to if now dead.
Dan Gohman6d29b322009-08-07 01:32:21 +0000204static bool StripDebugInfo(Module &M) {
Devang Patel2c98b522008-11-14 22:49:37 +0000205
Devang Patel57b83c72009-08-25 05:24:07 +0000206 // Remove all of the calls to the debugger intrinsics, and remove them from
207 // the module.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
209 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
210 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
211 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
212 Function *Declare = M.getFunction("llvm.dbg.declare");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 if (FuncStart) {
215 while (!FuncStart->use_empty()) {
216 CallInst *CI = cast<CallInst>(FuncStart->use_back());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 CI->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 }
219 FuncStart->eraseFromParent();
220 }
221 if (StopPoint) {
222 while (!StopPoint->use_empty()) {
223 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 CI->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 }
226 StopPoint->eraseFromParent();
227 }
228 if (RegionStart) {
229 while (!RegionStart->use_empty()) {
230 CallInst *CI = cast<CallInst>(RegionStart->use_back());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 CI->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 }
233 RegionStart->eraseFromParent();
234 }
235 if (RegionEnd) {
236 while (!RegionEnd->use_empty()) {
237 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 CI->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 }
240 RegionEnd->eraseFromParent();
241 }
242 if (Declare) {
243 while (!Declare->use_empty()) {
244 CallInst *CI = cast<CallInst>(Declare->use_back());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 CI->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 }
247 Declare->eraseFromParent();
248 }
249
Devang Patel57b83c72009-08-25 05:24:07 +0000250 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
251 if (NMD)
252 NMD->eraseFromParent();
Devang Patel2fa85562008-11-13 01:28:40 +0000253
Devang Patel57b83c72009-08-25 05:24:07 +0000254 // Remove dead metadata.
255 M.getContext().RemoveDeadMetadata();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 return true;
257}
Devang Patel159f7ce2008-11-18 21:34:39 +0000258
259bool StripSymbols::runOnModule(Module &M) {
260 bool Changed = false;
261 Changed |= StripDebugInfo(M);
262 if (!OnlyDebugInfo)
263 Changed |= StripSymbolNames(M, false);
264 return Changed;
265}
266
267bool StripNonDebugSymbols::runOnModule(Module &M) {
268 return StripSymbolNames(M, true);
269}
Devang Patel3876c8a2009-03-09 20:49:37 +0000270
271bool StripDebugDeclare::runOnModule(Module &M) {
272
273 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel3876c8a2009-03-09 20:49:37 +0000274 std::vector<Constant*> DeadConstants;
275
Dale Johannesendd031b72009-03-13 22:59:47 +0000276 if (Declare) {
277 while (!Declare->use_empty()) {
278 CallInst *CI = cast<CallInst>(Declare->use_back());
279 Value *Arg1 = CI->getOperand(1);
280 Value *Arg2 = CI->getOperand(2);
281 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
282 CI->eraseFromParent();
283 if (Arg1->use_empty()) {
284 if (Constant *C = dyn_cast<Constant>(Arg1))
285 DeadConstants.push_back(C);
286 else
Dan Gohmandb2b9462009-05-02 20:22:10 +0000287 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesendd031b72009-03-13 22:59:47 +0000288 }
289 if (Arg2->use_empty())
290 if (Constant *C = dyn_cast<Constant>(Arg2))
291 DeadConstants.push_back(C);
Devang Patel3876c8a2009-03-09 20:49:37 +0000292 }
Dale Johannesendd031b72009-03-13 22:59:47 +0000293 Declare->eraseFromParent();
Devang Patel3876c8a2009-03-09 20:49:37 +0000294 }
Devang Patel3876c8a2009-03-09 20:49:37 +0000295
Devang Patel3a402b02009-03-09 21:32:28 +0000296 // Delete all llvm.dbg.global_variables.
297 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
298 I != E; ++I) {
299 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
300 if (!GV) continue;
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000301 if (GV->use_empty() && GV->getName().startswith("llvm.dbg.global_variable"))
Devang Patel3a402b02009-03-09 21:32:28 +0000302 DeadConstants.push_back(GV);
303 }
304
Devang Patel3876c8a2009-03-09 20:49:37 +0000305 while (!DeadConstants.empty()) {
306 Constant *C = DeadConstants.back();
307 DeadConstants.pop_back();
308 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
309 if (GV->hasLocalLinkage())
310 RemoveDeadConstant(GV);
311 }
312 else
313 RemoveDeadConstant(C);
314 }
315
316 return true;
317}