builtin-launder.cpp
10.4 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// RUN: %clang_cc1 -triple=x86_64-linux-gnu -emit-llvm -fstrict-vtable-pointers -o - %s \
// RUN: | FileCheck --check-prefixes=CHECK,CHECK-STRICT %s
// RUN: %clang_cc1 -triple=x86_64-linux-gnu -emit-llvm -o - %s \
// RUN: | FileCheck --check-prefixes=CHECK,CHECK-NONSTRICT %s
//===----------------------------------------------------------------------===//
// Positive Cases
//===----------------------------------------------------------------------===//
struct TestVirtualFn {
virtual void foo() {}
};
// CHECK-LABEL: define void @test_builtin_launder_virtual_fn
extern "C" void test_builtin_launder_virtual_fn(TestVirtualFn *p) {
// CHECK: store [[TYPE:%[^ ]+]] %p, [[TYPE]]* %p.addr
// CHECK-NEXT: [[TMP0:%.*]] = load [[TYPE]], [[TYPE]]* %p.addr
// CHECK-NONSTRICT-NEXT: store [[TYPE]] [[TMP0]], [[TYPE]]* %d
// CHECK-STRICT-NEXT: [[TMP1:%.*]] = bitcast [[TYPE]] [[TMP0]] to i8*
// CHECK-STRICT-NEXT: [[TMP2:%.*]] = call i8* @llvm.launder.invariant.group.p0i8(i8* [[TMP1]])
// CHECK-STRICT-NEXT: [[TMP3:%.*]] = bitcast i8* [[TMP2]] to [[TYPE]]
// CHECK-STRICT-NEXT: store [[TYPE]] [[TMP3]], [[TYPE]]* %d
// CHECK-NEXT: ret void
TestVirtualFn *d = __builtin_launder(p);
}
struct TestPolyBase : TestVirtualFn {
};
// CHECK-LABEL: define void @test_builtin_launder_poly_base
extern "C" void test_builtin_launder_poly_base(TestPolyBase *p) {
// CHECK-STRICT-NOT: ret void
// CHECK-STRICT: @llvm.launder.invariant.group
// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group
// CHECK: ret void
TestPolyBase *d = __builtin_launder(p);
}
struct TestBase {};
struct TestVirtualBase : virtual TestBase {};
// CHECK-LABEL: define void @test_builtin_launder_virtual_base
extern "C" void test_builtin_launder_virtual_base(TestVirtualBase *p) {
// CHECK-STRICT-NOT: ret void
// CHECK-STRICT: @llvm.launder.invariant.group
// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group
// CHECK: ret void
TestVirtualBase *d = __builtin_launder(p);
}
//===----------------------------------------------------------------------===//
// Negative Cases
//===----------------------------------------------------------------------===//
// CHECK-LABEL: define void @test_builtin_launder_ommitted_one
extern "C" void test_builtin_launder_ommitted_one(int *p) {
// CHECK: entry
// CHECK-NEXT: %p.addr = alloca i32*
// CHECK-NEXT: %d = alloca i32*
// CHECK-NEXT: store i32* %p, i32** %p.addr, align 8
// CHECK-NEXT: [[TMP:%.*]] = load i32*, i32** %p.addr
// CHECK-NEXT: store i32* [[TMP]], i32** %d
// CHECK-NEXT: ret void
int *d = __builtin_launder(p);
}
struct TestNoInvariant {
int x;
};
// CHECK-LABEL: define void @test_builtin_launder_ommitted_two
extern "C" void test_builtin_launder_ommitted_two(TestNoInvariant *p) {
// CHECK: entry
// CHECK-NOT: llvm.launder.invariant.group
// CHECK-NEXT: %p.addr = alloca [[TYPE:%.*]], align 8
// CHECK-NEXT: %d = alloca [[TYPE]]
// CHECK-NEXT: store [[TYPE]] %p, [[TYPE]]* %p.addr
// CHECK-NEXT: [[TMP:%.*]] = load [[TYPE]], [[TYPE]]* %p.addr
// CHECK-NEXT: store [[TYPE]] [[TMP]], [[TYPE]]* %d
// CHECK-NEXT: ret void
TestNoInvariant *d = __builtin_launder(p);
}
struct TestVirtualMember {
TestVirtualFn member;
};
// CHECK-LABEL: define void @test_builtin_launder_virtual_member
extern "C" void test_builtin_launder_virtual_member(TestVirtualMember *p) {
// CHECK: entry
// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group
// CHECK-STRICT: @llvm.launder.invariant.group
// CHECK: ret void
TestVirtualMember *d = __builtin_launder(p);
}
struct TestVirtualMemberDepth2 {
TestVirtualMember member;
};
// CHECK-LABEL: define void @test_builtin_launder_virtual_member_depth_2
extern "C" void test_builtin_launder_virtual_member_depth_2(TestVirtualMemberDepth2 *p) {
// CHECK: entry
// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group
// CHECK-STRICT: @llvm.launder.invariant.group
// CHECK: ret void
TestVirtualMemberDepth2 *d = __builtin_launder(p);
}
struct TestVirtualReferenceMember {
TestVirtualFn &member;
};
// CHECK-LABEL: define void @test_builtin_launder_virtual_reference_member
extern "C" void test_builtin_launder_virtual_reference_member(TestVirtualReferenceMember *p) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
TestVirtualReferenceMember *d = __builtin_launder(p);
}
struct TestRecursiveMember {
TestRecursiveMember() : member(*this) {}
TestRecursiveMember &member;
};
// CHECK-LABEL: define void @test_builtin_launder_recursive_member
extern "C" void test_builtin_launder_recursive_member(TestRecursiveMember *p) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
TestRecursiveMember *d = __builtin_launder(p);
}
struct TestVirtualRecursiveMember {
TestVirtualRecursiveMember() : member(*this) {}
TestVirtualRecursiveMember &member;
virtual void foo();
};
// CHECK-LABEL: define void @test_builtin_launder_virtual_recursive_member
extern "C" void test_builtin_launder_virtual_recursive_member(TestVirtualRecursiveMember *p) {
// CHECK: entry
// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group
// CHECK-STRICT: @llvm.launder.invariant.group
// CHECK: ret void
TestVirtualRecursiveMember *d = __builtin_launder(p);
}
// CHECK-LABEL: define void @test_builtin_launder_array(
extern "C" void test_builtin_launder_array(TestVirtualFn (&Arr)[5]) {
// CHECK: entry
// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group
// CHECK-STRICT: @llvm.launder.invariant.group
// CHECK: ret void
TestVirtualFn *d = __builtin_launder(Arr);
}
// CHECK-LABEL: define void @test_builtin_launder_array_nested(
extern "C" void test_builtin_launder_array_nested(TestVirtualFn (&Arr)[5][2]) {
// CHECK: entry
// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group
// CHECK-STRICT: @llvm.launder.invariant.group
// CHECK: ret void
using RetTy = TestVirtualFn(*)[2];
RetTy d = __builtin_launder(Arr);
}
// CHECK-LABEL: define void @test_builtin_launder_array_no_invariant(
extern "C" void test_builtin_launder_array_no_invariant(TestNoInvariant (&Arr)[5]) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
TestNoInvariant *d = __builtin_launder(Arr);
}
// CHECK-LABEL: define void @test_builtin_launder_array_nested_no_invariant(
extern "C" void test_builtin_launder_array_nested_no_invariant(TestNoInvariant (&Arr)[5][2]) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
using RetTy = TestNoInvariant(*)[2];
RetTy d = __builtin_launder(Arr);
}
template <class Member>
struct WithMember {
Member mem;
};
template struct WithMember<TestVirtualFn[5]>;
// CHECK-LABEL: define void @test_builtin_launder_member_array(
extern "C" void test_builtin_launder_member_array(WithMember<TestVirtualFn[5]> *p) {
// CHECK: entry
// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group
// CHECK-STRICT: @llvm.launder.invariant.group
// CHECK: ret void
auto *d = __builtin_launder(p);
}
template struct WithMember<TestVirtualFn[5][2]>;
// CHECK-LABEL: define void @test_builtin_launder_member_array_nested(
extern "C" void test_builtin_launder_member_array_nested(WithMember<TestVirtualFn[5][2]> *p) {
// CHECK: entry
// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group
// CHECK-STRICT: @llvm.launder.invariant.group
// CHECK: ret void
auto *d = __builtin_launder(p);
}
template struct WithMember<TestNoInvariant[5]>;
// CHECK-LABEL: define void @test_builtin_launder_member_array_no_invariant(
extern "C" void test_builtin_launder_member_array_no_invariant(WithMember<TestNoInvariant[5]> *p) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
auto *d = __builtin_launder(p);
}
template struct WithMember<TestNoInvariant[5][2]>;
// CHECK-LABEL: define void @test_builtin_launder_member_array_nested_no_invariant(
extern "C" void test_builtin_launder_member_array_nested_no_invariant(WithMember<TestNoInvariant[5][2]> *p) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
auto *d = __builtin_launder(p);
}
template <class T>
struct WithBase : T {};
template struct WithBase<TestNoInvariant>;
// CHECK-LABEL: define void @test_builtin_launder_base_no_invariant(
extern "C" void test_builtin_launder_base_no_invariant(WithBase<TestNoInvariant> *p) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
auto *d = __builtin_launder(p);
}
template struct WithBase<TestVirtualFn>;
// CHECK-LABEL: define void @test_builtin_launder_base(
extern "C" void test_builtin_launder_base(WithBase<TestVirtualFn> *p) {
// CHECK: entry
// CHECK-NONSTRICT-NOT: @llvm.launder.invariant.group
// CHECK-STRICT: @llvm.launder.invariant.group
// CHECK: ret void
auto *d = __builtin_launder(p);
}
/// The test cases in this namespace technically need to be laundered according
/// to the language in the standard (ie they have const or reference subobjects)
/// but LLVM doesn't currently optimize on these cases -- so Clang emits
/// __builtin_launder as a nop.
///
/// NOTE: Adding optimizations for these cases later is an LTO ABI break. That's
/// probably OK for now -- but is something to keep in mind.
namespace pessimizing_cases {
struct TestConstMember {
const int x;
};
// CHECK-LABEL: define void @test_builtin_launder_const_member
extern "C" void test_builtin_launder_const_member(TestConstMember *p) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
TestConstMember *d = __builtin_launder(p);
}
struct TestConstSubobject {
TestConstMember x;
};
// CHECK-LABEL: define void @test_builtin_launder_const_subobject
extern "C" void test_builtin_launder_const_subobject(TestConstSubobject *p) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
TestConstSubobject *d = __builtin_launder(p);
}
struct TestConstObject {
const struct TestConstMember x;
};
// CHECK-LABEL: define void @test_builtin_launder_const_object
extern "C" void test_builtin_launder_const_object(TestConstObject *p) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
TestConstObject *d = __builtin_launder(p);
}
struct TestReferenceMember {
int &x;
};
// CHECK-LABEL: define void @test_builtin_launder_reference_member
extern "C" void test_builtin_launder_reference_member(TestReferenceMember *p) {
// CHECK: entry
// CHECK-NOT: @llvm.launder.invariant.group
// CHECK: ret void
TestReferenceMember *d = __builtin_launder(p);
}
} // namespace pessimizing_cases