blob: b1f758ecdadcd5222df1ababab3cc71cbe181f5f [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
Chris Lattnerd216e8b2006-12-19 22:17:40 +000017#define DEBUG_TYPE "lowerinvoke"
Chris Lattner86e44452003-10-05 19:14:42 +000018#include "llvm/Transforms/Scalar.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/Statistic.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Instructions.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/LLVMContext.h"
23#include "llvm/IR/Module.h"
Chris Lattner86e44452003-10-05 19:14:42 +000024#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Chris Lattnerdead9932003-12-10 20:22:42 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnerd216e8b2006-12-19 22:17:40 +000028STATISTIC(NumInvokes, "Number of invokes replaced");
Chris Lattner86e44452003-10-05 19:14:42 +000029
Chris Lattnerd216e8b2006-12-19 22:17:40 +000030namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000031 class LowerInvoke : public FunctionPass {
Chris Lattner86e44452003-10-05 19:14:42 +000032 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000033 static char ID; // Pass identification, replacement for typeid
Stephen Hines36b56882014-04-23 16:57:46 -070034 explicit LowerInvoke() : FunctionPass(ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +000035 initializeLowerInvokePass(*PassRegistry::getPassRegistry());
36 }
Stephen Hines36b56882014-04-23 16:57:46 -070037 bool runOnFunction(Function &F) override;
Chris Lattner86e44452003-10-05 19:14:42 +000038 };
Chris Lattner86e44452003-10-05 19:14:42 +000039}
40
Dan Gohman844731a2008-05-13 00:00:25 +000041char LowerInvoke::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +000042INITIALIZE_PASS(LowerInvoke, "lowerinvoke",
43 "Lower invoke and unwind, for unwindless code generators",
Owen Andersonce665bd2010-10-07 22:25:06 +000044 false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000045
Owen Anderson90c579d2010-08-06 18:33:48 +000046char &llvm::LowerInvokePassID = LowerInvoke::ID;
Chris Lattnercefc18e2004-02-13 16:16:16 +000047
Brian Gaeked0fde302003-11-11 22:41:34 +000048// Public Interface To the LowerInvoke pass.
Stephen Hines36b56882014-04-23 16:57:46 -070049FunctionPass *llvm::createLowerInvokePass() {
50 return new LowerInvoke();
Nate Begeman14b05292005-11-05 09:21:28 +000051}
Chris Lattner86e44452003-10-05 19:14:42 +000052
Stephen Hines36b56882014-04-23 16:57:46 -070053bool LowerInvoke::runOnFunction(Function &F) {
Chris Lattner86e44452003-10-05 19:14:42 +000054 bool Changed = false;
Chris Lattnerdead9932003-12-10 20:22:42 +000055 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
56 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
Jim Grosbach601f9d62010-06-01 17:56:41 +000057 SmallVector<Value*,16> CallArgs(II->op_begin(), II->op_end() - 3);
Chris Lattner86e44452003-10-05 19:14:42 +000058 // Insert a normal call instruction...
Gabor Greif051a9502008-04-06 20:25:17 +000059 CallInst *NewCall = CallInst::Create(II->getCalledValue(),
Jay Foada3efbb12011-07-15 08:37:34 +000060 CallArgs, "", II);
Chris Lattner86cc4232007-02-11 01:37:51 +000061 NewCall->takeName(II);
Chris Lattnerefd91682005-05-13 06:27:02 +000062 NewCall->setCallingConv(II->getCallingConv());
Devang Patel05988662008-09-25 21:00:45 +000063 NewCall->setAttributes(II->getAttributes());
Devang Patel3bcb3a62010-10-18 18:53:44 +000064 NewCall->setDebugLoc(II->getDebugLoc());
Chris Lattner86e44452003-10-05 19:14:42 +000065 II->replaceAllUsesWith(NewCall);
Misha Brukmanfd939082005-04-21 23:48:37 +000066
Chris Lattnerdead9932003-12-10 20:22:42 +000067 // Insert an unconditional branch to the normal destination.
Gabor Greif051a9502008-04-06 20:25:17 +000068 BranchInst::Create(II->getNormalDest(), II);
Chris Lattner86e44452003-10-05 19:14:42 +000069
Chris Lattnerdead9932003-12-10 20:22:42 +000070 // Remove any PHI node entries from the exception destination.
Chris Lattneraeb2a1d2004-02-08 21:44:31 +000071 II->getUnwindDest()->removePredecessor(BB);
Chris Lattnerdead9932003-12-10 20:22:42 +000072
Chris Lattner86e44452003-10-05 19:14:42 +000073 // Remove the invoke instruction now.
Chris Lattnerdead9932003-12-10 20:22:42 +000074 BB->getInstList().erase(II);
Chris Lattner86e44452003-10-05 19:14:42 +000075
Chris Lattnerf4e6c3a2005-09-27 21:18:17 +000076 ++NumInvokes; Changed = true;
Chris Lattner86e44452003-10-05 19:14:42 +000077 }
78 return Changed;
79}