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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-08-13 01:28:27 +08:00
|
|
|
#include "llvm/Transforms/Utils/LowerInvoke.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"
|
2016-04-18 17:17:29 +08:00
|
|
|
#include "llvm/Transforms/Scalar.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 {
|
2016-08-13 01:28:27 +08:00
|
|
|
class LowerInvokeLegacyPass : 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
|
2016-08-13 01:28:27 +08:00
|
|
|
explicit LowerInvokeLegacyPass() : FunctionPass(ID) {
|
|
|
|
initializeLowerInvokeLegacyPassPass(*PassRegistry::getPassRegistry());
|
2010-10-20 01:21:58 +08:00
|
|
|
}
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnFunction(Function &F) override;
|
2003-10-06 03:14:42 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:28:27 +08:00
|
|
|
char LowerInvokeLegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke",
|
2010-08-24 01:52:01 +08:00
|
|
|
"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
|
|
|
|
2016-08-13 01:28:27 +08:00
|
|
|
static bool runImpl(Function &F) {
|
2003-10-06 03:14:42 +08:00
|
|
|
bool Changed = false;
|
2016-06-26 20:28:59 +08:00
|
|
|
for (BasicBlock &BB : F)
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(BB.getTerminator())) {
|
2016-08-13 01:28:27 +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...
|
2016-08-13 01:28:27 +08:00
|
|
|
CallInst *NewCall =
|
|
|
|
CallInst::Create(II->getCalledValue(), 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.
|
2016-06-26 20:28:59 +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.
|
2016-06-26 20:28:59 +08:00
|
|
|
BB.getInstList().erase(II);
|
2003-10-06 03:14:42 +08:00
|
|
|
|
2016-08-13 01:28:27 +08:00
|
|
|
++NumInvokes;
|
|
|
|
Changed = true;
|
2003-10-06 03:14:42 +08:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
2016-08-13 01:28:27 +08:00
|
|
|
|
|
|
|
bool LowerInvokeLegacyPass::runOnFunction(Function &F) {
|
|
|
|
return runImpl(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
char &LowerInvokePassID = LowerInvokeLegacyPass::ID;
|
|
|
|
|
|
|
|
// Public Interface To the LowerInvoke pass.
|
|
|
|
FunctionPass *createLowerInvokePass() { return new LowerInvokeLegacyPass(); }
|
|
|
|
|
|
|
|
PreservedAnalyses LowerInvokePass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
|
|
|
bool Changed = runImpl(F);
|
|
|
|
if (!Changed)
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
}
|
|
|
|
}
|