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