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