blob: 5f8681ff454e427c5d34c657b32c7d39148dc241 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/SmallPtrSet.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000026#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000027#include "llvm/IR/Constants.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/Module.h"
Chandler Carruth4068e1a2013-01-07 15:43:51 +000031#include "llvm/IR/TypeFinder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000032#include "llvm/IR/ValueSymbolTable.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000033#include "llvm/Pass.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000034#include "llvm/Transforms/Utils/Local.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000035using namespace llvm;
36
37namespace {
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000038 class 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)
Owen Anderson081c34b2010-10-19 17:21:58 +000043 : ModulePass(ID), OnlyDebugInfo(ODI) {
44 initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
45 }
Chris Lattnere3ad43c2004-12-02 21:25:03 +000046
Devang Patelf17fc462008-11-18 21:34:39 +000047 virtual bool runOnModule(Module &M);
Devang Patel229de952008-11-14 22:49:37 +000048
Devang Patelf17fc462008-11-18 21:34:39 +000049 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.setPreservesAll();
51 }
52 };
53
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000054 class StripNonDebugSymbols : public ModulePass {
Devang Patelf17fc462008-11-18 21:34:39 +000055 public:
56 static char ID; // Pass identification, replacement for typeid
57 explicit StripNonDebugSymbols()
Owen Anderson081c34b2010-10-19 17:21:58 +000058 : ModulePass(ID) {
59 initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
60 }
Devang Patel229de952008-11-14 22:49:37 +000061
Chris Lattnere3ad43c2004-12-02 21:25:03 +000062 virtual bool runOnModule(Module &M);
63
64 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
65 AU.setPreservesAll();
66 }
67 };
Devang Patel23e528b2009-03-09 20:49:37 +000068
Nick Lewycky8aa9fba2009-09-03 06:43:15 +000069 class StripDebugDeclare : public ModulePass {
Devang Patel23e528b2009-03-09 20:49:37 +000070 public:
71 static char ID; // Pass identification, replacement for typeid
72 explicit StripDebugDeclare()
Owen Anderson081c34b2010-10-19 17:21:58 +000073 : ModulePass(ID) {
74 initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
75 }
Devang Patel23e528b2009-03-09 20:49:37 +000076
77 virtual bool runOnModule(Module &M);
78
79 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
80 AU.setPreservesAll();
81 }
82 };
Devang Patel26d14292010-07-01 19:49:20 +000083
84 class StripDeadDebugInfo : public ModulePass {
85 public:
86 static char ID; // Pass identification, replacement for typeid
87 explicit StripDeadDebugInfo()
Owen Anderson081c34b2010-10-19 17:21:58 +000088 : ModulePass(ID) {
89 initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
90 }
Devang Patel26d14292010-07-01 19:49:20 +000091
92 virtual bool runOnModule(Module &M);
93
94 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
95 AU.setPreservesAll();
96 }
97 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000098}
99
Dan Gohman844731a2008-05-13 00:00:25 +0000100char StripSymbols::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000101INITIALIZE_PASS(StripSymbols, "strip",
Owen Andersonce665bd2010-10-07 22:25:06 +0000102 "Strip all symbols from a module", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +0000103
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000104ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
105 return new StripSymbols(OnlyDebugInfo);
106}
107
Devang Patelf17fc462008-11-18 21:34:39 +0000108char StripNonDebugSymbols::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000109INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
110 "Strip all symbols, except dbg symbols, from a module",
Owen Andersonce665bd2010-10-07 22:25:06 +0000111 false, false)
Devang Patelf17fc462008-11-18 21:34:39 +0000112
113ModulePass *llvm::createStripNonDebugSymbolsPass() {
114 return new StripNonDebugSymbols();
115}
116
Devang Patel23e528b2009-03-09 20:49:37 +0000117char StripDebugDeclare::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000118INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
Owen Andersonce665bd2010-10-07 22:25:06 +0000119 "Strip all llvm.dbg.declare intrinsics", false, false)
Devang Patel23e528b2009-03-09 20:49:37 +0000120
121ModulePass *llvm::createStripDebugDeclarePass() {
122 return new StripDebugDeclare();
123}
124
Devang Patel26d14292010-07-01 19:49:20 +0000125char StripDeadDebugInfo::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000126INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
Owen Andersonce665bd2010-10-07 22:25:06 +0000127 "Strip debug info for unused symbols", false, false)
Devang Patel26d14292010-07-01 19:49:20 +0000128
129ModulePass *llvm::createStripDeadDebugInfoPass() {
130 return new StripDeadDebugInfo();
131}
132
Devang Patelbf5db812008-11-13 01:28:40 +0000133/// OnlyUsedBy - Return true if V is only used by Usr.
134static bool OnlyUsedBy(Value *V, Value *Usr) {
135 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
136 User *U = *I;
137 if (U != Usr)
138 return false;
139 }
140 return true;
141}
142
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000143static void RemoveDeadConstant(Constant *C) {
144 assert(C->use_empty() && "Constant is not dead!");
Chris Lattner0eeb9132009-10-28 05:14:34 +0000145 SmallPtrSet<Constant*, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000146 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
Chris Lattneraca50a92011-07-09 17:59:15 +0000147 if (OnlyUsedBy(C->getOperand(i), C))
Chris Lattner0eeb9132009-10-28 05:14:34 +0000148 Operands.insert(cast<Constant>(C->getOperand(i)));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000149 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000150 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000151 GV->eraseFromParent();
152 }
153 else if (!isa<Function>(C))
Devang Patelf23de862008-11-20 01:20:42 +0000154 if (isa<CompositeType>(C->getType()))
155 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +0000156
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000157 // If the constant referenced anything, see if we can delete it as well.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000158 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
Devang Patelbf5db812008-11-13 01:28:40 +0000159 OE = Operands.end(); OI != OE; ++OI)
160 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000161}
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000162
Chris Lattner7f1444b2007-02-07 06:22:45 +0000163// Strip the symbol table of its names.
164//
Devang Patelf17fc462008-11-18 21:34:39 +0000165static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner7f1444b2007-02-07 06:22:45 +0000166 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000167 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +0000168 ++VI;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000169 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar460f6562009-07-26 09:48:23 +0000170 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000171 // Set name to "", removing from symbol table!
172 V->setName("");
Chris Lattner7f1444b2007-02-07 06:22:45 +0000173 }
174 }
175}
176
Chris Lattner1afcace2011-07-09 17:41:24 +0000177// Strip any named types of their names.
178static void StripTypeNames(Module &M, bool PreserveDbgInfo) {
Bill Wendling573e9732012-08-03 00:30:35 +0000179 TypeFinder StructTypes;
180 StructTypes.run(M, false);
Chris Lattner1afcace2011-07-09 17:41:24 +0000181
182 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
183 StructType *STy = StructTypes[i];
Chris Lattner3ebb6492011-08-12 18:06:37 +0000184 if (STy->isLiteral() || STy->getName().empty()) continue;
Chris Lattner1afcace2011-07-09 17:41:24 +0000185
186 if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg"))
187 continue;
188
189 STy->setName("");
Devang Patelf17fc462008-11-18 21:34:39 +0000190 }
Chris Lattner7f1444b2007-02-07 06:22:45 +0000191}
192
Devang Patel4460a7e2008-11-18 21:13:41 +0000193/// Find values that are marked as llvm.used.
Chris Lattner401e10c2009-07-20 06:14:25 +0000194static void findUsedValues(GlobalVariable *LLVMUsed,
195 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
196 if (LLVMUsed == 0) return;
197 UsedValues.insert(LLVMUsed);
198
199 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
200 if (Inits == 0) return;
201
202 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
203 if (GlobalValue *GV =
204 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
205 UsedValues.insert(GV);
Devang Patel4460a7e2008-11-18 21:13:41 +0000206}
207
208/// StripSymbolNames - Strip symbol names.
Dan Gohman7db949d2009-08-07 01:32:21 +0000209static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel4460a7e2008-11-18 21:13:41 +0000210
211 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner401e10c2009-07-20 06:14:25 +0000212 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
213 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel4460a7e2008-11-18 21:13:41 +0000214
Devang Patel229de952008-11-14 22:49:37 +0000215 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
216 I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000217 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000218 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000219 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel229de952008-11-14 22:49:37 +0000220 }
221
222 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000223 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000224 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelf17fc462008-11-18 21:34:39 +0000225 I->setName(""); // Internal symbols can't participate in linkage
226 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel229de952008-11-14 22:49:37 +0000227 }
228
229 // Remove all names from types.
Chris Lattner1afcace2011-07-09 17:41:24 +0000230 StripTypeNames(M, PreserveDbgInfo);
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000231
Devang Patel229de952008-11-14 22:49:37 +0000232 return true;
233}
234
235// StripDebugInfo - Strip debug info in the module if it exists.
236// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
237// llvm.dbg.region.end calls, and any globals they point to if now dead.
Dan Gohman7db949d2009-08-07 01:32:21 +0000238static bool StripDebugInfo(Module &M) {
Devang Patel229de952008-11-14 22:49:37 +0000239
Devang Patel76e3e502009-11-17 00:47:06 +0000240 bool Changed = false;
241
Devang Patele4b27562009-08-28 23:24:31 +0000242 // Remove all of the calls to the debugger intrinsics, and remove them from
243 // the module.
Devang Patel76e3e502009-11-17 00:47:06 +0000244 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
Jim Laskey4ca97572006-03-23 18:11:33 +0000245 while (!Declare->use_empty()) {
246 CallInst *CI = cast<CallInst>(Declare->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000247 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000248 }
249 Declare->eraseFromParent();
Devang Patel76e3e502009-11-17 00:47:06 +0000250 Changed = true;
Jim Laskey4ca97572006-03-23 18:11:33 +0000251 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000252
Devang Pateldf9292c2010-02-10 21:19:56 +0000253 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
254 while (!DbgVal->use_empty()) {
255 CallInst *CI = cast<CallInst>(DbgVal->use_back());
256 CI->eraseFromParent();
257 }
258 DbgVal->eraseFromParent();
259 Changed = true;
260 }
261
Devang Patel444a08c2010-06-30 21:29:00 +0000262 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
263 NME = M.named_metadata_end(); NMI != NME;) {
264 NamedMDNode *NMD = NMI;
265 ++NMI;
Devang Patele62b2032010-07-01 18:27:46 +0000266 if (NMD->getName().startswith("llvm.dbg.")) {
Devang Patel444a08c2010-06-30 21:29:00 +0000267 NMD->eraseFromParent();
Devang Patele62b2032010-07-01 18:27:46 +0000268 Changed = true;
269 }
Devang Patel69b4d1c2010-05-20 16:49:22 +0000270 }
Duncan Sandsa7065b12010-06-29 14:52:10 +0000271
Duncan Sandsa7065b12010-06-29 14:52:10 +0000272 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
Devang Patel76e3e502009-11-17 00:47:06 +0000273 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
274 ++FI)
275 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
Duncan Sandsa7065b12010-06-29 14:52:10 +0000276 ++BI) {
Dan Gohman549979f2010-07-20 23:49:44 +0000277 if (!BI->getDebugLoc().isUnknown()) {
278 Changed = true;
279 BI->setDebugLoc(DebugLoc());
280 }
Duncan Sandsa7065b12010-06-29 14:52:10 +0000281 }
Devang Patelbf5db812008-11-13 01:28:40 +0000282
Duncan Sandsa7065b12010-06-29 14:52:10 +0000283 return Changed;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000284}
Devang Patelf17fc462008-11-18 21:34:39 +0000285
286bool StripSymbols::runOnModule(Module &M) {
287 bool Changed = false;
288 Changed |= StripDebugInfo(M);
289 if (!OnlyDebugInfo)
290 Changed |= StripSymbolNames(M, false);
291 return Changed;
292}
293
294bool StripNonDebugSymbols::runOnModule(Module &M) {
295 return StripSymbolNames(M, true);
296}
Devang Patel23e528b2009-03-09 20:49:37 +0000297
298bool StripDebugDeclare::runOnModule(Module &M) {
299
300 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel23e528b2009-03-09 20:49:37 +0000301 std::vector<Constant*> DeadConstants;
302
Dale Johannesen44252402009-03-13 22:59:47 +0000303 if (Declare) {
304 while (!Declare->use_empty()) {
305 CallInst *CI = cast<CallInst>(Declare->use_back());
Gabor Greife9af3522010-06-30 12:40:35 +0000306 Value *Arg1 = CI->getArgOperand(0);
307 Value *Arg2 = CI->getArgOperand(1);
Dale Johannesen44252402009-03-13 22:59:47 +0000308 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
309 CI->eraseFromParent();
310 if (Arg1->use_empty()) {
311 if (Constant *C = dyn_cast<Constant>(Arg1))
312 DeadConstants.push_back(C);
313 else
Dan Gohmane66f6f12009-05-02 20:22:10 +0000314 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesen44252402009-03-13 22:59:47 +0000315 }
316 if (Arg2->use_empty())
317 if (Constant *C = dyn_cast<Constant>(Arg2))
318 DeadConstants.push_back(C);
Devang Patel23e528b2009-03-09 20:49:37 +0000319 }
Dale Johannesen44252402009-03-13 22:59:47 +0000320 Declare->eraseFromParent();
Devang Patel23e528b2009-03-09 20:49:37 +0000321 }
Devang Patel23e528b2009-03-09 20:49:37 +0000322
323 while (!DeadConstants.empty()) {
324 Constant *C = DeadConstants.back();
325 DeadConstants.pop_back();
326 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
327 if (GV->hasLocalLinkage())
328 RemoveDeadConstant(GV);
Chris Lattner0eeb9132009-10-28 05:14:34 +0000329 } else
Devang Patel23e528b2009-03-09 20:49:37 +0000330 RemoveDeadConstant(C);
331 }
332
333 return true;
334}
Devang Patel26d14292010-07-01 19:49:20 +0000335
336/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
337/// printer to not emit usual symbol prefix before the symbol name is used then
338/// return linkage name after skipping this special LLVM prefix.
339static StringRef getRealLinkageName(StringRef LinkageName) {
340 char One = '\1';
341 if (LinkageName.startswith(StringRef(&One, 1)))
342 return LinkageName.substr(1);
343 return LinkageName;
344}
345
346bool StripDeadDebugInfo::runOnModule(Module &M) {
347 bool Changed = false;
348
349 // Debugging infomration is encoded in llvm IR using metadata. This is designed
350 // such a way that debug info for symbols preserved even if symbols are
351 // optimized away by the optimizer. This special pass removes debug info for
352 // such symbols.
353
354 // llvm.dbg.gv keeps track of debug info for global variables.
355 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
356 SmallVector<MDNode *, 8> MDs;
357 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
358 if (DIGlobalVariable(NMD->getOperand(i)).Verify())
359 MDs.push_back(NMD->getOperand(i));
360 else
361 Changed = true;
362 NMD->eraseFromParent();
363 NMD = NULL;
364
365 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
366 E = MDs.end(); I != E; ++I) {
Devang Patel1955cf12010-08-25 18:52:02 +0000367 GlobalVariable *GV = DIGlobalVariable(*I).getGlobal();
368 if (GV && M.getGlobalVariable(GV->getName(), true)) {
Devang Patel26d14292010-07-01 19:49:20 +0000369 if (!NMD)
370 NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
371 NMD->addOperand(*I);
372 }
373 else
374 Changed = true;
375 }
376 }
377
378 // llvm.dbg.sp keeps track of debug info for subprograms.
379 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
380 SmallVector<MDNode *, 8> MDs;
381 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
382 if (DISubprogram(NMD->getOperand(i)).Verify())
383 MDs.push_back(NMD->getOperand(i));
384 else
385 Changed = true;
386 NMD->eraseFromParent();
387 NMD = NULL;
388
389 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
390 E = MDs.end(); I != E; ++I) {
391 bool FnIsLive = false;
392 if (Function *F = DISubprogram(*I).getFunction())
393 if (M.getFunction(F->getName()))
394 FnIsLive = true;
395 if (FnIsLive) {
396 if (!NMD)
397 NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
398 NMD->addOperand(*I);
399 } else {
400 // Remove llvm.dbg.lv.fnname named mdnode which may have been used
401 // to hold debug info for dead function's local variables.
402 StringRef FName = DISubprogram(*I).getLinkageName();
403 if (FName.empty())
404 FName = DISubprogram(*I).getName();
405 if (NamedMDNode *LVNMD =
406 M.getNamedMetadata(Twine("llvm.dbg.lv.",
407 getRealLinkageName(FName))))
408 LVNMD->eraseFromParent();
409 }
410 }
411 }
412
413 return Changed;
414}