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
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#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"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2004-12-03 05:25:03 +08:00
|
|
|
#include "llvm/Pass.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +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();
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
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;
|
2015-06-26 04:51:38 +08:00
|
|
|
for (Value *Op : C->operands())
|
|
|
|
if (OnlyUsedBy(Op, C))
|
|
|
|
Operands.insert(cast<Constant>(Op));
|
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.
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Constant *O : Operands)
|
|
|
|
RemoveDeadConstant(O);
|
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,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!LLVMUsed) return;
|
2009-07-20 14:14:25 +08:00
|
|
|
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) {
|
2015-10-14 01:51:03 +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
|
|
|
|
2016-06-26 20:28:59 +08:00
|
|
|
for (Function &I : M) {
|
|
|
|
if (I.hasLocalLinkage() && llvmUsedValues.count(&I) == 0)
|
|
|
|
if (!PreserveDbgInfo || !I.getName().startswith("llvm.dbg"))
|
|
|
|
I.setName(""); // Internal symbols can't participate in linkage
|
2016-09-17 14:00:02 +08:00
|
|
|
if (auto *Symtab = I.getValueSymbolTable())
|
|
|
|
StripSymtab(*Symtab, 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) {
|
2016-04-23 06:06:11 +08:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
|
2008-11-19 05:34:39 +08:00
|
|
|
bool Changed = false;
|
|
|
|
Changed |= StripDebugInfo(M);
|
|
|
|
if (!OnlyDebugInfo)
|
|
|
|
Changed |= StripSymbolNames(M, false);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StripNonDebugSymbols::runOnModule(Module &M) {
|
2016-04-23 06:06:11 +08:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
|
2008-11-19 05:34:39 +08:00
|
|
|
return StripSymbolNames(M, true);
|
|
|
|
}
|
2009-03-10 04:49:37 +08:00
|
|
|
|
|
|
|
bool StripDebugDeclare::runOnModule(Module &M) {
|
2016-04-23 06:06:11 +08:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
2009-03-10 04:49:37 +08:00
|
|
|
|
|
|
|
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) {
|
2016-04-23 06:06:11 +08:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
|
2010-07-02 03:49:20 +08:00
|
|
|
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.
|
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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
SmallVector<Metadata *, 64> LiveGlobalVariables;
|
2016-12-20 10:09:43 +08:00
|
|
|
DenseSet<DIGlobalVariableExpression *> VisitedSet;
|
2013-08-23 08:23:24 +08:00
|
|
|
|
2016-12-20 10:09:43 +08:00
|
|
|
std::set<DIGlobalVariableExpression *> LiveGVs;
|
2016-09-13 09:12:59 +08:00
|
|
|
for (GlobalVariable &GV : M.globals()) {
|
2016-12-20 10:09:43 +08:00
|
|
|
SmallVector<DIGlobalVariableExpression *, 1> GVEs;
|
|
|
|
GV.getDebugInfo(GVEs);
|
|
|
|
for (auto *GVE : GVEs)
|
|
|
|
LiveGVs.insert(GVE);
|
2015-11-06 06:03:56 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 03:26:22 +08:00
|
|
|
std::set<DICompileUnit *> LiveCUs;
|
2017-04-11 21:32:11 +08:00
|
|
|
// Any CU referenced from a subprogram is live.
|
|
|
|
for (DISubprogram *SP : F.subprograms()) {
|
|
|
|
if (SP->getUnit())
|
2017-04-07 03:26:22 +08:00
|
|
|
LiveCUs.insert(SP->getUnit());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasDeadCUs = false;
|
2015-04-30 00:38:44 +08:00
|
|
|
for (DICompileUnit *DIC : F.compile_units()) {
|
2013-08-23 08:23:24 +08:00
|
|
|
// Create our live global variable list.
|
|
|
|
bool GlobalVariableChange = false;
|
2016-12-20 10:09:43 +08:00
|
|
|
for (auto *DIG : DIC->getGlobalVariables()) {
|
|
|
|
if (DIG->getExpression() && DIG->getExpression()->isConstant())
|
|
|
|
LiveGVs.insert(DIG);
|
|
|
|
|
2013-08-23 08:23:24 +08:00
|
|
|
// Make sure we only visit each global variable only once.
|
|
|
|
if (!VisitedSet.insert(DIG).second)
|
|
|
|
continue;
|
|
|
|
|
2016-09-13 09:12:59 +08:00
|
|
|
// If a global variable references DIG, the global variable is live.
|
|
|
|
if (LiveGVs.count(DIG))
|
2013-08-23 08:23:24 +08:00
|
|
|
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
|
|
|
|
2017-04-07 03:26:22 +08:00
|
|
|
if (!LiveGlobalVariables.empty())
|
|
|
|
LiveCUs.insert(DIC);
|
|
|
|
else if (!LiveCUs.count(DIC))
|
|
|
|
HasDeadCUs = true;
|
|
|
|
|
2016-04-15 23:57:41 +08:00
|
|
|
// If we found dead global variables, replace the current global
|
|
|
|
// variable list with our new live global variable list.
|
2013-08-23 08:23:24 +08:00
|
|
|
if (GlobalVariableChange) {
|
2015-04-16 07:19:27 +08:00
|
|
|
DIC->replaceGlobalVariables(MDTuple::get(C, LiveGlobalVariables));
|
2013-08-23 08:23:24 +08:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset lists for the next iteration.
|
|
|
|
LiveGlobalVariables.clear();
|
2010-07-02 03:49:20 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 03:26:22 +08:00
|
|
|
if (HasDeadCUs) {
|
|
|
|
// Delete the old node and replace it with a new one
|
|
|
|
NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
|
|
|
|
NMD->clearOperands();
|
|
|
|
if (!LiveCUs.empty()) {
|
|
|
|
for (DICompileUnit *CU : LiveCUs)
|
|
|
|
NMD->addOperand(CU);
|
|
|
|
}
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2010-07-02 03:49:20 +08:00
|
|
|
return Changed;
|
|
|
|
}
|