blob: 66d57b069fe7196f2846f9539a9cf4f8941e7330 [file] [log] [blame]
Stephen Hines36b56882014-04-23 16:57:46 -07001//===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner86e44452003-10-05 19:14:42 +00009//
10// This transformation is designed for use by code generators which do not yet
Stephen Hines36b56882014-04-23 16:57:46 -070011// 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 Lattner0e28eca2004-03-31 22:00:30 +000014//
Chris Lattner86e44452003-10-05 19:14:42 +000015//===----------------------------------------------------------------------===//
16
17#include "llvm/Transforms/Scalar.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Statistic.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Instructions.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/LLVMContext.h"
22#include "llvm/IR/Module.h"
Chris Lattner86e44452003-10-05 19:14:42 +000023#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattnerdead9932003-12-10 20:22:42 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Stephen Hinesdce4a402014-05-29 02:49:00 -070027#define DEBUG_TYPE "lowerinvoke"
28
Chris Lattnerd216e8b2006-12-19 22:17:40 +000029STATISTIC(NumInvokes, "Number of invokes replaced");
Chris Lattner86e44452003-10-05 19:14:42 +000030
Chris Lattnerd216e8b2006-12-19 22:17:40 +000031namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000032 class LowerInvoke : public FunctionPass {
Chris Lattner86e44452003-10-05 19:14:42 +000033 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000034 static char ID; // Pass identification, replacement for typeid
Stephen Hines36b56882014-04-23 16:57:46 -070035 explicit LowerInvoke() : FunctionPass(ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +000036 initializeLowerInvokePass(*PassRegistry::getPassRegistry());
37 }
Stephen Hines36b56882014-04-23 16:57:46 -070038 bool runOnFunction(Function &F) override;
Chris Lattner86e44452003-10-05 19:14:42 +000039 };
Chris Lattner86e44452003-10-05 19:14:42 +000040}
41
Dan Gohman844731a2008-05-13 00:00:25 +000042char LowerInvoke::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +000043INITIALIZE_PASS(LowerInvoke, "lowerinvoke",
44 "Lower invoke and unwind, for unwindless code generators",
Owen Andersonce665bd2010-10-07 22:25:06 +000045 false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000046
Owen Anderson90c579d2010-08-06 18:33:48 +000047char &llvm::LowerInvokePassID = LowerInvoke::ID;
Chris Lattnercefc18e2004-02-13 16:16:16 +000048
Brian Gaeked0fde302003-11-11 22:41:34 +000049// Public Interface To the LowerInvoke pass.
Stephen Hines36b56882014-04-23 16:57:46 -070050FunctionPass *llvm::createLowerInvokePass() {
51 return new LowerInvoke();
Nate Begeman14b05292005-11-05 09:21:28 +000052}
Chris Lattner86e44452003-10-05 19:14:42 +000053
Stephen Hines36b56882014-04-23 16:57:46 -070054bool LowerInvoke::runOnFunction(Function &F) {
Chris Lattner86e44452003-10-05 19:14:42 +000055 bool Changed = false;
Chris Lattnerdead9932003-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 Grosbach601f9d62010-06-01 17:56:41 +000058 SmallVector<Value*,16> CallArgs(II->op_begin(), II->op_end() - 3);
Chris Lattner86e44452003-10-05 19:14:42 +000059 // Insert a normal call instruction...
Gabor Greif051a9502008-04-06 20:25:17 +000060 CallInst *NewCall = CallInst::Create(II->getCalledValue(),
Jay Foada3efbb12011-07-15 08:37:34 +000061 CallArgs, "", II);
Chris Lattner86cc4232007-02-11 01:37:51 +000062 NewCall->takeName(II);
Chris Lattnerefd91682005-05-13 06:27:02 +000063 NewCall->setCallingConv(II->getCallingConv());
Devang Patel05988662008-09-25 21:00:45 +000064 NewCall->setAttributes(II->getAttributes());
Devang Patel3bcb3a62010-10-18 18:53:44 +000065 NewCall->setDebugLoc(II->getDebugLoc());
Chris Lattner86e44452003-10-05 19:14:42 +000066 II->replaceAllUsesWith(NewCall);
Misha Brukmanfd939082005-04-21 23:48:37 +000067
Chris Lattnerdead9932003-12-10 20:22:42 +000068 // Insert an unconditional branch to the normal destination.
Gabor Greif051a9502008-04-06 20:25:17 +000069 BranchInst::Create(II->getNormalDest(), II);
Chris Lattner86e44452003-10-05 19:14:42 +000070
Chris Lattnerdead9932003-12-10 20:22:42 +000071 // Remove any PHI node entries from the exception destination.
Chris Lattneraeb2a1d2004-02-08 21:44:31 +000072 II->getUnwindDest()->removePredecessor(BB);
Chris Lattnerdead9932003-12-10 20:22:42 +000073
Chris Lattner86e44452003-10-05 19:14:42 +000074 // Remove the invoke instruction now.
Chris Lattnerdead9932003-12-10 20:22:42 +000075 BB->getInstList().erase(II);
Chris Lattner86e44452003-10-05 19:14:42 +000076
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +000077 ++NumInvokes; Changed = true;
Chris Lattner86e44452003-10-05 19:14:42 +000078 }
79 return Changed;
80}