From ff7325ed4cf241968870d8d4df16b2e34ba00982 Mon Sep 17 00:00:00 2001 From: Michael Kuperstein Date: Fri, 12 Aug 2016 17:28:27 +0000 Subject: [PATCH] [PM] Port LowerInvoke to the new pass manager llvm-svn: 278531 --- include/llvm/InitializePasses.h | 2 +- include/llvm/Transforms/Utils/LowerInvoke.h | 30 +++++++++++++ lib/Passes/PassBuilder.cpp | 1 + lib/Passes/PassRegistry.def | 1 + lib/Transforms/Utils/LowerInvoke.cpp | 49 ++++++++++++++------- lib/Transforms/Utils/Utils.cpp | 2 +- test/Transforms/LowerInvoke/lowerinvoke.ll | 1 + 7 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 include/llvm/Transforms/Utils/LowerInvoke.h diff --git a/include/llvm/InitializePasses.h b/include/llvm/InitializePasses.h index ed85f2bd43a..980190ba74f 100644 --- a/include/llvm/InitializePasses.h +++ b/include/llvm/InitializePasses.h @@ -205,7 +205,7 @@ void initializeLowerEmuTLSPass(PassRegistry&); void initializeLowerExpectIntrinsicPass(PassRegistry&); void initializeLowerGuardIntrinsicLegacyPassPass(PassRegistry&); void initializeLowerIntrinsicsPass(PassRegistry&); -void initializeLowerInvokePass(PassRegistry&); +void initializeLowerInvokeLegacyPassPass(PassRegistry&); void initializeLowerSwitchPass(PassRegistry&); void initializeLowerTypeTestsPass(PassRegistry&); void initializeMIRPrintingPassPass(PassRegistry&); diff --git a/include/llvm/Transforms/Utils/LowerInvoke.h b/include/llvm/Transforms/Utils/LowerInvoke.h new file mode 100644 index 00000000000..12774c7fd1f --- /dev/null +++ b/include/llvm/Transforms/Utils/LowerInvoke.h @@ -0,0 +1,30 @@ +//===- LowerInvoke.h - Eliminate Invoke instructions ----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This transformation is designed for use by code generators which do not yet +// support stack unwinding. This pass converts 'invoke' instructions to 'call' +// instructions, so that any exception-handling 'landingpad' blocks become dead +// code (which can be removed by running the '-simplifycfg' pass afterwards). +// +//===----------------------------------------------------------------------===// +#ifndef LLVM_TRANSFORMS_UTILS_LOWERINVOKE_H +#define LLVM_TRANSFORMS_UTILS_LOWERINVOKE_H + +#include "llvm/IR/PassManager.h" + +namespace llvm { + +class LowerInvokePass : public PassInfoMixin { +public: + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); +}; + +} + +#endif // LLVM_TRANSFORMS_UTILS_LOWERINVOKE_H diff --git a/lib/Passes/PassBuilder.cpp b/lib/Passes/PassBuilder.cpp index cf0978f1086..9245a6e7b22 100644 --- a/lib/Passes/PassBuilder.cpp +++ b/lib/Passes/PassBuilder.cpp @@ -119,6 +119,7 @@ #include "llvm/Transforms/Utils/BreakCriticalEdges.h" #include "llvm/Transforms/Utils/LCSSA.h" #include "llvm/Transforms/Utils/LoopSimplify.h" +#include "llvm/Transforms/Utils/LowerInvoke.h" #include "llvm/Transforms/Utils/Mem2Reg.h" #include "llvm/Transforms/Utils/MemorySSA.h" #include "llvm/Transforms/Utils/NameAnonFunctions.h" diff --git a/lib/Passes/PassRegistry.def b/lib/Passes/PassRegistry.def index 7f9953cd85b..9f56004ac06 100644 --- a/lib/Passes/PassRegistry.def +++ b/lib/Passes/PassRegistry.def @@ -151,6 +151,7 @@ FUNCTION_PASS("lower-guard-intrinsic", LowerGuardIntrinsicPass()) FUNCTION_PASS("guard-widening", GuardWideningPass()) FUNCTION_PASS("gvn", GVN()) FUNCTION_PASS("loop-simplify", LoopSimplifyPass()) +FUNCTION_PASS("lowerinvoke", LowerInvokePass()) FUNCTION_PASS("mem2reg", PromotePass()) FUNCTION_PASS("memcpyopt", MemCpyOptPass()) FUNCTION_PASS("mldst-motion", MergedLoadStoreMotionPass()) diff --git a/lib/Transforms/Utils/LowerInvoke.cpp b/lib/Transforms/Utils/LowerInvoke.cpp index 1b31c5ae580..ee84541e526 100644 --- a/lib/Transforms/Utils/LowerInvoke.cpp +++ b/lib/Transforms/Utils/LowerInvoke.cpp @@ -14,6 +14,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Transforms/Utils/LowerInvoke.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Statistic.h" #include "llvm/IR/Instructions.h" @@ -28,36 +29,29 @@ using namespace llvm; STATISTIC(NumInvokes, "Number of invokes replaced"); namespace { - class LowerInvoke : public FunctionPass { + class LowerInvokeLegacyPass : public FunctionPass { public: static char ID; // Pass identification, replacement for typeid - explicit LowerInvoke() : FunctionPass(ID) { - initializeLowerInvokePass(*PassRegistry::getPassRegistry()); + explicit LowerInvokeLegacyPass() : FunctionPass(ID) { + initializeLowerInvokeLegacyPassPass(*PassRegistry::getPassRegistry()); } bool runOnFunction(Function &F) override; }; } -char LowerInvoke::ID = 0; -INITIALIZE_PASS(LowerInvoke, "lowerinvoke", +char LowerInvokeLegacyPass::ID = 0; +INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke", "Lower invoke and unwind, for unwindless code generators", false, false) -char &llvm::LowerInvokePassID = LowerInvoke::ID; - -// Public Interface To the LowerInvoke pass. -FunctionPass *llvm::createLowerInvokePass() { - return new LowerInvoke(); -} - -bool LowerInvoke::runOnFunction(Function &F) { +static bool runImpl(Function &F) { bool Changed = false; for (BasicBlock &BB : F) if (InvokeInst *II = dyn_cast(BB.getTerminator())) { - SmallVector CallArgs(II->op_begin(), II->op_end() - 3); + SmallVector CallArgs(II->op_begin(), II->op_end() - 3); // Insert a normal call instruction... - CallInst *NewCall = CallInst::Create(II->getCalledValue(), - CallArgs, "", II); + CallInst *NewCall = + CallInst::Create(II->getCalledValue(), CallArgs, "", II); NewCall->takeName(II); NewCall->setCallingConv(II->getCallingConv()); NewCall->setAttributes(II->getAttributes()); @@ -73,7 +67,28 @@ bool LowerInvoke::runOnFunction(Function &F) { // Remove the invoke instruction now. BB.getInstList().erase(II); - ++NumInvokes; Changed = true; + ++NumInvokes; + Changed = true; } return Changed; } + +bool LowerInvokeLegacyPass::runOnFunction(Function &F) { + return runImpl(F); +} + +namespace llvm { +char &LowerInvokePassID = LowerInvokeLegacyPass::ID; + +// Public Interface To the LowerInvoke pass. +FunctionPass *createLowerInvokePass() { return new LowerInvokeLegacyPass(); } + +PreservedAnalyses LowerInvokePass::run(Function &F, + FunctionAnalysisManager &AM) { + bool Changed = runImpl(F); + if (!Changed) + return PreservedAnalyses::all(); + + return PreservedAnalyses::none(); +} +} diff --git a/lib/Transforms/Utils/Utils.cpp b/lib/Transforms/Utils/Utils.cpp index 92b72453918..4cf07fc1f39 100644 --- a/lib/Transforms/Utils/Utils.cpp +++ b/lib/Transforms/Utils/Utils.cpp @@ -26,7 +26,7 @@ void llvm::initializeTransformUtils(PassRegistry &Registry) { initializeInstNamerPass(Registry); initializeLCSSAWrapperPassPass(Registry); initializeLoopSimplifyPass(Registry); - initializeLowerInvokePass(Registry); + initializeLowerInvokeLegacyPassPass(Registry); initializeLowerSwitchPass(Registry); initializeNameAnonFunctionLegacyPassPass(Registry); initializePromoteLegacyPassPass(Registry); diff --git a/test/Transforms/LowerInvoke/lowerinvoke.ll b/test/Transforms/LowerInvoke/lowerinvoke.ll index ad78df3e221..f3e6a88fbf4 100644 --- a/test/Transforms/LowerInvoke/lowerinvoke.ll +++ b/test/Transforms/LowerInvoke/lowerinvoke.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -lowerinvoke -S | FileCheck %s +; RUN: opt < %s -passes='lowerinvoke' -S | FileCheck %s declare i32 @external_func(i64 %arg)