blob: 1b31c5ae580a18b5f492297d7bf46153241b2295 [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
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"
Mehdi Aminib550cb12016-04-18 09:17:29 +000023#include "llvm/Transforms/Scalar.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 {
Nick Lewycky02d5f772009-10-25 06:33:48 +000031 class LowerInvoke : 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
Mark Seabornb6118c52014-03-20 19:54:47 +000034 explicit LowerInvoke() : FunctionPass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000035 initializeLowerInvokePass(*PassRegistry::getPassRegistry());
36 }
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
Dan Gohmand78c4002008-05-13 00:00:25 +000041char LowerInvoke::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +000042INITIALIZE_PASS(LowerInvoke, "lowerinvoke",
43 "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
Owen Andersona7aed182010-08-06 18:33:48 +000046char &llvm::LowerInvokePassID = LowerInvoke::ID;
Chris Lattner7cbb22a2004-02-13 16:16:16 +000047
Brian Gaeke960707c2003-11-11 22:41:34 +000048// Public Interface To the LowerInvoke pass.
Mark Seabornb6118c52014-03-20 19:54:47 +000049FunctionPass *llvm::createLowerInvokePass() {
50 return new LowerInvoke();
Nate Begeman848622f2005-11-05 09:21:28 +000051}
Chris Lattnera43b8f42003-10-05 19:14:42 +000052
Mark Seabornb6118c52014-03-20 19:54:47 +000053bool LowerInvoke::runOnFunction(Function &F) {
Chris Lattnera43b8f42003-10-05 19:14:42 +000054 bool Changed = false;
Benjamin Kramer135f7352016-06-26 12:28:59 +000055 for (BasicBlock &BB : F)
56 if (InvokeInst *II = dyn_cast<InvokeInst>(BB.getTerminator())) {
Jim Grosbach73521672010-06-01 17:56:41 +000057 SmallVector<Value*,16> CallArgs(II->op_begin(), II->op_end() - 3);
Chris Lattnera43b8f42003-10-05 19:14:42 +000058 // Insert a normal call instruction...
Gabor Greife9ecc682008-04-06 20:25:17 +000059 CallInst *NewCall = CallInst::Create(II->getCalledValue(),
Jay Foad5bd375a2011-07-15 08:37:34 +000060 CallArgs, "", II);
Chris Lattner8dd4cae2007-02-11 01:37:51 +000061 NewCall->takeName(II);
Chris Lattnerca968392005-05-13 06:27:02 +000062 NewCall->setCallingConv(II->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +000063 NewCall->setAttributes(II->getAttributes());
Devang Patel218f3202010-10-18 18:53:44 +000064 NewCall->setDebugLoc(II->getDebugLoc());
Chris Lattnera43b8f42003-10-05 19:14:42 +000065 II->replaceAllUsesWith(NewCall);
Misha Brukmanb1c93172005-04-21 23:48:37 +000066
Chris Lattner7e5bd592003-12-10 20:22:42 +000067 // Insert an unconditional branch to the normal destination.
Gabor Greife9ecc682008-04-06 20:25:17 +000068 BranchInst::Create(II->getNormalDest(), II);
Chris Lattnera43b8f42003-10-05 19:14:42 +000069
Chris Lattner7e5bd592003-12-10 20:22:42 +000070 // Remove any PHI node entries from the exception destination.
Benjamin Kramer135f7352016-06-26 12:28:59 +000071 II->getUnwindDest()->removePredecessor(&BB);
Chris Lattner7e5bd592003-12-10 20:22:42 +000072
Chris Lattnera43b8f42003-10-05 19:14:42 +000073 // Remove the invoke instruction now.
Benjamin Kramer135f7352016-06-26 12:28:59 +000074 BB.getInstList().erase(II);
Chris Lattnera43b8f42003-10-05 19:14:42 +000075
Chris Lattner87eb2492005-09-27 21:18:17 +000076 ++NumInvokes; Changed = true;
Chris Lattnera43b8f42003-10-05 19:14:42 +000077 }
78 return Changed;
79}