blob: fe67e191dc62a2e039c26a83b9be6644e6cf78d8 [file] [log] [blame]
Mark Seabornb6118c52014-03-20 19:54:47 +00001//===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattnera43b8f42003-10-05 19:14:42 +00008//
9// This transformation is designed for use by code generators which do not yet
Mark Seabornb6118c52014-03-20 19:54:47 +000010// support stack unwinding. This pass converts 'invoke' instructions to 'call'
11// instructions, so that any exception-handling 'landingpad' blocks become dead
12// code (which can be removed by running the '-simplifycfg' pass afterwards).
Chris Lattner61fab142004-03-31 22:00:30 +000013//
Chris Lattnera43b8f42003-10-05 19:14:42 +000014//===----------------------------------------------------------------------===//
15
Michael Kuperstein31b83992016-08-12 17:28:27 +000016#include "llvm/Transforms/Utils/LowerInvoke.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/Module.h"
Chris Lattnera43b8f42003-10-05 19:14:42 +000022#include "llvm/Pass.h"
David Blaikiea373d182018-03-28 17:44:36 +000023#include "llvm/Transforms/Utils.h"
Chris Lattner7e5bd592003-12-10 20:22:42 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chandler Carruth964daaa2014-04-22 02:55:47 +000026#define DEBUG_TYPE "lowerinvoke"
27
Chris Lattner45f966d2006-12-19 22:17:40 +000028STATISTIC(NumInvokes, "Number of invokes replaced");
Chris Lattnera43b8f42003-10-05 19:14:42 +000029
Chris Lattner45f966d2006-12-19 22:17:40 +000030namespace {
Michael Kuperstein31b83992016-08-12 17:28:27 +000031 class LowerInvokeLegacyPass : public FunctionPass {
Chris Lattnera43b8f42003-10-05 19:14:42 +000032 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000033 static char ID; // Pass identification, replacement for typeid
Michael Kuperstein31b83992016-08-12 17:28:27 +000034 explicit LowerInvokeLegacyPass() : FunctionPass(ID) {
35 initializeLowerInvokeLegacyPassPass(*PassRegistry::getPassRegistry());
Owen Anderson6c18d1a2010-10-19 17:21:58 +000036 }
Craig Topper3e4c6972014-03-05 09:10:37 +000037 bool runOnFunction(Function &F) override;
Chris Lattnera43b8f42003-10-05 19:14:42 +000038 };
Chris Lattnera43b8f42003-10-05 19:14:42 +000039}
40
Michael Kuperstein31b83992016-08-12 17:28:27 +000041char LowerInvokeLegacyPass::ID = 0;
42INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke",
Owen Andersond31d82d2010-08-23 17:52:01 +000043 "Lower invoke and unwind, for unwindless code generators",
Owen Andersondf7a4f22010-10-07 22:25:06 +000044 false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000045
Michael Kuperstein31b83992016-08-12 17:28:27 +000046static bool runImpl(Function &F) {
Chris Lattnera43b8f42003-10-05 19:14:42 +000047 bool Changed = false;
Benjamin Kramer135f7352016-06-26 12:28:59 +000048 for (BasicBlock &BB : F)
49 if (InvokeInst *II = dyn_cast<InvokeInst>(BB.getTerminator())) {
Heejin Ahnbf771692018-05-09 00:53:50 +000050 SmallVector<Value *, 16> CallArgs(II->arg_begin(), II->arg_end());
51 SmallVector<OperandBundleDef, 1> OpBundles;
52 II->getOperandBundlesAsDefs(OpBundles);
Chris Lattnera43b8f42003-10-05 19:14:42 +000053 // Insert a normal call instruction...
Michael Kuperstein31b83992016-08-12 17:28:27 +000054 CallInst *NewCall =
James Y Knight7976eb52019-02-01 20:43:25 +000055 CallInst::Create(II->getFunctionType(), II->getCalledValue(),
56 CallArgs, OpBundles, "", II);
Chris Lattner8dd4cae2007-02-11 01:37:51 +000057 NewCall->takeName(II);
Chris Lattnerca968392005-05-13 06:27:02 +000058 NewCall->setCallingConv(II->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +000059 NewCall->setAttributes(II->getAttributes());
Devang Patel218f3202010-10-18 18:53:44 +000060 NewCall->setDebugLoc(II->getDebugLoc());
Chris Lattnera43b8f42003-10-05 19:14:42 +000061 II->replaceAllUsesWith(NewCall);
Misha Brukmanb1c93172005-04-21 23:48:37 +000062
Chris Lattner7e5bd592003-12-10 20:22:42 +000063 // Insert an unconditional branch to the normal destination.
Gabor Greife9ecc682008-04-06 20:25:17 +000064 BranchInst::Create(II->getNormalDest(), II);
Chris Lattnera43b8f42003-10-05 19:14:42 +000065
Chris Lattner7e5bd592003-12-10 20:22:42 +000066 // Remove any PHI node entries from the exception destination.
Benjamin Kramer135f7352016-06-26 12:28:59 +000067 II->getUnwindDest()->removePredecessor(&BB);
Chris Lattner7e5bd592003-12-10 20:22:42 +000068
Chris Lattnera43b8f42003-10-05 19:14:42 +000069 // Remove the invoke instruction now.
Benjamin Kramer135f7352016-06-26 12:28:59 +000070 BB.getInstList().erase(II);
Chris Lattnera43b8f42003-10-05 19:14:42 +000071
Michael Kuperstein31b83992016-08-12 17:28:27 +000072 ++NumInvokes;
73 Changed = true;
Chris Lattnera43b8f42003-10-05 19:14:42 +000074 }
75 return Changed;
76}
Michael Kuperstein31b83992016-08-12 17:28:27 +000077
78bool LowerInvokeLegacyPass::runOnFunction(Function &F) {
79 return runImpl(F);
80}
81
82namespace llvm {
83char &LowerInvokePassID = LowerInvokeLegacyPass::ID;
84
85// Public Interface To the LowerInvoke pass.
86FunctionPass *createLowerInvokePass() { return new LowerInvokeLegacyPass(); }
87
88PreservedAnalyses LowerInvokePass::run(Function &F,
89 FunctionAnalysisManager &AM) {
90 bool Changed = runImpl(F);
91 if (!Changed)
92 return PreservedAnalyses::all();
93
94 return PreservedAnalyses::none();
95}
96}