blob: 046e0441b1dc64fa0c7b18bc62893fe98b590507 [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"
27#include "llvm/Module.h"
28#include "llvm/Pass.h"
Devang Patel166f8432009-06-26 01:49:18 +000029#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/ValueSymbolTable.h"
31#include "llvm/TypeSymbolTable.h"
Devang Patele68804a2009-03-03 21:31:02 +000032#include "llvm/Transforms/Utils/Local.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Support/Compiler.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 {
38 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
39 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
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 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
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 };
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!");
Devang Patel2fa85562008-11-13 01:28:40 +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))
119 Operands.insert(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.
Devang Patel2fa85562008-11-13 01:28:40 +0000129 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
130 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()) {
Devang Patel159f7ce2008-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("");
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.
159void findUsedValues(Module &M,
160 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
Devang Patel2c98b522008-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 Patel5dc8fde2008-01-16 03:33:05 +0000175 }
176 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 }
Devang Patel2d53e2d2008-11-18 21:13:41 +0000178}
179
180/// StripSymbolNames - Strip symbol names.
Devang Patel159f7ce2008-11-18 21:34:39 +0000181bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel2d53e2d2008-11-18 21:13:41 +0000182
183 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
184 findUsedValues(M, llvmUsedValues);
185
Devang Patel2c98b522008-11-14 22:49:37 +0000186 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
187 I != E; ++I) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000188 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patel159f7ce2008-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 Patel2c98b522008-11-14 22:49:37 +0000191 }
192
193 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000194 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patel159f7ce2008-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 Patel2c98b522008-11-14 22:49:37 +0000198 }
199
200 // Remove all names from types.
Devang Patel159f7ce2008-11-18 21:34:39 +0000201 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202
Devang Patel2c98b522008-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 Patel159f7ce2008-11-18 21:34:39 +0000209bool StripDebugInfo(Module &M) {
Devang Patel2c98b522008-11-14 22:49:37 +0000210
Devang Patel4ed9e402009-03-02 22:50:58 +0000211 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
212 findUsedValues(M, llvmUsedValues);
213
Devang Patel166f8432009-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 Patel4ed9e402009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +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");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249
Devang Patel2d53e2d2008-11-18 21:13:41 +0000250 std::vector<Constant*> DeadConstants;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251
252 // Remove all of the calls to the debugger intrinsics, and remove them from
253 // the module.
254 if (FuncStart) {
255 while (!FuncStart->use_empty()) {
256 CallInst *CI = cast<CallInst>(FuncStart->use_back());
257 Value *Arg = CI->getOperand(1);
258 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
259 CI->eraseFromParent();
260 if (Arg->use_empty())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000261 if (Constant *C = dyn_cast<Constant>(Arg))
262 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 }
264 FuncStart->eraseFromParent();
265 }
266 if (StopPoint) {
267 while (!StopPoint->use_empty()) {
268 CallInst *CI = cast<CallInst>(StopPoint->use_back());
269 Value *Arg = CI->getOperand(3);
270 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
271 CI->eraseFromParent();
272 if (Arg->use_empty())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000273 if (Constant *C = dyn_cast<Constant>(Arg))
274 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 }
276 StopPoint->eraseFromParent();
277 }
278 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 Patel2d53e2d2008-11-18 21:13:41 +0000285 if (Constant *C = dyn_cast<Constant>(Arg))
286 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 }
288 RegionStart->eraseFromParent();
289 }
290 if (RegionEnd) {
291 while (!RegionEnd->use_empty()) {
292 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
293 Value *Arg = CI->getOperand(1);
294 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
295 CI->eraseFromParent();
296 if (Arg->use_empty())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000297 if (Constant *C = dyn_cast<Constant>(Arg))
298 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 }
300 RegionEnd->eraseFromParent();
301 }
302 if (Declare) {
303 while (!Declare->use_empty()) {
304 CallInst *CI = cast<CallInst>(Declare->use_back());
Devang Patelabd69112008-11-20 01:20:42 +0000305 Value *Arg1 = CI->getOperand(1);
306 Value *Arg2 = CI->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
308 CI->eraseFromParent();
Devang Patelabd69112008-11-20 01:20:42 +0000309 if (Arg1->use_empty()) {
310 if (Constant *C = dyn_cast<Constant>(Arg1))
311 DeadConstants.push_back(C);
Devang Patele68804a2009-03-03 21:31:02 +0000312 else
Dan Gohmandb2b9462009-05-02 20:22:10 +0000313 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Devang Patelabd69112008-11-20 01:20:42 +0000314 }
315 if (Arg2->use_empty())
316 if (Constant *C = dyn_cast<Constant>(Arg2))
Devang Patel2d53e2d2008-11-18 21:13:41 +0000317 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 }
319 Declare->eraseFromParent();
320 }
321
Devang Patel2fa85562008-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 Espindolaa168fc92009-01-15 20:18:42 +0000324 // FIXME: Use private linkage maybe?
Devang Patel2fa85562008-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 Patel2d53e2d2008-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 Patel2fa85562008-11-13 01:28:40 +0000336
337 // Delete all dbg variables.
Devang Patel43f586b2008-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 Patel2fa85562008-11-13 01:28:40 +0000347
Devang Patel2d53e2d2008-11-18 21:13:41 +0000348 if (DeadConstants.empty())
Devang Patel2c98b522008-11-14 22:49:37 +0000349 return false;
350
Devang Patel2fa85562008-11-13 01:28:40 +0000351 // Delete any internal globals that were only used by the debugger intrinsics.
Devang Patel2d53e2d2008-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 Espindolaa168fc92009-01-15 20:18:42 +0000356 if (GV->hasLocalLinkage())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000357 RemoveDeadConstant(GV);
358 }
359 else
360 RemoveDeadConstant(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 }
362
Devang Patel2fa85562008-11-13 01:28:40 +0000363 // Remove all llvm.dbg types.
364 TypeSymbolTable &ST = M.getTypeSymbolTable();
Chris Lattnerba58d9c2008-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 Patel2fa85562008-11-13 01:28:40 +0000367 ST.remove(TI++);
368 else
369 ++TI;
370 }
371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 return true;
373}
Devang Patel159f7ce2008-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 Patel3876c8a2009-03-09 20:49:37 +0000386
387bool StripDebugDeclare::runOnModule(Module &M) {
388
389 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel3876c8a2009-03-09 20:49:37 +0000390 std::vector<Constant*> DeadConstants;
391
Dale Johannesendd031b72009-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 Gohmandb2b9462009-05-02 20:22:10 +0000403 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesendd031b72009-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 Patel3876c8a2009-03-09 20:49:37 +0000408 }
Dale Johannesendd031b72009-03-13 22:59:47 +0000409 Declare->eraseFromParent();
Devang Patel3876c8a2009-03-09 20:49:37 +0000410 }
Devang Patel3876c8a2009-03-09 20:49:37 +0000411
Devang Patel3a402b02009-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 Patel3876c8a2009-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}