2003-10-05 19:14:42 +00:00
|
|
|
//===- LowerInvoke.cpp - Eliminate Invoke & Unwind instructions -----------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
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.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-05 19:14:42 +00:00
|
|
|
//
|
|
|
|
// This transformation is designed for use by code generators which do not yet
|
2004-02-08 19:53:56 +00:00
|
|
|
// support stack unwinding. This pass supports two models of exception handling
|
|
|
|
// lowering, the 'cheap' support and the 'expensive' support.
|
|
|
|
//
|
|
|
|
// 'Cheap' exception handling support gives the program 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(). If the program does dynamically use the unwind instruction, the
|
|
|
|
// program will print a message then abort.
|
|
|
|
//
|
|
|
|
// 'Expensive' exception handling support gives the full exception handling
|
2005-05-02 14:47:42 +00:00
|
|
|
// support to the program at the cost of making the 'invoke' instruction
|
|
|
|
// really expensive. It basically inserts setjmp/longjmp calls to emulate the
|
|
|
|
// exception handling as necessary.
|
2004-02-08 19:53:56 +00:00
|
|
|
//
|
|
|
|
// Because the 'expensive' support slows down programs a lot, and EH is only
|
|
|
|
// used for a subset of the programs, it must be specifically enabled by an
|
|
|
|
// option.
|
2003-10-05 19:14:42 +00:00
|
|
|
//
|
2004-03-31 22:00:30 +00:00
|
|
|
// Note that after this pass runs the CFG is not entirely accurate (exceptional
|
|
|
|
// control flow edges are not correct anymore) so only very simple things should
|
|
|
|
// be done after the lowerinvoke pass has run (like generation of native code).
|
|
|
|
// This should not be used as a general purpose "my LLVM-to-LLVM pass doesn't
|
|
|
|
// support the invoke instruction yet" lowering pass.
|
|
|
|
//
|
2003-10-05 19:14:42 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-02-08 07:30:29 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-02-08 19:53:56 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2004-02-08 07:30:29 +00:00
|
|
|
#include "llvm/Module.h"
|
2003-10-05 19:14:42 +00:00
|
|
|
#include "llvm/Pass.h"
|
2004-02-08 19:53:56 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-02-08 19:53:56 +00:00
|
|
|
#include <csetjmp>
|
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");
|
2004-03-01 01:12:13 +00:00
|
|
|
cl::opt<bool> ExpensiveEHSupport("enable-correct-eh-support",
|
2004-02-08 19:53:56 +00:00
|
|
|
cl::desc("Make the -lowerinvoke pass insert expensive, but correct, EH code"));
|
2003-10-05 19:14:42 +00:00
|
|
|
|
|
|
|
class LowerInvoke : public FunctionPass {
|
2004-02-08 19:53:56 +00:00
|
|
|
// Used for both models.
|
2004-02-08 07:30:29 +00:00
|
|
|
Function *WriteFn;
|
2003-10-05 19:14:42 +00:00
|
|
|
Function *AbortFn;
|
2004-02-08 19:53:56 +00:00
|
|
|
Value *AbortMessage;
|
|
|
|
unsigned AbortMessageLength;
|
|
|
|
|
|
|
|
// Used for expensive EH support.
|
|
|
|
const Type *JBLinkTy;
|
|
|
|
GlobalVariable *JBListHead;
|
|
|
|
Function *SetJmpFn, *LongJmpFn;
|
2003-10-05 19:14:42 +00:00
|
|
|
public:
|
|
|
|
bool doInitialization(Module &M);
|
|
|
|
bool runOnFunction(Function &F);
|
2004-02-08 19:53:56 +00:00
|
|
|
private:
|
2004-11-13 19:07:32 +00:00
|
|
|
void createAbortMessage();
|
2004-02-08 22:14:44 +00:00
|
|
|
void writeAbortMessage(Instruction *IB);
|
2004-02-08 19:53:56 +00:00
|
|
|
bool insertCheapEHSupport(Function &F);
|
|
|
|
bool insertExpensiveEHSupport(Function &F);
|
2003-10-05 19:14:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RegisterOpt<LowerInvoke>
|
|
|
|
X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators");
|
|
|
|
}
|
|
|
|
|
2004-02-13 16:16:16 +00:00
|
|
|
const PassInfo *llvm::LowerInvokePassID = X.getPassInfo();
|
|
|
|
|
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) {
|
2004-02-08 19:53:56 +00:00
|
|
|
const Type *VoidPtrTy = PointerType::get(Type::SByteTy);
|
2004-02-09 22:48:47 +00:00
|
|
|
AbortMessage = 0;
|
2004-02-08 19:53:56 +00:00
|
|
|
if (ExpensiveEHSupport) {
|
|
|
|
// Insert a type for the linked list of jump buffers. Unfortunately, we
|
|
|
|
// don't know the size of the target's setjmp buffer, so we make a guess.
|
|
|
|
// If this guess turns out to be too small, bad stuff could happen.
|
|
|
|
unsigned JmpBufSize = 200; // PPC has 192 words
|
|
|
|
assert(sizeof(jmp_buf) <= JmpBufSize*sizeof(void*) &&
|
|
|
|
"LowerInvoke doesn't know about targets with jmp_buf size > 200 words!");
|
|
|
|
const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JmpBufSize);
|
|
|
|
|
|
|
|
{ // The type is recursive, so use a type holder.
|
|
|
|
std::vector<const Type*> Elements;
|
|
|
|
OpaqueType *OT = OpaqueType::get();
|
|
|
|
Elements.push_back(PointerType::get(OT));
|
|
|
|
Elements.push_back(JmpBufTy);
|
|
|
|
PATypeHolder JBLType(StructType::get(Elements));
|
|
|
|
OT->refineAbstractTypeTo(JBLType.get()); // Complete the cycle.
|
|
|
|
JBLinkTy = JBLType.get();
|
2004-05-28 05:02:13 +00:00
|
|
|
M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);
|
2004-02-08 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Type *PtrJBList = PointerType::get(JBLinkTy);
|
|
|
|
|
|
|
|
// Now that we've done that, insert the jmpbuf list head global, unless it
|
|
|
|
// already exists.
|
|
|
|
if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList)))
|
|
|
|
JBListHead = new GlobalVariable(PtrJBList, false,
|
|
|
|
GlobalValue::LinkOnceLinkage,
|
|
|
|
Constant::getNullValue(PtrJBList),
|
|
|
|
"llvm.sjljeh.jblist", &M);
|
2004-02-15 22:24:27 +00:00
|
|
|
SetJmpFn = M.getOrInsertFunction("llvm.setjmp", Type::IntTy,
|
2005-06-09 03:32:54 +00:00
|
|
|
PointerType::get(JmpBufTy), NULL);
|
2004-02-15 22:24:27 +00:00
|
|
|
LongJmpFn = M.getOrInsertFunction("llvm.longjmp", Type::VoidTy,
|
2004-02-08 19:53:56 +00:00
|
|
|
PointerType::get(JmpBufTy),
|
2005-06-09 03:32:54 +00:00
|
|
|
Type::IntTy, NULL);
|
2004-11-13 19:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need the 'write' and 'abort' functions for both models.
|
2005-06-09 03:32:54 +00:00
|
|
|
AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, NULL);
|
2004-11-12 22:42:57 +00:00
|
|
|
|
2004-11-13 19:07:32 +00:00
|
|
|
// Unfortunately, 'write' can end up being prototyped in several different
|
|
|
|
// ways. If the user defines a three (or more) operand function named 'write'
|
|
|
|
// we will use their prototype. We _do not_ want to insert another instance
|
|
|
|
// of a write prototype, because we don't know that the funcresolve pass will
|
|
|
|
// run after us. If there is a definition of a write function, but it's not
|
|
|
|
// suitable for our uses, we just don't emit write calls. If there is no
|
|
|
|
// write prototype at all, we just add one.
|
|
|
|
if (Function *WF = M.getNamedFunction("write")) {
|
|
|
|
if (WF->getFunctionType()->getNumParams() > 3 ||
|
|
|
|
WF->getFunctionType()->isVarArg())
|
|
|
|
WriteFn = WF;
|
|
|
|
else
|
|
|
|
WriteFn = 0;
|
|
|
|
} else {
|
|
|
|
WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::IntTy,
|
2005-06-09 03:32:54 +00:00
|
|
|
VoidPtrTy, Type::IntTy, NULL);
|
2004-11-13 19:07:32 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LowerInvoke::createAbortMessage() {
|
|
|
|
Module &M = *WriteFn->getParent();
|
|
|
|
if (ExpensiveEHSupport) {
|
2004-02-08 19:53:56 +00:00
|
|
|
// The abort message for expensive EH support tells the user that the
|
|
|
|
// program 'unwound' without an 'invoke' instruction.
|
|
|
|
Constant *Msg =
|
|
|
|
ConstantArray::get("ERROR: Exception thrown, but not caught!\n");
|
|
|
|
AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-11-12 22:42:57 +00:00
|
|
|
GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
|
|
|
|
GlobalValue::InternalLinkage,
|
|
|
|
Msg, "abortmsg", &M);
|
2005-05-13 06:10:12 +00:00
|
|
|
std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy));
|
2004-11-12 22:42:57 +00:00
|
|
|
AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
|
2004-02-08 19:53:56 +00:00
|
|
|
} else {
|
|
|
|
// The abort message for cheap EH support tells the user that EH is not
|
|
|
|
// enabled.
|
|
|
|
Constant *Msg =
|
|
|
|
ConstantArray::get("Exception handler needed, but not enabled. Recompile"
|
|
|
|
" program with -enable-correct-eh-support.\n");
|
|
|
|
AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
|
2004-11-12 22:42:57 +00:00
|
|
|
|
|
|
|
GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
|
|
|
|
GlobalValue::InternalLinkage,
|
|
|
|
Msg, "abortmsg", &M);
|
2005-05-13 06:10:12 +00:00
|
|
|
std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::IntTy));
|
2004-11-12 22:42:57 +00:00
|
|
|
AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, GEPIdx);
|
2004-02-08 19:53:56 +00:00
|
|
|
}
|
2003-10-05 19:14:42 +00:00
|
|
|
}
|
|
|
|
|
2004-11-13 19:07:32 +00:00
|
|
|
|
2004-02-08 22:14:44 +00:00
|
|
|
void LowerInvoke::writeAbortMessage(Instruction *IB) {
|
|
|
|
if (WriteFn) {
|
2004-11-13 19:07:32 +00:00
|
|
|
if (AbortMessage == 0) createAbortMessage();
|
|
|
|
|
2004-02-08 22:14:44 +00:00
|
|
|
// These are the arguments we WANT...
|
|
|
|
std::vector<Value*> Args;
|
|
|
|
Args.push_back(ConstantInt::get(Type::IntTy, 2));
|
|
|
|
Args.push_back(AbortMessage);
|
|
|
|
Args.push_back(ConstantInt::get(Type::IntTy, AbortMessageLength));
|
|
|
|
|
|
|
|
// If the actual declaration of write disagrees, insert casts as
|
|
|
|
// appropriate.
|
|
|
|
const FunctionType *FT = WriteFn->getFunctionType();
|
|
|
|
unsigned NumArgs = FT->getNumParams();
|
|
|
|
for (unsigned i = 0; i != 3; ++i)
|
|
|
|
if (i < NumArgs && FT->getParamType(i) != Args[i]->getType())
|
2005-04-21 23:48:37 +00:00
|
|
|
Args[i] = ConstantExpr::getCast(cast<Constant>(Args[i]),
|
2004-02-08 22:14:44 +00:00
|
|
|
FT->getParamType(i));
|
|
|
|
|
2005-05-06 06:48:21 +00:00
|
|
|
(new CallInst(WriteFn, Args, "", IB))->setTailCall();
|
2004-02-08 22:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-08 19:53:56 +00:00
|
|
|
bool LowerInvoke::insertCheapEHSupport(Function &F) {
|
2003-10-05 19:14:42 +00:00
|
|
|
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("");
|
2005-05-13 06:27:02 +00:00
|
|
|
CallInst *NewCall = new CallInst(II->getCalledValue(),
|
|
|
|
std::vector<Value*>(II->op_begin()+3,
|
|
|
|
II->op_end()), Name, II);
|
|
|
|
NewCall->setCallingConv(II->getCallingConv());
|
2003-10-05 19:14:42 +00:00
|
|
|
II->replaceAllUsesWith(NewCall);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
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.
|
2004-02-08 21:44:31 +00:00
|
|
|
II->getUnwindDest()->removePredecessor(BB);
|
2003-12-10 20:22:42 +00:00
|
|
|
|
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())) {
|
2004-02-08 07:30:29 +00:00
|
|
|
// Insert a new call to write(2, AbortMessage, AbortMessageLength);
|
2004-02-08 22:14:44 +00:00
|
|
|
writeAbortMessage(UI);
|
2004-02-08 07:30:29 +00:00
|
|
|
|
2003-10-05 19:14:42 +00:00
|
|
|
// Insert a call to abort()
|
2005-05-06 06:48:21 +00:00
|
|
|
(new CallInst(AbortFn, std::vector<Value*>(), "", UI))->setTailCall();
|
2003-10-05 19:14:42 +00:00
|
|
|
|
2004-02-08 19:53:56 +00:00
|
|
|
// Insert a return instruction. This really should be a "barrier", as it
|
|
|
|
// is unreachable.
|
2003-10-05 19:14:42 +00:00
|
|
|
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;
|
|
|
|
}
|
2004-02-08 19:53:56 +00:00
|
|
|
|
|
|
|
bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// If a function uses invoke, we have an alloca for the jump buffer.
|
|
|
|
AllocaInst *JmpBuf = 0;
|
|
|
|
|
|
|
|
// If this function contains an unwind instruction, two blocks get added: one
|
|
|
|
// to actually perform the longjmp, and one to terminate the program if there
|
|
|
|
// is no handler.
|
|
|
|
BasicBlock *UnwindBlock = 0, *TermBlock = 0;
|
|
|
|
std::vector<LoadInst*> JBPtrs;
|
|
|
|
|
|
|
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
|
|
|
|
if (JmpBuf == 0)
|
|
|
|
JmpBuf = new AllocaInst(JBLinkTy, 0, "jblink", F.begin()->begin());
|
|
|
|
|
|
|
|
// On the entry to the invoke, we must install our JmpBuf as the top of
|
|
|
|
// the stack.
|
|
|
|
LoadInst *OldEntry = new LoadInst(JBListHead, "oldehlist", II);
|
|
|
|
|
|
|
|
// Store this old value as our 'next' field, and store our alloca as the
|
|
|
|
// current jblist.
|
|
|
|
std::vector<Value*> Idx;
|
2004-04-05 01:30:19 +00:00
|
|
|
Idx.push_back(Constant::getNullValue(Type::IntTy));
|
|
|
|
Idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
|
2004-02-08 19:53:56 +00:00
|
|
|
Value *NextFieldPtr = new GetElementPtrInst(JmpBuf, Idx, "NextField", II);
|
|
|
|
new StoreInst(OldEntry, NextFieldPtr, II);
|
|
|
|
new StoreInst(JmpBuf, JBListHead, II);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-02-08 19:53:56 +00:00
|
|
|
// Call setjmp, passing in the address of the jmpbuffer.
|
2004-04-05 01:30:19 +00:00
|
|
|
Idx[1] = ConstantUInt::get(Type::UIntTy, 1);
|
2004-02-08 19:53:56 +00:00
|
|
|
Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx, "TheJmpBuf", II);
|
|
|
|
Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret", II);
|
|
|
|
|
|
|
|
// Compare the return value to zero.
|
|
|
|
Value *IsNormal = BinaryOperator::create(Instruction::SetEQ, SJRet,
|
|
|
|
Constant::getNullValue(SJRet->getType()),
|
|
|
|
"notunwind", II);
|
|
|
|
// Create the receiver block if there is a critical edge to the normal
|
|
|
|
// destination.
|
|
|
|
SplitCriticalEdge(II, 0, this);
|
2005-08-03 18:34:29 +00:00
|
|
|
|
2005-08-03 18:51:44 +00:00
|
|
|
// There should not be any PHI nodes in II->getNormalDest() now. It has
|
|
|
|
// a single predecessor, so any PHI nodes are unneeded. Remove them now
|
|
|
|
// by replacing them with their single input value.
|
|
|
|
assert(II->getNormalDest()->getSinglePredecessor() &&
|
|
|
|
"Split crit edge doesn't have a single predecessor!");
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2005-08-03 18:51:44 +00:00
|
|
|
BasicBlock::iterator InsertLoc = II->getNormalDest()->begin();
|
|
|
|
while (PHINode *PN = dyn_cast<PHINode>(InsertLoc)) {
|
|
|
|
PN->replaceAllUsesWith(PN->getIncomingValue(0));
|
|
|
|
PN->eraseFromParent();
|
|
|
|
InsertLoc = II->getNormalDest()->begin();
|
|
|
|
}
|
|
|
|
|
2004-02-08 19:53:56 +00:00
|
|
|
// Insert a normal call instruction on the normal execution path.
|
|
|
|
std::string Name = II->getName(); II->setName("");
|
2005-05-13 06:27:02 +00:00
|
|
|
CallInst *NewCall = new CallInst(II->getCalledValue(),
|
|
|
|
std::vector<Value*>(II->op_begin()+3,
|
|
|
|
II->op_end()), Name,
|
|
|
|
InsertLoc);
|
|
|
|
NewCall->setCallingConv(II->getCallingConv());
|
2004-02-08 19:53:56 +00:00
|
|
|
II->replaceAllUsesWith(NewCall);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-02-08 19:53:56 +00:00
|
|
|
// If we got this far, then no exception was thrown and we can pop our
|
|
|
|
// jmpbuf entry off.
|
|
|
|
new StoreInst(OldEntry, JBListHead, InsertLoc);
|
|
|
|
|
|
|
|
// Now we change the invoke into a branch instruction.
|
2004-02-08 21:44:31 +00:00
|
|
|
new BranchInst(II->getNormalDest(), II->getUnwindDest(), IsNormal, II);
|
2004-02-08 19:53:56 +00:00
|
|
|
|
|
|
|
// Remove the InvokeInst now.
|
|
|
|
BB->getInstList().erase(II);
|
2005-04-21 23:48:37 +00:00
|
|
|
++NumLowered; Changed = true;
|
|
|
|
|
2004-02-08 19:53:56 +00:00
|
|
|
} else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
|
|
|
|
if (UnwindBlock == 0) {
|
|
|
|
// Create two new blocks, the unwind block and the terminate block. Add
|
|
|
|
// them at the end of the function because they are not hot.
|
|
|
|
UnwindBlock = new BasicBlock("unwind", &F);
|
|
|
|
TermBlock = new BasicBlock("unwinderror", &F);
|
|
|
|
|
|
|
|
// Insert return instructions. These really should be "barrier"s, as
|
|
|
|
// they are unreachable.
|
|
|
|
new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 :
|
|
|
|
Constant::getNullValue(F.getReturnType()), UnwindBlock);
|
|
|
|
new ReturnInst(F.getReturnType() == Type::VoidTy ? 0 :
|
|
|
|
Constant::getNullValue(F.getReturnType()), TermBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the JBList, if it's null, then there was no catch!
|
|
|
|
LoadInst *Ptr = new LoadInst(JBListHead, "ehlist", UI);
|
|
|
|
Value *NotNull = BinaryOperator::create(Instruction::SetNE, Ptr,
|
|
|
|
Constant::getNullValue(Ptr->getType()),
|
|
|
|
"notnull", UI);
|
|
|
|
new BranchInst(UnwindBlock, TermBlock, NotNull, UI);
|
|
|
|
|
|
|
|
// Remember the loaded value so we can insert the PHI node as needed.
|
|
|
|
JBPtrs.push_back(Ptr);
|
|
|
|
|
|
|
|
// Remove the UnwindInst now.
|
|
|
|
BB->getInstList().erase(UI);
|
2005-04-21 23:48:37 +00:00
|
|
|
++NumLowered; Changed = true;
|
2004-02-08 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If an unwind instruction was inserted, we need to set up the Unwind and
|
|
|
|
// term blocks.
|
|
|
|
if (UnwindBlock) {
|
|
|
|
// In the unwind block, we know that the pointer coming in on the JBPtrs
|
|
|
|
// list are non-null.
|
|
|
|
Instruction *RI = UnwindBlock->getTerminator();
|
|
|
|
|
|
|
|
Value *RecPtr;
|
|
|
|
if (JBPtrs.size() == 1)
|
|
|
|
RecPtr = JBPtrs[0];
|
|
|
|
else {
|
|
|
|
// If there is more than one unwind in this function, make a PHI node to
|
|
|
|
// merge in all of the loaded values.
|
|
|
|
PHINode *PN = new PHINode(JBPtrs[0]->getType(), "jbptrs", RI);
|
|
|
|
for (unsigned i = 0, e = JBPtrs.size(); i != e; ++i)
|
|
|
|
PN->addIncoming(JBPtrs[i], JBPtrs[i]->getParent());
|
|
|
|
RecPtr = PN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have a pointer to the whole record, remove the entry from the
|
|
|
|
// JBList.
|
|
|
|
std::vector<Value*> Idx;
|
2005-05-13 06:10:12 +00:00
|
|
|
Idx.push_back(Constant::getNullValue(Type::IntTy));
|
2004-04-05 01:30:19 +00:00
|
|
|
Idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
|
2004-02-08 19:53:56 +00:00
|
|
|
Value *NextFieldPtr = new GetElementPtrInst(RecPtr, Idx, "NextField", RI);
|
|
|
|
Value *NextRec = new LoadInst(NextFieldPtr, "NextRecord", RI);
|
|
|
|
new StoreInst(NextRec, JBListHead, RI);
|
|
|
|
|
|
|
|
// Now that we popped the top of the JBList, get a pointer to the jmpbuf and
|
|
|
|
// longjmp.
|
2004-04-05 01:30:19 +00:00
|
|
|
Idx[1] = ConstantUInt::get(Type::UIntTy, 1);
|
2004-02-08 19:53:56 +00:00
|
|
|
Idx[0] = new GetElementPtrInst(RecPtr, Idx, "JmpBuf", RI);
|
|
|
|
Idx[1] = ConstantInt::get(Type::IntTy, 1);
|
|
|
|
new CallInst(LongJmpFn, Idx, "", RI);
|
|
|
|
|
|
|
|
// Now we set up the terminate block.
|
|
|
|
RI = TermBlock->getTerminator();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-02-08 19:53:56 +00:00
|
|
|
// Insert a new call to write(2, AbortMessage, AbortMessageLength);
|
2004-02-08 22:14:44 +00:00
|
|
|
writeAbortMessage(RI);
|
|
|
|
|
2004-02-08 19:53:56 +00:00
|
|
|
// Insert a call to abort()
|
2005-05-06 06:48:21 +00:00
|
|
|
(new CallInst(AbortFn, std::vector<Value*>(), "", RI))->setTailCall();
|
2004-02-08 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LowerInvoke::runOnFunction(Function &F) {
|
|
|
|
if (ExpensiveEHSupport)
|
|
|
|
return insertExpensiveEHSupport(F);
|
|
|
|
else
|
|
|
|
return insertCheapEHSupport(F);
|
|
|
|
}
|