File: microsoft-abi-dynamic-cast.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (121 lines) | stat: -rw-r--r-- 6,281 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// RUN: %clang_cc1 -emit-llvm -O1 -o - -fexceptions -triple=i386-pc-win32 %s | FileCheck %s

struct S { char a; };
struct V { virtual void f(); };
struct A : virtual V {};
struct B : S, virtual V {};
struct T {};

T* test0() { return dynamic_cast<T*>((B*)0); }
// CHECK-LABEL: define dso_local noalias noundef ptr @"?test0@@YAPAUT@@XZ"()
// CHECK:   ret ptr null

T* test1(V* x) { return &dynamic_cast<T&>(*x); }
// CHECK-LABEL: define dso_local noundef ptr @"?test1@@YAPAUT@@PAUV@@@Z"(ptr noundef %x)
// CHECK:   [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr %x, i32 0, ptr nonnull @"??_R0?AUV@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 1)
// CHECK-NEXT:   ret ptr [[CALL]]

T* test2(A* x) { return &dynamic_cast<T&>(*x); }
// CHECK-LABEL: define dso_local noundef ptr @"?test2@@YAPAUT@@PAUA@@@Z"(ptr noundef %x)
// CHECK:        [[VBTBL:%.*]] = load ptr, ptr %x, align 4
// CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT:   [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[VBOFFS]]
// CHECK-NEXT:   [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr nonnull [[ADJ]], i32 [[VBOFFS]], ptr nonnull @"??_R0?AUA@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 1)
// CHECK-NEXT:   ret ptr [[CALL]]

T* test3(B* x) { return &dynamic_cast<T&>(*x); }
// CHECK-LABEL: define dso_local noundef ptr @"?test3@@YAPAUT@@PAUB@@@Z"(ptr noundef %x)
// CHECK:        [[VBPTR:%.*]] = getelementptr inbounds i8, ptr %x, i32 4
// CHECK-NEXT:   [[VBTBL:%.*]] = load ptr, ptr [[VBPTR:%.*]], align 4
// CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT:   [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT:   [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
// CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[DELTA]]
// CHECK-NEXT:   [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr [[ADJ]], i32 [[DELTA]], ptr nonnull @"??_R0?AUB@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 1)
// CHECK-NEXT:   ret ptr [[CALL]]

T* test4(V* x) { return dynamic_cast<T*>(x); }
// CHECK-LABEL: define dso_local noundef ptr @"?test4@@YAPAUT@@PAUV@@@Z"(ptr noundef %x)
// CHECK:   [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr %x, i32 0, ptr nonnull @"??_R0?AUV@@@8", ptr nonnull @"??_R0?AUT@@@8", i32 0)
// CHECK-NEXT:   ret ptr [[CALL]]

T* test5(A* x) { return dynamic_cast<T*>(x); }
// CHECK-LABEL: define dso_local noundef ptr @"?test5@@YAPAUT@@PAUA@@@Z"(ptr noundef %x)
// CHECK:        [[CHECK:%.*]] = icmp eq ptr %x, null
// CHECK-NEXT:   br i1 [[CHECK]]
// CHECK:        [[VBTBL:%.*]] = load ptr, ptr %x, align 4
// CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT:   [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[VBOFFS]]
// CHECK-NEXT:   [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr nonnull [[ADJ]], i32 [[VBOFFS]], ptr {{.*}}@"??_R0?AUA@@@8", ptr {{.*}}@"??_R0?AUT@@@8", i32 0)
// CHECK-NEXT:   br label
// CHECK:        [[RET:%.*]] = phi ptr
// CHECK-NEXT:   ret ptr [[RET]]

