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:
|
2013-08-21 22:53:29 +00:00
|
|
|
//
|
2007-11-04 16:15:04 +00:00
|
|
|
// * 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"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
2014-03-06 00:46:21 +00:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-01-07 15:43:51 +00:00
|
|
|
#include "llvm/IR/TypeFinder.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/ValueSymbolTable.h"
|
2004-12-02 21:25:03 +00:00
|
|
|
#include "llvm/Pass.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.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
|
2013-08-21 22:53: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
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnModule(Module &M) override;
|
2008-11-18 21:34:39 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2008-11-18 21:34:39 +00:00
|
|
|
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
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnModule(Module &M) override;
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2004-12-02 21:25:03 +00:00
|
|
|
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
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnModule(Module &M) override;
|
2009-03-09 20:49:37 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2009-03-09 20:49:37 +00:00
|
|
|
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
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnModule(Module &M) override;
|
2010-07-01 19:49:20 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-07-01 19:49:20 +00:00
|
|
|
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) {
|
2014-03-09 03:16:01 +00:00
|
|
|
for (User *U : V->users())
|
2008-11-13 01:28:40 +00:00
|
|
|
if (U != Usr)
|
|
|
|
return false;
|
2014-03-09 03:16:01 +00:00
|
|
|
|
2008-11-13 01:28:40 +00:00
|
|
|
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)
|
2013-08-21 22:53:29 +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)) {
|
2013-12-05 05:44:44 +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.
|
2014-08-24 23:23:06 +00:00
|
|
|
for (Constant *O : Operands)
|
|
|
|
RemoveDeadConstant(O);
|
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) {
|
2012-08-03 00:30:35 +00: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.
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
|
|
|
|
|
|
|
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;
|
2013-08-21 22:53:29 +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
|
|
|
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,
|
2014-08-21 05:55:13 +00:00
|
|
|
SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!LLVMUsed) return;
|
2009-07-20 06:14:25 +00:00
|
|
|
UsedValues.insert(LLVMUsed);
|
2013-04-22 14:58:02 +00:00
|
|
|
|
|
|
|
ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
|
|
|
|
|
2009-07-20 06:14:25 +00:00
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
|
2013-08-21 22:53:29 +00:00
|
|
|
if (GlobalValue *GV =
|
2009-07-20 06:14:25 +00:00
|
|
|
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
|
|
|
}
|
2013-08-21 22:53:29 +00:00
|
|
|
|
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
|
|
|
}
|
2013-08-21 22:53:29 +00:00
|
|
|
|
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-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()) {
|
2014-03-09 03:16:01 +00:00
|
|
|
CallInst *CI = cast<CallInst>(Declare->user_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()) {
|
2013-08-21 22:53:29 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg1))
|
2009-03-13 22:59:47 +00:00
|
|
|
DeadConstants.push_back(C);
|
2013-08-21 22:53:29 +00:00
|
|
|
else
|
2009-05-02 20:22:10 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Arg1);
|
2009-03-13 22:59:47 +00:00
|
|
|
}
|
|
|
|
if (Arg2->use_empty())
|
2013-08-21 22:53:29 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg2))
|
2009-03-13 22:59:47 +00:00
|
|
|
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
|
|
|
|
2013-08-23 00:23:24 +00: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-01 19:49:20 +00:00
|
|
|
bool StripDeadDebugInfo::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
2013-08-23 00:23:24 +00:00
|
|
|
LLVMContext &C = M.getContext();
|
|
|
|
|
|
|
|
// Find all debug info in F. This is actually overkill in terms of what we
|
2013-08-27 04:43:03 +00:00
|
|
|
// want to do, but we want to try and be as resilient as possible in the face
|
2013-08-23 00:23:24 +00: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.
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
SmallVector<Metadata *, 64> LiveGlobalVariables;
|
|
|
|
SmallVector<Metadata *, 64> LiveSubprograms;
|
2013-08-23 00:23:24 +00:00
|
|
|
DenseSet<const MDNode *> VisitedSet;
|
|
|
|
|
2014-03-18 09:41:07 +00:00
|
|
|
for (DICompileUnit DIC : F.compile_units()) {
|
2013-08-23 00:23:24 +00: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-01 19:49:20 +00:00
|
|
|
else
|
2013-08-23 00:23:24 +00:00
|
|
|
SubprogramChange = true;
|
2010-07-01 19:49:20 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 00:23:24 +00: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-01 19:49:20 +00:00
|
|
|
else
|
2013-08-23 00:23:24 +00:00
|
|
|
GlobalVariableChange = true;
|
2010-07-01 19:49:20 +00:00
|
|
|
}
|
2013-08-23 00:23:24 +00: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) {
|
2014-10-03 20:01:09 +00:00
|
|
|
DIC.replaceSubprograms(DIArray(MDNode::get(C, LiveSubprograms)));
|
2013-08-23 00:23:24 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GlobalVariableChange) {
|
2014-10-03 20:01:09 +00:00
|
|
|
DIC.replaceGlobalVariables(DIArray(MDNode::get(C, LiveGlobalVariables)));
|
2013-08-23 00:23:24 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset lists for the next iteration.
|
|
|
|
LiveSubprograms.clear();
|
|
|
|
LiveGlobalVariables.clear();
|
2010-07-01 19:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|