1 // RUN: %clang_cc1 -emit-llvm -O1 -o - -fexceptions -triple=i386-pc-win32 %s | FileCheck %s 2 3 struct S { char a; }; 4 struct V { virtual void f(); }; 5 struct A : virtual V {}; 6 struct B : S, virtual V {}; 7 struct T {}; 8 9 T* test0() { return dynamic_cast<T*>((B*)0); } 10 // CHECK-LABEL: define noalias %struct.T* @"\01?test0@@YAPAUT@@XZ"() 11 // CHECK: ret %struct.T* null 12 13 T* test1(V* x) { return &dynamic_cast<T&>(*x); } 14 // CHECK-LABEL: define %struct.T* @"\01?test1@@YAPAUT@@PAUV@@@Z"(%struct.V* %x) 15 // CHECK: [[CAST:%.*]] = bitcast %struct.V* %x to i8* 16 // CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[CAST]], i32 0, i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUV@@@8" to i8*), i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 1) 17 // CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T* 18 // CHECK-NEXT: ret %struct.T* [[RET]] 19 20 T* test2(A* x) { return &dynamic_cast<T&>(*x); } 21 // CHECK-LABEL: define %struct.T* @"\01?test2@@YAPAUT@@PAUA@@@Z"(%struct.A* %x) 22 // CHECK: [[CAST:%.*]] = bitcast %struct.A* %x to i8* 23 // CHECK-NEXT: [[VBPTRPTR:%.*]] = getelementptr inbounds %struct.A, %struct.A* %x, i32 0, i32 0 24 // CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4 25 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1 26 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4 27 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[CAST]], i32 [[VBOFFS]] 28 // CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[VBOFFS]], i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUA@@@8" to i8*), i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 1) 29 // CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T* 30 // CHECK-NEXT: ret %struct.T* [[RET]] 31 32 T* test3(B* x) { return &dynamic_cast<T&>(*x); } 33 // CHECK-LABEL: define %struct.T* @"\01?test3@@YAPAUT@@PAUB@@@Z"(%struct.B* %x) 34 // CHECK: [[VOIDP:%.*]] = getelementptr inbounds %struct.B, %struct.B* %x, i32 0, i32 0, i32 0 35 // CHECK-NEXT: [[VBPTR:%.*]] = getelementptr inbounds i8, i8* [[VOIDP]], i32 4 36 // CHECK-NEXT: [[VBPTRPTR:%.*]] = bitcast i8* [[VBPTR:%.*]] to i32** 37 // CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4 38 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1 39 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4 40 // CHECK-NEXT: [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4 41 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[VOIDP]], i32 [[DELTA]] 42 // CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[DELTA]], i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUB@@@8" to i8*), i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 1) 43 // CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T* 44 // CHECK-NEXT: ret %struct.T* [[RET]] 45 46 T* test4(V* x) { return dynamic_cast<T*>(x); } 47 // CHECK-LABEL: define %struct.T* @"\01?test4@@YAPAUT@@PAUV@@@Z"(%struct.V* %x) 48 // CHECK: [[CAST:%.*]] = bitcast %struct.V* %x to i8* 49 // CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[CAST]], i32 0, i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUV@@@8" to i8*), i8* bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 0) 50 // CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T* 51 // CHECK-NEXT: ret %struct.T* [[RET]] 52 53 T* test5(A* x) { return dynamic_cast<T*>(x); } 54 // CHECK-LABEL: define %struct.T* @"\01?test5@@YAPAUT@@PAUA@@@Z"(%struct.A* %x) 55 // CHECK: [[CHECK:%.*]] = icmp eq %struct.A* %x, null 56 // CHECK-NEXT: br i1 [[CHECK]] 57 // CHECK: [[VOIDP:%.*]] = bitcast %struct.A* %x to i8* 58 // CHECK-NEXT: [[VBPTRPTR:%.*]] = getelementptr inbounds %struct.A, %struct.A* %x, i32 0, i32 0 59 // CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4 60 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1 61 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4 62 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[VOIDP]], i32 [[VBOFFS]] 63 // CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[VBOFFS]], i8* nonnull bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUA@@@8" to i8*), i8* nonnull bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 0) 64 // CHECK-NEXT: [[RES:%.*]] = bitcast i8* [[CALL]] to %struct.T* 65 // CHECK-NEXT: br label 66 // CHECK: [[RET:%.*]] = phi %struct.T* 67 // CHECK-NEXT: ret %struct.T* [[RET]] 68 69 T* test6(B* x) { return dynamic_cast<T*>(x); } 70 // CHECK-LABEL: define %struct.T* @"\01?test6@@YAPAUT@@PAUB@@@Z"(%struct.B* %x) 71 // CHECK: [[CHECK:%.*]] = icmp eq %struct.B* %x, null 72 // CHECK-NEXT: br i1 [[CHECK]] 73 // CHECK: [[CAST:%.*]] = getelementptr inbounds %struct.B, %struct.B* %x, i32 0, i32 0, i32 0 74 // CHECK-NEXT: [[VBPTR:%.*]] = getelementptr inbounds i8, i8* [[CAST]], i32 4 75 // CHECK-NEXT: [[VBPTRPTR:%.*]] = bitcast i8* [[VBPTR]] to i32** 76 // CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4 77 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1 78 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4 79 // CHECK-NEXT: [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4 80 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[CAST]], i32 [[DELTA]] 81 // CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTDynamicCast(i8* [[ADJ]], i32 [[DELTA]], i8* nonnull bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUB@@@8" to i8*), i8* nonnull bitcast (%rtti.TypeDescriptor7* @"\01??_R0?AUT@@@8" to i8*), i32 0) 82 // CHECK-NEXT: [[RES:%.*]] = bitcast i8* [[CALL]] to %struct.T* 83 // CHECK-NEXT: br label 84 // CHECK: [[RET:%.*]] = phi %struct.T* 85 // CHECK-NEXT: ret %struct.T* [[RET]] 86 87 void* test7(V* x) { return dynamic_cast<void*>(x); } 88 // CHECK-LABEL: define i8* @"\01?test7@@YAPAXPAUV@@@Z"(%struct.V* %x) 89 // CHECK: [[CAST:%.*]] = bitcast %struct.V* %x to i8* 90 // CHECK-NEXT: [[RET:%.*]] = tail call i8* @__RTCastToVoid(i8* [[CAST]]) 91 // CHECK-NEXT: ret i8* [[RET]] 92 93 void* test8(A* x) { return dynamic_cast<void*>(x); } 94 // CHECK-LABEL: define i8* @"\01?test8@@YAPAXPAUA@@@Z"(%struct.A* %x) 95 // CHECK: [[CHECK:%.*]] = icmp eq %struct.A* %x, null 96 // CHECK-NEXT: br i1 [[CHECK]] 97 // CHECK: [[VOIDP:%.*]] = bitcast %struct.A* %x to i8* 98 // CHECK-NEXT: [[VBPTRPTR:%.*]] = getelementptr inbounds %struct.A, %struct.A* %x, i32 0, i32 0 99 // CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4 100 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1 101 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4 102 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[VOIDP]], i32 [[VBOFFS]] 103 // CHECK-NEXT: [[RES:%.*]] = tail call i8* @__RTCastToVoid(i8* [[ADJ]]) 104 // CHECK-NEXT: br label 105 // CHECK: [[RET:%.*]] = phi i8* 106 // CHECK-NEXT: ret i8* [[RET]] 107 108 void* test9(B* x) { return dynamic_cast<void*>(x); } 109 // CHECK-LABEL: define i8* @"\01?test9@@YAPAXPAUB@@@Z"(%struct.B* %x) 110 // CHECK: [[CHECK:%.*]] = icmp eq %struct.B* %x, null 111 // CHECK-NEXT: br i1 [[CHECK]] 112 // CHECK: [[CAST:%.*]] = getelementptr inbounds %struct.B, %struct.B* %x, i32 0, i32 0, i32 0 113 // CHECK-NEXT: [[VBPTR:%.*]] = getelementptr inbounds i8, i8* [[CAST]], i32 4 114 // CHECK-NEXT: [[VBPTRPTR:%.*]] = bitcast i8* [[VBPTR]] to i32** 115 // CHECK-NEXT: [[VBTBL:%.*]] = load i32*, i32** [[VBPTRPTR]], align 4 116 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, i32* [[VBTBL]], i32 1 117 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, i32* [[VBOFFP]], align 4 118 // CHECK-NEXT: [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4 119 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, i8* [[CAST]], i32 [[DELTA]] 120 // CHECK-NEXT: [[CALL:%.*]] = tail call i8* @__RTCastToVoid(i8* [[ADJ]]) 121 // CHECK-NEXT: br label 122 // CHECK: [[RET:%.*]] = phi i8* 123 // CHECK-NEXT: ret i8* [[RET]] 124 125 namespace PR25606 { 126 struct Cleanup { 127 ~Cleanup(); 128 }; 129 struct S1 { virtual ~S1(); }; 130 struct S2 : virtual S1 {}; 131 struct S3 : S2 {}; 132 133 S3 *f(S2 &s) { 134 Cleanup c; 135 return dynamic_cast<S3 *>(&s); 136 } 137 // CHECK-LABEL: define %"struct.PR25606::S3"* @"\01?f@PR25606@@YAPAUS3@1@AAUS2@1@@Z"( 138 // CHECK: [[CALL:%.*]] = invoke i8* @__RTDynamicCast 139 140 // CHECK: [[BC:%.*]] = bitcast i8* [[CALL]] to %"struct.PR25606::S3"* 141 // CHECK: call x86_thiscallcc void @"\01??_DCleanup@PR25606@@QAE@XZ"( 142 // CHECK: ret %"struct.PR25606::S3"* [[BC]] 143 } 144