mirror of
https://github.com/RPCS3/llvm.git
synced 2024-11-24 20:30:06 +00:00
Fix a problem that lower invoke has with allocas (PR6694), and
add a version of createLowerInvokePass that allows the client to specify whether it wants "expensive" or "cheap" lowering. Patch by Alex Mac! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@102402 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
d61647d306
commit
c6f0aadc3b
@ -241,6 +241,8 @@ extern const PassInfo *const LowerSwitchID;
|
|||||||
// lowering pass.
|
// lowering pass.
|
||||||
//
|
//
|
||||||
FunctionPass *createLowerInvokePass(const TargetLowering *TLI = 0);
|
FunctionPass *createLowerInvokePass(const TargetLowering *TLI = 0);
|
||||||
|
FunctionPass *createLowerInvokePass(const TargetLowering *TLI,
|
||||||
|
bool useExpensiveEHSupport);
|
||||||
extern const PassInfo *const LowerInvokePassID;
|
extern const PassInfo *const LowerInvokePassID;
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
@ -70,15 +70,18 @@ namespace {
|
|||||||
// Used for expensive EH support.
|
// Used for expensive EH support.
|
||||||
const Type *JBLinkTy;
|
const Type *JBLinkTy;
|
||||||
GlobalVariable *JBListHead;
|
GlobalVariable *JBListHead;
|
||||||
Constant *SetJmpFn, *LongJmpFn;
|
Constant *SetJmpFn, *LongJmpFn, *StackSaveFn, *StackRestoreFn;
|
||||||
|
bool useExpensiveEHSupport;
|
||||||
|
|
||||||
// We peek in TLI to grab the target's jmp_buf size and alignment
|
// We peek in TLI to grab the target's jmp_buf size and alignment
|
||||||
const TargetLowering *TLI;
|
const TargetLowering *TLI;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static char ID; // Pass identification, replacement for typeid
|
static char ID; // Pass identification, replacement for typeid
|
||||||
explicit LowerInvoke(const TargetLowering *tli = NULL)
|
explicit LowerInvoke(const TargetLowering *tli = NULL,
|
||||||
: FunctionPass(&ID), TLI(tli) { }
|
bool useExpensiveEHSupport = ExpensiveEHSupport)
|
||||||
|
: FunctionPass(&ID), useExpensiveEHSupport(useExpensiveEHSupport),
|
||||||
|
TLI(tli) { }
|
||||||
bool doInitialization(Module &M);
|
bool doInitialization(Module &M);
|
||||||
bool runOnFunction(Function &F);
|
bool runOnFunction(Function &F);
|
||||||
|
|
||||||
@ -94,7 +97,8 @@ namespace {
|
|||||||
bool insertCheapEHSupport(Function &F);
|
bool insertCheapEHSupport(Function &F);
|
||||||
void splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes);
|
void splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes);
|
||||||
void rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
|
void rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
|
||||||
AllocaInst *InvokeNum, SwitchInst *CatchSwitch);
|
AllocaInst *InvokeNum, AllocaInst *StackPtr,
|
||||||
|
SwitchInst *CatchSwitch);
|
||||||
bool insertExpensiveEHSupport(Function &F);
|
bool insertExpensiveEHSupport(Function &F);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -107,7 +111,11 @@ const PassInfo *const llvm::LowerInvokePassID = &X;
|
|||||||
|
|
||||||
// Public Interface To the LowerInvoke pass.
|
// Public Interface To the LowerInvoke pass.
|
||||||
FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI) {
|
FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI) {
|
||||||
return new LowerInvoke(TLI);
|
return new LowerInvoke(TLI, ExpensiveEHSupport);
|
||||||
|
}
|
||||||
|
FunctionPass *llvm::createLowerInvokePass(const TargetLowering *TLI,
|
||||||
|
bool useExpensiveEHSupport) {
|
||||||
|
return new LowerInvoke(TLI, useExpensiveEHSupport);
|
||||||
}
|
}
|
||||||
|
|
||||||
// doInitialization - Make sure that there is a prototype for abort in the
|
// doInitialization - Make sure that there is a prototype for abort in the
|
||||||
@ -116,7 +124,7 @@ bool LowerInvoke::doInitialization(Module &M) {
|
|||||||
const Type *VoidPtrTy =
|
const Type *VoidPtrTy =
|
||||||
Type::getInt8PtrTy(M.getContext());
|
Type::getInt8PtrTy(M.getContext());
|
||||||
AbortMessage = 0;
|
AbortMessage = 0;
|
||||||
if (ExpensiveEHSupport) {
|
if (useExpensiveEHSupport) {
|
||||||
// Insert a type for the linked list of jump buffers.
|
// Insert a type for the linked list of jump buffers.
|
||||||
unsigned JBSize = TLI ? TLI->getJumpBufSize() : 0;
|
unsigned JBSize = TLI ? TLI->getJumpBufSize() : 0;
|
||||||
JBSize = JBSize ? JBSize : 200;
|
JBSize = JBSize ? JBSize : 200;
|
||||||
@ -160,6 +168,8 @@ bool LowerInvoke::doInitialization(Module &M) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
LongJmpFn = Intrinsic::getDeclaration(&M, Intrinsic::longjmp);
|
LongJmpFn = Intrinsic::getDeclaration(&M, Intrinsic::longjmp);
|
||||||
|
StackSaveFn = Intrinsic::getDeclaration(&M, Intrinsic::stacksave);
|
||||||
|
StackRestoreFn = Intrinsic::getDeclaration(&M, Intrinsic::stackrestore);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need the 'write' and 'abort' functions for both models.
|
// We need the 'write' and 'abort' functions for both models.
|
||||||
@ -175,7 +185,7 @@ bool LowerInvoke::doInitialization(Module &M) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LowerInvoke::createAbortMessage(Module *M) {
|
void LowerInvoke::createAbortMessage(Module *M) {
|
||||||
if (ExpensiveEHSupport) {
|
if (useExpensiveEHSupport) {
|
||||||
// The abort message for expensive EH support tells the user that the
|
// The abort message for expensive EH support tells the user that the
|
||||||
// program 'unwound' without an 'invoke' instruction.
|
// program 'unwound' without an 'invoke' instruction.
|
||||||
Constant *Msg =
|
Constant *Msg =
|
||||||
@ -229,7 +239,8 @@ bool LowerInvoke::insertCheapEHSupport(Function &F) {
|
|||||||
std::vector<Value*> CallArgs(II->op_begin(), II->op_end() - 3);
|
std::vector<Value*> CallArgs(II->op_begin(), II->op_end() - 3);
|
||||||
// Insert a normal call instruction...
|
// Insert a normal call instruction...
|
||||||
CallInst *NewCall = CallInst::Create(II->getCalledValue(),
|
CallInst *NewCall = CallInst::Create(II->getCalledValue(),
|
||||||
CallArgs.begin(), CallArgs.end(), "",II);
|
CallArgs.begin(), CallArgs.end(),
|
||||||
|
"",II);
|
||||||
NewCall->takeName(II);
|
NewCall->takeName(II);
|
||||||
NewCall->setCallingConv(II->getCallingConv());
|
NewCall->setCallingConv(II->getCallingConv());
|
||||||
NewCall->setAttributes(II->getAttributes());
|
NewCall->setAttributes(II->getAttributes());
|
||||||
@ -270,6 +281,7 @@ bool LowerInvoke::insertCheapEHSupport(Function &F) {
|
|||||||
/// specified invoke instruction with a call.
|
/// specified invoke instruction with a call.
|
||||||
void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
|
void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
|
||||||
AllocaInst *InvokeNum,
|
AllocaInst *InvokeNum,
|
||||||
|
AllocaInst *StackPtr,
|
||||||
SwitchInst *CatchSwitch) {
|
SwitchInst *CatchSwitch) {
|
||||||
ConstantInt *InvokeNoC = ConstantInt::get(Type::getInt32Ty(II->getContext()),
|
ConstantInt *InvokeNoC = ConstantInt::get(Type::getInt32Ty(II->getContext()),
|
||||||
InvokeNo);
|
InvokeNo);
|
||||||
@ -289,11 +301,21 @@ void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
|
|||||||
// location afterward.
|
// location afterward.
|
||||||
new StoreInst(InvokeNoC, InvokeNum, true, II); // volatile
|
new StoreInst(InvokeNoC, InvokeNum, true, II); // volatile
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
BasicBlock::iterator NI = II->getNormalDest()->getFirstNonPHI();
|
BasicBlock::iterator NI = II->getNormalDest()->getFirstNonPHI();
|
||||||
// nonvolatile.
|
// nonvolatile.
|
||||||
new StoreInst(Constant::getNullValue(Type::getInt32Ty(II->getContext())),
|
new StoreInst(Constant::getNullValue(Type::getInt32Ty(II->getContext())),
|
||||||
InvokeNum, false, NI);
|
InvokeNum, false, NI);
|
||||||
|
|
||||||
|
Instruction* StackPtrLoad = new LoadInst(StackPtr, "stackptr.restore", true,
|
||||||
|
II->getUnwindDest()->getFirstNonPHI()
|
||||||
|
);
|
||||||
|
CallInst::Create(StackRestoreFn, StackPtrLoad, "")->insertAfter(StackPtrLoad);
|
||||||
|
|
||||||
// Add a switch case to our unwind block.
|
// Add a switch case to our unwind block.
|
||||||
CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
|
CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
|
||||||
|
|
||||||
@ -500,6 +522,12 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
|
|||||||
BasicBlock *CatchBB =
|
BasicBlock *CatchBB =
|
||||||
BasicBlock::Create(F.getContext(), "setjmp.catch", &F);
|
BasicBlock::Create(F.getContext(), "setjmp.catch", &F);
|
||||||
|
|
||||||
|
// 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());
|
||||||
|
|
||||||
// Create an alloca which keeps track of which invoke is currently
|
// Create an alloca which keeps track of which invoke is currently
|
||||||
// executing. For normal calls it contains zero.
|
// executing. For normal calls it contains zero.
|
||||||
AllocaInst *InvokeNum = new AllocaInst(Type::getInt32Ty(F.getContext()), 0,
|
AllocaInst *InvokeNum = new AllocaInst(Type::getInt32Ty(F.getContext()), 0,
|
||||||
@ -546,7 +574,7 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
|
|||||||
|
|
||||||
// At this point, we are all set up, rewrite each invoke instruction.
|
// At this point, we are all set up, rewrite each invoke instruction.
|
||||||
for (unsigned i = 0, e = Invokes.size(); i != e; ++i)
|
for (unsigned i = 0, e = Invokes.size(); i != e; ++i)
|
||||||
rewriteExpensiveInvoke(Invokes[i], i+1, InvokeNum, CatchSwitch);
|
rewriteExpensiveInvoke(Invokes[i], i+1, InvokeNum, StackPtr, CatchSwitch);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We know that there is at least one unwind.
|
// We know that there is at least one unwind.
|
||||||
@ -622,7 +650,7 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool LowerInvoke::runOnFunction(Function &F) {
|
bool LowerInvoke::runOnFunction(Function &F) {
|
||||||
if (ExpensiveEHSupport)
|
if (useExpensiveEHSupport)
|
||||||
return insertExpensiveEHSupport(F);
|
return insertExpensiveEHSupport(F);
|
||||||
else
|
else
|
||||||
return insertCheapEHSupport(F);
|
return insertCheapEHSupport(F);
|
||||||
|
Loading…
Reference in New Issue
Block a user