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