blob: b868d1f0b516a7c938a1aed1b45794e14bcd8c14 [file] [log] [blame]
Stephen Hines651f13c2014-04-23 16:59:28 -07001// RUN: %clang_cc1 %s -fno-rtti -triple=i386-pc-win32 -emit-llvm -o %t
Timur Iskhodzhanov82552742013-10-16 18:24:06 +00002// RUN: FileCheck %s < %t
3// RUN: FileCheck --check-prefix=CHECK2 %s < %t
Peter Collingbournee1e35f72013-06-28 20:45:28 +00004
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +00005// For now, just make sure x86_64 doesn't crash.
Stephen Hines651f13c2014-04-23 16:59:28 -07006// RUN: %clang_cc1 %s -fno-rtti -triple=x86_64-pc-win32 -emit-llvm -o %t
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +00007
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00008struct VBase {
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +00009 virtual ~VBase();
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +000010 virtual void foo();
11 virtual void bar();
12 int field;
Peter Collingbournee1e35f72013-06-28 20:45:28 +000013};
14
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +000015struct B : virtual VBase {
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +000016 B();
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000017 virtual ~B();
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +000018 virtual void foo();
19 virtual void bar();
20};
21
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +000022B::B() {
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000023 // CHECK-LABEL: define x86_thiscallcc %struct.B* @"\01??0B@@QAE@XZ"
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070024 // CHECK: %[[THIS:.*]] = load %struct.B*, %struct.B**
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +000025 // CHECK: br i1 %{{.*}}, label %[[INIT_VBASES:.*]], label %[[SKIP_VBASES:.*]]
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000026
27 // Don't check the INIT_VBASES case as it's covered by the ctor tests.
28
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +000029 // CHECK: %[[SKIP_VBASES]]
30 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070031 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 0
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000032 // ...
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +000033 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070034 // CHECK: %[[VFPTR_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 %{{.*}}
Stephen Hines0e2c34f2015-03-23 12:09:02 -070035 // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VFPTR_i8]] to i32 (...)***
36 // CHECK: store i32 (...)** bitcast ([3 x i8*]* @"\01??_7B@@6B@" to i32 (...)**), i32 (...)*** %[[VFPTR]]
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000037
38 // Initialize vtorDisp:
39 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070040 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 0
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000041 // ...
42 // CHECK: %[[VBASE_OFFSET:.*]] = add nsw i32 0, %{{.*}}
43 // CHECK: %[[VTORDISP_VAL:.*]] = sub i32 %[[VBASE_OFFSET]], 8
44 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070045 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 %[[VBASE_OFFSET]]
46 // CHECK: %[[VTORDISP_i8:.*]] = getelementptr i8, i8* %[[VBASE_i8]], i32 -4
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000047 // CHECK: %[[VTORDISP_PTR:.*]] = bitcast i8* %[[VTORDISP_i8]] to i32*
48 // CHECK: store i32 %[[VTORDISP_VAL]], i32* %[[VTORDISP_PTR]]
49
50 // CHECK: ret
51}
52
53B::~B() {
54 // CHECK-LABEL: define x86_thiscallcc void @"\01??1B@@UAE@XZ"
Timur Iskhodzhanov82552742013-10-16 18:24:06 +000055 // Adjust the this parameter:
56 // CHECK: %[[THIS_PARAM_i8:.*]] = bitcast %struct.B* {{.*}} to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070057 // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_PARAM_i8]], i32 -8
Timur Iskhodzhanov82552742013-10-16 18:24:06 +000058 // CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %struct.B*
59 // CHECK: store %struct.B* %[[THIS]], %struct.B** %[[THIS_ADDR:.*]], align 4
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070060 // CHECK: %[[THIS:.*]] = load %struct.B*, %struct.B** %[[THIS_ADDR]]
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000061
62 // Restore the vfptr that could have been changed by a subclass.
63 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070064 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 0
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000065 // ...
66 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070067 // CHECK: %[[VFPTR_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 %{{.*}}
Stephen Hines0e2c34f2015-03-23 12:09:02 -070068 // CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VFPTR_i8]] to i32 (...)***
69 // CHECK: store i32 (...)** bitcast ([3 x i8*]* @"\01??_7B@@6B@" to i32 (...)**), i32 (...)*** %[[VFPTR]]
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000070
71 // Initialize vtorDisp:
72 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070073 // CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 0
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000074 // ...
75 // CHECK: %[[VBASE_OFFSET:.*]] = add nsw i32 0, %{{.*}}
76 // CHECK: %[[VTORDISP_VAL:.*]] = sub i32 %[[VBASE_OFFSET]], 8
77 // CHECK: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070078 // CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i32 %[[VBASE_OFFSET]]
79 // CHECK: %[[VTORDISP_i8:.*]] = getelementptr i8, i8* %[[VBASE_i8]], i32 -4
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +000080 // CHECK: %[[VTORDISP_PTR:.*]] = bitcast i8* %[[VTORDISP_i8]] to i32*
81 // CHECK: store i32 %[[VTORDISP_VAL]], i32* %[[VTORDISP_PTR]]
82
83 foo(); // Avoid the "trivial destructor" optimization.
84
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +000085 // CHECK: ret
Timur Iskhodzhanov82552742013-10-16 18:24:06 +000086
87 // CHECK2-LABEL: define linkonce_odr x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070088 // CHECK2: %[[THIS:.*]] = load %struct.B*, %struct.B** {{.*}}
Timur Iskhodzhanov82552742013-10-16 18:24:06 +000089 // CHECK2: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070090 // CHECK2: %[[B_i8:.*]] = getelementptr i8, i8* %[[THIS_i8]], i32 8
Timur Iskhodzhanov82552742013-10-16 18:24:06 +000091 // CHECK2: %[[B:.*]] = bitcast i8* %[[B_i8]] to %struct.B*
92 // CHECK2: call x86_thiscallcc void @"\01??1B@@UAE@XZ"(%struct.B* %[[B]])
93 // CHECK2: %[[THIS_i8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070094 // CHECK2: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_i8]], i64 8
Timur Iskhodzhanov82552742013-10-16 18:24:06 +000095 // CHECK2: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.VBase*
96 // CHECK2: call x86_thiscallcc void @"\01??1VBase@@UAE@XZ"(%struct.VBase* %[[VBASE]])
97 // CHECK2: ret
98
Stephen Hines176edba2014-12-01 14:53:08 -080099 // CHECK2-LABEL: define linkonce_odr x86_thiscallcc i8* @"\01??_GB@@UAEPAXI@Z"
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000100 // CHECK2: %[[THIS_PARAM_i8:.*]] = bitcast %struct.B* {{.*}} to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700101 // CHECK2: %[[THIS_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS_PARAM_i8:.*]], i32 -8
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000102 // CHECK2: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %struct.B*
103 // CHECK2: store %struct.B* %[[THIS]], %struct.B** %[[THIS_ADDR:.*]], align 4
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700104 // CHECK2: %[[THIS:.*]] = load %struct.B*, %struct.B** %[[THIS_ADDR]]
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000105 // CHECK2: call x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B* %[[THIS]])
106 // ...
107 // CHECK2: ret
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000108}
109
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000110void B::foo() {
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +0000111// CHECK-LABEL: define x86_thiscallcc void @"\01?foo@B@@UAEXXZ"(i8*
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000112//
113// B::foo gets 'this' cast to VBase* in ECX (i.e. this+8) so we
114// need to adjust 'this' before use.
115//
116// CHECK: %[[THIS_ADDR:.*]] = alloca %struct.B*, align 4
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700117// CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8, i8* %[[ECX:.*]], i32 -8
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000118// CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %struct.B*
119// CHECK: store %struct.B* %[[THIS]], %struct.B** %[[THIS_ADDR]], align 4
120
121 field = 42;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700122// CHECK: %[[THIS:.*]] = load %struct.B*, %struct.B** %[[THIS_ADDR]]
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000123// CHECK: %[[THIS8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700124// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[THIS8]], i32 0
Stephen Hines176edba2014-12-01 14:53:08 -0800125// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700126// CHECK: %[[VBTABLE:.*]] = load i32*, i32** %[[VBPTR8]]
127// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, i32* %[[VBTABLE]], i32 1
128// CHECK: %[[VBOFFSET32:.*]] = load i32, i32* %[[VBENTRY]]
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000129// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
130// CHECK: %[[THIS8:.*]] = bitcast %struct.B* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700131// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[THIS8]], i32 %[[VBOFFSET]]
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000132// CHECK: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.VBase*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700133// CHECK: %[[FIELD:.*]] = getelementptr inbounds %struct.VBase, %struct.VBase* %[[VBASE]], i32 0, i32 1
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000134// CHECK: store i32 42, i32* %[[FIELD]], align 4
135//
136// CHECK: ret void
137}
138
139void call_vbase_bar(B *obj) {
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +0000140// CHECK-LABEL: define void @"\01?call_vbase_bar@@YAXPAUB@@@Z"(%struct.B* %obj)
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000141// CHECK: %[[OBJ:.*]] = load %struct.B
142
143 obj->bar();
144// When calling a vbase's virtual method, one needs to adjust 'this'
145// at the caller site.
146//
147// CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700148// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 0
Stephen Hines176edba2014-12-01 14:53:08 -0800149// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700150// CHECK: %[[VBTABLE:.*]] = load i32*, i32** %[[VBPTR8]]
151// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, i32* %[[VBTABLE]], i32 1
152// CHECK: %[[VBOFFSET32:.*]] = load i32, i32* %[[VBENTRY]]
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000153// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700154// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000155// CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VBASE_i8]] to void (i8*)***
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700156// CHECK: %[[VFTABLE:.*]] = load void (i8*)**, void (i8*)*** %[[VFPTR]]
157// CHECK: %[[VFUN:.*]] = getelementptr inbounds void (i8*)*, void (i8*)** %[[VFTABLE]], i64 2
158// CHECK: %[[VFUN_VALUE:.*]] = load void (i8*)*, void (i8*)** %[[VFUN]]
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000159//
160// CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700161// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 0
Stephen Hines176edba2014-12-01 14:53:08 -0800162// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700163// CHECK: %[[VBTABLE:.*]] = load i32*, i32** %[[VBPTR8]]
164// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, i32* %[[VBTABLE]], i32 1
165// CHECK: %[[VBOFFSET32:.*]] = load i32, i32* %[[VBENTRY]]
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000166// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700167// CHECK: %[[VBASE:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000168//
169// CHECK: call x86_thiscallcc void %[[VFUN_VALUE]](i8* %[[VBASE]])
170//
171// CHECK: ret void
172}
Timur Iskhodzhanov2cb17a02013-10-09 09:23:58 +0000173
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000174void delete_B(B *obj) {
175// CHECK-LABEL: define void @"\01?delete_B@@YAXPAUB@@@Z"(%struct.B* %obj)
176// CHECK: %[[OBJ:.*]] = load %struct.B
177
178 delete obj;
179// CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700180// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 0
Stephen Hines176edba2014-12-01 14:53:08 -0800181// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700182// CHECK: %[[VBTABLE:.*]] = load i32*, i32** %[[VBPTR8]]
183// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, i32* %[[VBTABLE]], i32 1
184// CHECK: %[[VBOFFSET32:.*]] = load i32, i32* %[[VBENTRY]]
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000185// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700186// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
Stephen Hines176edba2014-12-01 14:53:08 -0800187// CHECK: %[[VFPTR:.*]] = bitcast i8* %[[VBASE_i8]] to i8* (%struct.B*, i32)***
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700188// CHECK: %[[VFTABLE:.*]] = load i8* (%struct.B*, i32)**, i8* (%struct.B*, i32)*** %[[VFPTR]]
189// CHECK: %[[VFUN:.*]] = getelementptr inbounds i8* (%struct.B*, i32)*, i8* (%struct.B*, i32)** %[[VFTABLE]], i64 0
190// CHECK: %[[VFUN_VALUE:.*]] = load i8* (%struct.B*, i32)*, i8* (%struct.B*, i32)** %[[VFUN]]
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000191//
192// CHECK: %[[OBJ_i8:.*]] = bitcast %struct.B* %[[OBJ]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700193// CHECK: %[[VBPTR:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 0
Stephen Hines176edba2014-12-01 14:53:08 -0800194// CHECK: %[[VBPTR8:.*]] = bitcast i8* %[[VBPTR]] to i32**
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700195// CHECK: %[[VBTABLE:.*]] = load i32*, i32** %[[VBPTR8]]
196// CHECK: %[[VBENTRY:.*]] = getelementptr inbounds i32, i32* %[[VBTABLE]], i32 1
197// CHECK: %[[VBOFFSET32:.*]] = load i32, i32* %[[VBENTRY]]
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000198// CHECK: %[[VBOFFSET:.*]] = add nsw i32 0, %[[VBOFFSET32]]
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700199// CHECK: %[[VBASE_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 %[[VBOFFSET]]
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000200// CHECK: %[[VBASE:.*]] = bitcast i8* %[[VBASE_i8]] to %struct.B*
201//
Stephen Hines176edba2014-12-01 14:53:08 -0800202// CHECK: call x86_thiscallcc i8* %[[VFUN_VALUE]](%struct.B* %[[VBASE]], i32 1)
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000203// CHECK: ret void
204}
205
206void call_complete_dtor() {
207 // CHECK-LABEL: define void @"\01?call_complete_dtor@@YAXXZ"
208 B b;
209 // CHECK: call x86_thiscallcc %struct.B* @"\01??0B@@QAE@XZ"(%struct.B* %[[B:.*]], i32 1)
210 // CHECK-NOT: getelementptr
211 // CHECK: call x86_thiscallcc void @"\01??_DB@@UAE@XZ"(%struct.B* %[[B]])
212 // CHECK: ret
213}
214
Timur Iskhodzhanov2cb17a02013-10-09 09:23:58 +0000215struct C : B {
216 C();
217 // has an implicit vdtor.
218};
219
220// Used to crash on an assertion.
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000221C::C() {
222// CHECK-LABEL: define x86_thiscallcc %struct.C* @"\01??0C@@QAE@XZ"
223}
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +0000224
225namespace multiple_vbases {
226struct A {
227 virtual void a();
228};
229
230struct B {
231 virtual void b();
232};
233
234struct C {
235 virtual void c();
236};
237
238struct D : virtual A, virtual B, virtual C {
239 virtual void a();
240 virtual void b();
241 virtual void c();
242 D();
243};
244
245D::D() {
246 // CHECK-LABEL: define x86_thiscallcc %"struct.multiple_vbases::D"* @"\01??0D@multiple_vbases@@QAE@XZ"
247 // Just make sure we emit 3 vtordisps after initializing vfptrs.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700248 // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7D@multiple_vbases@@6BA@1@@" to i32 (...)**), i32 (...)*** %{{.*}}
249 // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7D@multiple_vbases@@6BB@1@@" to i32 (...)**), i32 (...)*** %{{.*}}
250 // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7D@multiple_vbases@@6BC@1@@" to i32 (...)**), i32 (...)*** %{{.*}}
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +0000251 // ...
252 // CHECK: store i32 %{{.*}}, i32* %{{.*}}
253 // CHECK: store i32 %{{.*}}, i32* %{{.*}}
254 // CHECK: store i32 %{{.*}}, i32* %{{.*}}
255 // CHECK: ret
256}
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000257}
258
259namespace diamond {
260struct A {
261 A();
262 virtual ~A();
263};
264
265struct B : virtual A {
266 B();
267 ~B();
268};
269
270struct C : virtual A {
271 C();
272 ~C();
273 int c1, c2, c3;
274};
275
276struct Z {
277 int z;
278};
279
280struct D : virtual Z, B, C {
281 D();
282 ~D();
283} d;
284
285D::~D() {
286 // CHECK-LABEL: define x86_thiscallcc void @"\01??1D@diamond@@UAE@XZ"(%"struct.diamond::D"*)
287 // CHECK: %[[ARG_i8:.*]] = bitcast %"struct.diamond::D"* %{{.*}} to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700288 // CHECK: %[[THIS_i8:.*]] = getelementptr inbounds i8, i8* %[[ARG_i8]], i32 -24
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000289 // CHECK: %[[THIS:.*]] = bitcast i8* %[[THIS_i8]] to %"struct.diamond::D"*
290 // CHECK: store %"struct.diamond::D"* %[[THIS]], %"struct.diamond::D"** %[[THIS_VAL:.*]], align 4
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700291 // CHECK: %[[THIS:.*]] = load %"struct.diamond::D"*, %"struct.diamond::D"** %[[THIS_VAL]]
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000292 // CHECK: %[[D_i8:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700293 // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8, i8* %[[D_i8]], i64 4
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000294 // CHECK: %[[C:.*]] = bitcast i8* %[[C_i8]] to %"struct.diamond::C"*
295 // CHECK: %[[C_i8:.*]] = bitcast %"struct.diamond::C"* %[[C]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700296 // CHECK: %[[ARG_i8:.*]] = getelementptr i8, i8* %{{.*}}, i32 16
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000297 // FIXME: We might consider changing the dtor this parameter type to i8*.
298 // CHECK: %[[ARG:.*]] = bitcast i8* %[[ARG_i8]] to %"struct.diamond::C"*
299 // CHECK: call x86_thiscallcc void @"\01??1C@diamond@@UAE@XZ"(%"struct.diamond::C"* %[[ARG]])
300
301 // CHECK: %[[B:.*]] = bitcast %"struct.diamond::D"* %[[THIS]] to %"struct.diamond::B"*
302 // CHECK: %[[B_i8:.*]] = bitcast %"struct.diamond::B"* %[[B]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700303 // CHECK: %[[ARG_i8:.*]] = getelementptr i8, i8* %[[B_i8]], i32 4
Timur Iskhodzhanov82552742013-10-16 18:24:06 +0000304 // CHECK: %[[ARG:.*]] = bitcast i8* %[[ARG_i8]] to %"struct.diamond::B"*
305 // CHECK: call x86_thiscallcc void @"\01??1B@diamond@@UAE@XZ"(%"struct.diamond::B"* %[[ARG]])
306 // CHECK: ret void
307}
Timur Iskhodzhanov5bd0d442013-10-09 18:16:58 +0000308
309}
Stephen Hines651f13c2014-04-23 16:59:28 -0700310
311namespace test2 {
312struct A { A(); };
313struct B : virtual A { B() {} };
314struct C : B, A { C() {} };
315
316// PR18435: Order mattered here. We were generating code for the delegating
317// call to B() from C().
318void callC() { C x; }
319
320// CHECK-LABEL: define linkonce_odr x86_thiscallcc %"struct.test2::C"* @"\01??0C@test2@@QAE@XZ"
321// CHECK: (%"struct.test2::C"* returned %this, i32 %is_most_derived)
322// CHECK: br i1
323// Virtual bases
324// CHECK: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}})
325// CHECK: br label
326// Non-virtual bases
327// CHECK: call x86_thiscallcc %"struct.test2::B"* @"\01??0B@test2@@QAE@XZ"(%"struct.test2::B"* %{{.*}}, i32 0)
328// CHECK: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}})
329// CHECK: ret
330
331// CHECK2-LABEL: define linkonce_odr x86_thiscallcc %"struct.test2::B"* @"\01??0B@test2@@QAE@XZ"
332// CHECK2: (%"struct.test2::B"* returned %this, i32 %is_most_derived)
333// CHECK2: call x86_thiscallcc %"struct.test2::A"* @"\01??0A@test2@@QAE@XZ"(%"struct.test2::A"* %{{.*}})
334// CHECK2: ret
335
336}
337
338namespace test3 {
339// PR19104: A non-virtual call of a virtual method doesn't use vftable thunks,
340// so requires only static adjustment which is different to the one used
341// for virtual calls.
342struct A {
343 virtual void foo();
344};
345
346struct B : virtual A {
347 virtual void bar();
348};
349
350struct C : virtual A {
351 virtual void foo();
352};
353
354struct D : B, C {
355 virtual void bar();
356 int field; // Laid out between C and A subobjects in D.
357};
358
359void D::bar() {
360 // CHECK-LABEL: define x86_thiscallcc void @"\01?bar@D@test3@@UAEXXZ"(%"struct.test3::D"* %this)
361
362 C::foo();
363 // Shouldn't need any vbtable lookups. All we have to do is adjust to C*,
364 // then compensate for the adjustment performed in the C::foo() prologue.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700365 // CHECK-NOT: load i8*, i8**
Stephen Hines651f13c2014-04-23 16:59:28 -0700366 // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test3::D"* %{{.*}} to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700367 // CHECK: %[[C_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 8
Stephen Hines651f13c2014-04-23 16:59:28 -0700368 // CHECK: %[[C:.*]] = bitcast i8* %[[C_i8]] to %"struct.test3::C"*
369 // CHECK: %[[C_i8:.*]] = bitcast %"struct.test3::C"* %[[C]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700370 // CHECK: %[[ARG:.*]] = getelementptr i8, i8* %[[C_i8]], i32 4
Stephen Hines651f13c2014-04-23 16:59:28 -0700371 // CHECK: call x86_thiscallcc void @"\01?foo@C@test3@@UAEXXZ"(i8* %[[ARG]])
372 // CHECK: ret
373}
374}
375
376namespace test4{
377// PR19172: We used to merge method vftable locations wrong.
378
379struct A {
380 virtual ~A() {}
381};
382
383struct B {
384 virtual ~B() {}
385};
386
387struct C : virtual A, B {
388 virtual ~C();
389};
390
391void foo(void*);
392
393C::~C() {
394 // CHECK-LABEL: define x86_thiscallcc void @"\01??1C@test4@@UAE@XZ"(%"struct.test4::C"* %this)
395
396 // In this case "this" points to the most derived class, so no GEPs needed.
397 // CHECK-NOT: getelementptr
398 // CHECK-NOT: bitcast
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700399 // CHECK: %[[VFPTR_i8:.*]] = bitcast %"struct.test4::C"* %{{.*}} to i32 (...)***
400 // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7C@test4@@6BB@1@@" to i32 (...)**), i32 (...)*** %[[VFPTR_i8]]
Stephen Hines651f13c2014-04-23 16:59:28 -0700401
402 foo(this);
403 // CHECK: ret
404}
405
406void destroy(C *obj) {
407 // CHECK-LABEL: define void @"\01?destroy@test4@@YAXPAUC@1@@Z"(%"struct.test4::C"* %obj)
408
409 delete obj;
Stephen Hines176edba2014-12-01 14:53:08 -0800410 // CHECK: %[[VPTR:.*]] = bitcast %"struct.test4::C"* %[[OBJ:.*]] to i8* (%"struct.test4::C"*, i32)***
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700411 // CHECK: %[[VFTABLE:.*]] = load i8* (%"struct.test4::C"*, i32)**, i8* (%"struct.test4::C"*, i32)*** %[[VPTR]]
412 // CHECK: %[[VFTENTRY:.*]] = getelementptr inbounds i8* (%"struct.test4::C"*, i32)*, i8* (%"struct.test4::C"*, i32)** %[[VFTABLE]], i64 0
413 // CHECK: %[[VFUN:.*]] = load i8* (%"struct.test4::C"*, i32)*, i8* (%"struct.test4::C"*, i32)** %[[VFTENTRY]]
Stephen Hines176edba2014-12-01 14:53:08 -0800414 // CHECK: call x86_thiscallcc i8* %[[VFUN]](%"struct.test4::C"* %[[OBJ]], i32 1)
Stephen Hines651f13c2014-04-23 16:59:28 -0700415 // CHECK: ret
416}
417
418struct D {
419 virtual void d();
420};
421
422// The first non-virtual base doesn't have a vdtor,
423// but "this adjustment" is not needed.
424struct E : D, B, virtual A {
425 virtual ~E();
426};
427
428E::~E() {
429 // CHECK-LABEL: define x86_thiscallcc void @"\01??1E@test4@@UAE@XZ"(%"struct.test4::E"* %this)
430
431 // In this case "this" points to the most derived class, so no GEPs needed.
432 // CHECK-NOT: getelementptr
433 // CHECK-NOT: bitcast
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700434 // CHECK: %[[VFPTR_i8:.*]] = bitcast %"struct.test4::E"* %{{.*}} to i32 (...)***
435 // CHECK: store i32 (...)** bitcast ([1 x i8*]* @"\01??_7E@test4@@6BD@1@@" to i32 (...)**), i32 (...)*** %[[VFPTR_i8]]
Stephen Hines651f13c2014-04-23 16:59:28 -0700436 foo(this);
437}
438
439void destroy(E *obj) {
440 // CHECK-LABEL: define void @"\01?destroy@test4@@YAXPAUE@1@@Z"(%"struct.test4::E"* %obj)
441
442 // CHECK-NOT: getelementptr
443 // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test4::E"* %[[OBJ:.*]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700444 // CHECK: %[[B_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 4
Stephen Hines176edba2014-12-01 14:53:08 -0800445 // CHECK: %[[VPTR:.*]] = bitcast i8* %[[B_i8]] to i8* (%"struct.test4::E"*, i32)***
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700446 // CHECK: %[[VFTABLE:.*]] = load i8* (%"struct.test4::E"*, i32)**, i8* (%"struct.test4::E"*, i32)*** %[[VPTR]]
447 // CHECK: %[[VFTENTRY:.*]] = getelementptr inbounds i8* (%"struct.test4::E"*, i32)*, i8* (%"struct.test4::E"*, i32)** %[[VFTABLE]], i64 0
448 // CHECK: %[[VFUN:.*]] = load i8* (%"struct.test4::E"*, i32)*, i8* (%"struct.test4::E"*, i32)** %[[VFTENTRY]]
Stephen Hines651f13c2014-04-23 16:59:28 -0700449 // CHECK: %[[OBJ_i8:.*]] = bitcast %"struct.test4::E"* %[[OBJ]] to i8*
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700450 // CHECK: %[[B_i8:.*]] = getelementptr inbounds i8, i8* %[[OBJ_i8]], i32 4
Stephen Hines651f13c2014-04-23 16:59:28 -0700451 // FIXME: in fact, the call should take i8* and the bitcast is redundant.
452 // CHECK: %[[B_as_E:.*]] = bitcast i8* %[[B_i8]] to %"struct.test4::E"*
Stephen Hines176edba2014-12-01 14:53:08 -0800453 // CHECK: call x86_thiscallcc i8* %[[VFUN]](%"struct.test4::E"* %[[B_as_E]], i32 1)
Stephen Hines651f13c2014-04-23 16:59:28 -0700454 delete obj;
455}
456
457}