From e39e3a473df92c87da3f1d822529797abe504fed Mon Sep 17 00:00:00 2001 From: Justin Bogner Date: Fri, 30 Oct 2015 23:28:12 +0000 Subject: [PATCH] [PM] Port StripDeadPrototypes to the new pass manager This is a really straightforward port. Also adds a test for the pass, since it only seemed to be tested tangentially before. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@251726 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/InitializePasses.h | 2 +- .../llvm/Transforms/IPO/StripDeadPrototypes.h | 34 ++++++++++++ lib/Passes/PassBuilder.cpp | 1 + lib/Passes/PassRegistry.def | 1 + lib/Transforms/IPO/IPO.cpp | 2 +- lib/Transforms/IPO/StripDeadPrototypes.cpp | 52 +++++++++++-------- test/Transforms/LowerExpectIntrinsic/basic.ll | 2 +- test/Transforms/StripDeadPrototypes/basic.ll | 12 +++++ 8 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 include/llvm/Transforms/IPO/StripDeadPrototypes.h create mode 100644 test/Transforms/StripDeadPrototypes/basic.ll diff --git a/include/llvm/InitializePasses.h b/include/llvm/InitializePasses.h index 7e572232d5b..92127fa3301 100644 --- a/include/llvm/InitializePasses.h +++ b/include/llvm/InitializePasses.h @@ -260,7 +260,7 @@ void initializeStackColoringPass(PassRegistry&); void initializeStackSlotColoringPass(PassRegistry&); void initializeStraightLineStrengthReducePass(PassRegistry &); void initializeStripDeadDebugInfoPass(PassRegistry&); -void initializeStripDeadPrototypesPassPass(PassRegistry&); +void initializeStripDeadPrototypesLegacyPassPass(PassRegistry&); void initializeStripDebugDeclarePass(PassRegistry&); void initializeStripNonDebugSymbolsPass(PassRegistry&); void initializeStripSymbolsPass(PassRegistry&); diff --git a/include/llvm/Transforms/IPO/StripDeadPrototypes.h b/include/llvm/Transforms/IPO/StripDeadPrototypes.h new file mode 100644 index 00000000000..9dddd12871c --- /dev/null +++ b/include/llvm/Transforms/IPO/StripDeadPrototypes.h @@ -0,0 +1,34 @@ +//===-- StripDeadPrototypes.h - Remove unused function declarations -------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This pass loops over all of the functions in the input module, looking for +// dead declarations and removes them. Dead declarations are declarations of +// functions for which no implementation is available (i.e., declarations for +// unused library functions). +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_IPO_STRIPDEADPROTOTYPES_H +#define LLVM_TRANSFORMS_IPO_STRIPDEADPROTOTYPES_H + +#include "llvm/IR/Module.h" +#include "llvm/IR/PassManager.h" + +namespace llvm { + +/// Pass to remove unused function declarations. +class StripDeadPrototypesPass { +public: + static StringRef name() { return "StripDeadPrototypesPass"; } + PreservedAnalyses run(Module &M); +}; + +} + +#endif // LLVM_TRANSFORMS_IPO_STRIPDEADPROTOTYPES_H diff --git a/lib/Passes/PassBuilder.cpp b/lib/Passes/PassBuilder.cpp index f18fcb18575..b24c615f85f 100644 --- a/lib/Passes/PassBuilder.cpp +++ b/lib/Passes/PassBuilder.cpp @@ -30,6 +30,7 @@ #include "llvm/Support/Debug.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Transforms/InstCombine/InstCombine.h" +#include "llvm/Transforms/IPO/StripDeadPrototypes.h" #include "llvm/Transforms/Scalar/ADCE.h" #include "llvm/Transforms/Scalar/EarlyCSE.h" #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h" diff --git a/lib/Passes/PassRegistry.def b/lib/Passes/PassRegistry.def index 6f523301f92..638f606c402 100644 --- a/lib/Passes/PassRegistry.def +++ b/lib/Passes/PassRegistry.def @@ -31,6 +31,7 @@ MODULE_PASS("invalidate", InvalidateAllAnalysesPass()) MODULE_PASS("no-op-module", NoOpModulePass()) MODULE_PASS("print", PrintModulePass(dbgs())) MODULE_PASS("print-cg", LazyCallGraphPrinterPass(dbgs())) +MODULE_PASS("strip-dead-prototypes", StripDeadPrototypesPass()) MODULE_PASS("verify", VerifierPass()) #undef MODULE_PASS diff --git a/lib/Transforms/IPO/IPO.cpp b/lib/Transforms/IPO/IPO.cpp index 7ecc37710ed..97e45843682 100644 --- a/lib/Transforms/IPO/IPO.cpp +++ b/lib/Transforms/IPO/IPO.cpp @@ -40,7 +40,7 @@ void llvm::initializeIPO(PassRegistry &Registry) { initializeMergeFunctionsPass(Registry); initializePartialInlinerPass(Registry); initializePruneEHPass(Registry); - initializeStripDeadPrototypesPassPass(Registry); + initializeStripDeadPrototypesLegacyPassPass(Registry); initializeStripSymbolsPass(Registry); initializeStripDebugDeclarePass(Registry); initializeStripDeadDebugInfoPass(Registry); diff --git a/lib/Transforms/IPO/StripDeadPrototypes.cpp b/lib/Transforms/IPO/StripDeadPrototypes.cpp index 22ba1c6c674..c94cc7c74a8 100644 --- a/lib/Transforms/IPO/StripDeadPrototypes.cpp +++ b/lib/Transforms/IPO/StripDeadPrototypes.cpp @@ -14,35 +14,19 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Transforms/IPO.h" +#include "llvm/Transforms/IPO/StripDeadPrototypes.h" #include "llvm/ADT/Statistic.h" #include "llvm/IR/Module.h" #include "llvm/Pass.h" +#include "llvm/Transforms/IPO.h" + using namespace llvm; #define DEBUG_TYPE "strip-dead-prototypes" STATISTIC(NumDeadPrototypes, "Number of dead prototypes removed"); -namespace { - -/// @brief Pass to remove unused function declarations. -class StripDeadPrototypesPass : public ModulePass { -public: - static char ID; // Pass identification, replacement for typeid - StripDeadPrototypesPass() : ModulePass(ID) { - initializeStripDeadPrototypesPassPass(*PassRegistry::getPassRegistry()); - } - bool runOnModule(Module &M) override; -}; - -} // end anonymous namespace - -char StripDeadPrototypesPass::ID = 0; -INITIALIZE_PASS(StripDeadPrototypesPass, "strip-dead-prototypes", - "Strip Unused Function Prototypes", false, false) - -bool StripDeadPrototypesPass::runOnModule(Module &M) { +static bool stripDeadPrototypes(Module &M) { bool MadeChange = false; // Erase dead function prototypes. @@ -69,6 +53,32 @@ bool StripDeadPrototypesPass::runOnModule(Module &M) { return MadeChange; } +PreservedAnalyses StripDeadPrototypesPass::run(Module &M) { + if (stripDeadPrototypes(M)) + return PreservedAnalyses::none(); + return PreservedAnalyses::all(); +} + +namespace { + +class StripDeadPrototypesLegacyPass : public ModulePass { +public: + static char ID; // Pass identification, replacement for typeid + StripDeadPrototypesLegacyPass() : ModulePass(ID) { + initializeStripDeadPrototypesLegacyPassPass( + *PassRegistry::getPassRegistry()); + } + bool runOnModule(Module &M) override { + return stripDeadPrototypes(M); + } +}; + +} // end anonymous namespace + +char StripDeadPrototypesLegacyPass::ID = 0; +INITIALIZE_PASS(StripDeadPrototypesLegacyPass, "strip-dead-prototypes", + "Strip Unused Function Prototypes", false, false) + ModulePass *llvm::createStripDeadPrototypesPass() { - return new StripDeadPrototypesPass(); + return new StripDeadPrototypesLegacyPass(); } diff --git a/test/Transforms/LowerExpectIntrinsic/basic.ll b/test/Transforms/LowerExpectIntrinsic/basic.ll index 73d9f44ee7e..69e67cd7c1d 100644 --- a/test/Transforms/LowerExpectIntrinsic/basic.ll +++ b/test/Transforms/LowerExpectIntrinsic/basic.ll @@ -1,5 +1,5 @@ ; RUN: opt -lower-expect -strip-dead-prototypes -S -o - < %s | FileCheck %s -; RUN: opt -S -passes=lower-expect < %s | opt -strip-dead-prototypes -S | FileCheck %s +; RUN: opt -S -passes='function(lower-expect),strip-dead-prototypes' < %s | FileCheck %s ; CHECK-LABEL: @test1( define i32 @test1(i32 %x) nounwind uwtable ssp { diff --git a/test/Transforms/StripDeadPrototypes/basic.ll b/test/Transforms/StripDeadPrototypes/basic.ll new file mode 100644 index 00000000000..6845faf7d03 --- /dev/null +++ b/test/Transforms/StripDeadPrototypes/basic.ll @@ -0,0 +1,12 @@ +; RUN: opt -strip-dead-prototypes -S -o - < %s | FileCheck %s +; RUN: opt -S -passes=strip-dead-prototypes < %s | FileCheck %s + +; CHECK: declare i32 @f +declare i32 @f() +; CHECK-NOT: declare i32 @g +declare i32 @g() + +define i32 @foo() { + %call = call i32 @f() + ret i32 %call +} -- 2.34.1