2004-12-03 05:25:03 +08:00
|
|
|
//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-12-03 05:25:03 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-12-03 05:25:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-11-05 00:15:04 +08:00
|
|
|
// The StripSymbols transformation implements code stripping. Specifically, it
|
|
|
|
// can delete:
|
2013-08-22 06:53:29 +08:00
|
|
|
//
|
2007-11-05 00:15:04 +08:00
|
|
|
// * names for virtual registers
|
|
|
|
// * symbols for internal globals and functions
|
|
|
|
// * debug information
|
2004-12-03 05:25:03 +08:00
|
|
|
//
|
2007-11-05 00:15:04 +08: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-03 05:25:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-06-28 08:05:13 +08:00
|
|
|
#include "llvm/DebugInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-01-07 23:43:51 +08:00
|
|
|
#include "llvm/IR/TypeFinder.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/ValueSymbolTable.h"
|
2004-12-03 05:25:03 +08:00
|
|
|
#include "llvm/Pass.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2004-12-03 05:25:03 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-09-03 14:43:15 +08:00
|
|
|
class StripSymbols : public ModulePass {
|
2004-12-03 05:25:03 +08:00
|
|
|
bool OnlyDebugInfo;
|
|
|
|
public:
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2013-08-22 06:53:29 +08:00
|
|
|
explicit StripSymbols(bool ODI = false)
|
2010-10-20 01:21:58 +08:00
|
|
|
: ModulePass(ID), OnlyDebugInfo(ODI) {
|
|
|
|
initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2004-12-03 05:25:03 +08:00
|
|
|
|
2008-11-19 05:34:39 +08:00
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-11-15 06:49:37 +08:00
|
|
|
|
2009-09-03 14:43:15 +08:00
|
|
|
class StripNonDebugSymbols : public ModulePass {
|
2008-11-19 05:34:39 +08:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripNonDebugSymbols()
|
2010-10-20 01:21:58 +08:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-11-15 06:49:37 +08:00
|
|
|
|
2004-12-03 05:25:03 +08:00
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2009-03-10 04:49:37 +08:00
|
|
|
|
2009-09-03 14:43:15 +08:00
|
|
|
class StripDebugDeclare : public ModulePass {
|
2009-03-10 04:49:37 +08:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripDebugDeclare()
|
2010-10-20 01:21:58 +08:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2009-03-10 04:49:37 +08:00
|
|
|
|
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2010-07-02 03:49:20 +08:00
|
|
|
|
|
|
|
class StripDeadDebugInfo : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripDeadDebugInfo()
|
2010-10-20 01:21:58 +08:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-02 03:49:20 +08:00
|
|
|
|
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2004-12-03 05:25:03 +08:00
|
|
|
}
|
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char StripSymbols::ID = 0;
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(StripSymbols, "strip",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Strip all symbols from a module", false, false)
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2004-12-03 05:25:03 +08:00
|
|
|
ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
|
|
|
|
return new StripSymbols(OnlyDebugInfo);
|
|
|
|
}
|
|
|
|
|
2008-11-19 05:34:39 +08:00
|
|
|
char StripNonDebugSymbols::ID = 0;
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
|
|
|
|
"Strip all symbols, except dbg symbols, from a module",
|
2010-10-08 06:25:06 +08:00
|
|
|
false, false)
|
2008-11-19 05:34:39 +08:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripNonDebugSymbolsPass() {
|
|
|
|
return new StripNonDebugSymbols();
|
|
|
|
}
|
|
|
|
|
2009-03-10 04:49:37 +08:00
|
|
|
char StripDebugDeclare::ID = 0;
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Strip all llvm.dbg.declare intrinsics", false, false)
|
2009-03-10 04:49:37 +08:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripDebugDeclarePass() {
|
|
|
|
return new StripDebugDeclare();
|
|
|
|
}
|
|
|
|
|
2010-07-02 03:49:20 +08:00
|
|
|
char StripDeadDebugInfo::ID = 0;
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Strip debug info for unused symbols", false, false)
|
2010-07-02 03:49:20 +08:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripDeadDebugInfoPass() {
|
|
|
|
return new StripDeadDebugInfo();
|
|
|
|
}
|
|
|
|
|
2008-11-13 09:28:40 +08: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-04 00:22:08 +08:00
|
|
|
static void RemoveDeadConstant(Constant *C) {
|
|
|
|
assert(C->use_empty() && "Constant is not dead!");
|
2009-10-28 13:14:34 +08:00
|
|
|
SmallPtrSet<Constant*, 4> Operands;
|
2004-12-04 00:22:08 +08:00
|
|
|
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
|
2013-08-22 06:53:29 +08:00
|
|
|
if (OnlyUsedBy(C->getOperand(i), C))
|
2009-10-28 13:14:34 +08:00
|
|
|
Operands.insert(cast<Constant>(C->getOperand(i)));
|
2004-12-04 00:22:08 +08:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
|
2009-01-16 04:18:42 +08:00
|
|
|
if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
|
2004-12-04 00:22:08 +08:00
|
|
|
GV->eraseFromParent();
|
|
|
|
}
|
|
|
|
else if (!isa<Function>(C))
|
2008-11-20 09:20:42 +08:00
|
|
|
if (isa<CompositeType>(C->getType()))
|
|
|
|
C->destroyConstant();
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-12-04 00:22:08 +08:00
|
|
|
// If the constant referenced anything, see if we can delete it as well.
|
2009-10-28 13:14:34 +08:00
|
|
|
for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
|
2008-11-13 09:28:40 +08:00
|
|
|
OE = Operands.end(); OI != OE; ++OI)
|
|
|
|
RemoveDeadConstant(*OI);
|
2004-12-04 00:22:08 +08:00
|
|
|
}
|
2004-12-03 05:25:03 +08:00
|
|
|
|
2007-02-07 14:22:45 +08:00
|
|
|
// Strip the symbol table of its names.
|
|
|
|
//
|
2008-11-19 05:34:39 +08:00
|
|
|
static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
|
2007-02-07 14:22:45 +08:00
|
|
|
for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
|
2007-02-12 13:18:08 +08:00
|
|
|
Value *V = VI->getValue();
|
2007-02-07 14:22:45 +08:00
|
|
|
++VI;
|
2009-01-16 04:18:42 +08:00
|
|
|
if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
|
2009-07-26 17:48:23 +08:00
|
|
|
if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
|
2008-11-19 05:34:39 +08:00
|
|
|
// Set name to "", removing from symbol table!
|
|
|
|
V->setName("");
|
2007-02-07 14:22:45 +08: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.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
// Strip any named types of their names.
|
|
|
|
static void StripTypeNames(Module &M, bool PreserveDbgInfo) {
|
2012-08-03 08:30:35 +08:00
|
|
|
TypeFinder StructTypes;
|
|
|
|
StructTypes.run(M, false);
|
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.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
|
|
|
|
StructType *STy = StructTypes[i];
|
2011-08-13 02:06:37 +08:00
|
|
|
if (STy->isLiteral() || STy->getName().empty()) continue;
|
2013-08-22 06:53:29 +08: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.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
STy->setName("");
|
2008-11-19 05:34:39 +08:00
|
|
|
}
|
2007-02-07 14:22:45 +08:00
|
|
|
}
|
|
|
|
|
2008-11-19 05:13:41 +08:00
|
|
|
/// Find values that are marked as llvm.used.
|
2009-07-20 14:14:25 +08:00
|
|
|
static void findUsedValues(GlobalVariable *LLVMUsed,
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
|
|
|
|
if (LLVMUsed == 0) return;
|
|
|
|
UsedValues.insert(LLVMUsed);
|
2013-04-22 22:58:02 +08:00
|
|
|
|
|
|
|
ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
|
|
|
|
|
2009-07-20 14:14:25 +08:00
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
|
2013-08-22 06:53:29 +08:00
|
|
|
if (GlobalValue *GV =
|
2009-07-20 14:14:25 +08:00
|
|
|
dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
|
|
|
|
UsedValues.insert(GV);
|
2008-11-19 05:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// StripSymbolNames - Strip symbol names.
|
2009-08-07 09:32:21 +08:00
|
|
|
static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
|
2008-11-19 05:13:41 +08:00
|
|
|
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
|
2009-07-20 14:14:25 +08:00
|
|
|
findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
|
|
|
|
findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
|
2008-11-19 05:13:41 +08:00
|
|
|
|
2008-11-15 06:49:37 +08:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2009-01-16 04:18:42 +08:00
|
|
|
if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
|
2009-07-26 17:48:23 +08:00
|
|
|
if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
|
2008-11-19 05:34:39 +08:00
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
2008-11-15 06:49:37 +08:00
|
|
|
}
|
2013-08-22 06:53:29 +08:00
|
|
|
|
2008-11-15 06:49:37 +08:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
2009-01-16 04:18:42 +08:00
|
|
|
if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
|
2009-07-26 17:48:23 +08:00
|
|
|
if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
|
2008-11-19 05:34:39 +08:00
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
|
|
|
StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
|
2008-11-15 06:49:37 +08:00
|
|
|
}
|
2013-08-22 06:53:29 +08:00
|
|
|
|
2008-11-15 06:49:37 +08: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.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
StripTypeNames(M, PreserveDbgInfo);
|
2008-01-16 11:33:05 +08:00
|
|
|
|
2008-11-15 06:49:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
2004-12-03 05:25:03 +08:00
|
|
|
|
2013-08-22 06:53:29 +08:00
|
|
|
// StripDebugInfo - Strip debug info in the module if it exists.
|
|
|
|
// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
|
2008-11-15 06:49:37 +08:00
|
|
|
// llvm.dbg.region.end calls, and any globals they point to if now dead.
|
2009-08-07 09:32:21 +08:00
|
|
|
static bool StripDebugInfo(Module &M) {
|
2004-12-03 05:25:03 +08:00
|
|
|
|
2009-11-17 08:47:06 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
2009-08-29 07:24:31 +08:00
|
|
|
// Remove all of the calls to the debugger intrinsics, and remove them from
|
|
|
|
// the module.
|
2009-11-17 08:47:06 +08:00
|
|
|
if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
|
2006-03-24 02:11:33 +08:00
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
Declare->eraseFromParent();
|
2009-11-17 08:47:06 +08:00
|
|
|
Changed = true;
|
2006-03-24 02:11:33 +08:00
|
|
|
}
|
2004-12-04 00:22:08 +08:00
|
|
|
|
2010-02-11 05:19:56 +08: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-07-01 05:29:00 +08:00
|
|
|
for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
|
|
|
|
NME = M.named_metadata_end(); NMI != NME;) {
|
|
|
|
NamedMDNode *NMD = NMI;
|
|
|
|
++NMI;
|
2010-07-02 02:27:46 +08:00
|
|
|
if (NMD->getName().startswith("llvm.dbg.")) {
|
2010-07-01 05:29:00 +08:00
|
|
|
NMD->eraseFromParent();
|
2010-07-02 02:27:46 +08:00
|
|
|
Changed = true;
|
|
|
|
}
|
2010-05-21 00:49:22 +08:00
|
|
|
}
|
2010-06-29 22:52:10 +08:00
|
|
|
|
|
|
|
for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
|
2009-11-17 08:47:06 +08: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 22:52:10 +08:00
|
|
|
++BI) {
|
2010-07-21 07:49:44 +08:00
|
|
|
if (!BI->getDebugLoc().isUnknown()) {
|
|
|
|
Changed = true;
|
|
|
|
BI->setDebugLoc(DebugLoc());
|
|
|
|
}
|
2010-06-29 22:52:10 +08:00
|
|
|
}
|
2008-11-15 06:49:37 +08:00
|
|
|
|
2010-06-29 22:52:10 +08:00
|
|
|
return Changed;
|
2004-12-03 05:25:03 +08:00
|
|
|
}
|
2008-11-19 05:34:39 +08: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-10 04:49:37 +08:00
|
|
|
|
|
|
|
bool StripDebugDeclare::runOnModule(Module &M) {
|
|
|
|
|
|
|
|
Function *Declare = M.getFunction("llvm.dbg.declare");
|
|
|
|
std::vector<Constant*> DeadConstants;
|
|
|
|
|
2009-03-14 06:59:47 +08:00
|
|
|
if (Declare) {
|
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
2010-06-30 20:40:35 +08:00
|
|
|
Value *Arg1 = CI->getArgOperand(0);
|
|
|
|
Value *Arg2 = CI->getArgOperand(1);
|
2009-03-14 06:59:47 +08:00
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg1->use_empty()) {
|
2013-08-22 06:53:29 +08:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg1))
|
2009-03-14 06:59:47 +08:00
|
|
|
DeadConstants.push_back(C);
|
2013-08-22 06:53:29 +08:00
|
|
|
else
|
2009-05-03 04:22:10 +08:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Arg1);
|
2009-03-14 06:59:47 +08:00
|
|
|
}
|
|
|
|
if (Arg2->use_empty())
|
2013-08-22 06:53:29 +08:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg2))
|
2009-03-14 06:59:47 +08:00
|
|
|
DeadConstants.push_back(C);
|
2009-03-10 04:49:37 +08:00
|
|
|
}
|
2009-03-14 06:59:47 +08:00
|
|
|
Declare->eraseFromParent();
|
2009-03-10 04:49:37 +08: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 13:14:34 +08:00
|
|
|
} else
|
2009-03-10 04:49:37 +08:00
|
|
|
RemoveDeadConstant(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-02 03:49:20 +08:00
|
|
|
|
|
|
|
bool StripDeadDebugInfo::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
2013-08-22 06:53:54 +08:00
|
|
|
// Debugging information is encoded in llvm IR using metadata. This is designed
|
2010-07-02 03:49:20 +08:00
|
|
|
// such a way that debug info for symbols preserved even if symbols are
|
2013-08-22 06:53:29 +08:00
|
|
|
// optimized away by the optimizer. This special pass removes debug info for
|
2010-07-02 03:49:20 +08:00
|
|
|
// 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)
|
2013-06-28 13:43:10 +08:00
|
|
|
if (NMD->getOperand(i)) {
|
|
|
|
assert(DIGlobalVariable(NMD->getOperand(i)).isGlobalVariable() &&
|
|
|
|
"A MDNode in llvm.dbg.gv should be a DIGlobalVariable.");
|
2010-07-02 03:49:20 +08:00
|
|
|
MDs.push_back(NMD->getOperand(i));
|
2013-06-28 13:43:10 +08:00
|
|
|
}
|
2010-07-02 03:49:20 +08:00
|
|
|
else
|
|
|
|
Changed = true;
|
|
|
|
NMD->eraseFromParent();
|
|
|
|
NMD = NULL;
|
|
|
|
|
2013-07-04 09:31:24 +08:00
|
|
|
for (SmallVectorImpl<MDNode *>::iterator I = MDs.begin(),
|
2010-07-02 03:49:20 +08:00
|
|
|
E = MDs.end(); I != E; ++I) {
|
2010-08-26 02:52:02 +08:00
|
|
|
GlobalVariable *GV = DIGlobalVariable(*I).getGlobal();
|
|
|
|
if (GV && M.getGlobalVariable(GV->getName(), true)) {
|
2010-07-02 03:49:20 +08: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)
|
2013-06-28 13:43:10 +08:00
|
|
|
if (NMD->getOperand(i)) {
|
|
|
|
assert(DISubprogram(NMD->getOperand(i)).isSubprogram() &&
|
|
|
|
"A MDNode in llvm.dbg.sp should be a DISubprogram.");
|
2010-07-02 03:49:20 +08:00
|
|
|
MDs.push_back(NMD->getOperand(i));
|
2013-06-28 13:43:10 +08:00
|
|
|
}
|
2010-07-02 03:49:20 +08:00
|
|
|
else
|
|
|
|
Changed = true;
|
|
|
|
NMD->eraseFromParent();
|
|
|
|
NMD = NULL;
|
|
|
|
|
2013-07-04 09:31:24 +08:00
|
|
|
for (SmallVectorImpl<MDNode *>::iterator I = MDs.begin(),
|
2010-07-02 03:49:20 +08:00
|
|
|
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();
|
2013-06-02 01:51:14 +08:00
|
|
|
if (NamedMDNode *LVNMD = M.getNamedMetadata(
|
|
|
|
"llvm.dbg.lv." + Function::getRealLinkageName(FName)))
|
2010-07-02 03:49:20 +08:00
|
|
|
LVNMD->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|