opnsense-src/contrib/llvm/lib/Transforms/Utils/LowerInvoke.cpp

95 lines
3.1 KiB
C++
Raw Normal View History

//===- LowerInvoke.cpp - Eliminate Invoke instructions --------------------===//
2009-06-02 13:52:33 -04:00
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This transformation is designed for use by code generators which do not yet
// 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).
2009-06-02 13:52:33 -04:00
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/LowerInvoke.h"
2010-07-13 13:19:57 -04:00
#include "llvm/ADT/SmallVector.h"
2009-06-02 13:52:33 -04:00
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Scalar.h"
2009-06-02 13:52:33 -04:00
using namespace llvm;
#define DEBUG_TYPE "lowerinvoke"
2009-06-02 13:52:33 -04:00
STATISTIC(NumInvokes, "Number of invokes replaced");
2009-06-02 13:52:33 -04:00
namespace {
class LowerInvokeLegacyPass : public FunctionPass {
2009-06-02 13:52:33 -04:00
public:
static char ID; // Pass identification, replacement for typeid
explicit LowerInvokeLegacyPass() : FunctionPass(ID) {
initializeLowerInvokeLegacyPassPass(*PassRegistry::getPassRegistry());
}
bool runOnFunction(Function &F) override;
2009-06-02 13:52:33 -04:00
};
}
char LowerInvokeLegacyPass::ID = 0;
INITIALIZE_PASS(LowerInvokeLegacyPass, "lowerinvoke",
"Lower invoke and unwind, for unwindless code generators",
false, false)
2009-06-02 13:52:33 -04:00
static bool runImpl(Function &F) {
2009-06-02 13:52:33 -04:00
bool Changed = false;
for (BasicBlock &BB : F)
if (InvokeInst *II = dyn_cast<InvokeInst>(BB.getTerminator())) {
SmallVector<Value *, 16> CallArgs(II->op_begin(), II->op_end() - 3);
2009-06-02 13:52:33 -04:00
// Insert a normal call instruction...
CallInst *NewCall =
CallInst::Create(II->getCalledValue(), CallArgs, "", II);
2009-06-02 13:52:33 -04:00
NewCall->takeName(II);
NewCall->setCallingConv(II->getCallingConv());
NewCall->setAttributes(II->getAttributes());
NewCall->setDebugLoc(II->getDebugLoc());
2009-06-02 13:52:33 -04:00
II->replaceAllUsesWith(NewCall);
// Insert an unconditional branch to the normal destination.
BranchInst::Create(II->getNormalDest(), II);
// Remove any PHI node entries from the exception destination.
II->getUnwindDest()->removePredecessor(&BB);
2009-06-02 13:52:33 -04:00
// Remove the invoke instruction now.
BB.getInstList().erase(II);
2009-06-02 13:52:33 -04:00
++NumInvokes;
Changed = true;
2009-06-02 13:52:33 -04:00
}
return Changed;
}
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();
}
}