blob: 046e0441b1dc64fa0c7b18bc62893fe98b590507 [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"
Reid Spencer9133fe22007-02-05 23:32:05 +000033#include "llvm/Support/Compiler.h"
Devang Patel8c231e52008-01-16 03:33:05 +000034#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000035using namespace llvm;
36
37namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000038 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
Chris Lattnere3ad43c2004-12-02 21:25:03 +000039 bool OnlyDebugInfo;
40 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000041 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000042 explicit StripSymbols(bool ODI = false)
Dan Gohmanae73dc12008-09-04 17:05:41 +000043 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000044
Devang Patelf17fc462008-11-18 21:34:39 +000045 virtual bool runOnModule(Module &M);
Devang Patel229de952008-11-14 22:49:37 +000046
Devang Patelf17fc462008-11-18 21:34:39 +000047 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.setPreservesAll();
49 }
50 };
51
52 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
53 public:
54 static char ID; // Pass identification, replacement for typeid
55 explicit StripNonDebugSymbols()
56 : ModulePass(&ID) {}
Devang Patel229de952008-11-14 22:49:37 +000057
Chris Lattnere3ad43c2004-12-02 21:25:03 +000058 virtual bool runOnModule(Module &M);
59
60 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.setPreservesAll();
62 }
63 };
Devang Patel23e528b2009-03-09 20:49:37 +000064
65 class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
66 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 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000077}
78
Dan Gohman844731a2008-05-13 00:00:25 +000079char StripSymbols::ID = 0;
80static RegisterPass<StripSymbols>
81X("strip", "Strip all symbols from a module");
82
Chris Lattnere3ad43c2004-12-02 21:25:03 +000083ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
84 return new StripSymbols(OnlyDebugInfo);
85}
86
Devang Patelf17fc462008-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 Patel23e528b2009-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 Patelbf5db812008-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
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000113static void RemoveDeadConstant(Constant *C) {
114 assert(C->use_empty() && "Constant is not dead!");
Devang Patelbf5db812008-11-13 01:28:40 +0000115 SmallPtrSet<Constant *, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000116 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
117 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patelbf5db812008-11-13 01:28:40 +0000118 OnlyUsedBy(C->getOperand(i), C))
119 Operands.insert(C->getOperand(i));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000120 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000121 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000122 GV->eraseFromParent();
123 }
124 else if (!isa<Function>(C))
Devang Patelf23de862008-11-20 01:20:42 +0000125 if (isa<CompositeType>(C->getType()))
126 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +0000127
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000128 // If the constant referenced anything, see if we can delete it as well.
Devang Patelbf5db812008-11-13 01:28:40 +0000129 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
130 OE = Operands.end(); OI != OE; ++OI)
131 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000132}
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000133
Chris Lattner7f1444b2007-02-07 06:22:45 +0000134// Strip the symbol table of its names.
135//
Devang Patelf17fc462008-11-18 21:34:39 +0000136static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner7f1444b2007-02-07 06:22:45 +0000137 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000138 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +0000139 ++VI;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000140 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Devang Patelf17fc462008-11-18 21:34:39 +0000141 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
142 // Set name to "", removing from symbol table!
143 V->setName("");
Chris Lattner7f1444b2007-02-07 06:22:45 +0000144 }
145 }
146}
147
148// Strip the symbol table of its names.
Devang Patelf17fc462008-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 }
Chris Lattner7f1444b2007-02-07 06:22:45 +0000156}
157
Devang Patel4460a7e2008-11-18 21:13:41 +0000158/// Find values that are marked as llvm.used.
159void findUsedValues(Module &M,
160 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
Devang Patel229de952008-11-14 22:49:37 +0000161 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
162 llvmUsedValues.insert(LLVMUsed);
163 // Collect values that are preserved as per explicit request.
164 // llvm.used is used to list these values.
165 if (ConstantArray *Inits =
166 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
167 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
168 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
169 llvmUsedValues.insert(GV);
170 else if (ConstantExpr *CE =
171 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
172 if (CE->getOpcode() == Instruction::BitCast)
173 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
174 llvmUsedValues.insert(GV);
Devang Patel8c231e52008-01-16 03:33:05 +0000175 }
176 }
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000177 }
Devang Patel4460a7e2008-11-18 21:13:41 +0000178}
179
180/// StripSymbolNames - Strip symbol names.
Devang Patelf17fc462008-11-18 21:34:39 +0000181bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel4460a7e2008-11-18 21:13:41 +0000182
183 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
184 findUsedValues(M, llvmUsedValues);
185
Devang Patel229de952008-11-14 22:49:37 +0000186 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
187 I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000188 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patelf17fc462008-11-18 21:34:39 +0000189 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
190 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel229de952008-11-14 22:49:37 +0000191 }
192
193 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000194 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patelf17fc462008-11-18 21:34:39 +0000195 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
196 I->setName(""); // Internal symbols can't participate in linkage
197 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel229de952008-11-14 22:49:37 +0000198 }
199
200 // Remove all names from types.
Devang Patelf17fc462008-11-18 21:34:39 +0000201 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000202
Devang Patel229de952008-11-14 22:49:37 +0000203 return true;
204}
205
206// StripDebugInfo - Strip debug info in the module if it exists.
207// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
208// llvm.dbg.region.end calls, and any globals they point to if now dead.
Devang Patelf17fc462008-11-18 21:34:39 +0000209bool StripDebugInfo(Module &M) {
Devang Patel229de952008-11-14 22:49:37 +0000210
Devang Patel73df3c92009-03-02 22:50:58 +0000211 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
212 findUsedValues(M, llvmUsedValues);
213
Devang Patel13e16b62009-06-26 01:49:18 +0000214 SmallVector<GlobalVariable *, 2> CUs;
215 SmallVector<GlobalVariable *, 4> GVs;
216 SmallVector<GlobalVariable *, 4> SPs;
217 CollectDebugInfoAnchors(M, CUs, GVs, SPs);
218 // These anchors use LinkOnce linkage so that the optimizer does not
219 // remove them accidently. Set InternalLinkage for all these debug
220 // info anchors.
221 for (SmallVector<GlobalVariable *, 2>::iterator I = CUs.begin(),
222 E = CUs.end(); I != E; ++I)
223 (*I)->setLinkage(GlobalValue::InternalLinkage);
224 for (SmallVector<GlobalVariable *, 4>::iterator I = GVs.begin(),
225 E = GVs.end(); I != E; ++I)
226 (*I)->setLinkage(GlobalValue::InternalLinkage);
227 for (SmallVector<GlobalVariable *, 4>::iterator I = SPs.begin(),
228 E = SPs.end(); I != E; ++I)
229 (*I)->setLinkage(GlobalValue::InternalLinkage);
230
231
232 // Delete all dbg variables.
Devang Patel73df3c92009-03-02 22:50:58 +0000233 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
234 I != E; ++I) {
235 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
236 if (!GV) continue;
237 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
238 if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) {
239 GV->replaceAllUsesWith(UndefValue::get(GV->getType()));
240 }
241 }
242 }
243
Reid Spencer688b0492007-02-05 21:19:13 +0000244 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
245 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
246 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
247 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
248 Function *Declare = M.getFunction("llvm.dbg.declare");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000249
Devang Patel4460a7e2008-11-18 21:13:41 +0000250 std::vector<Constant*> DeadConstants;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000251
252 // Remove all of the calls to the debugger intrinsics, and remove them from
253 // the module.
254 if (FuncStart) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000255 while (!FuncStart->use_empty()) {
256 CallInst *CI = cast<CallInst>(FuncStart->use_back());
257 Value *Arg = CI->getOperand(1);
Jim Laskey4ca97572006-03-23 18:11:33 +0000258 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000259 CI->eraseFromParent();
260 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000261 if (Constant *C = dyn_cast<Constant>(Arg))
262 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000263 }
264 FuncStart->eraseFromParent();
265 }
266 if (StopPoint) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000267 while (!StopPoint->use_empty()) {
268 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyf4321a32006-03-13 13:07:37 +0000269 Value *Arg = CI->getOperand(3);
Jim Laskey4ca97572006-03-23 18:11:33 +0000270 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000271 CI->eraseFromParent();
272 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000273 if (Constant *C = dyn_cast<Constant>(Arg))
274 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000275 }
276 StopPoint->eraseFromParent();
277 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000278 if (RegionStart) {
279 while (!RegionStart->use_empty()) {
280 CallInst *CI = cast<CallInst>(RegionStart->use_back());
281 Value *Arg = CI->getOperand(1);
282 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
283 CI->eraseFromParent();
284 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000285 if (Constant *C = dyn_cast<Constant>(Arg))
286 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000287 }
288 RegionStart->eraseFromParent();
289 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000290 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000291 while (!RegionEnd->use_empty()) {
292 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000293 Value *Arg = CI->getOperand(1);
294 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000295 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000296 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000297 if (Constant *C = dyn_cast<Constant>(Arg))
298 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000299 }
300 RegionEnd->eraseFromParent();
301 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000302 if (Declare) {
303 while (!Declare->use_empty()) {
304 CallInst *CI = cast<CallInst>(Declare->use_back());
Devang Patelf23de862008-11-20 01:20:42 +0000305 Value *Arg1 = CI->getOperand(1);
306 Value *Arg2 = CI->getOperand(2);
Jim Laskey4ca97572006-03-23 18:11:33 +0000307 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
308 CI->eraseFromParent();
Devang Patelf23de862008-11-20 01:20:42 +0000309 if (Arg1->use_empty()) {
310 if (Constant *C = dyn_cast<Constant>(Arg1))
311 DeadConstants.push_back(C);
Devang Patel9adb01c2009-03-03 21:31:02 +0000312 else
Dan Gohmane66f6f12009-05-02 20:22:10 +0000313 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Devang Patelf23de862008-11-20 01:20:42 +0000314 }
315 if (Arg2->use_empty())
316 if (Constant *C = dyn_cast<Constant>(Arg2))
Devang Patel4460a7e2008-11-18 21:13:41 +0000317 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000318 }
319 Declare->eraseFromParent();
320 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000321
Devang Patelbf5db812008-11-13 01:28:40 +0000322 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
323 // but since we are removing all debug information, make them internal now.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000324 // FIXME: Use private linkage maybe?
Devang Patelbf5db812008-11-13 01:28:40 +0000325 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
326 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
327 GV->setLinkage(GlobalValue::InternalLinkage);
328
329 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
330 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
331 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patel4460a7e2008-11-18 21:13:41 +0000332
333 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
334 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
335 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patelbf5db812008-11-13 01:28:40 +0000336
337 // Delete all dbg variables.
Devang Patelfb5fd5a2008-11-19 00:22:02 +0000338 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
339 I != E; ++I) {
340 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
341 if (!GV) continue;
342 if (GV->use_empty() && llvmUsedValues.count(I) == 0
343 && (!GV->hasSection()
344 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
345 DeadConstants.push_back(GV);
346 }
Devang Patelbf5db812008-11-13 01:28:40 +0000347
Devang Patel4460a7e2008-11-18 21:13:41 +0000348 if (DeadConstants.empty())
Devang Patel229de952008-11-14 22:49:37 +0000349 return false;
350
Devang Patelbf5db812008-11-13 01:28:40 +0000351 // Delete any internal globals that were only used by the debugger intrinsics.
Devang Patel4460a7e2008-11-18 21:13:41 +0000352 while (!DeadConstants.empty()) {
353 Constant *C = DeadConstants.back();
354 DeadConstants.pop_back();
355 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000356 if (GV->hasLocalLinkage())
Devang Patel4460a7e2008-11-18 21:13:41 +0000357 RemoveDeadConstant(GV);
358 }
359 else
360 RemoveDeadConstant(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000361 }
362
Devang Patelbf5db812008-11-13 01:28:40 +0000363 // Remove all llvm.dbg types.
364 TypeSymbolTable &ST = M.getTypeSymbolTable();
Chris Lattner3f914f02008-11-16 06:35:18 +0000365 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
366 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
Devang Patelbf5db812008-11-13 01:28:40 +0000367 ST.remove(TI++);
368 else
369 ++TI;
370 }
371
Misha Brukmanfd939082005-04-21 23:48:37 +0000372 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000373}
Devang Patelf17fc462008-11-18 21:34:39 +0000374
375bool StripSymbols::runOnModule(Module &M) {
376 bool Changed = false;
377 Changed |= StripDebugInfo(M);
378 if (!OnlyDebugInfo)
379 Changed |= StripSymbolNames(M, false);
380 return Changed;
381}
382
383bool StripNonDebugSymbols::runOnModule(Module &M) {
384 return StripSymbolNames(M, true);
385}
Devang Patel23e528b2009-03-09 20:49:37 +0000386
387bool StripDebugDeclare::runOnModule(Module &M) {
388
389 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel23e528b2009-03-09 20:49:37 +0000390 std::vector<Constant*> DeadConstants;
391
Dale Johannesen44252402009-03-13 22:59:47 +0000392 if (Declare) {
393 while (!Declare->use_empty()) {
394 CallInst *CI = cast<CallInst>(Declare->use_back());
395 Value *Arg1 = CI->getOperand(1);
396 Value *Arg2 = CI->getOperand(2);
397 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
398 CI->eraseFromParent();
399 if (Arg1->use_empty()) {
400 if (Constant *C = dyn_cast<Constant>(Arg1))
401 DeadConstants.push_back(C);
402 else
Dan Gohmane66f6f12009-05-02 20:22:10 +0000403 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesen44252402009-03-13 22:59:47 +0000404 }
405 if (Arg2->use_empty())
406 if (Constant *C = dyn_cast<Constant>(Arg2))
407 DeadConstants.push_back(C);
Devang Patel23e528b2009-03-09 20:49:37 +0000408 }
Dale Johannesen44252402009-03-13 22:59:47 +0000409 Declare->eraseFromParent();
Devang Patel23e528b2009-03-09 20:49:37 +0000410 }
Devang Patel23e528b2009-03-09 20:49:37 +0000411
Devang Pateld07128c2009-03-09 21:32:28 +0000412 // Delete all llvm.dbg.global_variables.
413 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
414 I != E; ++I) {
415 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
416 if (!GV) continue;
417 if (GV->use_empty() && GV->hasName()
418 && strncmp(GV->getNameStart(), "llvm.dbg.global_variable", 24) == 0)
419 DeadConstants.push_back(GV);
420 }
421
Devang Patel23e528b2009-03-09 20:49:37 +0000422 while (!DeadConstants.empty()) {
423 Constant *C = DeadConstants.back();
424 DeadConstants.pop_back();
425 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
426 if (GV->hasLocalLinkage())
427 RemoveDeadConstant(GV);
428 }
429 else
430 RemoveDeadConstant(C);
431 }
432
433 return true;
434}