blob: b498211b3fda0c8f3246d944dae7f7b104d574c4 [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"
Owen Anderson14ce9ef2009-07-06 01:34:54 +000027#include "llvm/LLVMContext.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000028#include "llvm/Module.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000029#include "llvm/Pass.h"
Devang Patel13e16b62009-06-26 01:49:18 +000030#include "llvm/Analysis/DebugInfo.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000031#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000032#include "llvm/TypeSymbolTable.h"
Devang Patel9adb01c2009-03-03 21:31:02 +000033#include "llvm/Transforms/Utils/Local.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000034#include "llvm/Support/Compiler.h"
Devang Patel8c231e52008-01-16 03:33:05 +000035#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000036using namespace llvm;
37
38namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000039 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
Chris Lattnere3ad43c2004-12-02 21:25:03 +000040 bool OnlyDebugInfo;
41 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000042 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000043 explicit StripSymbols(bool ODI = false)
Dan Gohmanae73dc12008-09-04 17:05:41 +000044 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000045
Devang Patelf17fc462008-11-18 21:34:39 +000046 virtual bool runOnModule(Module &M);
Devang Patel229de952008-11-14 22:49:37 +000047
Devang Patelf17fc462008-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 Patel229de952008-11-14 22:49:37 +000058
Chris Lattnere3ad43c2004-12-02 21:25:03 +000059 virtual bool runOnModule(Module &M);
60
61 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
62 AU.setPreservesAll();
63 }
64 };
Devang Patel23e528b2009-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 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000078}
79
Dan Gohman844731a2008-05-13 00:00:25 +000080char StripSymbols::ID = 0;
81static RegisterPass<StripSymbols>
82X("strip", "Strip all symbols from a module");
83
Chris Lattnere3ad43c2004-12-02 21:25:03 +000084ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
85 return new StripSymbols(OnlyDebugInfo);
86}
87
Devang Patelf17fc462008-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 Patel23e528b2009-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 Patelbf5db812008-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
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000114static void RemoveDeadConstant(Constant *C) {
115 assert(C->use_empty() && "Constant is not dead!");
Devang Patelbf5db812008-11-13 01:28:40 +0000116 SmallPtrSet<Constant *, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000117 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
118 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patelbf5db812008-11-13 01:28:40 +0000119 OnlyUsedBy(C->getOperand(i), C))
120 Operands.insert(C->getOperand(i));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000121 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000122 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000123 GV->eraseFromParent();
124 }
125 else if (!isa<Function>(C))
Devang Patelf23de862008-11-20 01:20:42 +0000126 if (isa<CompositeType>(C->getType()))
127 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +0000128
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000129 // If the constant referenced anything, see if we can delete it as well.
Devang Patelbf5db812008-11-13 01:28:40 +0000130 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
131 OE = Operands.end(); OI != OE; ++OI)
132 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000133}
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000134
Chris Lattner7f1444b2007-02-07 06:22:45 +0000135// Strip the symbol table of its names.
136//
Devang Patelf17fc462008-11-18 21:34:39 +0000137static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner7f1444b2007-02-07 06:22:45 +0000138 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000139 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +0000140 ++VI;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000141 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar460f6562009-07-26 09:48:23 +0000142 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000143 // Set name to "", removing from symbol table!
144 V->setName("");
Chris Lattner7f1444b2007-02-07 06:22:45 +0000145 }
146 }
147}
148
149// Strip the symbol table of its names.
Devang Patelf17fc462008-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 }
Chris Lattner7f1444b2007-02-07 06:22:45 +0000157}
158
Devang Patel4460a7e2008-11-18 21:13:41 +0000159/// Find values that are marked as llvm.used.
Chris Lattner401e10c2009-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 Patel4460a7e2008-11-18 21:13:41 +0000172}
173
174/// StripSymbolNames - Strip symbol names.
Devang Patelf17fc462008-11-18 21:34:39 +0000175bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel4460a7e2008-11-18 21:13:41 +0000176
177 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner401e10c2009-07-20 06:14:25 +0000178 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
179 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel4460a7e2008-11-18 21:13:41 +0000180
Devang Patel229de952008-11-14 22:49:37 +0000181 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
182 I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000183 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000184 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000185 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel229de952008-11-14 22:49:37 +0000186 }
187
188 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000189 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000190 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000191 I->setName(""); // Internal symbols can't participate in linkage
192 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel229de952008-11-14 22:49:37 +0000193 }
194
195 // Remove all names from types.
Devang Patelf17fc462008-11-18 21:34:39 +0000196 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000197
Devang Patel229de952008-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.
Devang Patelf17fc462008-11-18 21:34:39 +0000204bool StripDebugInfo(Module &M) {
Devang Patel229de952008-11-14 22:49:37 +0000205
Devang Patel73df3c92009-03-02 22:50:58 +0000206 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner401e10c2009-07-20 06:14:25 +0000207 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
208 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel73df3c92009-03-02 22:50:58 +0000209
Devang Patel734068a2009-08-06 20:53:06 +0000210 DebugInfoFinder DbgFinder;
211 DbgFinder.processModule(M);
212
Devang Patel13e16b62009-06-26 01:49:18 +0000213 // These anchors use LinkOnce linkage so that the optimizer does not
214 // remove them accidently. Set InternalLinkage for all these debug
215 // info anchors.
Devang Patel734068a2009-08-06 20:53:06 +0000216 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
217 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +0000218 (*I)->setLinkage(GlobalValue::InternalLinkage);
Devang Patel734068a2009-08-06 20:53:06 +0000219 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
220 E = DbgFinder.global_variable_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +0000221 (*I)->setLinkage(GlobalValue::InternalLinkage);
Devang Patel734068a2009-08-06 20:53:06 +0000222 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
223 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel13e16b62009-06-26 01:49:18 +0000224 (*I)->setLinkage(GlobalValue::InternalLinkage);
225
226
227 // Delete all dbg variables.
Devang Patel73df3c92009-03-02 22:50:58 +0000228 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
229 I != E; ++I) {
230 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
231 if (!GV) continue;
232 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
Daniel Dunbar460f6562009-07-26 09:48:23 +0000233 if (GV->getName().startswith("llvm.dbg")) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000234 GV->replaceAllUsesWith(UndefValue::get(GV->getType()));
Devang Patel73df3c92009-03-02 22:50:58 +0000235 }
236 }
237 }
238
Reid Spencer688b0492007-02-05 21:19:13 +0000239 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
240 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
241 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
242 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
243 Function *Declare = M.getFunction("llvm.dbg.declare");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000244
Devang Patel4460a7e2008-11-18 21:13:41 +0000245 std::vector<Constant*> DeadConstants;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000246
247 // Remove all of the calls to the debugger intrinsics, and remove them from
248 // the module.
249 if (FuncStart) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000250 while (!FuncStart->use_empty()) {
251 CallInst *CI = cast<CallInst>(FuncStart->use_back());
252 Value *Arg = CI->getOperand(1);
Jim Laskey4ca97572006-03-23 18:11:33 +0000253 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000254 CI->eraseFromParent();
255 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000256 if (Constant *C = dyn_cast<Constant>(Arg))
257 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000258 }
259 FuncStart->eraseFromParent();
260 }
261 if (StopPoint) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000262 while (!StopPoint->use_empty()) {
263 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyf4321a32006-03-13 13:07:37 +0000264 Value *Arg = CI->getOperand(3);
Jim Laskey4ca97572006-03-23 18:11:33 +0000265 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000266 CI->eraseFromParent();
267 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000268 if (Constant *C = dyn_cast<Constant>(Arg))
269 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000270 }
271 StopPoint->eraseFromParent();
272 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000273 if (RegionStart) {
274 while (!RegionStart->use_empty()) {
275 CallInst *CI = cast<CallInst>(RegionStart->use_back());
276 Value *Arg = CI->getOperand(1);
277 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
278 CI->eraseFromParent();
279 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000280 if (Constant *C = dyn_cast<Constant>(Arg))
281 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000282 }
283 RegionStart->eraseFromParent();
284 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000285 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000286 while (!RegionEnd->use_empty()) {
287 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000288 Value *Arg = CI->getOperand(1);
289 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000290 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000291 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000292 if (Constant *C = dyn_cast<Constant>(Arg))
293 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000294 }
295 RegionEnd->eraseFromParent();
296 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000297 if (Declare) {
298 while (!Declare->use_empty()) {
299 CallInst *CI = cast<CallInst>(Declare->use_back());
Devang Patelf23de862008-11-20 01:20:42 +0000300 Value *Arg1 = CI->getOperand(1);
301 Value *Arg2 = CI->getOperand(2);
Jim Laskey4ca97572006-03-23 18:11:33 +0000302 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
303 CI->eraseFromParent();
Devang Patelf23de862008-11-20 01:20:42 +0000304 if (Arg1->use_empty()) {
305 if (Constant *C = dyn_cast<Constant>(Arg1))
306 DeadConstants.push_back(C);
Devang Patel9adb01c2009-03-03 21:31:02 +0000307 else
Dan Gohmane66f6f12009-05-02 20:22:10 +0000308 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Devang Patelf23de862008-11-20 01:20:42 +0000309 }
310 if (Arg2->use_empty())
311 if (Constant *C = dyn_cast<Constant>(Arg2))
Devang Patel4460a7e2008-11-18 21:13:41 +0000312 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000313 }
314 Declare->eraseFromParent();
315 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000316
Devang Patelbf5db812008-11-13 01:28:40 +0000317 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
318 // but since we are removing all debug information, make them internal now.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000319 // FIXME: Use private linkage maybe?
Devang Patelbf5db812008-11-13 01:28:40 +0000320 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
321 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
322 GV->setLinkage(GlobalValue::InternalLinkage);
323
324 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
325 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
326 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patel4460a7e2008-11-18 21:13:41 +0000327
328 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
329 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
330 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patelbf5db812008-11-13 01:28:40 +0000331
332 // Delete all dbg variables.
Devang Patelfb5fd5a2008-11-19 00:22:02 +0000333 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
334 I != E; ++I) {
335 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
336 if (!GV) continue;
337 if (GV->use_empty() && llvmUsedValues.count(I) == 0
338 && (!GV->hasSection()
339 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
340 DeadConstants.push_back(GV);
341 }
Devang Patelbf5db812008-11-13 01:28:40 +0000342
Devang Patel4460a7e2008-11-18 21:13:41 +0000343 if (DeadConstants.empty())
Devang Patel229de952008-11-14 22:49:37 +0000344 return false;
345
Devang Patelbf5db812008-11-13 01:28:40 +0000346 // Delete any internal globals that were only used by the debugger intrinsics.
Devang Patel4460a7e2008-11-18 21:13:41 +0000347 while (!DeadConstants.empty()) {
348 Constant *C = DeadConstants.back();
349 DeadConstants.pop_back();
350 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000351 if (GV->hasLocalLinkage())
Devang Patel4460a7e2008-11-18 21:13:41 +0000352 RemoveDeadConstant(GV);
353 }
354 else
355 RemoveDeadConstant(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000356 }
357
Devang Patelbf5db812008-11-13 01:28:40 +0000358 // Remove all llvm.dbg types.
359 TypeSymbolTable &ST = M.getTypeSymbolTable();
Chris Lattner3f914f02008-11-16 06:35:18 +0000360 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
361 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
Devang Patelbf5db812008-11-13 01:28:40 +0000362 ST.remove(TI++);
363 else
364 ++TI;
365 }
366
Misha Brukmanfd939082005-04-21 23:48:37 +0000367 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000368}
Devang Patelf17fc462008-11-18 21:34:39 +0000369
370bool StripSymbols::runOnModule(Module &M) {
371 bool Changed = false;
372 Changed |= StripDebugInfo(M);
373 if (!OnlyDebugInfo)
374 Changed |= StripSymbolNames(M, false);
375 return Changed;
376}
377
378bool StripNonDebugSymbols::runOnModule(Module &M) {
379 return StripSymbolNames(M, true);
380}
Devang Patel23e528b2009-03-09 20:49:37 +0000381
382bool StripDebugDeclare::runOnModule(Module &M) {
383
384 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel23e528b2009-03-09 20:49:37 +0000385 std::vector<Constant*> DeadConstants;
386
Dale Johannesen44252402009-03-13 22:59:47 +0000387 if (Declare) {
388 while (!Declare->use_empty()) {
389 CallInst *CI = cast<CallInst>(Declare->use_back());
390 Value *Arg1 = CI->getOperand(1);
391 Value *Arg2 = CI->getOperand(2);
392 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
393 CI->eraseFromParent();
394 if (Arg1->use_empty()) {
395 if (Constant *C = dyn_cast<Constant>(Arg1))
396 DeadConstants.push_back(C);
397 else
Dan Gohmane66f6f12009-05-02 20:22:10 +0000398 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesen44252402009-03-13 22:59:47 +0000399 }
400 if (Arg2->use_empty())
401 if (Constant *C = dyn_cast<Constant>(Arg2))
402 DeadConstants.push_back(C);
Devang Patel23e528b2009-03-09 20:49:37 +0000403 }
Dale Johannesen44252402009-03-13 22:59:47 +0000404 Declare->eraseFromParent();
Devang Patel23e528b2009-03-09 20:49:37 +0000405 }
Devang Patel23e528b2009-03-09 20:49:37 +0000406
Devang Pateld07128c2009-03-09 21:32:28 +0000407 // Delete all llvm.dbg.global_variables.
408 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
409 I != E; ++I) {
410 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
411 if (!GV) continue;
Daniel Dunbar460f6562009-07-26 09:48:23 +0000412 if (GV->use_empty() && GV->getName().startswith("llvm.dbg.global_variable"))
Devang Pateld07128c2009-03-09 21:32:28 +0000413 DeadConstants.push_back(GV);
414 }
415
Devang Patel23e528b2009-03-09 20:49:37 +0000416 while (!DeadConstants.empty()) {
417 Constant *C = DeadConstants.back();
418 DeadConstants.pop_back();
419 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
420 if (GV->hasLocalLinkage())
421 RemoveDeadConstant(GV);
422 }
423 else
424 RemoveDeadConstant(C);
425 }
426
427 return true;
428}