diff options
author | Douglas Gregor <dgregor@apple.com> | 2010-05-16 01:24:12 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2010-05-16 01:24:12 +0000 |
commit | 86a3a03667bdb0dcab7e6a2877dfd234b07a6d43 (patch) | |
tree | 4d7a3c092872a94b8b37bbd476c9a2857ec1dfa4 /lib/CodeGen/CGDeclCXX.cpp | |
parent | 1eb2e59338c4b9c0429fc39ca98662adc9e7a3f2 (diff) |
When initializing thread-safe statics, put the call to
__cxa_guard_abort along the exceptional edge into (in effect) a nested
"try" that rethrows after aborting. Fixes PR7144 and the remaining
Boost.ProgramOptions failures, along with the regressions that r103880
caused.
The crucial difference between this and r103880 is that we now follow
LLVM's little dance with the llvm.eh.exception and llvm.eh.selector
calls, then use _Unwind_Resume_or_Rethrow to rethrow.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103892 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CGDeclCXX.cpp')
-rw-r--r-- | lib/CodeGen/CGDeclCXX.cpp | 72 |
1 files changed, 64 insertions, 8 deletions
diff --git a/lib/CodeGen/CGDeclCXX.cpp b/lib/CodeGen/CGDeclCXX.cpp index 036be92885..f94ddd9886 100644 --- a/lib/CodeGen/CGDeclCXX.cpp +++ b/lib/CodeGen/CGDeclCXX.cpp @@ -13,6 +13,8 @@ #include "CodeGenFunction.h" #include "clang/CodeGen/CodeGenOptions.h" +#include "llvm/Intrinsics.h" + using namespace clang; using namespace CodeGen; @@ -321,7 +323,10 @@ CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, EmitBlock(InitCheckBlock); - if (ThreadsafeStatics) { + // Variables used when coping with thread-safe statics and exceptions. + llvm::BasicBlock *SavedLandingPad = 0; + llvm::BasicBlock *LandingPad = 0; + if (ThreadsafeStatics) { // Call __cxa_guard_acquire. V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable); @@ -330,14 +335,13 @@ CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), InitBlock, EndBlock); - EmitBlock(InitBlock); - if (Exceptions) { - EHCleanupBlock Cleanup(*this); - - // Call __cxa_guard_abort. - Builder.CreateCall(getGuardAbortFn(*this), GuardVariable); + SavedLandingPad = getInvokeDest(); + LandingPad = createBasicBlock("guard.lpad"); + setInvokeDest(LandingPad); } + + EmitBlock(InitBlock); } if (D.getType()->isReferenceType()) { @@ -353,7 +357,7 @@ CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, if (ThreadsafeStatics) { // Call __cxa_guard_release. - Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable); + Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable); } else { llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1); @@ -364,5 +368,57 @@ CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, if (!D.getType()->isReferenceType()) EmitDeclDestroy(*this, D, GV); + if (ThreadsafeStatics && Exceptions) { + // If an exception is thrown during initialization, call __cxa_guard_abort + // along the exceptional edge. + EmitBranch(EndBlock); + + // Construct the landing pad. + EmitBlock(LandingPad); + + // Personality function and LLVM intrinsics. + llvm::Constant *Personality = + CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty + (VMContext), + true), + "__gxx_personality_v0"); + Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty); + llvm::Value *llvm_eh_exception = + CGM.getIntrinsic(llvm::Intrinsic::eh_exception); + llvm::Value *llvm_eh_selector = + CGM.getIntrinsic(llvm::Intrinsic::eh_selector); + + // Exception object + llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc"); + llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow"); + + // Call the selector function. + const llvm::PointerType *PtrToInt8Ty + = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); + llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty); + llvm::Value* SelectorArgs[3] = { Exc, Personality, Null }; + Builder.CreateCall(llvm_eh_selector, SelectorArgs, SelectorArgs + 3, + "selector"); + Builder.CreateStore(Exc, RethrowPtr); + + // Call __cxa_guard_abort along the exceptional edge. + Builder.CreateCall(getGuardAbortFn(*this), GuardVariable); + + setInvokeDest(SavedLandingPad); + + // Rethrow the current exception. + if (getInvokeDest()) { + llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); + Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont, + getInvokeDest(), + Builder.CreateLoad(RethrowPtr)); + EmitBlock(Cont); + } else + Builder.CreateCall(getUnwindResumeOrRethrowFn(), + Builder.CreateLoad(RethrowPtr)); + + Builder.CreateUnreachable(); + } + EmitBlock(EndBlock); } |