forked from OSchip/llvm-project
When an exception needs to be freed by calling __cxa_exception_free, make sure to stash away the exception pointer somewhere.
This fixes an "Instruction does not dominate all uses!" verification error when compiling TableGen. llvm-svn: 91084
This commit is contained in:
parent
ff141799fb
commit
afd1edb52e
|
@ -127,21 +127,24 @@ static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
|
// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
|
||||||
// N is casted to the right type.
|
// DestPtr is casted to the right type.
|
||||||
static void CopyObject(CodeGenFunction &CGF, const Expr *E, llvm::Value *N) {
|
static void CopyObject(CodeGenFunction &CGF, const Expr *E,
|
||||||
|
llvm::Value *DestPtr, llvm::Value *ExceptionPtrPtr) {
|
||||||
QualType ObjectType = E->getType();
|
QualType ObjectType = E->getType();
|
||||||
|
|
||||||
// Store the throw exception in the exception object.
|
// Store the throw exception in the exception object.
|
||||||
if (!CGF.hasAggregateLLVMType(ObjectType)) {
|
if (!CGF.hasAggregateLLVMType(ObjectType)) {
|
||||||
llvm::Value *Value = CGF.EmitScalarExpr(E);
|
llvm::Value *Value = CGF.EmitScalarExpr(E);
|
||||||
const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
|
const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo();
|
||||||
|
|
||||||
CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
|
CGF.Builder.CreateStore(Value,
|
||||||
|
CGF.Builder.CreateBitCast(DestPtr, ValuePtrTy));
|
||||||
} else {
|
} else {
|
||||||
const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
|
const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo();
|
||||||
const CXXRecordDecl *RD;
|
const CXXRecordDecl *RD =
|
||||||
RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
|
cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
|
||||||
llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
|
|
||||||
|
llvm::Value *This = CGF.Builder.CreateBitCast(DestPtr, Ty);
|
||||||
if (RD->hasTrivialCopyConstructor()) {
|
if (RD->hasTrivialCopyConstructor()) {
|
||||||
CGF.EmitAggExpr(E, This, false);
|
CGF.EmitAggExpr(E, This, false);
|
||||||
} else if (CXXConstructorDecl *CopyCtor
|
} else if (CXXConstructorDecl *CopyCtor
|
||||||
|
@ -150,9 +153,9 @@ static void CopyObject(CodeGenFunction &CGF, const Expr *E, llvm::Value *N) {
|
||||||
if (CGF.Exceptions) {
|
if (CGF.Exceptions) {
|
||||||
CodeGenFunction::EHCleanupBlock Cleanup(CGF);
|
CodeGenFunction::EHCleanupBlock Cleanup(CGF);
|
||||||
llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
|
llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
|
||||||
const llvm::Type *Int8PtrTy
|
|
||||||
= llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
|
// Load the exception pointer.
|
||||||
llvm::Value *ExceptionPtr = CGF.Builder.CreateBitCast(N, Int8PtrTy);
|
llvm::Value *ExceptionPtr = CGF.Builder.CreateLoad(ExceptionPtrPtr);
|
||||||
CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
|
CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,7 +255,12 @@ void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
|
||||||
llvm::ConstantInt::get(SizeTy, TypeSize),
|
llvm::ConstantInt::get(SizeTy, TypeSize),
|
||||||
"exception");
|
"exception");
|
||||||
|
|
||||||
CopyObject(*this, E->getSubExpr(), ExceptionPtr);
|
llvm::Value *ExceptionPtrPtr =
|
||||||
|
CreateTempAlloca(ExceptionPtr->getType(), "exception.ptr");
|
||||||
|
Builder.CreateStore(ExceptionPtr, ExceptionPtrPtr);
|
||||||
|
|
||||||
|
|
||||||
|
CopyObject(*this, E->getSubExpr(), ExceptionPtr, ExceptionPtrPtr);
|
||||||
|
|
||||||
// Now throw the exception.
|
// Now throw the exception.
|
||||||
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
|
||||||
|
|
|
@ -11,7 +11,9 @@ void test1() {
|
||||||
|
|
||||||
// CHECK: define void @_Z5test1v() nounwind {
|
// CHECK: define void @_Z5test1v() nounwind {
|
||||||
// CHECK-NEXT:entry:
|
// CHECK-NEXT:entry:
|
||||||
|
// CHECK-NEXT: %exception.ptr = alloca i8*
|
||||||
// CHECK-NEXT: %exception = call i8* @__cxa_allocate_exception(i64 8)
|
// CHECK-NEXT: %exception = call i8* @__cxa_allocate_exception(i64 8)
|
||||||
|
// CHECK-NEXT: store i8* %exception, i8** %exception.ptr
|
||||||
// CHECK-NEXT: %0 = bitcast i8* %exception to %struct.test1_D*
|
// CHECK-NEXT: %0 = bitcast i8* %exception to %struct.test1_D*
|
||||||
// CHECK-NEXT: %tmp = bitcast %struct.test1_D* %0 to i8*
|
// CHECK-NEXT: %tmp = bitcast %struct.test1_D* %0 to i8*
|
||||||
// CHECK-NEXT: call void @llvm.memcpy.i64(i8* %tmp, i8* bitcast (%struct.test1_D* @d1 to i8*), i64 8, i32 8)
|
// CHECK-NEXT: call void @llvm.memcpy.i64(i8* %tmp, i8* bitcast (%struct.test1_D* @d1 to i8*), i64 8, i32 8)
|
||||||
|
@ -32,7 +34,9 @@ void test2() {
|
||||||
|
|
||||||
// CHECK: define void @_Z5test2v() nounwind {
|
// CHECK: define void @_Z5test2v() nounwind {
|
||||||
// CHECK-NEXT:entry:
|
// CHECK-NEXT:entry:
|
||||||
|
// CHECK-NEXT: %exception.ptr = alloca i8*
|
||||||
// CHECK-NEXT: %exception = call i8* @__cxa_allocate_exception(i64 16)
|
// CHECK-NEXT: %exception = call i8* @__cxa_allocate_exception(i64 16)
|
||||||
|
// CHECK-NEXT: store i8* %exception, i8** %exception.ptr
|
||||||
// CHECK-NEXT: %0 = bitcast i8* %exception to %struct.test2_D*
|
// CHECK-NEXT: %0 = bitcast i8* %exception to %struct.test2_D*
|
||||||
// CHECK: invoke void @_ZN7test2_DC1ERKS_(%struct.test2_D* %0, %struct.test2_D* @d2)
|
// CHECK: invoke void @_ZN7test2_DC1ERKS_(%struct.test2_D* %0, %struct.test2_D* @d2)
|
||||||
// CHECK-NEXT: to label %invoke.cont unwind label %terminate.handler
|
// CHECK-NEXT: to label %invoke.cont unwind label %terminate.handler
|
||||||
|
@ -52,7 +56,9 @@ void test3() {
|
||||||
|
|
||||||
// CHECK: define void @_Z5test3v() nounwind {
|
// CHECK: define void @_Z5test3v() nounwind {
|
||||||
// CHECK-NEXT: entry:
|
// CHECK-NEXT: entry:
|
||||||
|
// CHECK-NEXT: %exception.ptr = alloca i8*
|
||||||
// CHECK-NEXT: %exception = call i8* @__cxa_allocate_exception(i64 8)
|
// CHECK-NEXT: %exception = call i8* @__cxa_allocate_exception(i64 8)
|
||||||
|
// CHECK-NEXT: store i8* %exception, i8** %exception.ptr
|
||||||
// CHECK-NEXT: %0 = bitcast i8* %exception to %struct.test3_D**
|
// CHECK-NEXT: %0 = bitcast i8* %exception to %struct.test3_D**
|
||||||
// CHECK-NEXT: store %struct.test3_D* null, %struct.test3_D** %0
|
// CHECK-NEXT: store %struct.test3_D* null, %struct.test3_D** %0
|
||||||
// CHECK-NEXT: call void @__cxa_throw(i8* %exception, i8* bitcast (%1* @_ZTIPV7test3_D to i8*), i8* null) noreturn
|
// CHECK-NEXT: call void @__cxa_throw(i8* %exception, i8* bitcast (%1* @_ZTIPV7test3_D to i8*), i8* null) noreturn
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
// RUN: clang-cc %s -triple=x86_64-apple-darwin10 -emit-llvm -o - -fexceptions
|
||||||
|
|
||||||
|
struct allocator {
|
||||||
|
allocator();
|
||||||
|
allocator(const allocator&);
|
||||||
|
~allocator();
|
||||||
|
};
|
||||||
|
|
||||||
|
void f();
|
||||||
|
void g(bool b, bool c) {
|
||||||
|
if (b) {
|
||||||
|
if (!c)
|
||||||
|
throw allocator();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
f();
|
||||||
|
}
|
Loading…
Reference in New Issue