blob: 066487fe15929025c83484f03b307191cf70c2fe [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"
Reid Spenceref9b9a72007-02-05 20:47:22 +000029#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000030#include "llvm/TypeSymbolTable.h"
Devang Patel9adb01c2009-03-03 21:31:02 +000031#include "llvm/Transforms/Utils/Local.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000032#include "llvm/Support/Compiler.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 {
Reid Spencer9133fe22007-02-05 23:32:05 +000037 class VISIBILITY_HIDDEN 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
51 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
52 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
64 class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
65 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!");
Devang Patelbf5db812008-11-13 01:28:40 +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))
118 Operands.insert(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.
Devang Patelbf5db812008-11-13 01:28:40 +0000128 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
129 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()) {
Devang Patelf17fc462008-11-18 21:34:39 +0000140 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
141 // 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; ) {
150 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
151 ++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.
158void findUsedValues(Module &M,
159 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
Devang Patel229de952008-11-14 22:49:37 +0000160 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
161 llvmUsedValues.insert(LLVMUsed);
162 // Collect values that are preserved as per explicit request.
163 // llvm.used is used to list these values.
164 if (ConstantArray *Inits =
165 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
166 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
167 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
168 llvmUsedValues.insert(GV);
169 else if (ConstantExpr *CE =
170 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
171 if (CE->getOpcode() == Instruction::BitCast)
172 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
173 llvmUsedValues.insert(GV);
Devang Patel8c231e52008-01-16 03:33:05 +0000174 }
175 }
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000176 }
Devang Patel4460a7e2008-11-18 21:13:41 +0000177}
178
179/// StripSymbolNames - Strip symbol names.
Devang Patelf17fc462008-11-18 21:34:39 +0000180bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel4460a7e2008-11-18 21:13:41 +0000181
182 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
183 findUsedValues(M, llvmUsedValues);
184
Devang Patel229de952008-11-14 22:49:37 +0000185 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
186 I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000187 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patelf17fc462008-11-18 21:34:39 +0000188 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
189 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel229de952008-11-14 22:49:37 +0000190 }
191
192 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000193 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patelf17fc462008-11-18 21:34:39 +0000194 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
195 I->setName(""); // Internal symbols can't participate in linkage
196 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel229de952008-11-14 22:49:37 +0000197 }
198
199 // Remove all names from types.
Devang Patelf17fc462008-11-18 21:34:39 +0000200 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000201
Devang Patel229de952008-11-14 22:49:37 +0000202 return true;
203}
204
205// StripDebugInfo - Strip debug info in the module if it exists.
206// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
207// llvm.dbg.region.end calls, and any globals they point to if now dead.
Devang Patelf17fc462008-11-18 21:34:39 +0000208bool StripDebugInfo(Module &M) {
Devang Patel229de952008-11-14 22:49:37 +0000209
Devang Patel73df3c92009-03-02 22:50:58 +0000210 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
211 findUsedValues(M, llvmUsedValues);
212
213 // Delete all dbg variables.
214 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
215 I != E; ++I) {
216 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
217 if (!GV) continue;
218 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
219 if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) {
220 GV->replaceAllUsesWith(UndefValue::get(GV->getType()));
221 }
222 }
223 }
224
Reid Spencer688b0492007-02-05 21:19:13 +0000225 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
226 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
227 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
228 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
229 Function *Declare = M.getFunction("llvm.dbg.declare");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000230
Devang Patel4460a7e2008-11-18 21:13:41 +0000231 std::vector<Constant*> DeadConstants;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000232
233 // Remove all of the calls to the debugger intrinsics, and remove them from
234 // the module.
235 if (FuncStart) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000236 while (!FuncStart->use_empty()) {
237 CallInst *CI = cast<CallInst>(FuncStart->use_back());
238 Value *Arg = CI->getOperand(1);
Jim Laskey4ca97572006-03-23 18:11:33 +0000239 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000240 CI->eraseFromParent();
241 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000242 if (Constant *C = dyn_cast<Constant>(Arg))
243 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000244 }
245 FuncStart->eraseFromParent();
246 }
247 if (StopPoint) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000248 while (!StopPoint->use_empty()) {
249 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyf4321a32006-03-13 13:07:37 +0000250 Value *Arg = CI->getOperand(3);
Jim Laskey4ca97572006-03-23 18:11:33 +0000251 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000252 CI->eraseFromParent();
253 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000254 if (Constant *C = dyn_cast<Constant>(Arg))
255 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000256 }
257 StopPoint->eraseFromParent();
258 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000259 if (RegionStart) {
260 while (!RegionStart->use_empty()) {
261 CallInst *CI = cast<CallInst>(RegionStart->use_back());
262 Value *Arg = CI->getOperand(1);
263 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
264 CI->eraseFromParent();
265 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000266 if (Constant *C = dyn_cast<Constant>(Arg))
267 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000268 }
269 RegionStart->eraseFromParent();
270 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000271 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000272 while (!RegionEnd->use_empty()) {
273 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000274 Value *Arg = CI->getOperand(1);
275 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000276 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000277 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000278 if (Constant *C = dyn_cast<Constant>(Arg))
279 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000280 }
281 RegionEnd->eraseFromParent();
282 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000283 if (Declare) {
284 while (!Declare->use_empty()) {
285 CallInst *CI = cast<CallInst>(Declare->use_back());
Devang Patelf23de862008-11-20 01:20:42 +0000286 Value *Arg1 = CI->getOperand(1);
287 Value *Arg2 = CI->getOperand(2);
Jim Laskey4ca97572006-03-23 18:11:33 +0000288 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
289 CI->eraseFromParent();
Devang Patelf23de862008-11-20 01:20:42 +0000290 if (Arg1->use_empty()) {
291 if (Constant *C = dyn_cast<Constant>(Arg1))
292 DeadConstants.push_back(C);
Devang Patel9adb01c2009-03-03 21:31:02 +0000293 else
294 RecursivelyDeleteTriviallyDeadInstructions(Arg1, NULL);
Devang Patelf23de862008-11-20 01:20:42 +0000295 }
296 if (Arg2->use_empty())
297 if (Constant *C = dyn_cast<Constant>(Arg2))
Devang Patel4460a7e2008-11-18 21:13:41 +0000298 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000299 }
300 Declare->eraseFromParent();
301 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000302
Devang Patelbf5db812008-11-13 01:28:40 +0000303 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
304 // but since we are removing all debug information, make them internal now.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000305 // FIXME: Use private linkage maybe?
Devang Patelbf5db812008-11-13 01:28:40 +0000306 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
307 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
308 GV->setLinkage(GlobalValue::InternalLinkage);
309
310 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
311 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
312 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patel4460a7e2008-11-18 21:13:41 +0000313
314 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
315 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
316 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patelbf5db812008-11-13 01:28:40 +0000317
318 // Delete all dbg variables.
Devang Patelfb5fd5a2008-11-19 00:22:02 +0000319 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
320 I != E; ++I) {
321 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
322 if (!GV) continue;
323 if (GV->use_empty() && llvmUsedValues.count(I) == 0
324 && (!GV->hasSection()
325 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
326 DeadConstants.push_back(GV);
327 }
Devang Patelbf5db812008-11-13 01:28:40 +0000328
Devang Patel4460a7e2008-11-18 21:13:41 +0000329 if (DeadConstants.empty())
Devang Patel229de952008-11-14 22:49:37 +0000330 return false;
331
Devang Patelbf5db812008-11-13 01:28:40 +0000332 // Delete any internal globals that were only used by the debugger intrinsics.
Devang Patel4460a7e2008-11-18 21:13:41 +0000333 while (!DeadConstants.empty()) {
334 Constant *C = DeadConstants.back();
335 DeadConstants.pop_back();
336 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000337 if (GV->hasLocalLinkage())
Devang Patel4460a7e2008-11-18 21:13:41 +0000338 RemoveDeadConstant(GV);
339 }
340 else
341 RemoveDeadConstant(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000342 }
343
Devang Patelbf5db812008-11-13 01:28:40 +0000344 // Remove all llvm.dbg types.
345 TypeSymbolTable &ST = M.getTypeSymbolTable();
Chris Lattner3f914f02008-11-16 06:35:18 +0000346 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
347 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
Devang Patelbf5db812008-11-13 01:28:40 +0000348 ST.remove(TI++);
349 else
350 ++TI;
351 }
352
Misha Brukmanfd939082005-04-21 23:48:37 +0000353 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000354}
Devang Patelf17fc462008-11-18 21:34:39 +0000355
356bool StripSymbols::runOnModule(Module &M) {
357 bool Changed = false;
358 Changed |= StripDebugInfo(M);
359 if (!OnlyDebugInfo)
360 Changed |= StripSymbolNames(M, false);
361 return Changed;
362}
363
364bool StripNonDebugSymbols::runOnModule(Module &M) {
365 return StripSymbolNames(M, true);
366}
Devang Patel23e528b2009-03-09 20:49:37 +0000367
368bool StripDebugDeclare::runOnModule(Module &M) {
369
370 Function *Declare = M.getFunction("llvm.dbg.declare");
371
372 if (!Declare)
373 return false;
374
375 std::vector<Constant*> DeadConstants;
376
377 while (!Declare->use_empty()) {
378 CallInst *CI = cast<CallInst>(Declare->use_back());
379 Value *Arg1 = CI->getOperand(1);
380 Value *Arg2 = CI->getOperand(2);
381 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
382 CI->eraseFromParent();
383 if (Arg1->use_empty()) {
384 if (Constant *C = dyn_cast<Constant>(Arg1))
385 DeadConstants.push_back(C);
386 else
387 RecursivelyDeleteTriviallyDeadInstructions(Arg1, NULL);
388 }
389 if (Arg2->use_empty())
390 if (Constant *C = dyn_cast<Constant>(Arg2))
391 DeadConstants.push_back(C);
392 }
393 Declare->eraseFromParent();
394
395 while (!DeadConstants.empty()) {
396 Constant *C = DeadConstants.back();
397 DeadConstants.pop_back();
398 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
399 if (GV->hasLocalLinkage())
400 RemoveDeadConstant(GV);
401 }
402 else
403 RemoveDeadConstant(C);
404 }
405
406 return true;
407}