2014-03-20 19:54:47 +00:00
|
|
|
//===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-05 19:14:42 +00:00
|
|
|
//
|
|
|
|
// This transformation is designed for use by code generators which do not yet
|
2014-03-20 19:54:47 +00:00
|
|
|
// support stack unwinding. This pass converts 'invoke' instructions to 'call'
|
|
|
|
// instructions, so that any exception-handling 'landingpad' blocks become dead
|
|
|
|
// code (which can be removed by running the '-simplifycfg' pass afterwards).
|
2004-03-31 22:00:30 +00:00
|
|
|
//
|
2003-10-05 19:14:42 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2003-10-05 19:14:42 +00:00
|
|
|
#include "llvm/Pass.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2003-12-10 20:22:42 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "lowerinvoke"
|
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
STATISTIC(NumInvokes, "Number of invokes replaced");
|
2003-10-05 19:14:42 +00:00
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
class LowerInvoke : public FunctionPass {
|
2003-10-05 19:14:42 +00:00
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2014-03-20 19:54:47 +00:00
|
|
|
explicit LowerInvoke() : FunctionPass(ID) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeLowerInvokePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
2003-10-05 19:14:42 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char LowerInvoke::ID = 0;
|
2010-08-23 17:52:01 +00:00
|
|
|
INITIALIZE_PASS(LowerInvoke, "lowerinvoke",
|
|
|
|
"Lower invoke and unwind, for unwindless code generators",
|
2010-10-07 22:25:06 +00:00
|
|
|
false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2010-08-06 18:33:48 +00:00
|
|
|
char &llvm::LowerInvokePassID = LowerInvoke::ID;
|
2004-02-13 16:16:16 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
// Public Interface To the LowerInvoke pass.
|
2014-03-20 19:54:47 +00:00
|
|
|
FunctionPass *llvm::createLowerInvokePass() {
|
|
|
|
return new LowerInvoke();
|
2005-11-05 09:21:28 +00:00
|
|
|
}
|
2003-10-05 19:14:42 +00:00
|
|
|
|
2014-03-20 19:54:47 +00:00
|
|
|
bool LowerInvoke::runOnFunction(Function &F) {
|
2003-10-05 19:14:42 +00:00
|
|
|
bool Changed = false;
|
2003-12-10 20:22:42 +00:00
|
|
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
|
2010-06-01 17:56:41 +00:00
|
|
|
SmallVector<Value*,16> CallArgs(II->op_begin(), II->op_end() - 3);
|
2003-10-05 19:14:42 +00:00
|
|
|
// Insert a normal call instruction...
|
2008-04-06 20:25:17 +00:00
|
|
|
CallInst *NewCall = CallInst::Create(II->getCalledValue(),
|
2011-07-15 08:37:34 +00:00
|
|
|
CallArgs, "", II);
|
2007-02-11 01:37:51 +00:00
|
|
|
NewCall->takeName(II);
|
2005-05-13 06:27:02 +00:00
|
|
|
NewCall->setCallingConv(II->getCallingConv());
|
2008-09-25 21:00:45 +00:00
|
|
|
NewCall->setAttributes(II->getAttributes());
|
2010-10-18 18:53:44 +00:00
|
|
|
NewCall->setDebugLoc(II->getDebugLoc());
|
2003-10-05 19:14:42 +00:00
|
|
|
II->replaceAllUsesWith(NewCall);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-12-10 20:22:42 +00:00
|
|
|
// Insert an unconditional branch to the normal destination.
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(II->getNormalDest(), II);
|
2003-10-05 19:14:42 +00:00
|
|
|
|
2003-12-10 20:22:42 +00:00
|
|
|
// Remove any PHI node entries from the exception destination.
|
2004-02-08 21:44:31 +00:00
|
|
|
II->getUnwindDest()->removePredecessor(BB);
|
2003-12-10 20:22:42 +00:00
|
|
|
|
2003-10-05 19:14:42 +00:00
|
|
|
// Remove the invoke instruction now.
|
2003-12-10 20:22:42 +00:00
|
|
|
BB->getInstList().erase(II);
|
2003-10-05 19:14:42 +00:00
|
|
|
|
2005-09-27 21:18:17 +00:00
|
|
|
++NumInvokes; Changed = true;
|
2003-10-05 19:14:42 +00:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|