]> granicus.if.org Git - clang/commitdiff
When doing a base-to-derived cast we don't need to null check the derived value if...
authorAnders Carlsson <andersca@mac.com>
Sun, 31 Jan 2010 01:43:37 +0000 (01:43 +0000)
committerAnders Carlsson <andersca@mac.com>
Sun, 31 Jan 2010 01:43:37 +0000 (01:43 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94939 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGClass.cpp
test/CodeGenCXX/derived-to-base.cpp

index 18d33ad2beaf666bfd20911e688ca46d85f7e324..eaebf140fcfd61d77a644890fba9f4a52f67101b 100644 (file)
@@ -224,6 +224,14 @@ CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value,
     return Builder.CreateBitCast(Value, DerivedPtrTy);
   }
 
+  llvm::Value *NonVirtualOffset =
+    CGM.GetNonVirtualBaseClassOffset(DerivedClass, Class);
+  
+  if (!NonVirtualOffset) {
+    // No offset, we can just cast back.
+    return Builder.CreateBitCast(Value, DerivedPtrTy);
+  }
+  
   llvm::BasicBlock *CastNull = 0;
   llvm::BasicBlock *CastNotNull = 0;
   llvm::BasicBlock *CastEnd = 0;
@@ -240,16 +248,13 @@ CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value,
     EmitBlock(CastNotNull);
   }
   
-  if (llvm::Value *NonVirtualOffset =
-      CGM.GetNonVirtualBaseClassOffset(DerivedClass, Class)) {
-    // Apply the offset.
-    Value = Builder.CreatePtrToInt(Value, NonVirtualOffset->getType());
-    Value = Builder.CreateSub(Value, NonVirtualOffset);
-    Value = Builder.CreateIntToPtr(Value, DerivedPtrTy);
-  } else {
-    // Just cast.
-    Value = Builder.CreateBitCast(Value, DerivedPtrTy);
-  }
+  // Apply the offset.
+  Value = Builder.CreatePtrToInt(Value, NonVirtualOffset->getType());
+  Value = Builder.CreateSub(Value, NonVirtualOffset);
+  Value = Builder.CreateIntToPtr(Value, DerivedPtrTy);
+
+  // Just cast.
+  Value = Builder.CreateBitCast(Value, DerivedPtrTy);
 
   if (NullCheckValue) {
     Builder.CreateBr(CastEnd);
index 45728b7c0122671b24e30f42449286ca42d7abaa..79aeea70e3ee512b871dccdd2518c45f76118892 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -emit-llvm %s -o -
+// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
 struct A { 
   void f(); 
   
@@ -14,3 +14,10 @@ void f() {
   
   b.f();
 }
+
+// CHECK: define %struct.B* @_Z1fP1A(%struct.A* %a) nounwind
+B *f(A *a) {
+  // CHECK-NOT: br label
+  // CHECK: ret %struct.B*
+  return static_cast<B*>(a);
+}