blob: b213b014eb2902b154e5c74cf6d106f71415ba30 [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"
Devang Patel5dc8fde2008-01-16 03:33:05 +000034#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035using namespace llvm;
36
37namespace {
Nick Lewycky04791af2009-09-03 06:43:15 +000038 class StripSymbols : public ModulePass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 bool OnlyDebugInfo;
40 public:
41 static char ID; // Pass identification, replacement for typeid
Dan Gohman34c280e2007-08-01 15:32:29 +000042 explicit StripSymbols(bool ODI = false)
Dan Gohman26f8c272008-09-04 17:05:41 +000043 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
Devang Patel159f7ce2008-11-18 21:34:39 +000045 virtual bool runOnModule(Module &M);
Devang Patel2c98b522008-11-14 22:49:37 +000046
Devang Patel159f7ce2008-11-18 21:34:39 +000047 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.setPreservesAll();
49 }
50 };
51
Nick Lewycky04791af2009-09-03 06:43:15 +000052 class StripNonDebugSymbols : public ModulePass {
Devang Patel159f7ce2008-11-18 21:34:39 +000053 public:
54 static char ID; // Pass identification, replacement for typeid
55 explicit StripNonDebugSymbols()
56 : ModulePass(&ID) {}
Devang Patel2c98b522008-11-14 22:49:37 +000057
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 virtual bool runOnModule(Module &M);
59
60 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.setPreservesAll();
62 }
63 };
Devang Patel3876c8a2009-03-09 20:49:37 +000064
Nick Lewycky04791af2009-09-03 06:43:15 +000065 class StripDebugDeclare : public ModulePass {
Devang Patel3876c8a2009-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 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077}
78
Dan Gohman089efff2008-05-13 00:00:25 +000079char StripSymbols::ID = 0;
80static RegisterPass<StripSymbols>
81X("strip", "Strip all symbols from a module");
82
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
84 return new StripSymbols(OnlyDebugInfo);
85}
86
Devang Patel159f7ce2008-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 Patel3876c8a2009-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 Patel2fa85562008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113static void RemoveDeadConstant(Constant *C) {
114 assert(C->use_empty() && "Constant is not dead!");
Chris Lattner67abb532009-10-28 05:14:34 +0000115 SmallPtrSet<Constant*, 4> Operands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
117 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patel2fa85562008-11-13 01:28:40 +0000118 OnlyUsedBy(C->getOperand(i), C))
Chris Lattner67abb532009-10-28 05:14:34 +0000119 Operands.insert(cast<Constant>(C->getOperand(i)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000121 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 GV->eraseFromParent();
123 }
124 else if (!isa<Function>(C))
Devang Patelabd69112008-11-20 01:20:42 +0000125 if (isa<CompositeType>(C->getType()))
126 C->destroyConstant();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127
128 // If the constant referenced anything, see if we can delete it as well.
Chris Lattner67abb532009-10-28 05:14:34 +0000129 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
Devang Patel2fa85562008-11-13 01:28:40 +0000130 OE = Operands.end(); OI != OE; ++OI)
131 RemoveDeadConstant(*OI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132}
133
134// Strip the symbol table of its names.
135//
Devang Patel159f7ce2008-11-18 21:34:39 +0000136static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
138 Value *V = VI->getValue();
139 ++VI;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000140 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000141 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patel159f7ce2008-11-18 21:34:39 +0000142 // Set name to "", removing from symbol table!
143 V->setName("");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 }
145 }
146}
147
148// Strip the symbol table of its names.
Devang Patel159f7ce2008-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 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156}
157
Devang Patel2d53e2d2008-11-18 21:13:41 +0000158/// Find values that are marked as llvm.used.
Chris Lattner1e0e0d12009-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 Patel2d53e2d2008-11-18 21:13:41 +0000171}
172
173/// StripSymbolNames - Strip symbol names.
Dan Gohman6d29b322009-08-07 01:32:21 +0000174static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel2d53e2d2008-11-18 21:13:41 +0000175
176 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000177 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
178 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel2d53e2d2008-11-18 21:13:41 +0000179
Devang Patel2c98b522008-11-14 22:49:37 +0000180 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
181 I != E; ++I) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000182 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000183 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patel159f7ce2008-11-18 21:34:39 +0000184 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel2c98b522008-11-14 22:49:37 +0000185 }
186
187 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000188 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000189 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patel159f7ce2008-11-18 21:34:39 +0000190 I->setName(""); // Internal symbols can't participate in linkage
191 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel2c98b522008-11-14 22:49:37 +0000192 }
193
194 // Remove all names from types.
Devang Patel159f7ce2008-11-18 21:34:39 +0000195 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196
Devang Patel2c98b522008-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 Gohman6d29b322009-08-07 01:32:21 +0000203static bool StripDebugInfo(Module &M) {
Devang Patel2c98b522008-11-14 22:49:37 +0000204
Devang Pateldadd6cd2009-11-17 00:47:06 +0000205 bool Changed = false;
206
Devang Patel15e723d2009-08-28 23:24:31 +0000207 // Remove all of the calls to the debugger intrinsics, and remove them from
208 // the module.
Devang Pateldadd6cd2009-11-17 00:47:06 +0000209 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 while (!Declare->use_empty()) {
211 CallInst *CI = cast<CallInst>(Declare->use_back());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 CI->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 }
214 Declare->eraseFromParent();
Devang Pateldadd6cd2009-11-17 00:47:06 +0000215 Changed = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 }
217
Devang Patel15e723d2009-08-28 23:24:31 +0000218 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
Devang Pateldadd6cd2009-11-17 00:47:06 +0000219 if (NMD) {
220 Changed = true;
Devang Patel15e723d2009-08-28 23:24:31 +0000221 NMD->eraseFromParent();
Devang Pateldadd6cd2009-11-17 00:47:06 +0000222 }
223 MetadataContext &TheMetadata = M.getContext().getMetadata();
Chris Lattner9741b1d2009-12-28 20:45:51 +0000224 unsigned MDDbgKind = TheMetadata.getMDKindID("dbg");
Devang Pateldadd6cd2009-11-17 00:47:06 +0000225
226 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
227 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
228 ++FI)
229 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
230 ++BI)
231 TheMetadata.removeMD(MDDbgKind, BI);
Devang Patel2fa85562008-11-13 01:28:40 +0000232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 return true;
234}
Devang Patel159f7ce2008-11-18 21:34:39 +0000235
236bool StripSymbols::runOnModule(Module &M) {
237 bool Changed = false;
238 Changed |= StripDebugInfo(M);
239 if (!OnlyDebugInfo)
240 Changed |= StripSymbolNames(M, false);
241 return Changed;
242}
243
244bool StripNonDebugSymbols::runOnModule(Module &M) {
245 return StripSymbolNames(M, true);
246}
Devang Patel3876c8a2009-03-09 20:49:37 +0000247
248bool StripDebugDeclare::runOnModule(Module &M) {
249
250 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel3876c8a2009-03-09 20:49:37 +0000251 std::vector<Constant*> DeadConstants;
252
Dale Johannesendd031b72009-03-13 22:59:47 +0000253 if (Declare) {
254 while (!Declare->use_empty()) {
255 CallInst *CI = cast<CallInst>(Declare->use_back());
256 Value *Arg1 = CI->getOperand(1);
257 Value *Arg2 = CI->getOperand(2);
258 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
259 CI->eraseFromParent();
260 if (Arg1->use_empty()) {
261 if (Constant *C = dyn_cast<Constant>(Arg1))
262 DeadConstants.push_back(C);
263 else
Dan Gohmandb2b9462009-05-02 20:22:10 +0000264 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesendd031b72009-03-13 22:59:47 +0000265 }
266 if (Arg2->use_empty())
267 if (Constant *C = dyn_cast<Constant>(Arg2))
268 DeadConstants.push_back(C);
Devang Patel3876c8a2009-03-09 20:49:37 +0000269 }
Dale Johannesendd031b72009-03-13 22:59:47 +0000270 Declare->eraseFromParent();
Devang Patel3876c8a2009-03-09 20:49:37 +0000271 }
Devang Patel3876c8a2009-03-09 20:49:37 +0000272
273 while (!DeadConstants.empty()) {
274 Constant *C = DeadConstants.back();
275 DeadConstants.pop_back();
276 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
277 if (GV->hasLocalLinkage())
278 RemoveDeadConstant(GV);
Chris Lattner67abb532009-10-28 05:14:34 +0000279 } else
Devang Patel3876c8a2009-03-09 20:49:37 +0000280 RemoveDeadConstant(C);
281 }
282
283 return true;
284}