2004-12-02 22:25:03 +01:00
|
|
|
//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2004-12-02 22:25:03 +01:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2004-12-02 22:25:03 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-11-04 17:15:04 +01:00
|
|
|
// The StripSymbols transformation implements code stripping. Specifically, it
|
|
|
|
// can delete:
|
|
|
|
//
|
|
|
|
// * names for virtual registers
|
|
|
|
// * symbols for internal globals and functions
|
|
|
|
// * debug information
|
2004-12-02 22:25:03 +01:00
|
|
|
//
|
2007-11-04 17:15:04 +01:00
|
|
|
// Note that this transformation makes code much less readable, so it should
|
|
|
|
// only be used in situations where the 'strip' utility would be used, such as
|
|
|
|
// reducing code size or making it harder to reverse engineer code.
|
2004-12-02 22:25:03 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2004-12-03 17:22:08 +01:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2004-12-02 22:25:03 +01:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2009-06-26 03:49:18 +02:00
|
|
|
#include "llvm/Analysis/DebugInfo.h"
|
2007-02-05 21:47:22 +01:00
|
|
|
#include "llvm/ValueSymbolTable.h"
|
2007-01-06 08:24:44 +01:00
|
|
|
#include "llvm/TypeSymbolTable.h"
|
2009-03-03 22:31:02 +01:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2008-01-16 04:33:05 +01:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-12-02 22:25:03 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-09-03 08:43:15 +02:00
|
|
|
class StripSymbols : public ModulePass {
|
2004-12-02 22:25:03 +01:00
|
|
|
bool OnlyDebugInfo;
|
|
|
|
public:
|
2007-05-06 15:37:16 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-08-01 17:32:29 +02:00
|
|
|
explicit StripSymbols(bool ODI = false)
|
2010-10-19 19:21:58 +02:00
|
|
|
: ModulePass(ID), OnlyDebugInfo(ODI) {
|
|
|
|
initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2004-12-02 22:25:03 +01:00
|
|
|
|
2008-11-18 22:34:39 +01:00
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-11-14 23:49:37 +01:00
|
|
|
|
2009-09-03 08:43:15 +02:00
|
|
|
class StripNonDebugSymbols : public ModulePass {
|
2008-11-18 22:34:39 +01:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripNonDebugSymbols()
|
2010-10-19 19:21:58 +02:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-11-14 23:49:37 +01:00
|
|
|
|
2004-12-02 22:25:03 +01:00
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2009-03-09 21:49:37 +01:00
|
|
|
|
2009-09-03 08:43:15 +02:00
|
|
|
class StripDebugDeclare : public ModulePass {
|
2009-03-09 21:49:37 +01:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripDebugDeclare()
|
2010-10-19 19:21:58 +02:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2009-03-09 21:49:37 +01:00
|
|
|
|
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2010-07-01 21:49:20 +02:00
|
|
|
|
|
|
|
class StripDeadDebugInfo : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripDeadDebugInfo()
|
2010-10-19 19:21:58 +02:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-01 21:49:20 +02:00
|
|
|
|
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2004-12-02 22:25:03 +01:00
|
|
|
}
|
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char StripSymbols::ID = 0;
|
2010-07-22 00:09:45 +02:00
|
|
|
INITIALIZE_PASS(StripSymbols, "strip",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Strip all symbols from a module", false, false)
|
2008-05-13 02:00:25 +02:00
|
|
|
|
2004-12-02 22:25:03 +01:00
|
|
|
ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
|
|
|
|
return new StripSymbols(OnlyDebugInfo);
|
|
|
|
}
|
|
|
|
|
2008-11-18 22:34:39 +01:00
|
|
|
char StripNonDebugSymbols::ID = 0;
|
2010-07-22 00:09:45 +02:00
|
|
|
INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
|
|
|
|
"Strip all symbols, except dbg symbols, from a module",
|
2010-10-08 00:25:06 +02:00
|
|
|
false, false)
|
2008-11-18 22:34:39 +01:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripNonDebugSymbolsPass() {
|
|
|
|
return new StripNonDebugSymbols();
|
|
|
|
}
|
|
|
|
|
2009-03-09 21:49:37 +01:00
|
|
|
char StripDebugDeclare::ID = 0;
|
2010-07-22 00:09:45 +02:00
|
|
|
INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Strip all llvm.dbg.declare intrinsics", false, false)
|
2009-03-09 21:49:37 +01:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripDebugDeclarePass() {
|
|
|
|
return new StripDebugDeclare();
|
|
|
|
}
|
|
|
|
|
2010-07-01 21:49:20 +02:00
|
|
|
char StripDeadDebugInfo::ID = 0;
|
2010-07-22 00:09:45 +02:00
|
|
|
INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Strip debug info for unused symbols", false, false)
|
2010-07-01 21:49:20 +02:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripDeadDebugInfoPass() {
|
|
|
|
return new StripDeadDebugInfo();
|
|
|
|
}
|
|
|
|
|
2008-11-13 02:28:40 +01:00
|
|
|
/// OnlyUsedBy - Return true if V is only used by Usr.
|
|
|
|
static bool OnlyUsedBy(Value *V, Value *Usr) {
|
|
|
|
for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
|
|
|
|
User *U = *I;
|
|
|
|
if (U != Usr)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-03 17:22:08 +01:00
|
|
|
static void RemoveDeadConstant(Constant *C) {
|
|
|
|
assert(C->use_empty() && "Constant is not dead!");
|
2009-10-28 06:14:34 +01:00
|
|
|
SmallPtrSet<Constant*, 4> Operands;
|
2004-12-03 17:22:08 +01:00
|
|
|
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
|
|
|
|
if (isa<DerivedType>(C->getOperand(i)->getType()) &&
|
2008-11-13 02:28:40 +01:00
|
|
|
OnlyUsedBy(C->getOperand(i), C))
|
2009-10-28 06:14:34 +01:00
|
|
|
Operands.insert(cast<Constant>(C->getOperand(i)));
|
2004-12-03 17:22:08 +01:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
|
2009-01-15 21:18:42 +01:00
|
|
|
if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
|
2004-12-03 17:22:08 +01:00
|
|
|
GV->eraseFromParent();
|
|
|
|
}
|
|
|
|
else if (!isa<Function>(C))
|
2008-11-20 02:20:42 +01:00
|
|
|
if (isa<CompositeType>(C->getType()))
|
|
|
|
C->destroyConstant();
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2004-12-03 17:22:08 +01:00
|
|
|
// If the constant referenced anything, see if we can delete it as well.
|
2009-10-28 06:14:34 +01:00
|
|
|
for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
|
2008-11-13 02:28:40 +01:00
|
|
|
OE = Operands.end(); OI != OE; ++OI)
|
|
|
|
RemoveDeadConstant(*OI);
|
2004-12-03 17:22:08 +01:00
|
|
|
}
|
2004-12-02 22:25:03 +01:00
|
|
|
|
2007-02-07 07:22:45 +01:00
|
|
|
// Strip the symbol table of its names.
|
|
|
|
//
|
2008-11-18 22:34:39 +01:00
|
|
|
static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
|
2007-02-07 07:22:45 +01:00
|
|
|
for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
|
2007-02-12 06:18:08 +01:00
|
|
|
Value *V = VI->getValue();
|
2007-02-07 07:22:45 +01:00
|
|
|
++VI;
|
2009-01-15 21:18:42 +01:00
|
|
|
if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
|
2009-07-26 11:48:23 +02:00
|
|
|
if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
|
2008-11-18 22:34:39 +01:00
|
|
|
// Set name to "", removing from symbol table!
|
|
|
|
V->setName("");
|
2007-02-07 07:22:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the symbol table of its names.
|
2008-11-18 22:34:39 +01:00
|
|
|
static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
|
|
|
|
for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
|
2010-01-22 21:00:21 +01:00
|
|
|
if (PreserveDbgInfo && StringRef(TI->first).startswith("llvm.dbg"))
|
2008-11-18 22:34:39 +01:00
|
|
|
++TI;
|
|
|
|
else
|
|
|
|
ST.remove(TI++);
|
|
|
|
}
|
2007-02-07 07:22:45 +01:00
|
|
|
}
|
|
|
|
|
2008-11-18 22:13:41 +01:00
|
|
|
/// Find values that are marked as llvm.used.
|
2009-07-20 08:14:25 +02:00
|
|
|
static void findUsedValues(GlobalVariable *LLVMUsed,
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
|
|
|
|
if (LLVMUsed == 0) return;
|
|
|
|
UsedValues.insert(LLVMUsed);
|
|
|
|
|
|
|
|
ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
|
|
|
|
if (Inits == 0) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
|
|
|
|
if (GlobalValue *GV =
|
|
|
|
dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
|
|
|
|
UsedValues.insert(GV);
|
2008-11-18 22:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// StripSymbolNames - Strip symbol names.
|
2009-08-07 03:32:21 +02:00
|
|
|
static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
|
2008-11-18 22:13:41 +01:00
|
|
|
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
|
2009-07-20 08:14:25 +02:00
|
|
|
findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
|
|
|
|
findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
|
2008-11-18 22:13:41 +01:00
|
|
|
|
2008-11-14 23:49:37 +01:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2009-01-15 21:18:42 +01:00
|
|
|
if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
|
2009-07-26 11:48:23 +02:00
|
|
|
if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
|
2008-11-18 22:34:39 +01:00
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
2008-11-14 23:49:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
2009-01-15 21:18:42 +01:00
|
|
|
if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
|
2009-07-26 11:48:23 +02:00
|
|
|
if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
|
2008-11-18 22:34:39 +01:00
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
|
|
|
StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
|
2008-11-14 23:49:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all names from types.
|
2008-11-18 22:34:39 +01:00
|
|
|
StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
|
2008-01-16 04:33:05 +01:00
|
|
|
|
2008-11-14 23:49:37 +01:00
|
|
|
return true;
|
|
|
|
}
|
2004-12-02 22:25:03 +01:00
|
|
|
|
2008-11-14 23:49:37 +01:00
|
|
|
// StripDebugInfo - Strip debug info in the module if it exists.
|
|
|
|
// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
|
|
|
|
// llvm.dbg.region.end calls, and any globals they point to if now dead.
|
2009-08-07 03:32:21 +02:00
|
|
|
static bool StripDebugInfo(Module &M) {
|
2004-12-02 22:25:03 +01:00
|
|
|
|
2009-11-17 01:47:06 +01:00
|
|
|
bool Changed = false;
|
|
|
|
|
2009-08-29 01:24:31 +02:00
|
|
|
// Remove all of the calls to the debugger intrinsics, and remove them from
|
|
|
|
// the module.
|
2009-11-17 01:47:06 +01:00
|
|
|
if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
|
2006-03-23 19:11:33 +01:00
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
Declare->eraseFromParent();
|
2009-11-17 01:47:06 +01:00
|
|
|
Changed = true;
|
2006-03-23 19:11:33 +01:00
|
|
|
}
|
2004-12-03 17:22:08 +01:00
|
|
|
|
2010-02-10 22:19:56 +01:00
|
|
|
if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
|
|
|
|
while (!DbgVal->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(DbgVal->use_back());
|
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
DbgVal->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:29:00 +02:00
|
|
|
for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
|
|
|
|
NME = M.named_metadata_end(); NMI != NME;) {
|
|
|
|
NamedMDNode *NMD = NMI;
|
|
|
|
++NMI;
|
2010-07-01 20:27:46 +02:00
|
|
|
if (NMD->getName().startswith("llvm.dbg.")) {
|
2010-06-30 23:29:00 +02:00
|
|
|
NMD->eraseFromParent();
|
2010-07-01 20:27:46 +02:00
|
|
|
Changed = true;
|
|
|
|
}
|
2010-05-20 18:49:22 +02:00
|
|
|
}
|
2010-06-29 16:52:10 +02:00
|
|
|
|
|
|
|
for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
|
2009-11-17 01:47:06 +01:00
|
|
|
for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
|
|
|
|
++FI)
|
|
|
|
for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
|
2010-06-29 16:52:10 +02:00
|
|
|
++BI) {
|
2010-07-21 01:49:44 +02:00
|
|
|
if (!BI->getDebugLoc().isUnknown()) {
|
|
|
|
Changed = true;
|
|
|
|
BI->setDebugLoc(DebugLoc());
|
|
|
|
}
|
2010-06-29 16:52:10 +02:00
|
|
|
}
|
2008-11-14 23:49:37 +01:00
|
|
|
|
2010-06-29 16:52:10 +02:00
|
|
|
return Changed;
|
2004-12-02 22:25:03 +01:00
|
|
|
}
|
2008-11-18 22:34:39 +01:00
|
|
|
|
|
|
|
bool StripSymbols::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
Changed |= StripDebugInfo(M);
|
|
|
|
if (!OnlyDebugInfo)
|
|
|
|
Changed |= StripSymbolNames(M, false);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StripNonDebugSymbols::runOnModule(Module &M) {
|
|
|
|
return StripSymbolNames(M, true);
|
|
|
|
}
|
2009-03-09 21:49:37 +01:00
|
|
|
|
|
|
|
bool StripDebugDeclare::runOnModule(Module &M) {
|
|
|
|
|
|
|
|
Function *Declare = M.getFunction("llvm.dbg.declare");
|
|
|
|
std::vector<Constant*> DeadConstants;
|
|
|
|
|
2009-03-13 23:59:47 +01:00
|
|
|
if (Declare) {
|
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
2010-06-30 14:40:35 +02:00
|
|
|
Value *Arg1 = CI->getArgOperand(0);
|
|
|
|
Value *Arg2 = CI->getArgOperand(1);
|
2009-03-13 23:59:47 +01:00
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg1->use_empty()) {
|
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg1))
|
|
|
|
DeadConstants.push_back(C);
|
|
|
|
else
|
2009-05-02 22:22:10 +02:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Arg1);
|
2009-03-13 23:59:47 +01:00
|
|
|
}
|
|
|
|
if (Arg2->use_empty())
|
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg2))
|
|
|
|
DeadConstants.push_back(C);
|
2009-03-09 21:49:37 +01:00
|
|
|
}
|
2009-03-13 23:59:47 +01:00
|
|
|
Declare->eraseFromParent();
|
2009-03-09 21:49:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (!DeadConstants.empty()) {
|
|
|
|
Constant *C = DeadConstants.back();
|
|
|
|
DeadConstants.pop_back();
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
|
|
|
|
if (GV->hasLocalLinkage())
|
|
|
|
RemoveDeadConstant(GV);
|
2009-10-28 06:14:34 +01:00
|
|
|
} else
|
2009-03-09 21:49:37 +01:00
|
|
|
RemoveDeadConstant(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-01 21:49:20 +02:00
|
|
|
|
|
|
|
/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
|
|
|
|
/// printer to not emit usual symbol prefix before the symbol name is used then
|
|
|
|
/// return linkage name after skipping this special LLVM prefix.
|
|
|
|
static StringRef getRealLinkageName(StringRef LinkageName) {
|
|
|
|
char One = '\1';
|
|
|
|
if (LinkageName.startswith(StringRef(&One, 1)))
|
|
|
|
return LinkageName.substr(1);
|
|
|
|
return LinkageName;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StripDeadDebugInfo::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Debugging infomration is encoded in llvm IR using metadata. This is designed
|
|
|
|
// such a way that debug info for symbols preserved even if symbols are
|
|
|
|
// optimized away by the optimizer. This special pass removes debug info for
|
|
|
|
// such symbols.
|
|
|
|
|
|
|
|
// llvm.dbg.gv keeps track of debug info for global variables.
|
|
|
|
if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
|
|
|
|
SmallVector<MDNode *, 8> MDs;
|
|
|
|
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
|
|
|
|
if (DIGlobalVariable(NMD->getOperand(i)).Verify())
|
|
|
|
MDs.push_back(NMD->getOperand(i));
|
|
|
|
else
|
|
|
|
Changed = true;
|
|
|
|
NMD->eraseFromParent();
|
|
|
|
NMD = NULL;
|
|
|
|
|
|
|
|
for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
|
|
|
|
E = MDs.end(); I != E; ++I) {
|
2010-08-25 20:52:02 +02:00
|
|
|
GlobalVariable *GV = DIGlobalVariable(*I).getGlobal();
|
|
|
|
if (GV && M.getGlobalVariable(GV->getName(), true)) {
|
2010-07-01 21:49:20 +02:00
|
|
|
if (!NMD)
|
|
|
|
NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
|
|
|
|
NMD->addOperand(*I);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// llvm.dbg.sp keeps track of debug info for subprograms.
|
|
|
|
if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
|
|
|
|
SmallVector<MDNode *, 8> MDs;
|
|
|
|
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
|
|
|
|
if (DISubprogram(NMD->getOperand(i)).Verify())
|
|
|
|
MDs.push_back(NMD->getOperand(i));
|
|
|
|
else
|
|
|
|
Changed = true;
|
|
|
|
NMD->eraseFromParent();
|
|
|
|
NMD = NULL;
|
|
|
|
|
|
|
|
for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
|
|
|
|
E = MDs.end(); I != E; ++I) {
|
|
|
|
bool FnIsLive = false;
|
|
|
|
if (Function *F = DISubprogram(*I).getFunction())
|
|
|
|
if (M.getFunction(F->getName()))
|
|
|
|
FnIsLive = true;
|
|
|
|
if (FnIsLive) {
|
|
|
|
if (!NMD)
|
|
|
|
NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
|
|
|
|
NMD->addOperand(*I);
|
|
|
|
} else {
|
|
|
|
// Remove llvm.dbg.lv.fnname named mdnode which may have been used
|
|
|
|
// to hold debug info for dead function's local variables.
|
|
|
|
StringRef FName = DISubprogram(*I).getLinkageName();
|
|
|
|
if (FName.empty())
|
|
|
|
FName = DISubprogram(*I).getName();
|
|
|
|
if (NamedMDNode *LVNMD =
|
|
|
|
M.getNamedMetadata(Twine("llvm.dbg.lv.",
|
|
|
|
getRealLinkageName(FName))))
|
|
|
|
LVNMD->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|