2003-10-06 03:14:42 +08:00
|
|
|
//===- LowerInvoke.cpp - Eliminate Invoke & Unwind 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
|
2004-02-09 03:53:56 +08: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 22:47:42 +08: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-09 03:53:56 +08: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-06 03:14:42 +08:00
|
|
|
//
|
2004-04-01 06:00:30 +08: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-06 03:14:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-20 06:17:40 +08:00
|
|
|
#define DEBUG_TYPE "lowerinvoke"
|
2003-10-06 03:14:42 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-02-08 15:30:29 +08:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-02-09 03:53:56 +08:00
|
|
|
#include "llvm/Instructions.h"
|
2008-04-07 21:41:19 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2009-07-06 06:41:43 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2004-02-08 15:30:29 +08:00
|
|
|
#include "llvm/Module.h"
|
2003-10-06 03:14:42 +08:00
|
|
|
#include "llvm/Pass.h"
|
2004-02-09 03:53:56 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2005-09-28 05:18:17 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2010-06-02 01:56:41 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-09-04 14:21:35 +08:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2004-02-09 03:53:56 +08:00
|
|
|
#include <csetjmp>
|
2007-02-04 08:40:42 +08:00
|
|
|
#include <set>
|
2003-12-11 04:22:42 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2006-12-20 06:17:40 +08:00
|
|
|
STATISTIC(NumInvokes, "Number of invokes replaced");
|
|
|
|
STATISTIC(NumUnwinds, "Number of unwinds replaced");
|
|
|
|
STATISTIC(NumSpilled, "Number of registers live across unwind edges");
|
|
|
|
|
|
|
|
static cl::opt<bool> ExpensiveEHSupport("enable-correct-eh-support",
|
2004-02-09 03:53:56 +08:00
|
|
|
cl::desc("Make the -lowerinvoke pass insert expensive, but correct, EH code"));
|
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 {
|
2004-02-09 03:53:56 +08:00
|
|
|
// Used for both models.
|
2007-01-07 16:12:01 +08:00
|
|
|
Constant *AbortFn;
|
2004-02-09 03:53:56 +08:00
|
|
|
|
|
|
|
// Used for expensive EH support.
|
|
|
|
const Type *JBLinkTy;
|
|
|
|
GlobalVariable *JBListHead;
|
2010-04-27 07:49:32 +08:00
|
|
|
Constant *SetJmpFn, *LongJmpFn, *StackSaveFn, *StackRestoreFn;
|
|
|
|
bool useExpensiveEHSupport;
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2006-09-04 14:21:35 +08:00
|
|
|
// We peek in TLI to grab the target's jmp_buf size and alignment
|
|
|
|
const TargetLowering *TLI;
|
2009-04-18 07:30:55 +08:00
|
|
|
|
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
|
2010-04-27 07:49:32 +08:00
|
|
|
explicit LowerInvoke(const TargetLowering *tli = NULL,
|
|
|
|
bool useExpensiveEHSupport = ExpensiveEHSupport)
|
2010-08-07 02:33:48 +08:00
|
|
|
: FunctionPass(ID), useExpensiveEHSupport(useExpensiveEHSupport),
|
2010-04-27 07:49:32 +08:00
|
|
|
TLI(tli) { }
|
2003-10-06 03:14:42 +08:00
|
|
|
bool doInitialization(Module &M);
|
|
|
|
bool runOnFunction(Function &F);
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2006-05-18 05:05:27 +08:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2007-04-17 02:10:23 +08:00
|
|
|
// This is a cluster of orthogonal Transforms
|
2010-08-07 05:48:06 +08:00
|
|
|
AU.addPreserved("mem2reg");
|
2006-05-18 05:05:27 +08:00
|
|
|
AU.addPreservedID(LowerSwitchID);
|
|
|
|
}
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2004-02-09 03:53:56 +08:00
|
|
|
private:
|
|
|
|
bool insertCheapEHSupport(Function &F);
|
2010-06-02 05:06:46 +08:00
|
|
|
void splitLiveRangesLiveAcrossInvokes(SmallVectorImpl<InvokeInst*>&Invokes);
|
2005-09-28 05:18:17 +08:00
|
|
|
void rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
|
2010-04-27 07:49:32 +08:00
|
|
|
AllocaInst *InvokeNum, AllocaInst *StackPtr,
|
|
|
|
SwitchInst *CatchSwitch);
|
2004-02-09 03:53:56 +08:00
|
|
|
bool insertExpensiveEHSupport(Function &F);
|
2003-10-06 03:14:42 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char LowerInvoke::ID = 0;
|
|
|
|
static RegisterPass<LowerInvoke>
|
|
|
|
X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators");
|
|
|
|
|
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.
|
2009-04-18 07:30:55 +08:00
|
|
|
FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI) {
|
2010-04-27 07:49:32 +08:00
|
|
|
return new LowerInvoke(TLI, ExpensiveEHSupport);
|
|
|
|
}
|
|
|
|
FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI,
|
|
|
|
bool useExpensiveEHSupport) {
|
|
|
|
return new LowerInvoke(TLI, useExpensiveEHSupport);
|
2005-11-05 17:21:28 +08:00
|
|
|
}
|
2003-10-06 03:14:42 +08:00
|
|
|
|
|
|
|
// doInitialization - Make sure that there is a prototype for abort in the
|
|
|
|
// current module.
|
|
|
|
bool LowerInvoke::doInitialization(Module &M) {
|
2009-08-14 05:58:54 +08:00
|
|
|
const Type *VoidPtrTy =
|
2009-10-06 23:40:36 +08:00
|
|
|
Type::getInt8PtrTy(M.getContext());
|
2010-04-27 07:49:32 +08:00
|
|
|
if (useExpensiveEHSupport) {
|
2007-02-23 00:21:18 +08:00
|
|
|
// Insert a type for the linked list of jump buffers.
|
|
|
|
unsigned JBSize = TLI ? TLI->getJumpBufSize() : 0;
|
|
|
|
JBSize = JBSize ? JBSize : 200;
|
2009-07-30 06:17:13 +08:00
|
|
|
const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JBSize);
|
2007-02-23 00:21:18 +08:00
|
|
|
|
|
|
|
{ // The type is recursive, so use a type holder.
|
|
|
|
std::vector<const Type*> Elements;
|
|
|
|
Elements.push_back(JmpBufTy);
|
2009-08-14 07:27:32 +08:00
|
|
|
OpaqueType *OT = OpaqueType::get(M.getContext());
|
2009-07-30 06:17:13 +08:00
|
|
|
Elements.push_back(PointerType::getUnqual(OT));
|
2009-08-06 07:16:16 +08:00
|
|
|
PATypeHolder JBLType(StructType::get(M.getContext(), Elements));
|
2007-02-23 00:21:18 +08:00
|
|
|
OT->refineAbstractTypeTo(JBLType.get()); // Complete the cycle.
|
|
|
|
JBLinkTy = JBLType.get();
|
|
|
|
M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);
|
|
|
|
}
|
2004-02-09 03:53:56 +08:00
|
|
|
|
2009-07-30 06:17:13 +08:00
|
|
|
const Type *PtrJBList = PointerType::getUnqual(JBLinkTy);
|
2004-02-09 03:53:56 +08:00
|
|
|
|
2007-02-23 00:21:18 +08:00
|
|
|
// Now that we've done that, insert the jmpbuf list head global, unless it
|
|
|
|
// already exists.
|
|
|
|
if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) {
|
2009-07-09 03:03:57 +08:00
|
|
|
JBListHead = new GlobalVariable(M, PtrJBList, false,
|
Introduce new linkage types linkonce_odr, weak_odr, common_odr
and extern_weak_odr. These are the same as the non-odr versions,
except that they indicate that the global will only be overridden
by an *equivalent* global. In C, a function with weak linkage can
be overridden by a function which behaves completely differently.
This means that IP passes have to skip weak functions, since any
deductions made from the function definition might be wrong, since
the definition could be replaced by something completely different
at link time. This is not allowed in C++, thanks to the ODR
(One-Definition-Rule): if a function is replaced by another at
link-time, then the new function must be the same as the original
function. If a language knows that a function or other global can
only be overridden by an equivalent global, it can give it the
weak_odr linkage type, and the optimizers will understand that it
is alright to make deductions based on the function body. The
code generators on the other hand map weak and weak_odr linkage
to the same thing.
llvm-svn: 66339
2009-03-07 23:45:40 +08:00
|
|
|
GlobalValue::LinkOnceAnyLinkage,
|
2009-08-01 04:28:14 +08:00
|
|
|
Constant::getNullValue(PtrJBList),
|
2009-07-09 03:03:57 +08:00
|
|
|
"llvm.sjljeh.jblist");
|
2006-09-06 01:48:07 +08:00
|
|
|
}
|
2008-04-16 05:27:11 +08:00
|
|
|
|
2009-04-18 07:30:55 +08:00
|
|
|
// VisualStudio defines setjmp as _setjmp via #include <csetjmp> / <setjmp.h>,
|
2008-04-16 05:27:11 +08:00
|
|
|
// so it looks like Intrinsic::_setjmp
|
|
|
|
#if defined(_MSC_VER) && defined(setjmp)
|
|
|
|
#define setjmp_undefined_for_visual_studio
|
|
|
|
#undef setjmp
|
|
|
|
#endif
|
|
|
|
|
2008-04-07 21:41:19 +08:00
|
|
|
SetJmpFn = Intrinsic::getDeclaration(&M, Intrinsic::setjmp);
|
2008-04-16 05:27:11 +08:00
|
|
|
|
|
|
|
#if defined(_MSC_VER) && defined(setjmp_undefined_for_visual_studio)
|
2009-04-18 07:30:55 +08:00
|
|
|
// let's return it to _setjmp state in case anyone ever needs it after this
|
2008-04-16 05:27:11 +08:00
|
|
|
// point under VisualStudio
|
2009-04-18 07:30:55 +08:00
|
|
|
#define setjmp _setjmp
|
2008-04-16 05:27:11 +08:00
|
|
|
#endif
|
|
|
|
|
2008-04-07 21:41:19 +08:00
|
|
|
LongJmpFn = Intrinsic::getDeclaration(&M, Intrinsic::longjmp);
|
2010-04-27 07:49:32 +08:00
|
|
|
StackSaveFn = Intrinsic::getDeclaration(&M, Intrinsic::stacksave);
|
|
|
|
StackRestoreFn = Intrinsic::getDeclaration(&M, Intrinsic::stackrestore);
|
2007-02-22 06:49:50 +08:00
|
|
|
}
|
2007-02-23 00:21:18 +08:00
|
|
|
|
|
|
|
// We need the 'write' and 'abort' functions for both models.
|
2009-08-14 05:58:54 +08:00
|
|
|
AbortFn = M.getOrInsertFunction("abort", Type::getVoidTy(M.getContext()),
|
|
|
|
(Type *)0);
|
2004-11-14 03:07:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-02-09 03:53:56 +08:00
|
|
|
bool LowerInvoke::insertCheapEHSupport(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(),
|
2010-04-27 07:49:32 +08:00
|
|
|
CallArgs.begin(), CallArgs.end(),
|
|
|
|
"",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());
|
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-12-11 04:22:42 +08:00
|
|
|
} else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
|
2003-10-06 03:14:42 +08:00
|
|
|
// Insert a call to abort()
|
2008-04-07 04:25:17 +08:00
|
|
|
CallInst::Create(AbortFn, "", UI)->setTailCall();
|
2003-10-06 03:14:42 +08:00
|
|
|
|
2004-02-09 03:53:56 +08:00
|
|
|
// Insert a return instruction. This really should be a "barrier", as it
|
|
|
|
// is unreachable.
|
2009-08-14 05:58:54 +08:00
|
|
|
ReturnInst::Create(F.getContext(),
|
2010-01-05 21:12:22 +08:00
|
|
|
F.getReturnType()->isVoidTy() ?
|
2009-08-14 05:58:54 +08:00
|
|
|
0 : Constant::getNullValue(F.getReturnType()), UI);
|
2003-10-06 03:14:42 +08:00
|
|
|
|
|
|
|
// Remove the unwind instruction now.
|
2003-12-11 04:22:42 +08:00
|
|
|
BB->getInstList().erase(UI);
|
2003-10-06 03:14:42 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
++NumUnwinds; Changed = true;
|
2003-10-06 03:14:42 +08:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
2004-02-09 03:53:56 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
/// rewriteExpensiveInvoke - Insert code and hack the function to replace the
|
|
|
|
/// specified invoke instruction with a call.
|
|
|
|
void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
|
|
|
|
AllocaInst *InvokeNum,
|
2010-04-27 07:49:32 +08:00
|
|
|
AllocaInst *StackPtr,
|
2005-09-28 05:18:17 +08:00
|
|
|
SwitchInst *CatchSwitch) {
|
2009-08-14 05:58:54 +08:00
|
|
|
ConstantInt *InvokeNoC = ConstantInt::get(Type::getInt32Ty(II->getContext()),
|
|
|
|
InvokeNo);
|
2005-09-28 05:18:17 +08:00
|
|
|
|
2008-02-15 03:18:13 +08:00
|
|
|
// If the unwind edge has phi nodes, split the edge.
|
|
|
|
if (isa<PHINode>(II->getUnwindDest()->begin())) {
|
|
|
|
SplitCriticalEdge(II, 1, this);
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2008-02-15 03:18:13 +08:00
|
|
|
// If there are any phi nodes left, they must have a single predecessor.
|
|
|
|
while (PHINode *PN = dyn_cast<PHINode>(II->getUnwindDest()->begin())) {
|
|
|
|
PN->replaceAllUsesWith(PN->getIncomingValue(0));
|
|
|
|
PN->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Insert a store of the invoke num before the invoke and store zero into the
|
|
|
|
// location afterward.
|
|
|
|
new StoreInst(InvokeNoC, InvokeNum, true, II); // volatile
|
2010-04-27 07:49:32 +08:00
|
|
|
|
|
|
|
// Insert a store of the stack ptr before the invoke, so we can restore it
|
|
|
|
// later in the exception case.
|
|
|
|
CallInst* StackSaveRet = CallInst::Create(StackSaveFn, "ssret", II);
|
|
|
|
new StoreInst(StackSaveRet, StackPtr, true, II); // volatile
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2008-05-24 05:05:58 +08:00
|
|
|
BasicBlock::iterator NI = II->getNormalDest()->getFirstNonPHI();
|
2005-09-30 01:44:20 +08:00
|
|
|
// nonvolatile.
|
2009-08-14 05:58:54 +08:00
|
|
|
new StoreInst(Constant::getNullValue(Type::getInt32Ty(II->getContext())),
|
|
|
|
InvokeNum, false, NI);
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2010-04-27 07:49:32 +08:00
|
|
|
Instruction* StackPtrLoad = new LoadInst(StackPtr, "stackptr.restore", true,
|
|
|
|
II->getUnwindDest()->getFirstNonPHI()
|
|
|
|
);
|
|
|
|
CallInst::Create(StackRestoreFn, StackPtrLoad, "")->insertAfter(StackPtrLoad);
|
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Add a switch case to our unwind block.
|
|
|
|
CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Insert a normal call instruction.
|
2010-06-02 01:56:41 +08:00
|
|
|
SmallVector<Value*,16> CallArgs(II->op_begin(), II->op_end() - 3);
|
2008-04-07 04:25:17 +08:00
|
|
|
CallInst *NewCall = CallInst::Create(II->getCalledValue(),
|
|
|
|
CallArgs.begin(), CallArgs.end(), "",
|
|
|
|
II);
|
2007-02-11 09:37:51 +08:00
|
|
|
NewCall->takeName(II);
|
2005-09-28 05:18:17 +08:00
|
|
|
NewCall->setCallingConv(II->getCallingConv());
|
2008-09-26 05:00:45 +08:00
|
|
|
NewCall->setAttributes(II->getAttributes());
|
2005-09-28 05:18:17 +08:00
|
|
|
II->replaceAllUsesWith(NewCall);
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Replace the invoke with an uncond branch.
|
2008-04-07 04:25:17 +08:00
|
|
|
BranchInst::Create(II->getNormalDest(), NewCall->getParent());
|
2005-09-28 05:18:17 +08:00
|
|
|
II->eraseFromParent();
|
|
|
|
}
|
2004-02-09 03:53:56 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
/// MarkBlocksLiveIn - Insert BB and all of its predescessors into LiveBBs until
|
|
|
|
/// we reach blocks we've already seen.
|
|
|
|
static void MarkBlocksLiveIn(BasicBlock *BB, std::set<BasicBlock*> &LiveBBs) {
|
|
|
|
if (!LiveBBs.insert(BB).second) return; // already been here.
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
|
2009-04-18 07:30:55 +08:00
|
|
|
MarkBlocksLiveIn(*PI, LiveBBs);
|
2005-09-28 05:18:17 +08:00
|
|
|
}
|
2004-02-09 03:53:56 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// First thing we need to do is scan the whole function for values that are
|
|
|
|
// live across unwind edges. Each value that is live across an unwind edge
|
|
|
|
// we spill into a stack location, guaranteeing that there is nothing live
|
|
|
|
// across the unwind edge. This process also splits all critical edges
|
|
|
|
// coming out of invoke's.
|
|
|
|
void LowerInvoke::
|
2010-06-02 05:06:46 +08:00
|
|
|
splitLiveRangesLiveAcrossInvokes(SmallVectorImpl<InvokeInst*> &Invokes) {
|
2005-09-28 05:18:17 +08:00
|
|
|
// First step, split all critical edges from invoke instructions.
|
|
|
|
for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
|
|
|
|
InvokeInst *II = Invokes[i];
|
|
|
|
SplitCriticalEdge(II, 0, this);
|
|
|
|
SplitCriticalEdge(II, 1, this);
|
|
|
|
assert(!isa<PHINode>(II->getNormalDest()) &&
|
|
|
|
!isa<PHINode>(II->getUnwindDest()) &&
|
|
|
|
"critical edge splitting left single entry phi nodes?");
|
|
|
|
}
|
2004-02-09 03:53:56 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
Function *F = Invokes.back()->getParent()->getParent();
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// To avoid having to handle incoming arguments specially, we lower each arg
|
2006-11-27 09:05:10 +08:00
|
|
|
// to a copy instruction in the entry block. This ensures that the argument
|
2005-09-28 05:18:17 +08:00
|
|
|
// value itself cannot be live across the entry block.
|
|
|
|
BasicBlock::iterator AfterAllocaInsertPt = F->begin()->begin();
|
|
|
|
while (isa<AllocaInst>(AfterAllocaInsertPt) &&
|
|
|
|
isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsertPt)->getArraySize()))
|
|
|
|
++AfterAllocaInsertPt;
|
|
|
|
for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
|
|
|
|
AI != E; ++AI) {
|
2010-06-02 02:04:56 +08:00
|
|
|
const Type *Ty = AI->getType();
|
2010-07-01 06:22:59 +08:00
|
|
|
// Aggregate types can't be cast, but are legal argument types, so we have
|
2010-06-02 02:04:56 +08:00
|
|
|
// to handle them differently. We use an extract/insert pair as a
|
|
|
|
// lightweight method to achieve the same goal.
|
2010-07-01 06:22:59 +08:00
|
|
|
if (isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) {
|
|
|
|
Instruction *EI = ExtractValueInst::Create(AI, 0, "",AfterAllocaInsertPt);
|
2010-06-02 02:04:56 +08:00
|
|
|
Instruction *NI = InsertValueInst::Create(AI, EI, 0);
|
|
|
|
NI->insertAfter(EI);
|
|
|
|
AI->replaceAllUsesWith(NI);
|
2010-07-01 06:22:59 +08:00
|
|
|
// Set the operand of the instructions back to the AllocaInst.
|
2010-06-02 02:04:56 +08:00
|
|
|
EI->setOperand(0, AI);
|
|
|
|
NI->setOperand(0, AI);
|
|
|
|
} else {
|
|
|
|
// This is always a no-op cast because we're casting AI to AI->getType()
|
|
|
|
// so src and destination types are identical. BitCast is the only
|
|
|
|
// possibility.
|
|
|
|
CastInst *NC = new BitCastInst(
|
|
|
|
AI, AI->getType(), AI->getName()+".tmp", AfterAllocaInsertPt);
|
|
|
|
AI->replaceAllUsesWith(NC);
|
|
|
|
// Set the operand of the cast instruction back to the AllocaInst.
|
|
|
|
// Normally it's forbidden to replace a CastInst's operand because it
|
|
|
|
// could cause the opcode to reflect an illegal conversion. However,
|
|
|
|
// we're replacing it here with the same value it was constructed with.
|
|
|
|
// We do this because the above replaceAllUsesWith() clobbered the
|
|
|
|
// operand, but we want this one to remain.
|
|
|
|
NC->setOperand(0, AI);
|
|
|
|
}
|
2005-09-28 05:18:17 +08:00
|
|
|
}
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Finally, scan the code looking for instructions with bad live ranges.
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
|
|
|
|
for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
|
|
|
|
// Ignore obvious cases we don't have to handle. In particular, most
|
|
|
|
// instructions either have no uses or only have a single use inside the
|
|
|
|
// current block. Ignore them quickly.
|
|
|
|
Instruction *Inst = II;
|
|
|
|
if (Inst->use_empty()) continue;
|
|
|
|
if (Inst->hasOneUse() &&
|
|
|
|
cast<Instruction>(Inst->use_back())->getParent() == BB &&
|
|
|
|
!isa<PHINode>(Inst->use_back())) continue;
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:33:12 +08:00
|
|
|
// If this is an alloca in the entry block, it's not a real register
|
|
|
|
// value.
|
|
|
|
if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
|
|
|
|
if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin())
|
|
|
|
continue;
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Avoid iterator invalidation by copying users to a temporary vector.
|
2010-06-02 01:56:41 +08:00
|
|
|
SmallVector<Instruction*,16> Users;
|
2005-09-28 05:18:17 +08:00
|
|
|
for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end();
|
|
|
|
UI != E; ++UI) {
|
|
|
|
Instruction *User = cast<Instruction>(*UI);
|
|
|
|
if (User->getParent() != BB || isa<PHINode>(User))
|
|
|
|
Users.push_back(User);
|
2005-08-04 02:51:44 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Scan all of the uses and see if the live range is live across an unwind
|
|
|
|
// edge. If we find a use live across an invoke edge, create an alloca
|
|
|
|
// and spill the value.
|
|
|
|
std::set<InvokeInst*> InvokesWithStoreInserted;
|
|
|
|
|
|
|
|
// Find all of the blocks that this value is live in.
|
|
|
|
std::set<BasicBlock*> LiveBBs;
|
|
|
|
LiveBBs.insert(Inst->getParent());
|
|
|
|
while (!Users.empty()) {
|
|
|
|
Instruction *U = Users.back();
|
|
|
|
Users.pop_back();
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
if (!isa<PHINode>(U)) {
|
|
|
|
MarkBlocksLiveIn(U->getParent(), LiveBBs);
|
|
|
|
} else {
|
|
|
|
// Uses for a PHI node occur in their predecessor block.
|
|
|
|
PHINode *PN = cast<PHINode>(U);
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
|
|
|
if (PN->getIncomingValue(i) == Inst)
|
|
|
|
MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs);
|
|
|
|
}
|
|
|
|
}
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Now that we know all of the blocks that this thing is live in, see if
|
|
|
|
// it includes any of the unwind locations.
|
|
|
|
bool NeedsSpill = false;
|
|
|
|
for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
|
|
|
|
BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest();
|
|
|
|
if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) {
|
|
|
|
NeedsSpill = true;
|
|
|
|
}
|
2004-02-09 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// If we decided we need a spill, do it.
|
|
|
|
if (NeedsSpill) {
|
|
|
|
++NumSpilled;
|
2009-07-16 07:53:25 +08:00
|
|
|
DemoteRegToStack(*Inst, true);
|
2005-09-28 05:18:17 +08:00
|
|
|
}
|
2004-02-09 03:53:56 +08:00
|
|
|
}
|
2005-09-28 05:18:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
|
2010-06-02 01:56:41 +08:00
|
|
|
SmallVector<ReturnInst*,16> Returns;
|
|
|
|
SmallVector<UnwindInst*,16> Unwinds;
|
|
|
|
SmallVector<InvokeInst*,16> Invokes;
|
2004-02-09 03:53:56 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
|
|
|
|
// Remember all return instructions in case we insert an invoke into this
|
|
|
|
// function.
|
|
|
|
Returns.push_back(RI);
|
|
|
|
} else if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
|
|
|
|
Invokes.push_back(II);
|
|
|
|
} else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
|
|
|
|
Unwinds.push_back(UI);
|
2004-02-09 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
if (Unwinds.empty() && Invokes.empty()) return false;
|
|
|
|
|
|
|
|
NumInvokes += Invokes.size();
|
|
|
|
NumUnwinds += Unwinds.size();
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 06:44:59 +08:00
|
|
|
// TODO: This is not an optimal way to do this. In particular, this always
|
|
|
|
// inserts setjmp calls into the entries of functions with invoke instructions
|
|
|
|
// even though there are possibly paths through the function that do not
|
|
|
|
// execute any invokes. In particular, for functions with early exits, e.g.
|
|
|
|
// the 'addMove' method in hexxagon, it would be nice to not have to do the
|
|
|
|
// setjmp stuff on the early exit path. This requires a bit of dataflow, but
|
|
|
|
// would not be too hard to do.
|
2005-09-28 05:18:17 +08:00
|
|
|
|
|
|
|
// If we have an invoke instruction, insert a setjmp that dominates all
|
|
|
|
// invokes. After the setjmp, use a cond branch that goes to the original
|
|
|
|
// code path on zero, and to a designated 'catch' block of nonzero.
|
|
|
|
Value *OldJmpBufPtr = 0;
|
|
|
|
if (!Invokes.empty()) {
|
|
|
|
// First thing we need to do is scan the whole function for values that are
|
|
|
|
// live across unwind edges. Each value that is live across an unwind edge
|
|
|
|
// we spill into a stack location, guaranteeing that there is nothing live
|
|
|
|
// across the unwind edge. This process also splits all critical edges
|
|
|
|
// coming out of invoke's.
|
2009-04-18 07:30:55 +08:00
|
|
|
splitLiveRangesLiveAcrossInvokes(Invokes);
|
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
BasicBlock *EntryBB = F.begin();
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Create an alloca for the incoming jump buffer ptr and the new jump buffer
|
|
|
|
// that needs to be restored on all exits from the function. This is an
|
|
|
|
// alloca because the value needs to be live across invokes.
|
2006-09-06 01:48:07 +08:00
|
|
|
unsigned Align = TLI ? TLI->getJumpBufAlignment() : 0;
|
2009-04-18 07:30:55 +08:00
|
|
|
AllocaInst *JmpBuf =
|
2009-07-16 07:53:25 +08:00
|
|
|
new AllocaInst(JBLinkTy, 0, Align,
|
2009-07-15 07:09:55 +08:00
|
|
|
"jblink", F.begin()->begin());
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2010-06-02 05:06:46 +08:00
|
|
|
Value *Idx[] = { Constant::getNullValue(Type::getInt32Ty(F.getContext())),
|
|
|
|
ConstantInt::get(Type::getInt32Ty(F.getContext()), 1) };
|
|
|
|
OldJmpBufPtr = GetElementPtrInst::Create(JmpBuf, &Idx[0], &Idx[2],
|
2009-07-15 07:09:55 +08:00
|
|
|
"OldBuf",
|
2010-06-02 05:06:46 +08:00
|
|
|
EntryBB->getTerminator());
|
2005-09-28 05:18:17 +08:00
|
|
|
|
|
|
|
// Copy the JBListHead to the alloca.
|
|
|
|
Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true,
|
|
|
|
EntryBB->getTerminator());
|
|
|
|
new StoreInst(OldBuf, OldJmpBufPtr, true, EntryBB->getTerminator());
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Add the new jumpbuf to the list.
|
|
|
|
new StoreInst(JmpBuf, JBListHead, true, EntryBB->getTerminator());
|
|
|
|
|
|
|
|
// Create the catch block. The catch block is basically a big switch
|
|
|
|
// statement that goes to all of the invoke catch blocks.
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *CatchBB =
|
|
|
|
BasicBlock::Create(F.getContext(), "setjmp.catch", &F);
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2010-04-27 07:49:32 +08:00
|
|
|
// Create an alloca which keeps track of the stack pointer before every
|
|
|
|
// invoke, this allows us to properly restore the stack pointer after
|
|
|
|
// long jumping.
|
|
|
|
AllocaInst *StackPtr = new AllocaInst(Type::getInt8PtrTy(F.getContext()), 0,
|
|
|
|
"stackptr", EntryBB->begin());
|
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Create an alloca which keeps track of which invoke is currently
|
|
|
|
// executing. For normal calls it contains zero.
|
2009-08-14 05:58:54 +08:00
|
|
|
AllocaInst *InvokeNum = new AllocaInst(Type::getInt32Ty(F.getContext()), 0,
|
2009-07-15 07:09:55 +08:00
|
|
|
"invokenum",EntryBB->begin());
|
2009-08-14 05:58:54 +08:00
|
|
|
new StoreInst(ConstantInt::get(Type::getInt32Ty(F.getContext()), 0),
|
|
|
|
InvokeNum, true, EntryBB->getTerminator());
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Insert a load in the Catch block, and a switch on its value. By default,
|
|
|
|
// we go to a block that just does an unwind (which is the correct action
|
|
|
|
// for a standard call).
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *UnwindBB = BasicBlock::Create(F.getContext(), "unwindbb", &F);
|
|
|
|
Unwinds.push_back(new UnwindInst(F.getContext(), UnwindBB));
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
Value *CatchLoad = new LoadInst(InvokeNum, "invoke.num", true, CatchBB);
|
2008-04-07 04:25:17 +08:00
|
|
|
SwitchInst *CatchSwitch =
|
|
|
|
SwitchInst::Create(CatchLoad, UnwindBB, Invokes.size(), CatchBB);
|
2005-09-28 05:18:17 +08:00
|
|
|
|
|
|
|
// Now that things are set up, insert the setjmp call itself.
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Split the entry block to insert the conditional branch for the setjmp.
|
|
|
|
BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(),
|
|
|
|
"setjmp.cont");
|
|
|
|
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[1] = ConstantInt::get(Type::getInt32Ty(F.getContext()), 0);
|
2010-06-02 05:06:46 +08:00
|
|
|
Value *JmpBufPtr = GetElementPtrInst::Create(JmpBuf, &Idx[0], &Idx[2],
|
2008-04-07 04:25:17 +08:00
|
|
|
"TheJmpBuf",
|
|
|
|
EntryBB->getTerminator());
|
2009-08-14 05:58:54 +08:00
|
|
|
JmpBufPtr = new BitCastInst(JmpBufPtr,
|
2009-10-06 23:40:36 +08:00
|
|
|
Type::getInt8PtrTy(F.getContext()),
|
2008-04-07 21:41:19 +08:00
|
|
|
"tmp", EntryBB->getTerminator());
|
2008-04-07 04:25:17 +08:00
|
|
|
Value *SJRet = CallInst::Create(SetJmpFn, JmpBufPtr, "sjret",
|
|
|
|
EntryBB->getTerminator());
|
2008-04-07 21:41:19 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Compare the return value to zero.
|
2009-07-10 07:48:35 +08:00
|
|
|
Value *IsNormal = new ICmpInst(EntryBB->getTerminator(),
|
|
|
|
ICmpInst::ICMP_EQ, SJRet,
|
2009-08-01 04:28:14 +08:00
|
|
|
Constant::getNullValue(SJRet->getType()),
|
2009-07-10 07:48:35 +08:00
|
|
|
"notunwind");
|
2005-09-28 05:18:17 +08:00
|
|
|
// Nuke the uncond branch.
|
|
|
|
EntryBB->getTerminator()->eraseFromParent();
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Put in a new condbranch in its place.
|
2008-04-07 04:25:17 +08:00
|
|
|
BranchInst::Create(ContBlock, CatchBB, IsNormal, EntryBB);
|
2005-09-28 05:18:17 +08:00
|
|
|
|
|
|
|
// At this point, we are all set up, rewrite each invoke instruction.
|
|
|
|
for (unsigned i = 0, e = Invokes.size(); i != e; ++i)
|
2010-04-27 07:49:32 +08:00
|
|
|
rewriteExpensiveInvoke(Invokes[i], i+1, InvokeNum, StackPtr, CatchSwitch);
|
2004-02-09 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// We know that there is at least one unwind.
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Create three new blocks, the block to load the jmpbuf ptr and compare
|
|
|
|
// against null, the block to do the longjmp, and the error block for if it
|
|
|
|
// is null. Add them at the end of the function because they are not hot.
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *UnwindHandler = BasicBlock::Create(F.getContext(),
|
|
|
|
"dounwind", &F);
|
|
|
|
BasicBlock *UnwindBlock = BasicBlock::Create(F.getContext(), "unwind", &F);
|
|
|
|
BasicBlock *TermBlock = BasicBlock::Create(F.getContext(), "unwinderror", &F);
|
2005-09-28 05:18:17 +08:00
|
|
|
|
|
|
|
// If this function contains an invoke, restore the old jumpbuf ptr.
|
|
|
|
Value *BufPtr;
|
|
|
|
if (OldJmpBufPtr) {
|
|
|
|
// Before the return, insert a copy from the saved value to the new value.
|
|
|
|
BufPtr = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", UnwindHandler);
|
|
|
|
new StoreInst(BufPtr, JBListHead, UnwindHandler);
|
|
|
|
} else {
|
|
|
|
BufPtr = new LoadInst(JBListHead, "ehlist", UnwindHandler);
|
|
|
|
}
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Load the JBList, if it's null, then there was no catch!
|
2009-07-10 07:48:35 +08:00
|
|
|
Value *NotNull = new ICmpInst(*UnwindHandler, ICmpInst::ICMP_NE, BufPtr,
|
2009-08-01 04:28:14 +08:00
|
|
|
Constant::getNullValue(BufPtr->getType()),
|
2009-07-10 07:48:35 +08:00
|
|
|
"notnull");
|
2008-04-07 04:25:17 +08:00
|
|
|
BranchInst::Create(UnwindBlock, TermBlock, NotNull, UnwindHandler);
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Create the block to do the longjmp.
|
|
|
|
// Get a pointer to the jmpbuf and longjmp.
|
2010-06-02 05:06:46 +08:00
|
|
|
Value *Idx[] = { Constant::getNullValue(Type::getInt32Ty(F.getContext())),
|
|
|
|
ConstantInt::get(Type::getInt32Ty(F.getContext()), 0) };
|
|
|
|
Idx[0] = GetElementPtrInst::Create(BufPtr, &Idx[0], &Idx[2], "JmpBuf",
|
2008-04-07 04:25:17 +08:00
|
|
|
UnwindBlock);
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[0] = new BitCastInst(Idx[0],
|
2009-10-06 23:40:36 +08:00
|
|
|
Type::getInt8PtrTy(F.getContext()),
|
2008-04-07 21:41:19 +08:00
|
|
|
"tmp", UnwindBlock);
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[1] = ConstantInt::get(Type::getInt32Ty(F.getContext()), 1);
|
2010-06-02 05:06:46 +08:00
|
|
|
CallInst::Create(LongJmpFn, &Idx[0], &Idx[2], "", UnwindBlock);
|
2009-08-14 05:58:54 +08:00
|
|
|
new UnreachableInst(F.getContext(), UnwindBlock);
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Set up the term block ("throw without a catch").
|
2009-08-14 05:58:54 +08:00
|
|
|
new UnreachableInst(F.getContext(), TermBlock);
|
2005-09-28 05:18:17 +08:00
|
|
|
|
|
|
|
// Insert a call to abort()
|
2008-04-07 04:25:17 +08:00
|
|
|
CallInst::Create(AbortFn, "",
|
|
|
|
TermBlock->getTerminator())->setTailCall();
|
2009-04-18 07:30:55 +08:00
|
|
|
|
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Replace all unwinds with a branch to the unwind handler.
|
|
|
|
for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) {
|
2008-04-07 04:25:17 +08:00
|
|
|
BranchInst::Create(UnwindHandler, Unwinds[i]);
|
2009-04-18 07:30:55 +08:00
|
|
|
Unwinds[i]->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Finally, for any returns from this function, if this function contains an
|
|
|
|
// invoke, restore the old jmpbuf pointer to its input value.
|
|
|
|
if (OldJmpBufPtr) {
|
|
|
|
for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
|
|
|
|
ReturnInst *R = Returns[i];
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
// Before the return, insert a copy from the saved value to the new value.
|
|
|
|
Value *OldBuf = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", true, R);
|
|
|
|
new StoreInst(OldBuf, JBListHead, true, R);
|
|
|
|
}
|
|
|
|
}
|
2009-04-18 07:30:55 +08:00
|
|
|
|
2005-09-28 05:18:17 +08:00
|
|
|
return true;
|
2004-02-09 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LowerInvoke::runOnFunction(Function &F) {
|
2010-04-27 07:49:32 +08:00
|
|
|
if (useExpensiveEHSupport)
|
2004-02-09 03:53:56 +08:00
|
|
|
return insertExpensiveEHSupport(F);
|
|
|
|
else
|
|
|
|
return insertCheapEHSupport(F);
|
|
|
|
}
|