Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / microsoft-abi-dynamic-cast.cpp
blob5c4b86744287ec30f2e5faf7b6bdee1435355724
1 // RUN: %clang_cc1 -emit-llvm -O1 -o - -fexceptions -triple=i386-pc-win32 %s | FileCheck %s
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 {};
9 T* test0() { return dynamic_cast<T*>((B*)0); }
10 // CHECK-LABEL: define dso_local noalias noundef ptr @"?test0@@YAPAUT@@XZ"()
11 // CHECK: ret ptr null
13 T* test1(V* x) { return &dynamic_cast<T&>(*x); }
14 // CHECK-LABEL: define dso_local noundef ptr @"?test1@@YAPAUT@@PAUV@@@Z"(ptr noundef %x)
15 // CHECK: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr %x, i32 0, ptr nonnull @"??_R0?AUV@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 1)
16 // CHECK-NEXT: ret ptr [[CALL]]
18 T* test2(A* x) { return &dynamic_cast<T&>(*x); }
19 // CHECK-LABEL: define dso_local noundef ptr @"?test2@@YAPAUT@@PAUA@@@Z"(ptr noundef %x)
20 // CHECK: [[VBTBL:%.*]] = load ptr, ptr %x, align 4
21 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
22 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
23 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[VBOFFS]]
24 // CHECK-NEXT: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr nonnull [[ADJ]], i32 [[VBOFFS]], ptr nonnull @"??_R0?AUA@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 1)
25 // CHECK-NEXT: ret ptr [[CALL]]
27 T* test3(B* x) { return &dynamic_cast<T&>(*x); }
28 // CHECK-LABEL: define dso_local noundef ptr @"?test3@@YAPAUT@@PAUB@@@Z"(ptr noundef %x)
29 // CHECK: [[VBPTR:%.*]] = getelementptr inbounds i8, ptr %x, i32 4
30 // CHECK-NEXT: [[VBTBL:%.*]] = load ptr, ptr [[VBPTR:%.*]], align 4
31 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
32 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
33 // CHECK-NEXT: [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
34 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[DELTA]]
35 // CHECK-NEXT: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr [[ADJ]], i32 [[DELTA]], ptr nonnull @"??_R0?AUB@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 1)
36 // CHECK-NEXT: ret ptr [[CALL]]
38 T* test4(V* x) { return dynamic_cast<T*>(x); }
39 // CHECK-LABEL: define dso_local noundef ptr @"?test4@@YAPAUT@@PAUV@@@Z"(ptr noundef %x)
40 // CHECK: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr %x, i32 0, ptr nonnull @"??_R0?AUV@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 0)
41 // CHECK-NEXT: ret ptr [[CALL]]
43 T* test5(A* x) { return dynamic_cast<T*>(x); }
44 // CHECK-LABEL: define dso_local noundef ptr @"?test5@@YAPAUT@@PAUA@@@Z"(ptr noundef %x)
45 // CHECK: [[CHECK:%.*]] = icmp eq ptr %x, null
46 // CHECK-NEXT: br i1 [[CHECK]]
47 // CHECK: [[VBTBL:%.*]] = load ptr, ptr %x, align 4
48 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
49 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
50 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[VBOFFS]]
51 // CHECK-NEXT: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr nonnull [[ADJ]], i32 [[VBOFFS]], ptr {{.*}}@"??_R0?AUA@@@8", ptr {{.*}}@"??_R0?AUT@@@8", i32 0)
52 // CHECK-NEXT: br label
53 // CHECK: [[RET:%.*]] = phi ptr
54 // CHECK-NEXT: ret ptr [[RET]]
56 T* test6(B* x) { return dynamic_cast<T*>(x); }
57 // CHECK-LABEL: define dso_local noundef ptr @"?test6@@YAPAUT@@PAUB@@@Z"(ptr noundef %x)
58 // CHECK: [[CHECK:%.*]] = icmp eq ptr %x, null
59 // CHECK-NEXT: br i1 [[CHECK]]
60 // CHECK: [[VBPTR:%.*]] = getelementptr inbounds i8, ptr %x, i32 4
61 // CHECK-NEXT: [[VBTBL:%.*]] = load ptr, ptr [[VBPTR]], align 4
62 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
63 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
64 // CHECK-NEXT: [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
65 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[DELTA]]
66 // CHECK-NEXT: [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr nonnull [[ADJ]], i32 [[DELTA]], ptr {{.*}}@"??_R0?AUB@@@8", ptr {{.*}}@"??_R0?AUT@@@8", i32 0)
67 // CHECK-NEXT: br label
68 // CHECK: [[RET:%.*]] = phi ptr
69 // CHECK-NEXT: ret ptr [[RET]]
71 void* test7(V* x) { return dynamic_cast<void*>(x); }
72 // CHECK-LABEL: define dso_local noundef ptr @"?test7@@YAPAXPAUV@@@Z"(ptr noundef %x)
73 // CHECK: [[RET:%.*]] = tail call ptr @__RTCastToVoid(ptr %x)
74 // CHECK-NEXT: ret ptr [[RET]]
76 void* test8(A* x) { return dynamic_cast<void*>(x); }
77 // CHECK-LABEL: define dso_local noundef ptr @"?test8@@YAPAXPAUA@@@Z"(ptr noundef %x)
78 // CHECK: [[CHECK:%.*]] = icmp eq ptr %x, null
79 // CHECK-NEXT: br i1 [[CHECK]]
80 // CHECK: [[VBTBL:%.*]] = load ptr, ptr %x, align 4
81 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
82 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
83 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[VBOFFS]]
84 // CHECK-NEXT: [[RES:%.*]] = tail call ptr @__RTCastToVoid(ptr nonnull [[ADJ]])
85 // CHECK-NEXT: br label
86 // CHECK: [[RET:%.*]] = phi ptr
87 // CHECK-NEXT: ret ptr [[RET]]
89 void* test9(B* x) { return dynamic_cast<void*>(x); }
90 // CHECK-LABEL: define dso_local noundef ptr @"?test9@@YAPAXPAUB@@@Z"(ptr noundef %x)
91 // CHECK: [[CHECK:%.*]] = icmp eq ptr %x, null
92 // CHECK-NEXT: br i1 [[CHECK]]
93 // CHECK: [[VBPTR:%.*]] = getelementptr inbounds i8, ptr %x, i32 4
94 // CHECK-NEXT: [[VBTBL:%.*]] = load ptr, ptr [[VBPTR]], align 4
95 // CHECK-NEXT: [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
96 // CHECK-NEXT: [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
97 // CHECK-NEXT: [[BASE:%.*]] = getelementptr i8, ptr %x, i32 [[VBOFFS]]
98 // CHECK-NEXT: [[ADJ:%.*]] = getelementptr i8, ptr [[BASE]], i32 4
99 // CHECK-NEXT: [[CALL:%.*]] = tail call ptr @__RTCastToVoid(ptr [[ADJ]])
100 // CHECK-NEXT: br label
101 // CHECK: [[RET:%.*]] = phi ptr
102 // CHECK-NEXT: ret ptr [[RET]]
104 namespace PR25606 {
105 struct Cleanup {
106 ~Cleanup();
108 struct S1 { virtual ~S1(); };
109 struct S2 : virtual S1 {};
110 struct S3 : S2 {};
112 S3 *f(S2 &s) {
113 Cleanup c;
114 return dynamic_cast<S3 *>(&s);
116 // CHECK-LABEL: define dso_local noundef ptr @"?f@PR25606@@YAPAUS3@1@AAUS2@1@@Z"(
117 // CHECK: [[CALL:%.*]] = invoke ptr @__RTDynamicCast
119 // CHECK: call x86_thiscallcc void @"??1Cleanup@PR25606@@QAE@XZ"(
120 // CHECK: ret ptr [[CALL]]