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.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@91084 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGException.cpp b/lib/CodeGen/CGException.cpp
index b460fa9..07e7546 100644
--- a/lib/CodeGen/CGException.cpp
+++ b/lib/CodeGen/CGException.cpp
@@ -127,21 +127,24 @@
 }
 
 // CopyObject - Utility to copy an object.  Calls copy constructor as necessary.
-// N is casted to the right type.
-static void CopyObject(CodeGenFunction &CGF, const Expr *E, llvm::Value *N) {
+// DestPtr is casted to the right type.
+static void CopyObject(CodeGenFunction &CGF, const Expr *E, 
+                       llvm::Value *DestPtr, llvm::Value *ExceptionPtrPtr) {
   QualType ObjectType = E->getType();
 
   // Store the throw exception in the exception object.
   if (!CGF.hasAggregateLLVMType(ObjectType)) {
     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 {
-    const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
-    const CXXRecordDecl *RD;
-    RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
-    llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
+    const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo();
+    const CXXRecordDecl *RD =
+      cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
+    
+    llvm::Value *This = CGF.Builder.CreateBitCast(DestPtr, Ty);
     if (RD->hasTrivialCopyConstructor()) {
       CGF.EmitAggExpr(E, This, false);
     } else if (CXXConstructorDecl *CopyCtor
@@ -150,9 +153,9 @@
       if (CGF.Exceptions) {
         CodeGenFunction::EHCleanupBlock Cleanup(CGF);
         llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
-        const llvm::Type *Int8PtrTy
-          = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
-        llvm::Value *ExceptionPtr = CGF.Builder.CreateBitCast(N, Int8PtrTy);
+        
+        // Load the exception pointer.
+        llvm::Value *ExceptionPtr = CGF.Builder.CreateLoad(ExceptionPtrPtr);
         CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
       }
 
@@ -251,8 +254,13 @@
     Builder.CreateCall(AllocExceptionFn,
                        llvm::ConstantInt::get(SizeTy, TypeSize),
                        "exception");
+  
+  llvm::Value *ExceptionPtrPtr = 
+    CreateTempAlloca(ExceptionPtr->getType(), "exception.ptr");
+  Builder.CreateStore(ExceptionPtr, ExceptionPtrPtr);
 
-  CopyObject(*this, E->getSubExpr(), ExceptionPtr);
+
+  CopyObject(*this, E->getSubExpr(), ExceptionPtr, ExceptionPtrPtr);
 
   // Now throw the exception.
   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());