blob: ee84541e526db38deb5d0cf2dd70230c12faa756 [file] [log] [blame]
Mark Seabornb6118c52014-03-20 19:54:47 +00001//===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnera43b8f42003-10-05 19:14:42 +00009//
10// This transformation is designed for use by code generators which do not yet
Mark Seabornb6118c52014-03-20 19:54:47 +000011// support stack unwinding. This pass converts 'invoke' instructions to 'call'
12// instructions, so that any exception-handling 'landingpad' blocks become dead
13// code (which can be removed by running the '-simplifycfg' pass afterwards).
Chris Lattner61fab142004-03-31 22:00:30 +000014//
Chris Lattnera43b8f42003-10-05 19:14:42 +000015//===----------------------------------------------------------------------===//
16
Michael Kuperstein31b83992016-08-12 17:28:27 +000017#include "llvm/Transforms/Utils/LowerInvoke.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/LLVMContext.h"
22#include "llvm/IR/Module.h"
Chris Lattnera43b8f42003-10-05 19:14:42 +000023#include "llvm/Pass.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattner7e5bd592003-12-10 20:22:42 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chandler Carruth964daaa2014-04-22 02:55:47 +000027#define DEBUG_TYPE "lowerinvoke"
28
Chris Lattner45f966d2006-12-19 22:17:40 +000029STATISTIC(NumInvokes, "Number of invokes replaced");
Chris Lattnera43b8f42003-10-05 19:14:42 +000030
Chris Lattner45f966d2006-12-19 22:17:40 +000031namespace {
Michael Kuperstein31b83992016-08-12 17:28:27 +000032 class LowerInvokeLegacyPass : public FunctionPass {
Chris Lattnera43b8f42003-10-05 19:14:42 +000033 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000034 static char ID; // Pass identification, replacement for typeid
Michael Kuperstein31b83992016-08-12 17:28:27 +000035 explicit LowerInvokeLegacyPass() : FunctionPass(ID) {
36 initializeLowerInvokeLegacyPassPass(*PassRegistry::getPassRegistry());
Owen Anderson6c18d1a2010-10-19 17:21:58 +000037 }
Craig Topper3e4c6972014-03-05 09:10:37 +000038 bool runOnFunction(Function &F) override;
Chris Lattnera43b8f42003-10-05 19:14:42 +000039 };
Chris Lattnera43b8f42003-10-05 19:14:42 +000040}
41
Michael Kuperstein31b83992016-08-12 17:28:27 +000042char LowerInvokeLegacyPass::ID = 0;
43INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke",
Owen Andersond31d82d2010-08-23 17:52:01 +000044 "Lower invoke and unwind, for unwindless code generators",
Owen Andersondf7a4f22010-10-07 22:25:06 +000045 false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000046
Michael Kuperstein31b83992016-08-12 17:28:27 +000047static bool runImpl(Function &F) {
Chris Lattnera43b8f42003-10-05 19:14:42 +000048 bool Changed = false;
Benjamin Kramer135f7352016-06-26 12:28:59 +000049 for (BasicBlock &BB : F)
50 if (InvokeInst *II = dyn_cast<InvokeInst>(BB.getTerminator())) {
Michael Kuperstein31b83992016-08-12 17:28:27 +000051 SmallVector<Value *, 16> CallArgs(II->op_begin(), II->op_end() - 3);
Chris Lattnera43b8f42003-10-05 19:14:42 +000052 // Insert a normal call instruction...
Michael Kuperstein31b83992016-08-12 17:28:27 +000053 CallInst *NewCall =
54 CallInst::Create(II->getCalledValue(), CallArgs, "", II);
Chris Lattner8dd4cae2007-02-11 01:37:51 +000055 NewCall->takeName(II);
Chris Lattnerca968392005-05-13 06:27:02 +000056 NewCall->setCallingConv(II->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +000057 NewCall->setAttributes(II->getAttributes());
Devang Patel218f3202010-10-18 18:53:44 +000058 NewCall->setDebugLoc(II->getDebugLoc());
Chris Lattnera43b8f42003-10-05 19:14:42 +000059 II->replaceAllUsesWith(NewCall);
Misha Brukmanb1c93172005-04-21 23:48:37 +000060
Chris Lattner7e5bd592003-12-10 20:22:42 +000061 // Insert an unconditional branch to the normal destination.
Gabor Greife9ecc682008-04-06 20:25:17 +000062 BranchInst::Create(II->getNormalDest(), II);
Chris Lattnera43b8f42003-10-05 19:14:42 +000063
Chris Lattner7e5bd592003-12-10 20:22:42 +000064 // Remove any PHI node entries from the exception destination.
Benjamin Kramer135f7352016-06-26 12:28:59 +000065 II->getUnwindDest()->removePredecessor(&BB);
Chris Lattner7e5bd592003-12-10 20:22:42 +000066
Chris Lattnera43b8f42003-10-05 19:14:42 +000067 // Remove the invoke instruction now.
Benjamin Kramer135f7352016-06-26 12:28:59 +000068 BB.getInstList().erase(II);
Chris Lattnera43b8f42003-10-05 19:14:42 +000069
Michael Kuperstein31b83992016-08-12 17:28:27 +000070 ++NumInvokes;
71 Changed = true;
Chris Lattnera43b8f42003-10-05 19:14:42 +000072 }
73 return Changed;
74}
Michael Kuperstein31b83992016-08-12 17:28:27 +000075
76bool LowerInvokeLegacyPass::runOnFunction(Function &F) {
77 return runImpl(F);
78}
79
80namespace llvm {
81char &LowerInvokePassID = LowerInvokeLegacyPass::ID;
82
83// Public Interface To the LowerInvoke pass.
84FunctionPass *createLowerInvokePass() { return new LowerInvokeLegacyPass(); }
85
86PreservedAnalyses LowerInvokePass::run(Function &F,
87 FunctionAnalysisManager &AM) {
88 bool Changed = runImpl(F);
89 if (!Changed)
90 return PreservedAnalyses::all();
91
92 return PreservedAnalyses::none();
93}
94}