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