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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
#define DEBUG_TYPE "lowerinvoke"
|
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"
|
2005-09-27 21:18:17 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-09-04 06:21:35 +00:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2004-02-08 19:53:56 +00:00
|
|
|
#include <csetjmp>
|
2007-02-04 00:40:42 +00:00
|
|
|
#include <set>
|
2003-12-10 20:22:42 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 22:17:40 +00: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-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
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
namespace {
|
2006-06-28 22:08:15 +00:00
|
|
|
class VISIBILITY_HIDDEN LowerInvoke : public FunctionPass {
|
2004-02-08 19:53:56 +00:00
|
|
|
// Used for both models.
|
2007-01-07 08:12:01 +00:00
|
|
|
Constant *WriteFn;
|
|
|
|
Constant *AbortFn;
|
2004-02-08 19:53:56 +00:00
|
|
|
Value *AbortMessage;
|
|
|
|
unsigned AbortMessageLength;
|
|
|
|
|
|
|
|
// Used for expensive EH support.
|
|
|
|
const Type *JBLinkTy;
|
|
|
|
GlobalVariable *JBListHead;
|
2007-01-07 08:12:01 +00:00
|
|
|
Constant *SetJmpFn, *LongJmpFn;
|
2006-09-04 06:21:35 +00:00
|
|
|
|
|
|
|
// We peek in TLI to grab the target's jmp_buf size and alignment
|
|
|
|
const TargetLowering *TLI;
|
|
|
|
|
2003-10-05 19:14:42 +00:00
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-08-01 15:32:29 +00:00
|
|
|
explicit LowerInvoke(const TargetLowering *tli = NULL)
|
|
|
|
: FunctionPass((intptr_t)&ID), TLI(tli) { }
|
2003-10-05 19:14:42 +00:00
|
|
|
bool doInitialization(Module &M);
|
|
|
|
bool runOnFunction(Function &F);
|
2006-05-17 21:05:27 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2007-04-16 18:10:23 +00:00
|
|
|
// This is a cluster of orthogonal Transforms
|
2006-05-17 21:05:27 +00:00
|
|
|
AU.addPreservedID(PromoteMemoryToRegisterID);
|
|
|
|
AU.addPreservedID(LowerSelectID);
|
|
|
|
AU.addPreservedID(LowerSwitchID);
|
|
|
|
AU.addPreservedID(LowerAllocationsID);
|
|
|
|
}
|
|
|
|
|
2004-02-08 19:53:56 +00:00
|
|
|
private:
|
2007-01-07 08:12:01 +00:00
|
|
|
void createAbortMessage(Module *M);
|
2004-02-08 22:14:44 +00:00
|
|
|
void writeAbortMessage(Instruction *IB);
|
2004-02-08 19:53:56 +00:00
|
|
|
bool insertCheapEHSupport(Function &F);
|
2005-09-27 21:18:17 +00:00
|
|
|
void splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes);
|
|
|
|
void rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
|
|
|
|
AllocaInst *InvokeNum, SwitchInst *CatchSwitch);
|
2004-02-08 19:53:56 +00:00
|
|
|
bool insertExpensiveEHSupport(Function &F);
|
2003-10-05 19:14:42 +00:00
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char LowerInvoke::ID = 0;
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<LowerInvoke>
|
2003-10-05 19:14:42 +00:00
|
|
|
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.
|
2006-09-04 06:21:35 +00:00
|
|
|
FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI) {
|
|
|
|
return new LowerInvoke(TLI);
|
2005-11-05 09:21:28 +00:00
|
|
|
}
|
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) {
|
2007-12-17 01:12:55 +00:00
|
|
|
const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
|
2007-02-22 16:21:18 +00:00
|
|
|
AbortMessage = 0;
|
|
|
|
if (ExpensiveEHSupport) {
|
|
|
|
// Insert a type for the linked list of jump buffers.
|
|
|
|
unsigned JBSize = TLI ? TLI->getJumpBufSize() : 0;
|
|
|
|
JBSize = JBSize ? JBSize : 200;
|
|
|
|
const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JBSize);
|
|
|
|
|
|
|
|
{ // The type is recursive, so use a type holder.
|
|
|
|
std::vector<const Type*> Elements;
|
|
|
|
Elements.push_back(JmpBufTy);
|
|
|
|
OpaqueType *OT = OpaqueType::get();
|
2007-12-17 01:12:55 +00:00
|
|
|
Elements.push_back(PointerType::getUnqual(OT));
|
2007-02-22 16:21:18 +00:00
|
|
|
PATypeHolder JBLType(StructType::get(Elements));
|
|
|
|
OT->refineAbstractTypeTo(JBLType.get()); // Complete the cycle.
|
|
|
|
JBLinkTy = JBLType.get();
|
|
|
|
M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);
|
|
|
|
}
|
2004-02-08 19:53:56 +00:00
|
|
|
|
2007-12-17 01:12:55 +00:00
|
|
|
const Type *PtrJBList = PointerType::getUnqual(JBLinkTy);
|
2004-02-08 19:53:56 +00:00
|
|
|
|
2007-02-22 16:21:18 +00: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))) {
|
|
|
|
JBListHead = new GlobalVariable(PtrJBList, false,
|
|
|
|
GlobalValue::LinkOnceLinkage,
|
|
|
|
Constant::getNullValue(PtrJBList),
|
|
|
|
"llvm.sjljeh.jblist", &M);
|
2006-09-05 17:48:07 +00:00
|
|
|
}
|
2007-02-22 16:21:18 +00:00
|
|
|
SetJmpFn = M.getOrInsertFunction("llvm.setjmp", Type::Int32Ty,
|
2007-12-17 01:12:55 +00:00
|
|
|
PointerType::getUnqual(JmpBufTy),
|
|
|
|
(Type *)0);
|
2007-02-22 16:21:18 +00:00
|
|
|
LongJmpFn = M.getOrInsertFunction("llvm.longjmp", Type::VoidTy,
|
2007-12-17 01:12:55 +00:00
|
|
|
PointerType::getUnqual(JmpBufTy),
|
2007-02-22 16:21:18 +00:00
|
|
|
Type::Int32Ty, (Type *)0);
|
2007-02-21 22:49:50 +00:00
|
|
|
}
|
2007-02-22 16:21:18 +00:00
|
|
|
|
|
|
|
// We need the 'write' and 'abort' functions for both models.
|
|
|
|
AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, (Type *)0);
|
2007-04-20 22:40:10 +00:00
|
|
|
#if 0 // "write" is Unix-specific.. code is going away soon anyway.
|
2007-02-22 16:21:18 +00:00
|
|
|
WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::Int32Ty,
|
|
|
|
VoidPtrTy, Type::Int32Ty, (Type *)0);
|
2007-04-20 22:40:10 +00:00
|
|
|
#else
|
|
|
|
WriteFn = 0;
|
|
|
|
#endif
|
2004-11-13 19:07:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-01-07 08:12:01 +00:00
|
|
|
void LowerInvoke::createAbortMessage(Module *M) {
|
2004-11-13 19:07:32 +00:00
|
|
|
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,
|
2007-01-07 08:12:01 +00:00
|
|
|
Msg, "abortmsg", M);
|
2006-12-31 05:48:39 +00:00
|
|
|
std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
|
2007-02-19 07:34:47 +00:00
|
|
|
AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
|
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,
|
2007-01-07 08:12:01 +00:00
|
|
|
Msg, "abortmsg", M);
|
2006-12-31 05:48:39 +00:00
|
|
|
std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
|
2007-02-19 07:34:47 +00:00
|
|
|
AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
|
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) {
|
2007-04-20 22:40:10 +00:00
|
|
|
#if 0
|
2007-01-07 08:12:01 +00:00
|
|
|
if (AbortMessage == 0)
|
|
|
|
createAbortMessage(IB->getParent()->getParent()->getParent());
|
|
|
|
|
|
|
|
// These are the arguments we WANT...
|
2007-02-13 02:10:56 +00:00
|
|
|
Value* Args[3];
|
|
|
|
Args[0] = ConstantInt::get(Type::Int32Ty, 2);
|
|
|
|
Args[1] = AbortMessage;
|
|
|
|
Args[2] = ConstantInt::get(Type::Int32Ty, AbortMessageLength);
|
|
|
|
(new CallInst(WriteFn, Args, 3, "", IB))->setTailCall();
|
2007-04-20 22:40:10 +00:00
|
|
|
#endif
|
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())) {
|
2007-02-13 02:10:56 +00:00
|
|
|
std::vector<Value*> CallArgs(II->op_begin()+3, II->op_end());
|
2003-10-05 19:14:42 +00:00
|
|
|
// Insert a normal call instruction...
|
2005-05-13 06:27:02 +00:00
|
|
|
CallInst *NewCall = new CallInst(II->getCalledValue(),
|
2007-11-27 13:23:08 +00:00
|
|
|
CallArgs.begin(), CallArgs.end(), "",II);
|
2007-02-11 01:37:51 +00:00
|
|
|
NewCall->takeName(II);
|
2005-05-13 06:27:02 +00:00
|
|
|
NewCall->setCallingConv(II->getCallingConv());
|
2007-11-27 13:23:08 +00:00
|
|
|
NewCall->setParamAttrs(II->getParamAttrs());
|
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
|
|
|
|
2005-09-27 21:18:17 +00:00
|
|
|
++NumInvokes; 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()
|
2007-02-13 02:10:56 +00:00
|
|
|
(new CallInst(AbortFn, "", 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
|
|
|
|
2005-09-27 21:18:17 +00:00
|
|
|
++NumUnwinds; Changed = true;
|
2003-10-05 19:14:42 +00:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
2004-02-08 19:53:56 +00:00
|
|
|
|
2005-09-27 21:18:17 +00: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,
|
|
|
|
SwitchInst *CatchSwitch) {
|
2006-12-31 05:48:39 +00:00
|
|
|
ConstantInt *InvokeNoC = ConstantInt::get(Type::Int32Ty, InvokeNo);
|
2005-09-27 21:18:17 +00: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
|
2005-09-29 17:44:20 +00:00
|
|
|
|
|
|
|
BasicBlock::iterator NI = II->getNormalDest()->begin();
|
|
|
|
while (isa<PHINode>(NI)) ++NI;
|
|
|
|
// nonvolatile.
|
2006-12-31 05:48:39 +00:00
|
|
|
new StoreInst(Constant::getNullValue(Type::Int32Ty), InvokeNum, false, NI);
|
2005-09-27 21:18:17 +00:00
|
|
|
|
|
|
|
// Add a switch case to our unwind block.
|
|
|
|
CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
|
|
|
|
|
|
|
|
// Insert a normal call instruction.
|
2007-02-13 02:10:56 +00:00
|
|
|
std::vector<Value*> CallArgs(II->op_begin()+3, II->op_end());
|
2005-09-27 21:18:17 +00:00
|
|
|
CallInst *NewCall = new CallInst(II->getCalledValue(),
|
2007-08-01 03:43:44 +00:00
|
|
|
CallArgs.begin(), CallArgs.end(), "",
|
2005-09-27 21:18:17 +00:00
|
|
|
II);
|
2007-02-11 01:37:51 +00:00
|
|
|
NewCall->takeName(II);
|
2005-09-27 21:18:17 +00:00
|
|
|
NewCall->setCallingConv(II->getCallingConv());
|
2007-11-27 13:23:08 +00:00
|
|
|
NewCall->setParamAttrs(II->getParamAttrs());
|
2005-09-27 21:18:17 +00:00
|
|
|
II->replaceAllUsesWith(NewCall);
|
|
|
|
|
|
|
|
// Replace the invoke with an uncond branch.
|
|
|
|
new BranchInst(II->getNormalDest(), NewCall->getParent());
|
|
|
|
II->eraseFromParent();
|
|
|
|
}
|
2004-02-08 19:53:56 +00:00
|
|
|
|
2005-09-27 21:18:17 +00: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.
|
|
|
|
|
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
|
|
|
|
MarkBlocksLiveIn(*PI, LiveBBs);
|
|
|
|
}
|
2004-02-08 19:53:56 +00:00
|
|
|
|
2005-09-27 21:18:17 +00: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::
|
|
|
|
splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) {
|
|
|
|
// 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-08 19:53:56 +00:00
|
|
|
|
2005-09-27 21:18:17 +00:00
|
|
|
Function *F = Invokes.back()->getParent()->getParent();
|
|
|
|
|
|
|
|
// To avoid having to handle incoming arguments specially, we lower each arg
|
2006-11-27 01:05:10 +00:00
|
|
|
// to a copy instruction in the entry block. This ensures that the argument
|
2005-09-27 21:18:17 +00: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) {
|
2006-11-27 01:05:10 +00:00
|
|
|
// 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);
|
2005-09-27 21:18:17 +00:00
|
|
|
AI->replaceAllUsesWith(NC);
|
2006-11-27 01:05:10 +00:00
|
|
|
// Normally its is 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 to simply
|
|
|
|
// make NC its user.
|
|
|
|
NC->setOperand(0, AI);
|
2005-09-27 21:18:17 +00: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;
|
2005-08-03 18:34:29 +00:00
|
|
|
|
2005-09-27 21:33:12 +00: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;
|
|
|
|
|
2005-09-27 21:18:17 +00:00
|
|
|
// Avoid iterator invalidation by copying users to a temporary vector.
|
|
|
|
std::vector<Instruction*> Users;
|
|
|
|
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-03 18:51:44 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2005-09-27 21:18:17 +00: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();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
2005-09-27 21:18:17 +00:00
|
|
|
// If we decided we need a spill, do it.
|
|
|
|
if (NeedsSpill) {
|
|
|
|
++NumSpilled;
|
|
|
|
DemoteRegToStack(*Inst, true);
|
|
|
|
}
|
2004-02-08 19:53:56 +00:00
|
|
|
}
|
2005-09-27 21:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
|
|
|
|
std::vector<ReturnInst*> Returns;
|
|
|
|
std::vector<UnwindInst*> Unwinds;
|
|
|
|
std::vector<InvokeInst*> Invokes;
|
2004-02-08 19:53:56 +00:00
|
|
|
|
2005-09-27 21:18:17 +00: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-08 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
2005-09-27 21:18:17 +00:00
|
|
|
if (Unwinds.empty() && Invokes.empty()) return false;
|
|
|
|
|
|
|
|
NumInvokes += Invokes.size();
|
|
|
|
NumUnwinds += Unwinds.size();
|
2005-09-27 22:44:59 +00: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-27 21:18:17 +00: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.
|
|
|
|
splitLiveRangesLiveAcrossInvokes(Invokes);
|
|
|
|
|
|
|
|
BasicBlock *EntryBB = F.begin();
|
|
|
|
|
|
|
|
// 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-05 17:48:07 +00:00
|
|
|
unsigned Align = TLI ? TLI->getJumpBufAlignment() : 0;
|
2005-09-27 21:18:17 +00:00
|
|
|
AllocaInst *JmpBuf =
|
2006-09-05 17:48:07 +00:00
|
|
|
new AllocaInst(JBLinkTy, 0, Align, "jblink", F.begin()->begin());
|
2005-09-27 21:18:17 +00:00
|
|
|
|
2004-02-08 19:53:56 +00:00
|
|
|
std::vector<Value*> Idx;
|
2006-12-31 05:48:39 +00:00
|
|
|
Idx.push_back(Constant::getNullValue(Type::Int32Ty));
|
|
|
|
Idx.push_back(ConstantInt::get(Type::Int32Ty, 1));
|
2007-09-04 15:46:09 +00:00
|
|
|
OldJmpBufPtr = new GetElementPtrInst(JmpBuf, Idx.begin(), Idx.end(),
|
|
|
|
"OldBuf", EntryBB->getTerminator());
|
2005-09-27 21:18:17 +00:00
|
|
|
|
|
|
|
// Copy the JBListHead to the alloca.
|
|
|
|
Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true,
|
|
|
|
EntryBB->getTerminator());
|
|
|
|
new StoreInst(OldBuf, OldJmpBufPtr, true, EntryBB->getTerminator());
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
BasicBlock *CatchBB = new BasicBlock("setjmp.catch", &F);
|
|
|
|
|
|
|
|
// Create an alloca which keeps track of which invoke is currently
|
|
|
|
// executing. For normal calls it contains zero.
|
2006-12-31 05:48:39 +00:00
|
|
|
AllocaInst *InvokeNum = new AllocaInst(Type::Int32Ty, 0, "invokenum",
|
2005-09-27 21:18:17 +00:00
|
|
|
EntryBB->begin());
|
2006-12-31 05:48:39 +00:00
|
|
|
new StoreInst(ConstantInt::get(Type::Int32Ty, 0), InvokeNum, true,
|
2005-09-27 21:18:17 +00:00
|
|
|
EntryBB->getTerminator());
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
BasicBlock *UnwindBB = new BasicBlock("unwindbb", &F);
|
|
|
|
Unwinds.push_back(new UnwindInst(UnwindBB));
|
|
|
|
|
|
|
|
Value *CatchLoad = new LoadInst(InvokeNum, "invoke.num", true, CatchBB);
|
|
|
|
SwitchInst *CatchSwitch =
|
|
|
|
new SwitchInst(CatchLoad, UnwindBB, Invokes.size(), CatchBB);
|
|
|
|
|
|
|
|
// Now that things are set up, insert the setjmp call itself.
|
|
|
|
|
|
|
|
// Split the entry block to insert the conditional branch for the setjmp.
|
|
|
|
BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(),
|
|
|
|
"setjmp.cont");
|
|
|
|
|
2006-12-31 05:48:39 +00:00
|
|
|
Idx[1] = ConstantInt::get(Type::Int32Ty, 0);
|
2007-09-04 15:46:09 +00:00
|
|
|
Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx.begin(), Idx.end(),
|
2007-02-12 22:56:41 +00:00
|
|
|
"TheJmpBuf",
|
2005-09-27 21:18:17 +00:00
|
|
|
EntryBB->getTerminator());
|
|
|
|
Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret",
|
|
|
|
EntryBB->getTerminator());
|
|
|
|
|
|
|
|
// Compare the return value to zero.
|
2006-12-23 06:05:41 +00:00
|
|
|
Value *IsNormal = new ICmpInst(ICmpInst::ICMP_EQ, SJRet,
|
|
|
|
Constant::getNullValue(SJRet->getType()),
|
|
|
|
"notunwind", EntryBB->getTerminator());
|
2005-09-27 21:18:17 +00:00
|
|
|
// Nuke the uncond branch.
|
|
|
|
EntryBB->getTerminator()->eraseFromParent();
|
|
|
|
|
|
|
|
// Put in a new condbranch in its place.
|
|
|
|
new BranchInst(ContBlock, CatchBB, IsNormal, EntryBB);
|
|
|
|
|
|
|
|
// At this point, we are all set up, rewrite each invoke instruction.
|
|
|
|
for (unsigned i = 0, e = Invokes.size(); i != e; ++i)
|
|
|
|
rewriteExpensiveInvoke(Invokes[i], i+1, InvokeNum, CatchSwitch);
|
2004-02-08 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
2005-09-27 21:18:17 +00:00
|
|
|
// We know that there is at least one unwind.
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
BasicBlock *UnwindHandler = new BasicBlock("dounwind", &F);
|
|
|
|
BasicBlock *UnwindBlock = new BasicBlock("unwind", &F);
|
|
|
|
BasicBlock *TermBlock = new BasicBlock("unwinderror", &F);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the JBList, if it's null, then there was no catch!
|
2006-12-23 06:05:41 +00:00
|
|
|
Value *NotNull = new ICmpInst(ICmpInst::ICMP_NE, BufPtr,
|
|
|
|
Constant::getNullValue(BufPtr->getType()),
|
|
|
|
"notnull", UnwindHandler);
|
2005-09-27 21:18:17 +00:00
|
|
|
new BranchInst(UnwindBlock, TermBlock, NotNull, UnwindHandler);
|
|
|
|
|
|
|
|
// Create the block to do the longjmp.
|
|
|
|
// Get a pointer to the jmpbuf and longjmp.
|
|
|
|
std::vector<Value*> Idx;
|
2006-12-31 05:48:39 +00:00
|
|
|
Idx.push_back(Constant::getNullValue(Type::Int32Ty));
|
|
|
|
Idx.push_back(ConstantInt::get(Type::Int32Ty, 0));
|
2007-09-04 15:46:09 +00:00
|
|
|
Idx[0] = new GetElementPtrInst(BufPtr, Idx.begin(), Idx.end(), "JmpBuf",
|
|
|
|
UnwindBlock);
|
2006-12-31 05:48:39 +00:00
|
|
|
Idx[1] = ConstantInt::get(Type::Int32Ty, 1);
|
2007-08-01 03:43:44 +00:00
|
|
|
new CallInst(LongJmpFn, Idx.begin(), Idx.end(), "", UnwindBlock);
|
2005-09-27 21:18:17 +00:00
|
|
|
new UnreachableInst(UnwindBlock);
|
|
|
|
|
|
|
|
// Set up the term block ("throw without a catch").
|
|
|
|
new UnreachableInst(TermBlock);
|
|
|
|
|
|
|
|
// Insert a new call to write(2, AbortMessage, AbortMessageLength);
|
|
|
|
writeAbortMessage(TermBlock->getTerminator());
|
|
|
|
|
|
|
|
// Insert a call to abort()
|
2007-02-13 02:10:56 +00:00
|
|
|
(new CallInst(AbortFn, "",
|
2005-09-27 21:18:17 +00:00
|
|
|
TermBlock->getTerminator()))->setTailCall();
|
|
|
|
|
|
|
|
|
|
|
|
// Replace all unwinds with a branch to the unwind handler.
|
|
|
|
for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) {
|
|
|
|
new BranchInst(UnwindHandler, Unwinds[i]);
|
|
|
|
Unwinds[i]->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2004-02-08 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LowerInvoke::runOnFunction(Function &F) {
|
|
|
|
if (ExpensiveEHSupport)
|
|
|
|
return insertExpensiveEHSupport(F);
|
|
|
|
else
|
|
|
|
return insertCheapEHSupport(F);
|
|
|
|
}
|