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"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2014-03-06 08:46:21 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#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
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnModule(Module &M) override;
|
2008-11-19 05:34:39 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2008-11-19 05:34:39 +08:00
|
|
|
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
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnModule(Module &M) override;
|
2004-12-03 05:25:03 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2004-12-03 05:25:03 +08:00
|
|
|
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
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnModule(Module &M) override;
|
2009-03-10 04:49:37 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2009-03-10 04:49:37 +08:00
|
|
|
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
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnModule(Module &M) override;
|
2010-07-02 03:49:20 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-07-02 03:49:20 +08:00
|
|
|
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) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : V->users())
|
2008-11-13 09:28:40 +08:00
|
|
|
if (U != Usr)
|
|
|
|
return false;
|
2014-03-09 11:16:01 +08:00
|
|
|
|
2008-11-13 09:28:40 +08:00
|
|
|
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)) {
|
2013-12-05 13:44:44 +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
|
|
|
|
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()) {
|
2014-03-09 11:16:01 +08:00
|
|
|
CallInst *CI = cast<CallInst>(Declare->user_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
|
|
|
|
2013-08-23 08:23:24 +08:00
|
|
|
/// Remove any debug info for global variables/functions in the given module for
|
|
|
|
/// which said global variable/function no longer exists (i.e. is null).
|
|
|
|
///
|
|
|
|
/// Debugging information 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.
|
2010-07-02 03:49:20 +08:00
|
|
|
bool StripDeadDebugInfo::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
2013-08-23 08:23:24 +08:00
|
|
|
LLVMContext &C = M.getContext();
|
|
|
|
|
|
|
|
// Find all debug info in F. This is actually overkill in terms of what we
|
2013-08-27 12:43:03 +08:00
|
|
|
// want to do, but we want to try and be as resilient as possible in the face
|
2013-08-23 08:23:24 +08:00
|
|
|
// of potential debug info changes by using the formal interfaces given to us
|
|
|
|
// as much as possible.
|
|
|
|
DebugInfoFinder F;
|
|
|
|
F.processModule(M);
|
|
|
|
|
|
|
|
// For each compile unit, find the live set of global variables/functions and
|
|
|
|
// replace the current list of potentially dead global variables/functions
|
|
|
|
// with the live list.
|
|
|
|
SmallVector<Value *, 64> LiveGlobalVariables;
|
|
|
|
SmallVector<Value *, 64> LiveSubprograms;
|
|
|
|
DenseSet<const MDNode *> VisitedSet;
|
|
|
|
|
2014-03-18 17:41:07 +08:00
|
|
|
for (DICompileUnit DIC : F.compile_units()) {
|
2013-08-23 08:23:24 +08:00
|
|
|
assert(DIC.Verify() && "DIC must verify as a DICompileUnit.");
|
|
|
|
|
|
|
|
// Create our live subprogram list.
|
|
|
|
DIArray SPs = DIC.getSubprograms();
|
|
|
|
bool SubprogramChange = false;
|
|
|
|
for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
|
|
|
|
DISubprogram DISP(SPs.getElement(i));
|
|
|
|
assert(DISP.Verify() && "DISP must verify as a DISubprogram.");
|
|
|
|
|
|
|
|
// Make sure we visit each subprogram only once.
|
|
|
|
if (!VisitedSet.insert(DISP).second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the function referenced by DISP is not null, the function is live.
|
|
|
|
if (DISP.getFunction())
|
|
|
|
LiveSubprograms.push_back(DISP);
|
2010-07-02 03:49:20 +08:00
|
|
|
else
|
2013-08-23 08:23:24 +08:00
|
|
|
SubprogramChange = true;
|
2010-07-02 03:49:20 +08:00
|
|
|
}
|
|
|
|
|
2013-08-23 08:23:24 +08:00
|
|
|
// Create our live global variable list.
|
|
|
|
DIArray GVs = DIC.getGlobalVariables();
|
|
|
|
bool GlobalVariableChange = false;
|
|
|
|
for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
|
|
|
|
DIGlobalVariable DIG(GVs.getElement(i));
|
|
|
|
assert(DIG.Verify() && "DIG must verify as DIGlobalVariable.");
|
|
|
|
|
|
|
|
// Make sure we only visit each global variable only once.
|
|
|
|
if (!VisitedSet.insert(DIG).second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the global variable referenced by DIG is not null, the global
|
|
|
|
// variable is live.
|
|
|
|
if (DIG.getGlobal())
|
|
|
|
LiveGlobalVariables.push_back(DIG);
|
2010-07-02 03:49:20 +08:00
|
|
|
else
|
2013-08-23 08:23:24 +08:00
|
|
|
GlobalVariableChange = true;
|
2010-07-02 03:49:20 +08:00
|
|
|
}
|
2013-08-23 08:23:24 +08:00
|
|
|
|
|
|
|
// If we found dead subprograms or global variables, replace the current
|
|
|
|
// subprogram list/global variable list with our new live subprogram/global
|
|
|
|
// variable list.
|
|
|
|
if (SubprogramChange) {
|
|
|
|
// Make sure that 9 is still the index of the subprograms. This is to make
|
|
|
|
// sure that an assert is hit if the location of the subprogram array
|
|
|
|
// changes. This is just to make sure that this is updated if such an
|
|
|
|
// event occurs.
|
|
|
|
assert(DIC->getNumOperands() >= 10 &&
|
|
|
|
SPs == DIC->getOperand(9) &&
|
|
|
|
"DICompileUnits is expected to store Subprograms in operand "
|
|
|
|
"9.");
|
|
|
|
DIC->replaceOperandWith(9, MDNode::get(C, LiveSubprograms));
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GlobalVariableChange) {
|
|
|
|
// Make sure that 10 is still the index of global variables. This is to
|
|
|
|
// make sure that an assert is hit if the location of the subprogram array
|
|
|
|
// changes. This is just to make sure that this index is updated if such
|
|
|
|
// an event occurs.
|
|
|
|
assert(DIC->getNumOperands() >= 11 &&
|
|
|
|
GVs == DIC->getOperand(10) &&
|
|
|
|
"DICompileUnits is expected to store Global Variables in operand "
|
|
|
|
"10.");
|
|
|
|
DIC->replaceOperandWith(10, MDNode::get(C, LiveGlobalVariables));
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset lists for the next iteration.
|
|
|
|
LiveSubprograms.clear();
|
|
|
|
LiveGlobalVariables.clear();
|
2010-07-02 03:49:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|