2004-12-02 21:25:03 +00:00
|
|
|
//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-12-02 21:25:03 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-12-02 21:25:03 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-11-04 16:15:04 +00: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 21:25:03 +00:00
|
|
|
//
|
2007-11-04 16:15:04 +00: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 21:25:03 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2004-12-03 16:22:08 +00:00
|
|
|
#include "llvm/Constants.h"
|
2012-06-28 00:05:13 +00:00
|
|
|
#include "llvm/DebugInfo.h"
|
2004-12-03 16:22:08 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2004-12-02 21:25:03 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2007-02-05 20:47:22 +00:00
|
|
|
#include "llvm/ValueSymbolTable.h"
|
2009-03-03 21:31:02 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2008-01-16 03:33:05 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-12-02 21:25:03 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-09-03 06:43:15 +00:00
|
|
|
class StripSymbols : public ModulePass {
|
2004-12-02 21:25:03 +00:00
|
|
|
bool OnlyDebugInfo;
|
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-08-01 15:32:29 +00:00
|
|
|
explicit StripSymbols(bool ODI = false)
|
2010-10-19 17:21:58 +00:00
|
|
|
: ModulePass(ID), OnlyDebugInfo(ODI) {
|
|
|
|
initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2008-11-18 21:34:39 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-11-14 22:49:37 +00:00
|
|
|
|
2009-09-03 06:43:15 +00:00
|
|
|
class StripNonDebugSymbols : public ModulePass {
|
2008-11-18 21:34:39 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripNonDebugSymbols()
|
2010-10-19 17:21:58 +00:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-11-14 22:49:37 +00:00
|
|
|
|
2004-12-02 21:25:03 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2009-03-09 20:49:37 +00:00
|
|
|
|
2009-09-03 06:43:15 +00:00
|
|
|
class StripDebugDeclare : public ModulePass {
|
2009-03-09 20:49:37 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripDebugDeclare()
|
2010-10-19 17:21:58 +00:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2009-03-09 20:49:37 +00:00
|
|
|
|
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2010-07-01 19:49:20 +00:00
|
|
|
|
|
|
|
class StripDeadDebugInfo : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripDeadDebugInfo()
|
2010-10-19 17:21:58 +00:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-01 19:49:20 +00:00
|
|
|
|
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2004-12-02 21:25:03 +00:00
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char StripSymbols::ID = 0;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(StripSymbols, "strip",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Strip all symbols from a module", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2004-12-02 21:25:03 +00:00
|
|
|
ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
|
|
|
|
return new StripSymbols(OnlyDebugInfo);
|
|
|
|
}
|
|
|
|
|
2008-11-18 21:34:39 +00:00
|
|
|
char StripNonDebugSymbols::ID = 0;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
|
|
|
|
"Strip all symbols, except dbg symbols, from a module",
|
2010-10-07 22:25:06 +00:00
|
|
|
false, false)
|
2008-11-18 21:34:39 +00:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripNonDebugSymbolsPass() {
|
|
|
|
return new StripNonDebugSymbols();
|
|
|
|
}
|
|
|
|
|
2009-03-09 20:49:37 +00:00
|
|
|
char StripDebugDeclare::ID = 0;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Strip all llvm.dbg.declare intrinsics", false, false)
|
2009-03-09 20:49:37 +00:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripDebugDeclarePass() {
|
|
|
|
return new StripDebugDeclare();
|
|
|
|
}
|
|
|
|
|
2010-07-01 19:49:20 +00:00
|
|
|
char StripDeadDebugInfo::ID = 0;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Strip debug info for unused symbols", false, false)
|
2010-07-01 19:49:20 +00:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripDeadDebugInfoPass() {
|
|
|
|
return new StripDeadDebugInfo();
|
|
|
|
}
|
|
|
|
|
2008-11-13 01:28:40 +00: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 16:22:08 +00:00
|
|
|
static void RemoveDeadConstant(Constant *C) {
|
|
|
|
assert(C->use_empty() && "Constant is not dead!");
|
2009-10-28 05:14:34 +00:00
|
|
|
SmallPtrSet<Constant*, 4> Operands;
|
2004-12-03 16:22:08 +00:00
|
|
|
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
|
2011-07-09 17:59:15 +00:00
|
|
|
if (OnlyUsedBy(C->getOperand(i), C))
|
2009-10-28 05:14:34 +00:00
|
|
|
Operands.insert(cast<Constant>(C->getOperand(i)));
|
2004-12-03 16:22:08 +00:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
|
2009-01-15 20:18:42 +00:00
|
|
|
if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
|
2004-12-03 16:22:08 +00:00
|
|
|
GV->eraseFromParent();
|
|
|
|
}
|
|
|
|
else if (!isa<Function>(C))
|
2008-11-20 01:20:42 +00:00
|
|
|
if (isa<CompositeType>(C->getType()))
|
|
|
|
C->destroyConstant();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-12-03 16:22:08 +00:00
|
|
|
// If the constant referenced anything, see if we can delete it as well.
|
2009-10-28 05:14:34 +00:00
|
|
|
for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
|
2008-11-13 01:28:40 +00:00
|
|
|
OE = Operands.end(); OI != OE; ++OI)
|
|
|
|
RemoveDeadConstant(*OI);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2007-02-07 06:22:45 +00:00
|
|
|
// Strip the symbol table of its names.
|
|
|
|
//
|
2008-11-18 21:34:39 +00:00
|
|
|
static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
|
2007-02-07 06:22:45 +00:00
|
|
|
for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
|
2007-02-12 05:18:08 +00:00
|
|
|
Value *V = VI->getValue();
|
2007-02-07 06:22:45 +00:00
|
|
|
++VI;
|
2009-01-15 20:18:42 +00:00
|
|
|
if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
|
2009-07-26 09:48:23 +00:00
|
|
|
if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
|
2008-11-18 21:34:39 +00:00
|
|
|
// Set name to "", removing from symbol table!
|
|
|
|
V->setName("");
|
2007-02-07 06:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
// Strip any named types of their names.
|
|
|
|
static void StripTypeNames(Module &M, bool PreserveDbgInfo) {
|
|
|
|
std::vector<StructType*> StructTypes;
|
|
|
|
M.findUsedStructTypes(StructTypes);
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
|
|
|
|
StructType *STy = StructTypes[i];
|
2011-08-12 18:06:37 +00:00
|
|
|
if (STy->isLiteral() || STy->getName().empty()) continue;
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
|
|
|
|
if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
STy->setName("");
|
2008-11-18 21:34:39 +00:00
|
|
|
}
|
2007-02-07 06:22:45 +00:00
|
|
|
}
|
|
|
|
|
2008-11-18 21:13:41 +00:00
|
|
|
/// Find values that are marked as llvm.used.
|
2009-07-20 06:14:25 +00: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 21:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// StripSymbolNames - Strip symbol names.
|
2009-08-07 01:32:21 +00:00
|
|
|
static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
|
2008-11-18 21:13:41 +00:00
|
|
|
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
|
2009-07-20 06:14:25 +00:00
|
|
|
findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
|
|
|
|
findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
|
2008-11-18 21:13:41 +00:00
|
|
|
|
2008-11-14 22:49:37 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2009-01-15 20:18:42 +00:00
|
|
|
if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
|
2009-07-26 09:48:23 +00:00
|
|
|
if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
|
2008-11-18 21:34:39 +00:00
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
2008-11-14 22:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
2009-01-15 20:18:42 +00:00
|
|
|
if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
|
2009-07-26 09:48:23 +00:00
|
|
|
if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
|
2008-11-18 21:34:39 +00:00
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
|
|
|
StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
|
2008-11-14 22:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all names from types.
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
StripTypeNames(M, PreserveDbgInfo);
|
2008-01-16 03:33:05 +00:00
|
|
|
|
2008-11-14 22:49:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2008-11-14 22:49:37 +00: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 01:32:21 +00:00
|
|
|
static bool StripDebugInfo(Module &M) {
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2009-11-17 00:47:06 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
2009-08-28 23:24:31 +00:00
|
|
|
// Remove all of the calls to the debugger intrinsics, and remove them from
|
|
|
|
// the module.
|
2009-11-17 00:47:06 +00:00
|
|
|
if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
|
2006-03-23 18:11:33 +00:00
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
Declare->eraseFromParent();
|
2009-11-17 00:47:06 +00:00
|
|
|
Changed = true;
|
2006-03-23 18:11:33 +00:00
|
|
|
}
|
2004-12-03 16:22:08 +00:00
|
|
|
|
2010-02-10 21:19:56 +00: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 21:29:00 +00: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 18:27:46 +00:00
|
|
|
if (NMD->getName().startswith("llvm.dbg.")) {
|
2010-06-30 21:29:00 +00:00
|
|
|
NMD->eraseFromParent();
|
2010-07-01 18:27:46 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
2010-05-20 16:49:22 +00:00
|
|
|
}
|
2010-06-29 14:52:10 +00:00
|
|
|
|
|
|
|
for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
|
2009-11-17 00:47:06 +00: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 14:52:10 +00:00
|
|
|
++BI) {
|
2010-07-20 23:49:44 +00:00
|
|
|
if (!BI->getDebugLoc().isUnknown()) {
|
|
|
|
Changed = true;
|
|
|
|
BI->setDebugLoc(DebugLoc());
|
|
|
|
}
|
2010-06-29 14:52:10 +00:00
|
|
|
}
|
2008-11-14 22:49:37 +00:00
|
|
|
|
2010-06-29 14:52:10 +00:00
|
|
|
return Changed;
|
2004-12-02 21:25:03 +00:00
|
|
|
}
|
2008-11-18 21:34:39 +00: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 20:49:37 +00:00
|
|
|
|
|
|
|
bool StripDebugDeclare::runOnModule(Module &M) {
|
|
|
|
|
|
|
|
Function *Declare = M.getFunction("llvm.dbg.declare");
|
|
|
|
std::vector<Constant*> DeadConstants;
|
|
|
|
|
2009-03-13 22:59:47 +00:00
|
|
|
if (Declare) {
|
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
2010-06-30 12:40:35 +00:00
|
|
|
Value *Arg1 = CI->getArgOperand(0);
|
|
|
|
Value *Arg2 = CI->getArgOperand(1);
|
2009-03-13 22:59:47 +00: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 20:22:10 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Arg1);
|
2009-03-13 22:59:47 +00:00
|
|
|
}
|
|
|
|
if (Arg2->use_empty())
|
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg2))
|
|
|
|
DeadConstants.push_back(C);
|
2009-03-09 20:49:37 +00:00
|
|
|
}
|
2009-03-13 22:59:47 +00:00
|
|
|
Declare->eraseFromParent();
|
2009-03-09 20:49:37 +00: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 05:14:34 +00:00
|
|
|
} else
|
2009-03-09 20:49:37 +00:00
|
|
|
RemoveDeadConstant(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-01 19:49:20 +00: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 18:52:02 +00:00
|
|
|
GlobalVariable *GV = DIGlobalVariable(*I).getGlobal();
|
|
|
|
if (GV && M.getGlobalVariable(GV->getName(), true)) {
|
2010-07-01 19:49:20 +00: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;
|
|
|
|
}
|