1 // RUN: %clang_cc1 -emit-llvm -O1 -o - -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 
test0()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 
test1(V * x)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 
test2(A * x)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* %x, i32 0, i32 0
24 // CHECK-NEXT:   [[VBTBL:%.*]] = load i32** [[VBPTRPTR]], align 4
25 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32* [[VBTBL]], i32 1
26 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFP]], align 4
27 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds 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 
test3(B * x)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* %x, i32 0, i32 0, i32 0
35 // CHECK-NEXT:   [[VBPTR:%.*]] = getelementptr inbounds i8* [[VOIDP]], i32 4
36 // CHECK-NEXT:   [[VBPTRPTR:%.*]] = bitcast i8* [[VBPTR:%.*]] to i32**
37 // CHECK-NEXT:   [[VBTBL:%.*]] = load i32** [[VBPTRPTR]], align 4
38 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32* [[VBTBL]], i32 1
39 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFP]], align 4
40 // CHECK-NEXT:   [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
41 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds 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 
test4(V * x)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 
test5(A * x)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* %x, i32 0, i32 0
59 // CHECK-NEXT:   [[VBTBL:%.*]] = load i32** [[VBPTRPTR]], align 4
60 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32* [[VBTBL]], i32 1
61 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFP]], align 4
62 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8* [[VOIDP]], i32 [[VBOFFS]]
63 // 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 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 
test6(B * x)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* %x, i32 0, i32 0, i32 0
74 // CHECK-NEXT:   [[VBPTR:%.*]] = getelementptr inbounds i8* [[CAST]], i32 4
75 // CHECK-NEXT:   [[VBPTRPTR:%.*]] = bitcast i8* [[VBPTR]] to i32**
76 // CHECK-NEXT:   [[VBTBL:%.*]] = load i32** [[VBPTRPTR]], align 4
77 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32* [[VBTBL]], i32 1
78 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFP]], align 4
79 // CHECK-NEXT:   [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
80 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8* [[CAST]], i32 [[DELTA]]
81 // 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 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 
test7(V * x)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 
test8(A * x)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* %x, i32 0, i32 0
99 // CHECK-NEXT:   [[VBTBL:%.*]] = load i32** [[VBPTRPTR]], align 4
100 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32* [[VBTBL]], i32 1
101 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFP]], align 4
102 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds 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 
test9(B * x)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* %x, i32 0, i32 0, i32 0
113 // CHECK-NEXT:   [[VBPTR:%.*]] = getelementptr inbounds i8* [[CAST]], i32 4
114 // CHECK-NEXT:   [[VBPTRPTR:%.*]] = bitcast i8* [[VBPTR]] to i32**
115 // CHECK-NEXT:   [[VBTBL:%.*]] = load i32** [[VBPTRPTR]], align 4
116 // CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32* [[VBTBL]], i32 1
117 // CHECK-NEXT:   [[VBOFFS:%.*]] = load i32* [[VBOFFP]], align 4
118 // CHECK-NEXT:   [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
119 // CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds 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