blob: b5caa9a557c9dfcc7272565d88d5e0e027966431 [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"
Devang Patel9adb01c2009-03-03 21:31:02 +000031#include "llvm/Transforms/Utils/Local.h"
Chris Lattner1afcace2011-07-09 17:41:24 +000032#include "llvm/ADT/DenseMap.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)
Owen Anderson081c34b2010-10-19 17:21:58 +000042 : ModulePass(ID), OnlyDebugInfo(ODI) {
43 initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
44 }
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
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000053 class StripNonDebugSymbols : public ModulePass {
Devang Patelf17fc462008-11-18 21:34:39 +000054 public:
55 static char ID; // Pass identification, replacement for typeid
56 explicit StripNonDebugSymbols()
Owen Anderson081c34b2010-10-19 17:21:58 +000057 : ModulePass(ID) {
58 initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
59 }
Devang Patel229de952008-11-14 22:49:37 +000060
Chris Lattnere3ad43c2004-12-02 21:25:03 +000061 virtual bool runOnModule(Module &M);
62
63 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.setPreservesAll();
65 }
66 };
Devang Patel23e528b2009-03-09 20:49:37 +000067
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000068 class StripDebugDeclare : public ModulePass {
Devang Patel23e528b2009-03-09 20:49:37 +000069 public:
70 static char ID; // Pass identification, replacement for typeid
71 explicit StripDebugDeclare()
Owen Anderson081c34b2010-10-19 17:21:58 +000072 : ModulePass(ID) {
73 initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
74 }
Devang Patel23e528b2009-03-09 20:49:37 +000075
76 virtual bool runOnModule(Module &M);
77
78 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
79 AU.setPreservesAll();
80 }
81 };
Devang Patel26d14292010-07-01 19:49:20 +000082
83 class StripDeadDebugInfo : public ModulePass {
84 public:
85 static char ID; // Pass identification, replacement for typeid
86 explicit StripDeadDebugInfo()
Owen Anderson081c34b2010-10-19 17:21:58 +000087 : ModulePass(ID) {
88 initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
89 }
Devang Patel26d14292010-07-01 19:49:20 +000090
91 virtual bool runOnModule(Module &M);
92
93 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
94 AU.setPreservesAll();
95 }
96 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000097}
98
Dan Gohman844731a2008-05-13 00:00:25 +000099char StripSymbols::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000100INITIALIZE_PASS(StripSymbols, "strip",
Owen Andersonce665bd2010-10-07 22:25:06 +0000101 "Strip all symbols from a module", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +0000102
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000103ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
104 return new StripSymbols(OnlyDebugInfo);
105}
106
Devang Patelf17fc462008-11-18 21:34:39 +0000107char StripNonDebugSymbols::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000108INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
109 "Strip all symbols, except dbg symbols, from a module",
Owen Andersonce665bd2010-10-07 22:25:06 +0000110 false, false)
Devang Patelf17fc462008-11-18 21:34:39 +0000111
112ModulePass *llvm::createStripNonDebugSymbolsPass() {
113 return new StripNonDebugSymbols();
114}
115
Devang Patel23e528b2009-03-09 20:49:37 +0000116char StripDebugDeclare::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000117INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
Owen Andersonce665bd2010-10-07 22:25:06 +0000118 "Strip all llvm.dbg.declare intrinsics", false, false)
Devang Patel23e528b2009-03-09 20:49:37 +0000119
120ModulePass *llvm::createStripDebugDeclarePass() {
121 return new StripDebugDeclare();
122}
123
Devang Patel26d14292010-07-01 19:49:20 +0000124char StripDeadDebugInfo::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000125INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
Owen Andersonce665bd2010-10-07 22:25:06 +0000126 "Strip debug info for unused symbols", false, false)
Devang Patel26d14292010-07-01 19:49:20 +0000127
128ModulePass *llvm::createStripDeadDebugInfoPass() {
129 return new StripDeadDebugInfo();
130}
131
Devang Patelbf5db812008-11-13 01:28:40 +0000132/// OnlyUsedBy - Return true if V is only used by Usr.
133static bool OnlyUsedBy(Value *V, Value *Usr) {
134 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
135 User *U = *I;
136 if (U != Usr)
137 return false;
138 }
139 return true;
140}
141
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000142static void RemoveDeadConstant(Constant *C) {
143 assert(C->use_empty() && "Constant is not dead!");
Chris Lattner0eeb9132009-10-28 05:14:34 +0000144 SmallPtrSet<Constant*, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000145 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
Chris Lattneraca50a92011-07-09 17:59:15 +0000146 if (OnlyUsedBy(C->getOperand(i), C))
Chris Lattner0eeb9132009-10-28 05:14:34 +0000147 Operands.insert(cast<Constant>(C->getOperand(i)));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000148 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000149 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000150 GV->eraseFromParent();
151 }
152 else if (!isa<Function>(C))
Devang Patelf23de862008-11-20 01:20:42 +0000153 if (isa<CompositeType>(C->getType()))
154 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +0000155
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000156 // If the constant referenced anything, see if we can delete it as well.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000157 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
Devang Patelbf5db812008-11-13 01:28:40 +0000158 OE = Operands.end(); OI != OE; ++OI)
159 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000160}
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000161
Chris Lattner7f1444b2007-02-07 06:22:45 +0000162// Strip the symbol table of its names.
163//
Devang Patelf17fc462008-11-18 21:34:39 +0000164static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner7f1444b2007-02-07 06:22:45 +0000165 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000166 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +0000167 ++VI;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000168 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar460f6562009-07-26 09:48:23 +0000169 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000170 // Set name to "", removing from symbol table!
171 V->setName("");
Chris Lattner7f1444b2007-02-07 06:22:45 +0000172 }
173 }
174}
175
Chris Lattner1afcace2011-07-09 17:41:24 +0000176// Strip any named types of their names.
177static void StripTypeNames(Module &M, bool PreserveDbgInfo) {
178 std::vector<StructType*> StructTypes;
179 M.findUsedStructTypes(StructTypes);
180
181 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
182 StructType *STy = StructTypes[i];
Chris Lattner3ebb6492011-08-12 18:06:37 +0000183 if (STy->isLiteral() || STy->getName().empty()) continue;
Chris Lattner1afcace2011-07-09 17:41:24 +0000184
185 if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg"))
186 continue;
187
188 STy->setName("");
Devang Patelf17fc462008-11-18 21:34:39 +0000189 }
Chris Lattner7f1444b2007-02-07 06:22:45 +0000190}
191
Devang Patel4460a7e2008-11-18 21:13:41 +0000192/// Find values that are marked as llvm.used.
Chris Lattner401e10c2009-07-20 06:14:25 +0000193static void findUsedValues(GlobalVariable *LLVMUsed,
194 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
195 if (LLVMUsed == 0) return;
196 UsedValues.insert(LLVMUsed);
197
198 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
199 if (Inits == 0) return;
200
201 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
202 if (GlobalValue *GV =
203 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
204 UsedValues.insert(GV);
Devang Patel4460a7e2008-11-18 21:13:41 +0000205}
206
207/// StripSymbolNames - Strip symbol names.
Dan Gohman7db949d2009-08-07 01:32:21 +0000208static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel4460a7e2008-11-18 21:13:41 +0000209
210 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner401e10c2009-07-20 06:14:25 +0000211 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
212 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel4460a7e2008-11-18 21:13:41 +0000213
Devang Patel229de952008-11-14 22:49:37 +0000214 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
215 I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000216 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000217 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000218 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel229de952008-11-14 22:49:37 +0000219 }
220
221 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000222 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000223 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000224 I->setName(""); // Internal symbols can't participate in linkage
225 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel229de952008-11-14 22:49:37 +0000226 }
227
228 // Remove all names from types.
Chris Lattner1afcace2011-07-09 17:41:24 +0000229 StripTypeNames(M, PreserveDbgInfo);
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000230
Devang Patel229de952008-11-14 22:49:37 +0000231 return true;
232}
233
234// StripDebugInfo - Strip debug info in the module if it exists.
235// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
236// llvm.dbg.region.end calls, and any globals they point to if now dead.
Dan Gohman7db949d2009-08-07 01:32:21 +0000237static bool StripDebugInfo(Module &M) {
Devang Patel229de952008-11-14 22:49:37 +0000238
Devang Patel76e3e502009-11-17 00:47:06 +0000239 bool Changed = false;
240
Devang Patele4b27562009-08-28 23:24:31 +0000241 // Remove all of the calls to the debugger intrinsics, and remove them from
242 // the module.
Devang Patel76e3e502009-11-17 00:47:06 +0000243 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
Jim Laskey4ca97572006-03-23 18:11:33 +0000244 while (!Declare->use_empty()) {
245 CallInst *CI = cast<CallInst>(Declare->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000246 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000247 }
248 Declare->eraseFromParent();
Devang Patel76e3e502009-11-17 00:47:06 +0000249 Changed = true;
Jim Laskey4ca97572006-03-23 18:11:33 +0000250 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000251
Devang Pateldf9292c2010-02-10 21:19:56 +0000252 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
253 while (!DbgVal->use_empty()) {
254 CallInst *CI = cast<CallInst>(DbgVal->use_back());
255 CI->eraseFromParent();
256 }
257 DbgVal->eraseFromParent();
258 Changed = true;
259 }
260
Devang Patel444a08c2010-06-30 21:29:00 +0000261 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
262 NME = M.named_metadata_end(); NMI != NME;) {
263 NamedMDNode *NMD = NMI;
264 ++NMI;
Devang Patele62b2032010-07-01 18:27:46 +0000265 if (NMD->getName().startswith("llvm.dbg.")) {
Devang Patel444a08c2010-06-30 21:29:00 +0000266 NMD->eraseFromParent();
Devang Patele62b2032010-07-01 18:27:46 +0000267 Changed = true;
268 }
Devang Patel69b4d1c2010-05-20 16:49:22 +0000269 }
Duncan Sandsa7065b12010-06-29 14:52:10 +0000270
Duncan Sandsa7065b12010-06-29 14:52:10 +0000271 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
Devang Patel76e3e502009-11-17 00:47:06 +0000272 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
273 ++FI)
274 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
Duncan Sandsa7065b12010-06-29 14:52:10 +0000275 ++BI) {
Dan Gohman549979f2010-07-20 23:49:44 +0000276 if (!BI->getDebugLoc().isUnknown()) {
277 Changed = true;
278 BI->setDebugLoc(DebugLoc());
279 }
Duncan Sandsa7065b12010-06-29 14:52:10 +0000280 }
Devang Patelbf5db812008-11-13 01:28:40 +0000281
Duncan Sandsa7065b12010-06-29 14:52:10 +0000282 return Changed;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000283}
Devang Patelf17fc462008-11-18 21:34:39 +0000284
285bool StripSymbols::runOnModule(Module &M) {
286 bool Changed = false;
287 Changed |= StripDebugInfo(M);
288 if (!OnlyDebugInfo)
289 Changed |= StripSymbolNames(M, false);
290 return Changed;
291}
292
293bool StripNonDebugSymbols::runOnModule(Module &M) {
294 return StripSymbolNames(M, true);
295}
Devang Patel23e528b2009-03-09 20:49:37 +0000296
297bool StripDebugDeclare::runOnModule(Module &M) {
298
299 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel23e528b2009-03-09 20:49:37 +0000300 std::vector<Constant*> DeadConstants;
301
Dale Johannesen44252402009-03-13 22:59:47 +0000302 if (Declare) {
303 while (!Declare->use_empty()) {
304 CallInst *CI = cast<CallInst>(Declare->use_back());
Gabor Greife9af3522010-06-30 12:40:35 +0000305 Value *Arg1 = CI->getArgOperand(0);
306 Value *Arg2 = CI->getArgOperand(1);
Dale Johannesen44252402009-03-13 22:59:47 +0000307 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
308 CI->eraseFromParent();
309 if (Arg1->use_empty()) {
310 if (Constant *C = dyn_cast<Constant>(Arg1))
311 DeadConstants.push_back(C);
312 else
Dan Gohmane66f6f12009-05-02 20:22:10 +0000313 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesen44252402009-03-13 22:59:47 +0000314 }
315 if (Arg2->use_empty())
316 if (Constant *C = dyn_cast<Constant>(Arg2))
317 DeadConstants.push_back(C);
Devang Patel23e528b2009-03-09 20:49:37 +0000318 }
Dale Johannesen44252402009-03-13 22:59:47 +0000319 Declare->eraseFromParent();
Devang Patel23e528b2009-03-09 20:49:37 +0000320 }
Devang Patel23e528b2009-03-09 20:49:37 +0000321
322 while (!DeadConstants.empty()) {
323 Constant *C = DeadConstants.back();
324 DeadConstants.pop_back();
325 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
326 if (GV->hasLocalLinkage())
327 RemoveDeadConstant(GV);
Chris Lattner0eeb9132009-10-28 05:14:34 +0000328 } else
Devang Patel23e528b2009-03-09 20:49:37 +0000329 RemoveDeadConstant(C);
330 }
331
332 return true;
333}
Devang Patel26d14292010-07-01 19:49:20 +0000334
335/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
336/// printer to not emit usual symbol prefix before the symbol name is used then
337/// return linkage name after skipping this special LLVM prefix.
338static StringRef getRealLinkageName(StringRef LinkageName) {
339 char One = '\1';
340 if (LinkageName.startswith(StringRef(&One, 1)))
341 return LinkageName.substr(1);
342 return LinkageName;
343}
344
345bool StripDeadDebugInfo::runOnModule(Module &M) {
346 bool Changed = false;
347
348 // Debugging infomration is encoded in llvm IR using metadata. This is designed
349 // such a way that debug info for symbols preserved even if symbols are
350 // optimized away by the optimizer. This special pass removes debug info for
351 // such symbols.
352
353 // llvm.dbg.gv keeps track of debug info for global variables.
354 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
355 SmallVector<MDNode *, 8> MDs;
356 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
357 if (DIGlobalVariable(NMD->getOperand(i)).Verify())
358 MDs.push_back(NMD->getOperand(i));
359 else
360 Changed = true;
361 NMD->eraseFromParent();
362 NMD = NULL;
363
364 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
365 E = MDs.end(); I != E; ++I) {
Devang Patel1955cf12010-08-25 18:52:02 +0000366 GlobalVariable *GV = DIGlobalVariable(*I).getGlobal();
367 if (GV && M.getGlobalVariable(GV->getName(), true)) {
Devang Patel26d14292010-07-01 19:49:20 +0000368 if (!NMD)
369 NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
370 NMD->addOperand(*I);
371 }
372 else
373 Changed = true;
374 }
375 }
376
377 // llvm.dbg.sp keeps track of debug info for subprograms.
378 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
379 SmallVector<MDNode *, 8> MDs;
380 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
381 if (DISubprogram(NMD->getOperand(i)).Verify())
382 MDs.push_back(NMD->getOperand(i));
383 else
384 Changed = true;
385 NMD->eraseFromParent();
386 NMD = NULL;
387
388 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
389 E = MDs.end(); I != E; ++I) {
390 bool FnIsLive = false;
391 if (Function *F = DISubprogram(*I).getFunction())
392 if (M.getFunction(F->getName()))
393 FnIsLive = true;
394 if (FnIsLive) {
395 if (!NMD)
396 NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
397 NMD->addOperand(*I);
398 } else {
399 // Remove llvm.dbg.lv.fnname named mdnode which may have been used
400 // to hold debug info for dead function's local variables.
401 StringRef FName = DISubprogram(*I).getLinkageName();
402 if (FName.empty())
403 FName = DISubprogram(*I).getName();
404 if (NamedMDNode *LVNMD =
405 M.getNamedMetadata(Twine("llvm.dbg.lv.",
406 getRealLinkageName(FName))))
407 LVNMD->eraseFromParent();
408 }
409 }
410 }
411
412 return Changed;
413}