blob: 9fceaae9fc8a5a52f598ac9d730a379846d352ff [file] [log] [blame]
Warren Huntb57b4b02014-06-06 22:12:37 +00001// RUN: %clang_cc1 -emit-llvm -O2 -optzns -o - -triple=i386-pc-win32 2>/dev/null %s | FileCheck %s
2
3struct S { char a; };
4struct V { virtual void f(){} };
5struct A : virtual V {};
6struct B : S, virtual V {};
7struct T {};
8
9T* test0() { return dynamic_cast<T*>((B*)0); }
10// CHECK: define noalias %struct.T* @"\01?test0@@YAPAUT@@XZ"() #0 {
11// CHECK-NEXT: entry:
12// CHECK-NEXT: ret %struct.T* null
13// CHECK-NEXT: }
14
15T* test1(V* x) { return &dynamic_cast<T&>(*x); }
16// CHECK: define %struct.T* @"\01?test1@@YAPAUT@@PAUV@@@Z"(%struct.V* %x) #1 {
17// CHECK-NEXT: entry:
18// CHECK-NEXT: %0 = bitcast %struct.V* %x to i8*
19// CHECK-NEXT: %1 = tail call i8* @__RTDynamicCast(i8* %0, i32 0, i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUV@@@8" to i8*), i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUT@@@8" to i8*), i32 1) #2
20// CHECK-NEXT: %2 = bitcast i8* %1 to %struct.T*
21// CHECK-NEXT: ret %struct.T* %2
22// CHECK-NEXT: }
23
24T* test2(A* x) { return &dynamic_cast<T&>(*x); }
25// CHECK: define %struct.T* @"\01?test2@@YAPAUT@@PAUA@@@Z"(%struct.A* %x) #1 {
26// CHECK-NEXT: entry:
27// CHECK-NEXT: %0 = bitcast %struct.A* %x to i8*
28// CHECK-NEXT: %1 = bitcast %struct.A* %x to i8**
29// CHECK-NEXT: %vbtable = load i8** %1, align 4
30// CHECK-NEXT: %2 = getelementptr inbounds i8* %vbtable, i32 4
31// CHECK-NEXT: %3 = bitcast i8* %2 to i32*
32// CHECK-NEXT: %vbase_offs = load i32* %3, align 4
33// CHECK-NEXT: %4 = getelementptr inbounds i8* %0, i32 %vbase_offs
34// CHECK-NEXT: %5 = tail call i8* @__RTDynamicCast(i8* %4, i32 %vbase_offs, i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUA@@@8" to i8*), i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUT@@@8" to i8*), i32 1) #2
35// CHECK-NEXT: %6 = bitcast i8* %5 to %struct.T*
36// CHECK-NEXT: ret %struct.T* %6
37// CHECK-NEXT: }
38
39T* test3(B* x) { return &dynamic_cast<T&>(*x); }
40// CHECK: define %struct.T* @"\01?test3@@YAPAUT@@PAUB@@@Z"(%struct.B* %x) #1 {
41// CHECK-NEXT: entry:
42// CHECK-NEXT: %0 = getelementptr inbounds %struct.B* %x, i32 0, i32 0, i32 0
43// CHECK-NEXT: %vbptr = getelementptr inbounds i8* %0, i32 4
44// CHECK-NEXT: %1 = bitcast i8* %vbptr to i8**
45// CHECK-NEXT: %vbtable = load i8** %1, align 4
46// CHECK-NEXT: %2 = getelementptr inbounds i8* %vbtable, i32 4
47// CHECK-NEXT: %3 = bitcast i8* %2 to i32*
48// CHECK-NEXT: %vbase_offs = load i32* %3, align 4
49// CHECK-NEXT: %4 = add nsw i32 %vbase_offs, 4
50// CHECK-NEXT: %5 = getelementptr inbounds i8* %0, i32 %4
51// CHECK-NEXT: %6 = tail call i8* @__RTDynamicCast(i8* %5, i32 %4, i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUB@@@8" to i8*), i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUT@@@8" to i8*), i32 1) #2
52// CHECK-NEXT: %7 = bitcast i8* %6 to %struct.T*
53// CHECK-NEXT: ret %struct.T* %7
54// CHECK-NEXT: }
55
56T* test4(V* x) { return dynamic_cast<T*>(x); }
57// CHECK: define %struct.T* @"\01?test4@@YAPAUT@@PAUV@@@Z"(%struct.V* %x) #1 {
58// CHECK-NEXT: entry:
59// CHECK-NEXT: %0 = bitcast %struct.V* %x to i8*
60// CHECK-NEXT: %1 = tail call i8* @__RTDynamicCast(i8* %0, i32 0, i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUV@@@8" to i8*), i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUT@@@8" to i8*), i32 0) #2
61// CHECK-NEXT: %2 = bitcast i8* %1 to %struct.T*
62// CHECK-NEXT: ret %struct.T* %2
63// CHECK-NEXT: }
64
65T* test5(A* x) { return dynamic_cast<T*>(x); }
66// CHECK: define %struct.T* @"\01?test5@@YAPAUT@@PAUA@@@Z"(%struct.A* %x) #1 {
67// CHECK-NEXT: entry:
68// CHECK-NEXT: %0 = icmp eq %struct.A* %x, null
69// CHECK-NEXT: br i1 %0, label %dynamic_cast.call, label %dynamic_cast.valid
70// CHECK: dynamic_cast.valid: ; preds = %entry
71// CHECK-NEXT: %1 = bitcast %struct.A* %x to i8*
72// CHECK-NEXT: %2 = bitcast %struct.A* %x to i8**
73// CHECK-NEXT: %vbtable = load i8** %2, align 4
74// CHECK-NEXT: %3 = getelementptr inbounds i8* %vbtable, i32 4
75// CHECK-NEXT: %4 = bitcast i8* %3 to i32*
76// CHECK-NEXT: %vbase_offs = load i32* %4, align 4
77// CHECK-NEXT: %5 = getelementptr inbounds i8* %1, i32 %vbase_offs
78// CHECK-NEXT: %6 = tail call i8* @__RTDynamicCast(i8* %5, i32 %vbase_offs, i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUA@@@8" to i8*), i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUT@@@8" to i8*), i32 0) #2
79// CHECK-NEXT: %phitmp = bitcast i8* %6 to %struct.T*
80// CHECK-NEXT: br label %dynamic_cast.call
81// CHECK: dynamic_cast.call: ; preds = %dynamic_cast.valid, %entry
82// CHECK-NEXT: %7 = phi %struct.T* [ %phitmp, %dynamic_cast.valid ], [ null, %entry ]
83// CHECK-NEXT: ret %struct.T* %7
84// CHECK-NEXT: }
85
86T* test6(B* x) { return dynamic_cast<T*>(x); }
87// CHECK: define %struct.T* @"\01?test6@@YAPAUT@@PAUB@@@Z"(%struct.B* %x) #1 {
88// CHECK-NEXT: entry:
89// CHECK-NEXT: %0 = icmp eq %struct.B* %x, null
90// CHECK-NEXT: br i1 %0, label %dynamic_cast.call, label %dynamic_cast.valid
91// CHECK: dynamic_cast.valid: ; preds = %entry
92// CHECK-NEXT: %1 = getelementptr inbounds %struct.B* %x, i32 0, i32 0, i32 0
93// CHECK-NEXT: %vbptr = getelementptr inbounds i8* %1, i32 4
94// CHECK-NEXT: %2 = bitcast i8* %vbptr to i8**
95// CHECK-NEXT: %vbtable = load i8** %2, align 4
96// CHECK-NEXT: %3 = getelementptr inbounds i8* %vbtable, i32 4
97// CHECK-NEXT: %4 = bitcast i8* %3 to i32*
98// CHECK-NEXT: %vbase_offs = load i32* %4, align 4
99// CHECK-NEXT: %5 = add nsw i32 %vbase_offs, 4
100// CHECK-NEXT: %6 = getelementptr inbounds i8* %1, i32 %5
101// CHECK-NEXT: %7 = tail call i8* @__RTDynamicCast(i8* %6, i32 %5, i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUB@@@8" to i8*), i8* bitcast (%"MSRTTITypeDescriptor\07"* @"\01??_R0?AUT@@@8" to i8*), i32 0) #2
102// CHECK-NEXT: %phitmp = bitcast i8* %7 to %struct.T*
103// CHECK-NEXT: br label %dynamic_cast.call
104// CHECK: dynamic_cast.call: ; preds = %dynamic_cast.valid, %entry
105// CHECK-NEXT: %8 = phi %struct.T* [ %phitmp, %dynamic_cast.valid ], [ null, %entry ]
106// CHECK-NEXT: ret %struct.T* %8
107// CHECK-NEXT: }
108
109void* test7(V* x) { return dynamic_cast<void*>(x); }
110// CHECK: define i8* @"\01?test7@@YAPAXPAUV@@@Z"(%struct.V* %x) #1 {
111// CHECK-NEXT: entry:
112// CHECK-NEXT: %0 = bitcast %struct.V* %x to i8*
113// CHECK-NEXT: %1 = tail call i8* @__RTCastToVoid(i8* %0) #2
114// CHECK-NEXT: ret i8* %1
115// CHECK-NEXT: }
116
117void* test8(A* x) { return dynamic_cast<void*>(x); }
118// CHECK: define i8* @"\01?test8@@YAPAXPAUA@@@Z"(%struct.A* %x) #1 {
119// CHECK-NEXT: entry:
120// CHECK-NEXT: %0 = icmp eq %struct.A* %x, null
121// CHECK-NEXT: br i1 %0, label %dynamic_cast.call, label %dynamic_cast.valid
122// CHECK: dynamic_cast.valid: ; preds = %entry
123// CHECK-NEXT: %1 = bitcast %struct.A* %x to i8*
124// CHECK-NEXT: %2 = bitcast %struct.A* %x to i8**
125// CHECK-NEXT: %vbtable = load i8** %2, align 4
126// CHECK-NEXT: %3 = getelementptr inbounds i8* %vbtable, i32 4
127// CHECK-NEXT: %4 = bitcast i8* %3 to i32*
128// CHECK-NEXT: %vbase_offs = load i32* %4, align 4
129// CHECK-NEXT: %5 = getelementptr inbounds i8* %1, i32 %vbase_offs
130// CHECK-NEXT: %6 = tail call i8* @__RTCastToVoid(i8* %5) #2
131// CHECK-NEXT: br label %dynamic_cast.call
132// CHECK: dynamic_cast.call: ; preds = %dynamic_cast.valid, %entry
133// CHECK-NEXT: %7 = phi i8* [ %6, %dynamic_cast.valid ], [ null, %entry ]
134// CHECK-NEXT: ret i8* %7
135// CHECK-NEXT: }
136
137void* test9(B* x) { return dynamic_cast<void*>(x); }
138// CHECK: define i8* @"\01?test9@@YAPAXPAUB@@@Z"(%struct.B* %x) #1 {
139// CHECK-NEXT: entry:
140// CHECK-NEXT: %0 = icmp eq %struct.B* %x, null
141// CHECK-NEXT: br i1 %0, label %dynamic_cast.call, label %dynamic_cast.valid
142// CHECK: dynamic_cast.valid: ; preds = %entry
143// CHECK-NEXT: %1 = getelementptr inbounds %struct.B* %x, i32 0, i32 0, i32 0
144// CHECK-NEXT: %vbptr = getelementptr inbounds i8* %1, i32 4
145// CHECK-NEXT: %2 = bitcast i8* %vbptr to i8**
146// CHECK-NEXT: %vbtable = load i8** %2, align 4
147// CHECK-NEXT: %3 = getelementptr inbounds i8* %vbtable, i32 4
148// CHECK-NEXT: %4 = bitcast i8* %3 to i32*
149// CHECK-NEXT: %vbase_offs = load i32* %4, align 4
150// CHECK-NEXT: %5 = add nsw i32 %vbase_offs, 4
151// CHECK-NEXT: %6 = getelementptr inbounds i8* %1, i32 %5
152// CHECK-NEXT: %7 = tail call i8* @__RTCastToVoid(i8* %6) #2
153// CHECK-NEXT: br label %dynamic_cast.call
154// CHECK: dynamic_cast.call: ; preds = %dynamic_cast.valid, %entry
155// CHECK-NEXT: %8 = phi i8* [ %7, %dynamic_cast.valid ], [ null, %entry ]
156// CHECK-NEXT: ret i8* %8
157// CHECK-NEXT: }