2004-12-02 22:25:03 +01:00
|
|
|
//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01: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 01:48:37 +02:00
|
|
|
//
|
2004-12-02 22:25:03 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-11-04 17:15:04 +01:00
|
|
|
// The StripSymbols transformation implements code stripping. Specifically, it
|
|
|
|
// can delete:
|
2013-08-22 00:53:29 +02:00
|
|
|
//
|
2007-11-04 17:15:04 +01:00
|
|
|
// * names for virtual registers
|
|
|
|
// * symbols for internal globals and functions
|
|
|
|
// * debug information
|
2004-12-02 22:25:03 +01:00
|
|
|
//
|
2007-11-04 17:15:04 +01: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 22:25:03 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-09-14 23:37:46 +02:00
|
|
|
#include "llvm/Transforms/IPO/StripSymbols.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Constants.h"
|
2014-03-06 01:46:21 +01:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2020-09-14 23:37:46 +02:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2013-01-07 16:43:51 +01:00
|
|
|
#include "llvm/IR/TypeFinder.h"
|
2013-01-02 12:36:10 +01: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-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2004-12-02 22:25:03 +01:00
|
|
|
#include "llvm/Pass.h"
|
2017-06-06 13:49:48 +02: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-13 22:15:01 +01:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2020-09-14 23:37:46 +02:00
|
|
|
|
2004-12-02 22:25:03 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-09-03 08:43:15 +02:00
|
|
|
class StripSymbols : public ModulePass {
|
2004-12-02 22:25:03 +01:00
|
|
|
bool OnlyDebugInfo;
|
|
|
|
public:
|
2007-05-06 15:37:16 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2013-08-22 00:53:29 +02:00
|
|
|
explicit StripSymbols(bool ODI = false)
|
2010-10-19 19:21:58 +02:00
|
|
|
: ModulePass(ID), OnlyDebugInfo(ODI) {
|
|
|
|
initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2004-12-02 22:25:03 +01:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
bool runOnModule(Module &M) override;
|
2008-11-18 22:34:39 +01:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2008-11-18 22:34:39 +01:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-11-14 23:49:37 +01:00
|
|
|
|
2009-09-03 08:43:15 +02:00
|
|
|
class StripNonDebugSymbols : public ModulePass {
|
2008-11-18 22:34:39 +01:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripNonDebugSymbols()
|
2010-10-19 19:21:58 +02:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-11-14 23:49:37 +01:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
bool runOnModule(Module &M) override;
|
2004-12-02 22:25:03 +01:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2004-12-02 22:25:03 +01:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2009-03-09 21:49:37 +01:00
|
|
|
|
2009-09-03 08:43:15 +02:00
|
|
|
class StripDebugDeclare : public ModulePass {
|
2009-03-09 21:49:37 +01:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripDebugDeclare()
|
2010-10-19 19:21:58 +02:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2009-03-09 21:49:37 +01:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
bool runOnModule(Module &M) override;
|
2009-03-09 21:49:37 +01:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2009-03-09 21:49:37 +01:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2010-07-01 21:49:20 +02:00
|
|
|
|
|
|
|
class StripDeadDebugInfo : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripDeadDebugInfo()
|
2010-10-19 19:21:58 +02:00
|
|
|
: ModulePass(ID) {
|
|
|
|
initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-01 21:49:20 +02:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
bool runOnModule(Module &M) override;
|
2010-07-01 21:49:20 +02:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-07-01 21:49:20 +02:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 11:49:53 +02:00
|
|
|
}
|
2004-12-02 22:25:03 +01:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char StripSymbols::ID = 0;
|
2010-07-22 00:09:45 +02:00
|
|
|
INITIALIZE_PASS(StripSymbols, "strip",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Strip all symbols from a module", false, false)
|
2008-05-13 02:00:25 +02:00
|
|
|
|
2004-12-02 22:25:03 +01:00
|
|
|
ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
|
|
|
|
return new StripSymbols(OnlyDebugInfo);
|
|
|
|
}
|
|
|
|
|
2008-11-18 22:34:39 +01:00
|
|
|
char StripNonDebugSymbols::ID = 0;
|
2010-07-22 00:09:45 +02:00
|
|
|
INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
|
|
|
|
"Strip all symbols, except dbg symbols, from a module",
|
2010-10-08 00:25:06 +02:00
|
|
|
false, false)
|
2008-11-18 22:34:39 +01:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripNonDebugSymbolsPass() {
|
|
|
|
return new StripNonDebugSymbols();
|
|
|
|
}
|
|
|
|
|
2009-03-09 21:49:37 +01:00
|
|
|
char StripDebugDeclare::ID = 0;
|
2010-07-22 00:09:45 +02:00
|
|
|
INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Strip all llvm.dbg.declare intrinsics", false, false)
|
2009-03-09 21:49:37 +01:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripDebugDeclarePass() {
|
|
|
|
return new StripDebugDeclare();
|
|
|
|
}
|
|
|
|
|
2010-07-01 21:49:20 +02:00
|
|
|
char StripDeadDebugInfo::ID = 0;
|
2010-07-22 00:09:45 +02:00
|
|
|
INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Strip debug info for unused symbols", false, false)
|
2010-07-01 21:49:20 +02:00
|
|
|
|
|
|
|
ModulePass *llvm::createStripDeadDebugInfoPass() {
|
|
|
|
return new StripDeadDebugInfo();
|
|
|
|
}
|
|
|
|
|
2008-11-13 02:28:40 +01:00
|
|
|
/// OnlyUsedBy - Return true if V is only used by Usr.
|
|
|
|
static bool OnlyUsedBy(Value *V, Value *Usr) {
|
2014-03-09 04:16:01 +01:00
|
|
|
for (User *U : V->users())
|
2008-11-13 02:28:40 +01:00
|
|
|
if (U != Usr)
|
|
|
|
return false;
|
2014-03-09 04:16:01 +01:00
|
|
|
|
2008-11-13 02:28:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-03 17:22:08 +01:00
|
|
|
static void RemoveDeadConstant(Constant *C) {
|
|
|
|
assert(C->use_empty() && "Constant is not dead!");
|
2009-10-28 06:14:34 +01:00
|
|
|
SmallPtrSet<Constant*, 4> Operands;
|
2015-06-25 22:51:38 +02:00
|
|
|
for (Value *Op : C->operands())
|
|
|
|
if (OnlyUsedBy(Op, C))
|
|
|
|
Operands.insert(cast<Constant>(Op));
|
2004-12-03 17:22:08 +01:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
|
2013-12-05 06:44:44 +01:00
|
|
|
if (!GV->hasLocalLinkage()) return; // Don't delete non-static globals.
|
2004-12-03 17:22:08 +01:00
|
|
|
GV->eraseFromParent();
|
2020-03-04 00:42:16 +01:00
|
|
|
} else if (!isa<Function>(C)) {
|
|
|
|
// FIXME: Why does the type of the constant matter here?
|
|
|
|
if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType()) ||
|
|
|
|
isa<VectorType>(C->getType()))
|
2008-11-20 02:20:42 +01:00
|
|
|
C->destroyConstant();
|
2020-03-04 00:42:16 +01:00
|
|
|
}
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2004-12-03 17:22:08 +01:00
|
|
|
// If the constant referenced anything, see if we can delete it as well.
|
2014-08-25 01:23:06 +02:00
|
|
|
for (Constant *O : Operands)
|
|
|
|
RemoveDeadConstant(O);
|
2004-12-03 17:22:08 +01:00
|
|
|
}
|
2004-12-02 22:25:03 +01:00
|
|
|
|
2007-02-07 07:22:45 +01:00
|
|
|
// Strip the symbol table of its names.
|
|
|
|
//
|
2008-11-18 22:34:39 +01:00
|
|
|
static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
|
2007-02-07 07:22:45 +01:00
|
|
|
for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
|
2007-02-12 06:18:08 +01:00
|
|
|
Value *V = VI->getValue();
|
2007-02-07 07:22:45 +01:00
|
|
|
++VI;
|
2009-01-15 21:18:42 +01:00
|
|
|
if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
|
2009-07-26 11:48:23 +02:00
|
|
|
if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
|
2008-11-18 22:34:39 +01:00
|
|
|
// Set name to "", removing from symbol table!
|
|
|
|
V->setName("");
|
2007-02-07 07:22:45 +01: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-09 19:41:24 +02:00
|
|
|
// Strip any named types of their names.
|
|
|
|
static void StripTypeNames(Module &M, bool PreserveDbgInfo) {
|
2012-08-03 02:30:35 +02: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-09 19:41:24 +02:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
|
|
|
|
StructType *STy = StructTypes[i];
|
2011-08-12 20:06:37 +02:00
|
|
|
if (STy->isLiteral() || STy->getName().empty()) continue;
|
2013-08-22 00:53:29 +02: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-09 19:41:24 +02:00
|
|
|
if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
STy->setName("");
|
2008-11-18 22:34:39 +01:00
|
|
|
}
|
2007-02-07 07:22:45 +01:00
|
|
|
}
|
|
|
|
|
2008-11-18 22:13:41 +01:00
|
|
|
/// Find values that are marked as llvm.used.
|
2009-07-20 08:14:25 +02:00
|
|
|
static void findUsedValues(GlobalVariable *LLVMUsed,
|
2014-08-21 07:55:13 +02:00
|
|
|
SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
|
2014-04-25 07:29:35 +02:00
|
|
|
if (!LLVMUsed) return;
|
2009-07-20 08:14:25 +02:00
|
|
|
UsedValues.insert(LLVMUsed);
|
2013-04-22 16:58:02 +02:00
|
|
|
|
|
|
|
ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
|
|
|
|
|
2009-07-20 08:14:25 +02:00
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
|
2013-08-22 00:53:29 +02:00
|
|
|
if (GlobalValue *GV =
|
2009-07-20 08:14:25 +02:00
|
|
|
dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
|
|
|
|
UsedValues.insert(GV);
|
2008-11-18 22:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// StripSymbolNames - Strip symbol names.
|
2009-08-07 03:32:21 +02:00
|
|
|
static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
|
2008-11-18 22:13:41 +01:00
|
|
|
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
|
2009-07-20 08:14:25 +02:00
|
|
|
findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
|
|
|
|
findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
|
2008-11-18 22:13:41 +01:00
|
|
|
|
2021-02-09 07:33:53 +01:00
|
|
|
for (GlobalVariable &GV : M.globals()) {
|
|
|
|
if (GV.hasLocalLinkage() && llvmUsedValues.count(&GV) == 0)
|
|
|
|
if (!PreserveDbgInfo || !GV.getName().startswith("llvm.dbg"))
|
|
|
|
GV.setName(""); // Internal symbols can't participate in linkage
|
2008-11-14 23:49:37 +01:00
|
|
|
}
|
2013-08-22 00:53:29 +02:00
|
|
|
|
2016-06-26 14:28:59 +02: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 08:00:02 +02:00
|
|
|
if (auto *Symtab = I.getValueSymbolTable())
|
|
|
|
StripSymtab(*Symtab, PreserveDbgInfo);
|
2008-11-14 23:49:37 +01:00
|
|
|
}
|
2013-08-22 00:53:29 +02:00
|
|
|
|
2008-11-14 23:49:37 +01: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-09 19:41:24 +02:00
|
|
|
StripTypeNames(M, PreserveDbgInfo);
|
2008-01-16 04:33:05 +01:00
|
|
|
|
2008-11-14 23:49:37 +01:00
|
|
|
return true;
|
|
|
|
}
|
2004-12-02 22:25:03 +01:00
|
|
|
|
2008-11-18 22:34:39 +01:00
|
|
|
bool StripSymbols::runOnModule(Module &M) {
|
2016-04-23 00:06:11 +02:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
|
2008-11-18 22:34:39 +01:00
|
|
|
bool Changed = false;
|
|
|
|
Changed |= StripDebugInfo(M);
|
|
|
|
if (!OnlyDebugInfo)
|
|
|
|
Changed |= StripSymbolNames(M, false);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StripNonDebugSymbols::runOnModule(Module &M) {
|
2016-04-23 00:06:11 +02:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
|
2008-11-18 22:34:39 +01:00
|
|
|
return StripSymbolNames(M, true);
|
|
|
|
}
|
2009-03-09 21:49:37 +01:00
|
|
|
|
2020-09-14 23:37:46 +02:00
|
|
|
static bool stripDebugDeclareImpl(Module &M) {
|
2009-03-09 21:49:37 +01:00
|
|
|
|
|
|
|
Function *Declare = M.getFunction("llvm.dbg.declare");
|
|
|
|
std::vector<Constant*> DeadConstants;
|
|
|
|
|
2009-03-13 23:59:47 +01:00
|
|
|
if (Declare) {
|
|
|
|
while (!Declare->use_empty()) {
|
2014-03-09 04:16:01 +01:00
|
|
|
CallInst *CI = cast<CallInst>(Declare->user_back());
|
2010-06-30 14:40:35 +02:00
|
|
|
Value *Arg1 = CI->getArgOperand(0);
|
|
|
|
Value *Arg2 = CI->getArgOperand(1);
|
2009-03-13 23:59:47 +01:00
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg1->use_empty()) {
|
2013-08-22 00:53:29 +02:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg1))
|
2009-03-13 23:59:47 +01:00
|
|
|
DeadConstants.push_back(C);
|
2013-08-22 00:53:29 +02:00
|
|
|
else
|
2009-05-02 22:22:10 +02:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Arg1);
|
2009-03-13 23:59:47 +01:00
|
|
|
}
|
|
|
|
if (Arg2->use_empty())
|
2013-08-22 00:53:29 +02:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg2))
|
2009-03-13 23:59:47 +01:00
|
|
|
DeadConstants.push_back(C);
|
2009-03-09 21:49:37 +01:00
|
|
|
}
|
2009-03-13 23:59:47 +01:00
|
|
|
Declare->eraseFromParent();
|
2009-03-09 21:49:37 +01: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 06:14:34 +01:00
|
|
|
} else
|
2009-03-09 21:49:37 +01:00
|
|
|
RemoveDeadConstant(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-01 21:49:20 +02:00
|
|
|
|
2020-09-14 23:37:46 +02:00
|
|
|
bool StripDebugDeclare::runOnModule(Module &M) {
|
2016-04-23 00:06:11 +02:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
2020-09-14 23:37:46 +02:00
|
|
|
return stripDebugDeclareImpl(M);
|
|
|
|
}
|
2016-04-23 00:06:11 +02:00
|
|
|
|
2020-09-14 23:37:46 +02:00
|
|
|
static bool stripDeadDebugInfoImpl(Module &M) {
|
2010-07-01 21:49:20 +02:00
|
|
|
bool Changed = false;
|
|
|
|
|
2013-08-23 02:23:24 +02:00
|
|
|
LLVMContext &C = M.getContext();
|
|
|
|
|
|
|
|
// Find all debug info in F. This is actually overkill in terms of what we
|
2013-08-27 06:43:03 +02:00
|
|
|
// want to do, but we want to try and be as resilient as possible in the face
|
2013-08-23 02:23:24 +02: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-09 19:38:53 +01:00
|
|
|
SmallVector<Metadata *, 64> LiveGlobalVariables;
|
2016-12-20 03:09:43 +01:00
|
|
|
DenseSet<DIGlobalVariableExpression *> VisitedSet;
|
2013-08-23 02:23:24 +02:00
|
|
|
|
2016-12-20 03:09:43 +01:00
|
|
|
std::set<DIGlobalVariableExpression *> LiveGVs;
|
2016-09-13 03:12:59 +02:00
|
|
|
for (GlobalVariable &GV : M.globals()) {
|
2016-12-20 03:09:43 +01:00
|
|
|
SmallVector<DIGlobalVariableExpression *, 1> GVEs;
|
|
|
|
GV.getDebugInfo(GVEs);
|
|
|
|
for (auto *GVE : GVEs)
|
|
|
|
LiveGVs.insert(GVE);
|
2015-11-05 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
2017-04-06 21:26:22 +02:00
|
|
|
std::set<DICompileUnit *> LiveCUs;
|
2017-04-11 15:32:11 +02:00
|
|
|
// Any CU referenced from a subprogram is live.
|
|
|
|
for (DISubprogram *SP : F.subprograms()) {
|
|
|
|
if (SP->getUnit())
|
2017-04-06 21:26:22 +02:00
|
|
|
LiveCUs.insert(SP->getUnit());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasDeadCUs = false;
|
2015-04-29 18:38:44 +02:00
|
|
|
for (DICompileUnit *DIC : F.compile_units()) {
|
2013-08-23 02:23:24 +02:00
|
|
|
// Create our live global variable list.
|
|
|
|
bool GlobalVariableChange = false;
|
2016-12-20 03:09:43 +01:00
|
|
|
for (auto *DIG : DIC->getGlobalVariables()) {
|
|
|
|
if (DIG->getExpression() && DIG->getExpression()->isConstant())
|
|
|
|
LiveGVs.insert(DIG);
|
|
|
|
|
2013-08-23 02:23:24 +02:00
|
|
|
// Make sure we only visit each global variable only once.
|
|
|
|
if (!VisitedSet.insert(DIG).second)
|
|
|
|
continue;
|
|
|
|
|
2016-09-13 03:12:59 +02:00
|
|
|
// If a global variable references DIG, the global variable is live.
|
|
|
|
if (LiveGVs.count(DIG))
|
2013-08-23 02:23:24 +02:00
|
|
|
LiveGlobalVariables.push_back(DIG);
|
2010-07-01 21:49:20 +02:00
|
|
|
else
|
2013-08-23 02:23:24 +02:00
|
|
|
GlobalVariableChange = true;
|
2010-07-01 21:49:20 +02:00
|
|
|
}
|
2013-08-23 02:23:24 +02:00
|
|
|
|
2017-04-06 21:26:22 +02:00
|
|
|
if (!LiveGlobalVariables.empty())
|
|
|
|
LiveCUs.insert(DIC);
|
|
|
|
else if (!LiveCUs.count(DIC))
|
|
|
|
HasDeadCUs = true;
|
|
|
|
|
2016-04-15 17:57:41 +02:00
|
|
|
// If we found dead global variables, replace the current global
|
|
|
|
// variable list with our new live global variable list.
|
2013-08-23 02:23:24 +02:00
|
|
|
if (GlobalVariableChange) {
|
2015-04-16 01:19:27 +02:00
|
|
|
DIC->replaceGlobalVariables(MDTuple::get(C, LiveGlobalVariables));
|
2013-08-23 02:23:24 +02:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset lists for the next iteration.
|
|
|
|
LiveGlobalVariables.clear();
|
2010-07-01 21:49:20 +02:00
|
|
|
}
|
|
|
|
|
2017-04-06 21:26:22 +02: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-01 21:49:20 +02:00
|
|
|
return Changed;
|
|
|
|
}
|
2020-09-14 23:37:46 +02: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.
|
|
|
|
bool StripDeadDebugInfo::runOnModule(Module &M) {
|
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
return stripDeadDebugInfoImpl(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses StripSymbolsPass::run(Module &M, ModuleAnalysisManager &AM) {
|
|
|
|
StripDebugInfo(M);
|
|
|
|
StripSymbolNames(M, false);
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses StripNonDebugSymbolsPass::run(Module &M,
|
|
|
|
ModuleAnalysisManager &AM) {
|
|
|
|
StripSymbolNames(M, true);
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses StripDebugDeclarePass::run(Module &M,
|
|
|
|
ModuleAnalysisManager &AM) {
|
|
|
|
stripDebugDeclareImpl(M);
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses StripDeadDebugInfoPass::run(Module &M,
|
|
|
|
ModuleAnalysisManager &AM) {
|
|
|
|
stripDeadDebugInfoImpl(M);
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|