2003-10-05 19:14:42 +00:00
|
|
|
//===- LowerInvoke.cpp - Eliminate Invoke & Unwind instructions -----------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-05 19:14:42 +00:00
|
|
|
//
|
|
|
|
// This transformation is designed for use by code generators which do not yet
|
|
|
|
// support stack unwinding. This pass gives them the ability to execute any
|
|
|
|
// program which does not throw an exception, by turning 'invoke' instructions
|
|
|
|
// into calls and by turning 'unwind' instructions into calls to abort().
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/iTerminators.h"
|
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/Constant.h"
|
|
|
|
#include "Support/Statistic.h"
|
2003-12-10 20:22:42 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-10-05 19:14:42 +00:00
|
|
|
namespace {
|
|
|
|
Statistic<> NumLowered("lowerinvoke", "Number of invoke & unwinds replaced");
|
|
|
|
|
|
|
|
class LowerInvoke : public FunctionPass {
|
|
|
|
Function *AbortFn;
|
|
|
|
public:
|
|
|
|
bool doInitialization(Module &M);
|
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
};
|
|
|
|
|
|
|
|
RegisterOpt<LowerInvoke>
|
|
|
|
X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators");
|
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
// Public Interface To the LowerInvoke pass.
|
2003-12-10 20:22:42 +00:00
|
|
|
FunctionPass *llvm::createLowerInvokePass() { return new LowerInvoke(); }
|
2003-10-05 19:14:42 +00:00
|
|
|
|
|
|
|
// doInitialization - Make sure that there is a prototype for abort in the
|
|
|
|
// current module.
|
|
|
|
bool LowerInvoke::doInitialization(Module &M) {
|
|
|
|
AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LowerInvoke::runOnFunction(Function &F) {
|
|
|
|
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())) {
|
2003-10-05 19:14:42 +00:00
|
|
|
// Insert a normal call instruction...
|
|
|
|
std::string Name = II->getName(); II->setName("");
|
|
|
|
Value *NewCall = new CallInst(II->getCalledValue(),
|
|
|
|
std::vector<Value*>(II->op_begin()+3,
|
|
|
|
II->op_end()), Name,II);
|
|
|
|
II->replaceAllUsesWith(NewCall);
|
|
|
|
|
2003-12-10 20:22:42 +00:00
|
|
|
// Insert an unconditional branch to the normal destination.
|
2003-10-05 19:14:42 +00:00
|
|
|
new BranchInst(II->getNormalDest(), II);
|
|
|
|
|
2003-12-10 20:22:42 +00:00
|
|
|
// Remove any PHI node entries from the exception destination.
|
|
|
|
II->getExceptionalDest()->removePredecessor(BB);
|
|
|
|
|
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
|
|
|
|
|
|
|
++NumLowered; Changed = true;
|
2003-12-10 20:22:42 +00:00
|
|
|
} else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
|
2003-10-05 19:14:42 +00:00
|
|
|
// Insert a call to abort()
|
|
|
|
new CallInst(AbortFn, std::vector<Value*>(), "", UI);
|
|
|
|
|
|
|
|
// Insert a return instruction.
|
|
|
|
new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 :
|
|
|
|
Constant::getNullValue(F.getReturnType()), UI);
|
|
|
|
|
|
|
|
// Remove the unwind instruction now.
|
2003-12-10 20:22:42 +00:00
|
|
|
BB->getInstList().erase(UI);
|
2003-10-05 19:14:42 +00:00
|
|
|
|
|
|
|
++NumLowered; Changed = true;
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|