#define DEBUG_TYPE "lowerinvoke"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/Intrinsics.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Target/TargetLowering.h"
#include <csetjmp>
#include <set>
using namespace llvm;
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",
cl::desc("Make the -lowerinvoke pass insert expensive, but correct, EH code"));
namespace {
class VISIBILITY_HIDDEN LowerInvoke : public FunctionPass {
Constant *WriteFn;
Constant *AbortFn;
Value *AbortMessage;
unsigned AbortMessageLength;
const Type *JBLinkTy;
GlobalVariable *JBListHead;
Constant *SetJmpFn, *LongJmpFn;
const TargetLowering *TLI;
public:
static char ID; explicit LowerInvoke(const TargetLowering *tli = NULL)
: FunctionPass(&ID), TLI(tli) { }
bool doInitialization(Module &M);
bool runOnFunction(Function &F);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addPreservedID(PromoteMemoryToRegisterID);
AU.addPreservedID(LowerSwitchID);
AU.addPreservedID(LowerAllocationsID);
}
private:
void createAbortMessage(Module *M);
void writeAbortMessage(Instruction *IB);
bool insertCheapEHSupport(Function &F);
void splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes);
void rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
AllocaInst *InvokeNum, SwitchInst *CatchSwitch);
bool insertExpensiveEHSupport(Function &F);
};
}
char LowerInvoke::ID = 0;
static RegisterPass<LowerInvoke>
X("lowerinvoke", "Lower invoke and unwind, for unwindless code generators");
const PassInfo *const llvm::LowerInvokePassID = &X;
FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI) {
return new LowerInvoke(TLI);
}
bool LowerInvoke::doInitialization(Module &M) {
const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
AbortMessage = 0;
if (ExpensiveEHSupport) {
unsigned JBSize = TLI ? TLI->getJumpBufSize() : 0;
JBSize = JBSize ? JBSize : 200;
const Type *JmpBufTy = ArrayType::get(VoidPtrTy, JBSize);
{ std::vector<const Type*> Elements;
Elements.push_back(JmpBufTy);
OpaqueType *OT = OpaqueType::get();
Elements.push_back(PointerType::getUnqual(OT));
PATypeHolder JBLType(StructType::get(Elements));
OT->refineAbstractTypeTo(JBLType.get()); JBLinkTy = JBLType.get();
M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);
}
const Type *PtrJBList = PointerType::getUnqual(JBLinkTy);
if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) {
JBListHead = new GlobalVariable(PtrJBList, false,
GlobalValue::LinkOnceAnyLinkage,
Constant::getNullValue(PtrJBList),
"llvm.sjljeh.jblist", &M);
}
#if defined(_MSC_VER) && defined(setjmp)
#define setjmp_undefined_for_visual_studio
#undef setjmp
#endif
SetJmpFn = Intrinsic::getDeclaration(&M, Intrinsic::setjmp);
#if defined(_MSC_VER) && defined(setjmp_undefined_for_visual_studio)
#define setjmp _setjmp
#endif
LongJmpFn = Intrinsic::getDeclaration(&M, Intrinsic::longjmp);
}
AbortFn = M.getOrInsertFunction("abort", Type::VoidTy, (Type *)0);
#if 0 // "write" is Unix-specific.. code is going away soon anyway.
WriteFn = M.getOrInsertFunction("write", Type::VoidTy, Type::Int32Ty,
VoidPtrTy, Type::Int32Ty, (Type *)0);
#else
WriteFn = 0;
#endif
return true;
}
void LowerInvoke::createAbortMessage(Module *M) {
if (ExpensiveEHSupport) {
Constant *Msg =
ConstantArray::get("ERROR: Exception thrown, but not caught!\n");
AbortMessageLength = Msg->getNumOperands()-1;
GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
GlobalValue::InternalLinkage,
Msg, "abortmsg", M);
std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
} else {
Constant *Msg =
ConstantArray::get("Exception handler needed, but not enabled. Recompile"
" program with -enable-correct-eh-support.\n");
AbortMessageLength = Msg->getNumOperands()-1;
GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true,
GlobalValue::InternalLinkage,
Msg, "abortmsg", M);
std::vector<Constant*> GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
}
}
void LowerInvoke::writeAbortMessage(Instruction *IB) {
#if 0
if (AbortMessage == 0)
createAbortMessage(IB->getParent()->getParent()->getParent());
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();
#endif
}
bool LowerInvoke::insertCheapEHSupport(Function &F) {
bool Changed = false;
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
std::vector<Value*> CallArgs(II->op_begin()+3, II->op_end());
CallInst *NewCall = CallInst::Create(II->getCalledValue(),
CallArgs.begin(), CallArgs.end(), "",II);
NewCall->takeName(II);
NewCall->setCallingConv(II->getCallingConv());
NewCall->setAttributes(II->getAttributes());
II->replaceAllUsesWith(NewCall);
BranchInst::Create(II->getNormalDest(), II);
II->getUnwindDest()->removePredecessor(BB);
BB->getInstList().erase(II);
++NumInvokes; Changed = true;
} else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
writeAbortMessage(UI);
CallInst::Create(AbortFn, "", UI)->setTailCall();
ReturnInst::Create(F.getReturnType() == Type::VoidTy ? 0 :
Constant::getNullValue(F.getReturnType()), UI);
BB->getInstList().erase(UI);
++NumUnwinds; Changed = true;
}
return Changed;
}
void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
AllocaInst *InvokeNum,
SwitchInst *CatchSwitch) {
ConstantInt *InvokeNoC = ConstantInt::get(Type::Int32Ty, InvokeNo);
if (isa<PHINode>(II->getUnwindDest()->begin())) {
SplitCriticalEdge(II, 1, this);
while (PHINode *PN = dyn_cast<PHINode>(II->getUnwindDest()->begin())) {
PN->replaceAllUsesWith(PN->getIncomingValue(0));
PN->eraseFromParent();
}
}
new StoreInst(InvokeNoC, InvokeNum, true, II);
BasicBlock::iterator NI = II->getNormalDest()->getFirstNonPHI();
new StoreInst(Constant::getNullValue(Type::Int32Ty), InvokeNum, false, NI);
CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
std::vector<Value*> CallArgs(II->op_begin()+3, II->op_end());
CallInst *NewCall = CallInst::Create(II->getCalledValue(),
CallArgs.begin(), CallArgs.end(), "",
II);
NewCall->takeName(II);
NewCall->setCallingConv(II->getCallingConv());
NewCall->setAttributes(II->getAttributes());
II->replaceAllUsesWith(NewCall);
BranchInst::Create(II->getNormalDest(), NewCall->getParent());
II->eraseFromParent();
}
static void MarkBlocksLiveIn(BasicBlock *BB, std::set<BasicBlock*> &LiveBBs) {
if (!LiveBBs.insert(BB).second) return;
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
MarkBlocksLiveIn(*PI, LiveBBs);
}
void LowerInvoke::
splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) {
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?");
}
Function *F = Invokes.back()->getParent()->getParent();
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) {
CastInst *NC = new BitCastInst(
AI, AI->getType(), AI->getName()+".tmp", AfterAllocaInsertPt);
AI->replaceAllUsesWith(NC);
NC->setOperand(0, AI);
}
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
Instruction *Inst = II;
if (Inst->use_empty()) continue;
if (Inst->hasOneUse() &&
cast<Instruction>(Inst->use_back())->getParent() == BB &&
!isa<PHINode>(Inst->use_back())) continue;
if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin())
continue;
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);
}
std::set<InvokeInst*> InvokesWithStoreInserted;
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 {
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);
}
}
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;
}
}
if (NeedsSpill) {
++NumSpilled;
DemoteRegToStack(*Inst, true);
}
}
}
bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
std::vector<ReturnInst*> Returns;
std::vector<UnwindInst*> Unwinds;
std::vector<InvokeInst*> Invokes;
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
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);
}
if (Unwinds.empty() && Invokes.empty()) return false;
NumInvokes += Invokes.size();
NumUnwinds += Unwinds.size();
Value *OldJmpBufPtr = 0;
if (!Invokes.empty()) {
splitLiveRangesLiveAcrossInvokes(Invokes);
BasicBlock *EntryBB = F.begin();
unsigned Align = TLI ? TLI->getJumpBufAlignment() : 0;
AllocaInst *JmpBuf =
new AllocaInst(JBLinkTy, 0, Align, "jblink", F.begin()->begin());
std::vector<Value*> Idx;
Idx.push_back(Constant::getNullValue(Type::Int32Ty));
Idx.push_back(ConstantInt::get(Type::Int32Ty, 1));
OldJmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx.begin(), Idx.end(),
"OldBuf", EntryBB->getTerminator());
Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true,
EntryBB->getTerminator());
new StoreInst(OldBuf, OldJmpBufPtr, true, EntryBB->getTerminator());
new StoreInst(JmpBuf, JBListHead, true, EntryBB->getTerminator());
BasicBlock *CatchBB = BasicBlock::Create("setjmp.catch", &F);
AllocaInst *InvokeNum = new AllocaInst(Type::Int32Ty, 0, "invokenum",
EntryBB->begin());
new StoreInst(ConstantInt::get(Type::Int32Ty, 0), InvokeNum, true,
EntryBB->getTerminator());
BasicBlock *UnwindBB = BasicBlock::Create("unwindbb", &F);
Unwinds.push_back(new UnwindInst(UnwindBB));
Value *CatchLoad = new LoadInst(InvokeNum, "invoke.num", true, CatchBB);
SwitchInst *CatchSwitch =
SwitchInst::Create(CatchLoad, UnwindBB, Invokes.size(), CatchBB);
BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(),
"setjmp.cont");
Idx[1] = ConstantInt::get(Type::Int32Ty, 0);
Value *JmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx.begin(), Idx.end(),
"TheJmpBuf",
EntryBB->getTerminator());
JmpBufPtr = new BitCastInst(JmpBufPtr, PointerType::getUnqual(Type::Int8Ty),
"tmp", EntryBB->getTerminator());
Value *SJRet = CallInst::Create(SetJmpFn, JmpBufPtr, "sjret",
EntryBB->getTerminator());
Value *IsNormal = new ICmpInst(ICmpInst::ICMP_EQ, SJRet,
Constant::getNullValue(SJRet->getType()),
"notunwind", EntryBB->getTerminator());
EntryBB->getTerminator()->eraseFromParent();
BranchInst::Create(ContBlock, CatchBB, IsNormal, EntryBB);
for (unsigned i = 0, e = Invokes.size(); i != e; ++i)
rewriteExpensiveInvoke(Invokes[i], i+1, InvokeNum, CatchSwitch);
}
BasicBlock *UnwindHandler = BasicBlock::Create("dounwind", &F);
BasicBlock *UnwindBlock = BasicBlock::Create("unwind", &F);
BasicBlock *TermBlock = BasicBlock::Create("unwinderror", &F);
Value *BufPtr;
if (OldJmpBufPtr) {
BufPtr = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", UnwindHandler);
new StoreInst(BufPtr, JBListHead, UnwindHandler);
} else {
BufPtr = new LoadInst(JBListHead, "ehlist", UnwindHandler);
}
Value *NotNull = new ICmpInst(ICmpInst::ICMP_NE, BufPtr,
Constant::getNullValue(BufPtr->getType()),
"notnull", UnwindHandler);
BranchInst::Create(UnwindBlock, TermBlock, NotNull, UnwindHandler);
std::vector<Value*> Idx;
Idx.push_back(Constant::getNullValue(Type::Int32Ty));
Idx.push_back(ConstantInt::get(Type::Int32Ty, 0));
Idx[0] = GetElementPtrInst::Create(BufPtr, Idx.begin(), Idx.end(), "JmpBuf",
UnwindBlock);
Idx[0] = new BitCastInst(Idx[0], PointerType::getUnqual(Type::Int8Ty),
"tmp", UnwindBlock);
Idx[1] = ConstantInt::get(Type::Int32Ty, 1);
CallInst::Create(LongJmpFn, Idx.begin(), Idx.end(), "", UnwindBlock);
new UnreachableInst(UnwindBlock);
new UnreachableInst(TermBlock);
writeAbortMessage(TermBlock->getTerminator());
CallInst::Create(AbortFn, "",
TermBlock->getTerminator())->setTailCall();
for (unsigned i = 0, e = Unwinds.size(); i != e; ++i) {
BranchInst::Create(UnwindHandler, Unwinds[i]);
Unwinds[i]->eraseFromParent();
}
if (OldJmpBufPtr) {
for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
ReturnInst *R = Returns[i];
Value *OldBuf = new LoadInst(OldJmpBufPtr, "oldjmpbufptr", true, R);
new StoreInst(OldBuf, JBListHead, true, R);
}
}
return true;
}
bool LowerInvoke::runOnFunction(Function &F) {
if (ExpensiveEHSupport)
return insertExpensiveEHSupport(F);
else
return insertCheapEHSupport(F);
}