mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-02-02 13:03:13 +01:00
Add a pass to strip the bodies of available_externally functions so string
literals and `TypeInfo`s only referenced by them can be deleted by `-globaldce`.
This commit is contained in:
@@ -222,6 +222,9 @@ static void addPassesForOptLevel(PassManager& pm) {
|
||||
}
|
||||
|
||||
if (optimizeLevel >= 1) {
|
||||
#if LLVM_REV >= 68940
|
||||
addPass(pm, createStripExternalsPass());
|
||||
#endif
|
||||
addPass(pm, createGlobalDCEPass());
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,11 @@ llvm::FunctionPass* createSimplifyDRuntimeCalls();
|
||||
|
||||
#ifdef USE_METADATA
|
||||
llvm::FunctionPass* createGarbageCollect2Stack();
|
||||
llvm::ModulePass *createStripMetaData();
|
||||
llvm::ModulePass* createStripMetaData();
|
||||
#endif
|
||||
|
||||
#if LLVM_REV >= 68940
|
||||
llvm::ModulePass* createStripExternalsPass();
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
92
gen/passes/StripExternals.cpp
Normal file
92
gen/passes/StripExternals.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
//===-- StripExternals.cpp - Strip available_externally symbols -----------===//
|
||||
//
|
||||
// The LLVM D Compiler
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This transform stips the bodies of available_externally functions and
|
||||
// initializers of available_externally globals, turning them into external
|
||||
// declarations.
|
||||
// This is useful to allow Global DCE (-globaldce) to clean up references to
|
||||
// globals only used by available_externally functions and initializers.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "gen/llvm-version.h"
|
||||
|
||||
#if LLVM_REV >= 68940
|
||||
|
||||
|
||||
#define DEBUG_TYPE "strip-externals"
|
||||
|
||||
#include "Passes.h"
|
||||
|
||||
#include "llvm/Module.h"
|
||||
#include "llvm/Pass.h"
|
||||
#include "llvm/ADT/Statistic.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
#include "llvm/Support/Debug.h"
|
||||
using namespace llvm;
|
||||
|
||||
STATISTIC(NumFunctions, "Number of function bodies removed");
|
||||
STATISTIC(NumVariables, "Number of global initializers removed");
|
||||
|
||||
namespace {
|
||||
struct VISIBILITY_HIDDEN StripExternals : public ModulePass {
|
||||
static char ID; // Pass identification, replacement for typeid
|
||||
StripExternals() : ModulePass(&ID) {}
|
||||
|
||||
// run - Do the StripExternals pass on the specified module.
|
||||
//
|
||||
bool runOnModule(Module &M);
|
||||
};
|
||||
}
|
||||
|
||||
char StripExternals::ID = 0;
|
||||
static RegisterPass<StripExternals>
|
||||
X("strip-externals", "Strip available_externally bodies and initializers");
|
||||
|
||||
ModulePass *createStripExternalsPass() { return new StripExternals(); }
|
||||
|
||||
bool StripExternals::runOnModule(Module &M) {
|
||||
bool Changed = false;
|
||||
|
||||
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
||||
if (I->hasAvailableExternallyLinkage()) {
|
||||
assert(!I->isDeclaration()&&"Declarations can't be available_externally");
|
||||
Changed = true;
|
||||
++NumFunctions;
|
||||
if (I->use_empty()) {
|
||||
DOUT << "Deleting function: " << *I;
|
||||
I->eraseFromParent();
|
||||
} else {
|
||||
I->deleteBody();
|
||||
DOUT << "Deleted function body: " << *I;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
||||
I != E; ++I) {
|
||||
if (I->hasAvailableExternallyLinkage()) {
|
||||
assert(!I->isDeclaration()&&"Declarations can't be available_externally");
|
||||
Changed = true;
|
||||
++NumVariables;
|
||||
if (I->use_empty()) {
|
||||
DOUT << "Deleting global: " << *I;
|
||||
I->eraseFromParent();
|
||||
} else {
|
||||
I->setInitializer(0);
|
||||
I->setLinkage(GlobalValue::ExternalLinkage);
|
||||
DOUT << "Deleted initializer: " << *I;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Changed;
|
||||
}
|
||||
|
||||
#endif //LLVM_REV >= 68940
|
||||
Reference in New Issue
Block a user