blob: f2e9da7cca1f7a50fd4cb91d40df1beb09dc4eab [file] [log] [blame]
David Majnemer1162d252014-06-22 19:05:33 +00001// RUN: %clang_cc1 -emit-llvm -O1 -o - -triple=i386-pc-win32 %s | FileCheck %s
Warren Huntb57b4b02014-06-06 22:12:37 +00002
3struct S { char a; };
David Majnemer1162d252014-06-22 19:05:33 +00004struct V { virtual void f(); };
Warren Huntb57b4b02014-06-06 22:12:37 +00005struct A : virtual V {};
6struct B : S, virtual V {};
7struct T {};
8
9T* test0() { return dynamic_cast<T*>((B*)0); }
David Majnemer1162d252014-06-22 19:05:33 +000010// CHECK-LABEL: define noalias %struct.T* @"\01?test0@@YAPAUT@@XZ"()
11// CHECK: ret %struct.T* null
Warren Huntb57b4b02014-06-06 22:12:37 +000012
13T* test1(V* x) { return &dynamic_cast<T&>(*x); }
David Majnemer1162d252014-06-22 19:05:33 +000014// CHECK-LABEL: define %struct.T* @"\01?test1@@YAPAUT@@PAUV@@@Z"(%struct.V* %x)
15// CHECK: [[CAST:%.*]] = bitcast %struct.V* %x to i8*
David Majnemer611cdb92014-07-07 08:09:15 +000016// 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)
David Majnemer1162d252014-06-22 19:05:33 +000017// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
18// CHECK-NEXT: ret %struct.T* [[RET]]
Warren Huntb57b4b02014-06-06 22:12:37 +000019
20T* test2(A* x) { return &dynamic_cast<T&>(*x); }
David Majnemer1162d252014-06-22 19:05:33 +000021// CHECK-LABEL: define %struct.T* @"\01?test2@@YAPAUT@@PAUA@@@Z"(%struct.A* %x)
22// CHECK: [[CAST:%.*]] = bitcast %struct.A* %x to i8*
Reid Kleckner0ba8ba42014-10-22 17:26:00 +000023// 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
David Majnemer1162d252014-06-22 19:05:33 +000027// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8* [[CAST]], i32 [[VBOFFS]]
David Majnemer611cdb92014-07-07 08:09:15 +000028// 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)
David Majnemer1162d252014-06-22 19:05:33 +000029// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
30// CHECK-NEXT: ret %struct.T* [[RET]]
Warren Huntb57b4b02014-06-06 22:12:37 +000031
32T* test3(B* x) { return &dynamic_cast<T&>(*x); }
David Majnemer1162d252014-06-22 19:05:33 +000033// 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
Reid Kleckner0ba8ba42014-10-22 17:26:00 +000036// 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
David Majnemer1162d252014-06-22 19:05:33 +000040// CHECK-NEXT: [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
41// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8* [[VOIDP]], i32 [[DELTA]]
David Majnemer611cdb92014-07-07 08:09:15 +000042// 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)
David Majnemer1162d252014-06-22 19:05:33 +000043// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
44// CHECK-NEXT: ret %struct.T* [[RET]]
Warren Huntb57b4b02014-06-06 22:12:37 +000045
46T* test4(V* x) { return dynamic_cast<T*>(x); }
David Majnemer1162d252014-06-22 19:05:33 +000047// CHECK-LABEL: define %struct.T* @"\01?test4@@YAPAUT@@PAUV@@@Z"(%struct.V* %x)
48// CHECK: [[CAST:%.*]] = bitcast %struct.V* %x to i8*
David Majnemer611cdb92014-07-07 08:09:15 +000049// 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)
David Majnemer1162d252014-06-22 19:05:33 +000050// CHECK-NEXT: [[RET:%.*]] = bitcast i8* [[CALL]] to %struct.T*
51// CHECK-NEXT: ret %struct.T* [[RET]]
Warren Huntb57b4b02014-06-06 22:12:37 +000052
53T* test5(A* x) { return dynamic_cast<T*>(x); }
David Majnemer1162d252014-06-22 19:05:33 +000054// 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*
Reid Kleckner0ba8ba42014-10-22 17:26:00 +000058// 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
David Majnemer1162d252014-06-22 19:05:33 +000062// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8* [[VOIDP]], i32 [[VBOFFS]]
David Majnemer611cdb92014-07-07 08:09:15 +000063// 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)
David Majnemer1162d252014-06-22 19:05:33 +000064// 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]]
Warren Huntb57b4b02014-06-06 22:12:37 +000068
69T* test6(B* x) { return dynamic_cast<T*>(x); }
David Majnemer1162d252014-06-22 19:05:33 +000070// 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
Reid Kleckner0ba8ba42014-10-22 17:26:00 +000075// 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
David Majnemer1162d252014-06-22 19:05:33 +000079// CHECK-NEXT: [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
80// CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8* [[CAST]], i32 [[DELTA]]
David Majnemer611cdb92014-07-07 08:09:15 +000081// 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)
David Majnemer1162d252014-06-22 19:05:33 +000082// 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]]
Warren Huntb57b4b02014-06-06 22:12:37 +000086
87void* test7(V* x) { return dynamic_cast<void*>(x); }
David Majnemer1162d252014-06-22 19:05:33 +000088// 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]]
Warren Huntb57b4b02014-06-06 22:12:37 +000092
93void* test8(A* x) { return dynamic_cast<void*>(x); }
David Majnemer1162d252014-06-22 19:05:33 +000094// 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*
Reid Kleckner0ba8ba42014-10-22 17:26:00 +000098// 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
David Majnemer1162d252014-06-22 19:05:33 +0000102// 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]]
Warren Huntb57b4b02014-06-06 22:12:37 +0000107
108void* test9(B* x) { return dynamic_cast<void*>(x); }
David Majnemer1162d252014-06-22 19:05:33 +0000109// 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
Reid Kleckner0ba8ba42014-10-22 17:26:00 +0000114// 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
David Majnemer1162d252014-06-22 19:05:33 +0000118// 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