[cfe-commits] r88852 - in /cfe/trunk: lib/CodeGen/CGCXXExpr.cpp test/CodeGenCXX/rtti.cpp
Mike Stump
mrs at apple.com
Sun Nov 15 08:52:53 PST 2009
Author: mrs
Date: Sun Nov 15 10:52:53 2009
New Revision: 88852
URL: http://llvm.org/viewvc/llvm-project?rev=88852&view=rev
Log:
Finish off zero check for typeid(*p) so that it will do a __cxa_bad_typeid.
Modified:
cfe/trunk/lib/CodeGen/CGCXXExpr.cpp
cfe/trunk/test/CodeGenCXX/rtti.cpp
Modified: cfe/trunk/lib/CodeGen/CGCXXExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCXXExpr.cpp?rev=88852&r1=88851&r2=88852&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXXExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXXExpr.cpp Sun Nov 15 10:52:53 2009
@@ -357,8 +357,31 @@
// FIXME: if subE is an lvalue do
LValue Obj = EmitLValue(subE);
llvm::Value *This = Obj.getAddress();
- // FIXME: need to do a 0 check here for *p on This
- llvm::Value *V = Builder.CreateBitCast(This, LTy->getPointerTo()->getPointerTo());
+ LTy = LTy->getPointerTo()->getPointerTo();
+ llvm::Value *V = Builder.CreateBitCast(This, LTy);
+ // We need to do a zero check for *p, unless it has NonNullAttr.
+ // FIXME: PointerType->hasAttr<NonNullAttr>()
+ bool CanBeZero = false;
+ if (UnaryOperator *UO = dyn_cast<UnaryOperator>(subE))
+ if (UO->getOpcode() == UnaryOperator::Deref)
+ CanBeZero = true;
+ if (CanBeZero) {
+ llvm::BasicBlock *NonZeroBlock = createBasicBlock();
+ llvm::BasicBlock *ZeroBlock = createBasicBlock();
+
+ llvm::Value *Zero = llvm::Constant::getNullValue(LTy);
+ Builder.CreateCondBr(Builder.CreateICmpNE(V, Zero),
+ NonZeroBlock, ZeroBlock);
+ EmitBlock(ZeroBlock);
+ /// Call __cxa_bad_typeid
+ const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
+ const llvm::FunctionType *FTy;
+ FTy = llvm::FunctionType::get(ResultType, false);
+ llvm::Value *F = CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
+ Builder.CreateCall(F);
+ Builder.CreateUnreachable();
+ EmitBlock(NonZeroBlock);
+ }
V = Builder.CreateLoad(V, "vtable");
V = Builder.CreateConstInBoundsGEP1_64(V, -1ULL);
V = Builder.CreateLoad(V);
Modified: cfe/trunk/test/CodeGenCXX/rtti.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/rtti.cpp?rev=88852&r1=88851&r2=88852&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/rtti.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/rtti.cpp Sun Nov 15 10:52:53 2009
@@ -102,6 +102,8 @@
test2_1();
if (typeid(NP) == typeid(test1_D))
test2_1();
+ if (typeid(*dp) == typeid(test1_D))
+ test2_1();
}
// CHECK-LL:define void @_Z7test2_2P7test1_D(%class.test1_B7* %dp) nounwind {
@@ -113,3 +115,15 @@
// CHECK-LL-NEXT: %call = call zeroext i1 @_ZNK3std9type_infoeqERKS0_(%"class.std::type_info"* %2, %"class.std::type_info"* bitcast (%1* @_ZTI7test1_D to %"class.std::type_info"*))
// CHECK-LL: %call2 = call zeroext i1 @_ZNK3std9type_infoeqERKS0_(%"class.std::type_info"* bitcast (%0* @_ZTI2NP to %"class.std::type_info"*), %"class.std::type_info"* bitcast (%1* @_ZTI7test1_D to %"class.std::type_info"*))
+
+// CHECK-LL: %3 = bitcast %class.test1_B7* %tmp5 to %"class.std::type_info"***
+// CHECK-LL-NEXT: %4 = icmp ne %"class.std::type_info"*** %3, null
+// CHECK-LL-NEXT: br i1 %4, label %6, label %5
+// CHECK-LL: ; <label>:5
+// CHECK-LL-NEXT: call void @__cxa_bad_typeid()
+// CHECK-LL-NEXT: unreachable
+// CHECK-LL: ; <label>:6
+// CHECK-LL-NEXT: %vtable6 = load %"class.std::type_info"*** %3
+// CHECK-LL-NEXT: %7 = getelementptr inbounds %"class.std::type_info"** %vtable6, i64 -1
+// CHECK-LL-NEXT: %8 = load %"class.std::type_info"** %7
+// CHECK-LL-NEXT: %call7 = call zeroext i1 @_ZNK3std9type_infoeqERKS0_(%"class.std::type_info"* %8, %"class.std::type_info"* bitcast (%1* @_ZTI7test1_D to %"class.std::type_info"*))
More information about the cfe-commits
mailing list