T* test6(B* x) { return dynamic_cast<T*>(x); }
// CHECK-LABEL: define dso_local noundef ptr @"?test6@@YAPAUT@@PAUB@@@Z"(ptr noundef %x)
// CHECK:        [[CHECK:%.*]] = icmp eq ptr %x, null
// CHECK-NEXT:   br i1 [[CHECK]]
// CHECK:        [[VBPTR:%.*]] = getelementptr inbounds i8, ptr %x, i32 4
// CHECK-NEXT:   [[VBTBL:%.*]] = load ptr, ptr [[VBPTR]], align 4
// CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT:   [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT:   [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
// CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[DELTA]]
// CHECK-NEXT:   [[CALL:%.*]] = tail call ptr @__RTDynamicCast(ptr nonnull [[ADJ]], i32 [[DELTA]], ptr {{.*}}@"??_R0?AUB@@@8", ptr {{.*}}@"??_R0?AUT@@@8", i32 0)
// CHECK-NEXT:   br label
// CHECK:        [[RET:%.*]] = phi ptr
// CHECK-NEXT:   ret ptr [[RET]]

void* test7(V* x) { return dynamic_cast<void*>(x); }
// CHECK-LABEL: define dso_local noundef ptr @"?test7@@YAPAXPAUV@@@Z"(ptr noundef %x)
// CHECK:   [[RET:%.*]] = tail call ptr @__RTCastToVoid(ptr %x)
// CHECK-NEXT:   ret ptr [[RET]]

void* test8(A* x) { return dynamic_cast<void*>(x); }
// CHECK-LABEL: define dso_local noundef ptr @"?test8@@YAPAXPAUA@@@Z"(ptr noundef %x)
// CHECK:        [[CHECK:%.*]] = icmp eq ptr %x, null
// CHECK-NEXT:   br i1 [[CHECK]]
// CHECK:        [[VBTBL:%.*]] = load ptr, ptr %x, align 4
// CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT:   [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[VBOFFS]]
// CHECK-NEXT:   [[RES:%.*]] = tail call ptr @__RTCastToVoid(ptr nonnull [[ADJ]])
// CHECK-NEXT:   br label
// CHECK:        [[RET:%.*]] = phi ptr
// CHECK-NEXT:   ret ptr [[RET]]

void* test9(B* x) { return dynamic_cast<void*>(x); }
// CHECK-LABEL: define dso_local noundef ptr @"?test9@@YAPAXPAUB@@@Z"(ptr noundef %x)
// CHECK:        [[CHECK:%.*]] = icmp eq ptr %x, null
// CHECK-NEXT:   br i1 [[CHECK]]
// CHECK:        [[VBPTR:%.*]] = getelementptr inbounds i8, ptr %x, i32 4
// CHECK-NEXT:   [[VBTBL:%.*]] = load ptr, ptr [[VBPTR]], align 4
// CHECK-NEXT:   [[VBOFFP:%.*]] = getelementptr inbounds i32, ptr [[VBTBL]], i32 1
// CHECK-NEXT:   [[VBOFFS:%.*]] = load i32, ptr [[VBOFFP]], align 4
// CHECK-NEXT:   [[DELTA:%.*]] = add nsw i32 [[VBOFFS]], 4
// CHECK-NEXT:   [[ADJ:%.*]] = getelementptr inbounds i8, ptr %x, i32 [[DELTA]]
// CHECK-NEXT:   [[CALL:%.*]] = tail call ptr @__RTCastToVoid(ptr nonnull [[ADJ]])
// CHECK-NEXT:   br label
// CHECK:        [[RET:%.*]] = phi ptr
// CHECK-NEXT:   ret ptr [[RET]]

namespace PR25606 {
struct Cleanup {
  ~Cleanup();
};
struct S1 { virtual ~S1(); };
struct S2 : virtual S1 {};
struct S3 : S2 {};

S3 *f(S2 &s) {
  Cleanup c;
  return dynamic_cast<S3 *>(&s);
}
// CHECK-LABEL: define dso_local noundef ptr @"?f@PR25606@@YAPAUS3@1@AAUS2@1@@Z"(
// CHECK:    [[CALL:%.*]] = invoke ptr @__RTDynamicCast

// CHECK:    call x86_thiscallcc void @"??1Cleanup@PR25606@@QAE@XZ"(
// CHECK:    ret ptr [[CALL]]
}