blob: 0e0d83af42184970edf2e617e5feda037f20f21f [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"
Devang Patel13e16b62009-06-26 01:49:18 +000029#include "llvm/Analysis/DebugInfo.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000030#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000031#include "llvm/TypeSymbolTable.h"
Devang Patel9adb01c2009-03-03 21:31:02 +000032#include "llvm/Transforms/Utils/Local.h"
Devang Patel8c231e52008-01-16 03:33:05 +000033#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000034using namespace llvm;
35
36namespace {
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000037 class StripSymbols : public ModulePass {
Chris Lattnere3ad43c2004-12-02 21:25:03 +000038 bool OnlyDebugInfo;
39 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000040 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000041 explicit StripSymbols(bool ODI = false)
Dan Gohmanae73dc12008-09-04 17:05:41 +000042 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000043
Devang Patelf17fc462008-11-18 21:34:39 +000044 virtual bool runOnModule(Module &M);
Devang Patel229de952008-11-14 22:49:37 +000045
Devang Patelf17fc462008-11-18 21:34:39 +000046 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.setPreservesAll();
48 }
49 };
50
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000051 class StripNonDebugSymbols : public ModulePass {
Devang Patelf17fc462008-11-18 21:34:39 +000052 public:
53 static char ID; // Pass identification, replacement for typeid
54 explicit StripNonDebugSymbols()
55 : ModulePass(&ID) {}
Devang Patel229de952008-11-14 22:49:37 +000056
Chris Lattnere3ad43c2004-12-02 21:25:03 +000057 virtual bool runOnModule(Module &M);
58
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesAll();
61 }
62 };
Devang Patel23e528b2009-03-09 20:49:37 +000063
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000064 class StripDebugDeclare : public ModulePass {
Devang Patel23e528b2009-03-09 20:49:37 +000065 public:
66 static char ID; // Pass identification, replacement for typeid
67 explicit StripDebugDeclare()
68 : ModulePass(&ID) {}
69
70 virtual bool runOnModule(Module &M);
71
72 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.setPreservesAll();
74 }
75 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000076}
77
Dan Gohman844731a2008-05-13 00:00:25 +000078char StripSymbols::ID = 0;
79static RegisterPass<StripSymbols>
80X("strip", "Strip all symbols from a module");
81
Chris Lattnere3ad43c2004-12-02 21:25:03 +000082ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
83 return new StripSymbols(OnlyDebugInfo);
84}
85
Devang Patelf17fc462008-11-18 21:34:39 +000086char StripNonDebugSymbols::ID = 0;
87static RegisterPass<StripNonDebugSymbols>
88Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
89
90ModulePass *llvm::createStripNonDebugSymbolsPass() {
91 return new StripNonDebugSymbols();
92}
93
Devang Patel23e528b2009-03-09 20:49:37 +000094char StripDebugDeclare::ID = 0;
95static RegisterPass<StripDebugDeclare>
96Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
97
98ModulePass *llvm::createStripDebugDeclarePass() {
99 return new StripDebugDeclare();
100}
101
Devang Patelbf5db812008-11-13 01:28:40 +0000102/// OnlyUsedBy - Return true if V is only used by Usr.
103static bool OnlyUsedBy(Value *V, Value *Usr) {
104 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
105 User *U = *I;
106 if (U != Usr)
107 return false;
108 }
109 return true;
110}
111
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000112static void RemoveDeadConstant(Constant *C) {
113 assert(C->use_empty() && "Constant is not dead!");
Chris Lattner0eeb9132009-10-28 05:14:34 +0000114 SmallPtrSet<Constant*, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000115 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
116 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patelbf5db812008-11-13 01:28:40 +0000117 OnlyUsedBy(C->getOperand(i), C))
Chris Lattner0eeb9132009-10-28 05:14:34 +0000118 Operands.insert(cast<Constant>(C->getOperand(i)));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000119 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000120 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000121 GV->eraseFromParent();
122 }
123 else if (!isa<Function>(C))
Devang Patelf23de862008-11-20 01:20:42 +0000124 if (isa<CompositeType>(C->getType()))
125 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +0000126
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000127 // If the constant referenced anything, see if we can delete it as well.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000128 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
Devang Patelbf5db812008-11-13 01:28:40 +0000129 OE = Operands.end(); OI != OE; ++OI)
130 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000131}
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000132
Chris Lattner7f1444b2007-02-07 06:22:45 +0000133// Strip the symbol table of its names.
134//
Devang Patelf17fc462008-11-18 21:34:39 +0000135static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner7f1444b2007-02-07 06:22:45 +0000136 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000137 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +0000138 ++VI;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000139 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar460f6562009-07-26 09:48:23 +0000140 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000141 // Set name to "", removing from symbol table!
142 V->setName("");
Chris Lattner7f1444b2007-02-07 06:22:45 +0000143 }
144 }
145}
146
147// Strip the symbol table of its names.
Devang Patelf17fc462008-11-18 21:34:39 +0000148static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
149 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
Benjamin Kramerb0706d12010-01-22 20:00:21 +0000150 if (PreserveDbgInfo && StringRef(TI->first).startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000151 ++TI;
152 else
153 ST.remove(TI++);
154 }
Chris Lattner7f1444b2007-02-07 06:22:45 +0000155}
156
Devang Patel4460a7e2008-11-18 21:13:41 +0000157/// Find values that are marked as llvm.used.
Chris Lattner401e10c2009-07-20 06:14:25 +0000158static void findUsedValues(GlobalVariable *LLVMUsed,
159 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
160 if (LLVMUsed == 0) return;
161 UsedValues.insert(LLVMUsed);
162
163 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
164 if (Inits == 0) return;
165
166 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
167 if (GlobalValue *GV =
168 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
169 UsedValues.insert(GV);
Devang Patel4460a7e2008-11-18 21:13:41 +0000170}
171
172/// StripSymbolNames - Strip symbol names.
Dan Gohman7db949d2009-08-07 01:32:21 +0000173static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel4460a7e2008-11-18 21:13:41 +0000174
175 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner401e10c2009-07-20 06:14:25 +0000176 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
177 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel4460a7e2008-11-18 21:13:41 +0000178
Devang Patel229de952008-11-14 22:49:37 +0000179 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
180 I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000181 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000182 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000183 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel229de952008-11-14 22:49:37 +0000184 }
185
186 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000187 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000188 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000189 I->setName(""); // Internal symbols can't participate in linkage
190 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel229de952008-11-14 22:49:37 +0000191 }
192
193 // Remove all names from types.
Devang Patelf17fc462008-11-18 21:34:39 +0000194 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000195
Devang Patel229de952008-11-14 22:49:37 +0000196 return true;
197}
198
199// StripDebugInfo - Strip debug info in the module if it exists.
200// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
201// llvm.dbg.region.end calls, and any globals they point to if now dead.
Dan Gohman7db949d2009-08-07 01:32:21 +0000202static bool StripDebugInfo(Module &M) {
Devang Patel229de952008-11-14 22:49:37 +0000203
Devang Patel76e3e502009-11-17 00:47:06 +0000204 bool Changed = false;
205
Devang Patele4b27562009-08-28 23:24:31 +0000206 // Remove all of the calls to the debugger intrinsics, and remove them from
207 // the module.
Devang Patel76e3e502009-11-17 00:47:06 +0000208 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
Jim Laskey4ca97572006-03-23 18:11:33 +0000209 while (!Declare->use_empty()) {
210 CallInst *CI = cast<CallInst>(Declare->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000211 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000212 }
213 Declare->eraseFromParent();
Devang Patel76e3e502009-11-17 00:47:06 +0000214 Changed = true;
Jim Laskey4ca97572006-03-23 18:11:33 +0000215 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000216
Devang Patele4b27562009-08-28 23:24:31 +0000217 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
Devang Patel76e3e502009-11-17 00:47:06 +0000218 if (NMD) {
219 Changed = true;
Devang Patele4b27562009-08-28 23:24:31 +0000220 NMD->eraseFromParent();
Devang Patel76e3e502009-11-17 00:47:06 +0000221 }
Chris Lattner08113472009-12-29 09:01:33 +0000222
223 unsigned MDDbgKind = M.getMDKindID("dbg");
Devang Patel76e3e502009-11-17 00:47:06 +0000224 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
225 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
226 ++FI)
227 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
228 ++BI)
Chris Lattner3990b122009-12-28 23:41:32 +0000229 BI->setMetadata(MDDbgKind, 0);
Devang Patelbf5db812008-11-13 01:28:40 +0000230
Misha Brukmanfd939082005-04-21 23:48:37 +0000231 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000232}
Devang Patelf17fc462008-11-18 21:34:39 +0000233
234bool StripSymbols::runOnModule(Module &M) {
235 bool Changed = false;
236 Changed |= StripDebugInfo(M);
237 if (!OnlyDebugInfo)
238 Changed |= StripSymbolNames(M, false);
239 return Changed;
240}
241
242bool StripNonDebugSymbols::runOnModule(Module &M) {
243 return StripSymbolNames(M, true);
244}
Devang Patel23e528b2009-03-09 20:49:37 +0000245
246bool StripDebugDeclare::runOnModule(Module &M) {
247
248 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel23e528b2009-03-09 20:49:37 +0000249 std::vector<Constant*> DeadConstants;
250
Dale Johannesen44252402009-03-13 22:59:47 +0000251 if (Declare) {
252 while (!Declare->use_empty()) {
253 CallInst *CI = cast<CallInst>(Declare->use_back());
254 Value *Arg1 = CI->getOperand(1);
255 Value *Arg2 = CI->getOperand(2);
256 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
257 CI->eraseFromParent();
258 if (Arg1->use_empty()) {
259 if (Constant *C = dyn_cast<Constant>(Arg1))
260 DeadConstants.push_back(C);
261 else
Dan Gohmane66f6f12009-05-02 20:22:10 +0000262 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesen44252402009-03-13 22:59:47 +0000263 }
264 if (Arg2->use_empty())
265 if (Constant *C = dyn_cast<Constant>(Arg2))
266 DeadConstants.push_back(C);
Devang Patel23e528b2009-03-09 20:49:37 +0000267 }
Dale Johannesen44252402009-03-13 22:59:47 +0000268 Declare->eraseFromParent();
Devang Patel23e528b2009-03-09 20:49:37 +0000269 }
Devang Patel23e528b2009-03-09 20:49:37 +0000270
271 while (!DeadConstants.empty()) {
272 Constant *C = DeadConstants.back();
273 DeadConstants.pop_back();
274 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
275 if (GV->hasLocalLinkage())
276 RemoveDeadConstant(GV);
Chris Lattner0eeb9132009-10-28 05:14:34 +0000277 } else
Devang Patel23e528b2009-03-09 20:49:37 +0000278 RemoveDeadConstant(C);
279 }
280
281 return true;
282}