1 // RUN: %clang_cc1 -std=c++11 -triple x86_64-none-linux-gnu -fmerge-all-constants -emit-llvm -o - %s | FileCheck -check-prefixes=X86,CHECK %s
2 // RUN: %clang_cc1 -std=c++11 -triple amdgcn-amd-amdhsa -DNO_TLS -fmerge-all-constants -emit-llvm -o - %s | FileCheck -check-prefixes=AMDGCN,CHECK %s
5 typedef decltype(sizeof(int)) size_t;
7 // libc++'s implementation
14 initializer_list(const _E
* __b
, size_t __s
)
20 typedef _E value_type
;
21 typedef const _E
& reference
;
22 typedef const _E
& const_reference
;
23 typedef size_t size_type
;
25 typedef const _E
* iterator
;
26 typedef const _E
* const_iterator
;
28 initializer_list() : __begin_(nullptr), __size_(0) {}
30 size_t size() const {return __size_
;}
31 const _E
* begin() const {return __begin_
;}
32 const _E
* end() const {return __begin_
+ __size_
;}
43 witharg1(const destroyme1
&);
47 wantslist1(std::initializer_list
<destroyme1
>);
50 // X86: @_ZGR15globalInitList1_ = internal constant [3 x i32] [i32 1, i32 2, i32 3]
51 // X86: @globalInitList1 ={{.*}} global %{{[^ ]+}} { ptr @_ZGR15globalInitList1_, i{{32|64}} 3 }
52 // AMDGCN: @_ZGR15globalInitList1_ = internal addrspace(1) constant [3 x i32] [i32 1, i32 2, i32 3]
53 // AMDGCN: @globalInitList1 ={{.*}} addrspace(1) global %{{[^ ]+}} { ptr addrspacecast (ptr addrspace(1) @_ZGR15globalInitList1_ to ptr), i{{32|64}} 3 }
54 std::initializer_list
<int> globalInitList1
= {1, 2, 3};
57 namespace thread_local_global_array
{
58 // FIXME: We should be able to constant-evaluate this even though the
59 // initializer is not a constant expression (pointers to thread_local
60 // objects aren't really a problem).
62 // X86: @_ZN25thread_local_global_array1xE ={{.*}} thread_local global
63 // X86: @_ZGRN25thread_local_global_array1xE_ = internal thread_local constant [4 x i32] [i32 1, i32 2, i32 3, i32 4]
64 std::initializer_list
<int> thread_local x
= {1, 2, 3, 4};
68 // X86: @globalInitList2 ={{.*}} global %{{[^ ]+}} zeroinitializer
69 // X86: @_ZGR15globalInitList2_ = internal global [2 x %[[WITHARG:[^ ]*]]] zeroinitializer
70 // AMDGCN: @globalInitList2 ={{.*}} addrspace(1) global %{{[^ ]+}} zeroinitializer
71 // AMDGCN: @_ZGR15globalInitList2_ = internal addrspace(1) global [2 x %[[WITHARG:[^ ]*]]] zeroinitializer
73 // X86: @_ZN15partly_constant1kE ={{.*}} global i32 0, align 4
74 // X86: @_ZN15partly_constant2ilE ={{.*}} global {{.*}} null, align 8
75 // X86: @[[PARTLY_CONSTANT_OUTER:_ZGRN15partly_constant2ilE_]] = internal global {{.*}} zeroinitializer, align 8
76 // X86: @[[PARTLY_CONSTANT_INNER:_ZGRN15partly_constant2ilE0_]] = internal global [3 x {{.*}}] zeroinitializer, align 8
77 // X86: @[[PARTLY_CONSTANT_FIRST:_ZGRN15partly_constant2ilE1_]] = internal constant [3 x i32] [i32 1, i32 2, i32 3], align 4
78 // X86: @[[PARTLY_CONSTANT_SECOND:_ZGRN15partly_constant2ilE2_]] = internal global [2 x i32] zeroinitializer, align 4
79 // X86: @[[PARTLY_CONSTANT_THIRD:_ZGRN15partly_constant2ilE3_]] = internal constant [4 x i32] [i32 5, i32 6, i32 7, i32 8], align 4
80 // AMDGCN: @_ZN15partly_constant1kE ={{.*}} addrspace(1) global i32 0, align 4
81 // AMDGCN: @_ZN15partly_constant2ilE ={{.*}} addrspace(1) global {{.*}} null, align 8
82 // AMDGCN: @[[PARTLY_CONSTANT_OUTER:_ZGRN15partly_constant2ilE_]] = internal addrspace(1) global {{.*}} zeroinitializer, align 8
83 // AMDGCN: @[[PARTLY_CONSTANT_INNER:_ZGRN15partly_constant2ilE0_]] = internal addrspace(1) global [3 x {{.*}}] zeroinitializer, align 8
84 // AMDGCN: @[[PARTLY_CONSTANT_FIRST:_ZGRN15partly_constant2ilE1_]] = internal addrspace(1) constant [3 x i32] [i32 1, i32 2, i32 3], align 4
85 // AMDGCN: @[[PARTLY_CONSTANT_SECOND:_ZGRN15partly_constant2ilE2_]] = internal addrspace(1) global [2 x i32] zeroinitializer, align 4
86 // AMDGCN: @[[PARTLY_CONSTANT_THIRD:_ZGRN15partly_constant2ilE3_]] = internal addrspace(1) constant [4 x i32] [i32 5, i32 6, i32 7, i32 8], align 4
88 // X86: @[[REFTMP1:.*]] = private constant [2 x i32] [i32 42, i32 43], align 4
89 // X86: @[[REFTMP2:.*]] = private constant [3 x %{{.*}}] [%{{.*}} { i32 1 }, %{{.*}} { i32 2 }, %{{.*}} { i32 3 }], align 4
90 // AMDGCN: @[[REFTMP1:.*]] = private addrspace(4) constant [2 x i32] [i32 42, i32 43], align 4
91 // AMDGCN: @[[REFTMP2:.*]] = private addrspace(4) constant [3 x %{{.*}}] [%{{.*}} { i32 1 }, %{{.*}} { i32 2 }, %{{.*}} { i32 3 }], align 4
93 // CHECK: appending global
95 // thread_local initializer:
96 // X86-LABEL: define internal void @__cxx_global_var_init
97 // X86: [[ADDR:%.*]] = call {{.*}} ptr @llvm.threadlocal.address.p0(ptr {{.*}} @_ZN25thread_local_global_array1xE)
98 // X86: store ptr @_ZGRN25thread_local_global_array1xE_, ptr [[ADDR]], align 8
99 // X86: store i64 4, ptr getelementptr inbounds ({{.*}}, ptr @_ZN25thread_local_global_array1xE, i32 0, i32 1), align 8
101 // CHECK-LABEL: define internal void @__cxx_global_var_init
102 // X86: call void @_ZN8witharg1C1ERK10destroyme1(ptr {{[^,]*}} @_ZGR15globalInitList2_
103 // X86: call void @_ZN8witharg1C1ERK10destroyme1(ptr {{[^,]*}} getelementptr inbounds (%[[WITHARG]], ptr @_ZGR15globalInitList2_, i{{32|64}} 1)
104 // AMDGCN: call void @_ZN8witharg1C1ERK10destroyme1(ptr {{[^,]*}} addrspacecast ({{[^@]+}} @_ZGR15globalInitList2_ {{[^)]+}}
105 // AMDGCN: call void @_ZN8witharg1C1ERK10destroyme1(ptr {{[^,]*}} getelementptr inbounds (%[[WITHARG]], ptr addrspacecast ({{[^@]+}} @_ZGR15globalInitList2_ {{[^)]+}}), i{{32|64}} 1
106 // CHECK: call i32 @__cxa_atexit
107 // X86: store ptr @_ZGR15globalInitList2_, ptr @globalInitList2, align 8
108 // X86: store i64 2, ptr getelementptr inbounds (%{{.*}}, ptr @globalInitList2, i32 0, i32 1), align 8
109 // AMDGCN: store ptr addrspacecast ({{[^@]+}} @_ZGR15globalInitList2_ {{[^)]+}}),
110 // AMDGCN: ptr addrspacecast ({{[^@]+}} @globalInitList2 {{[^)]+}}), align 8
111 // AMDGCN: store i64 2, ptr getelementptr inbounds (%{{.*}}, ptr addrspacecast ({{[^@]+}} @globalInitList2 {{[^)]+}}), i32 0, i32 1), align 8
112 // CHECK: call void @_ZN10destroyme1D1Ev
113 // CHECK-NEXT: call void @_ZN10destroyme1D1Ev
114 // CHECK-NEXT: ret void
115 std::initializer_list
<witharg1
> globalInitList2
= {
116 witharg1(destroyme1()), witharg1(destroyme1())
120 // CHECK-LABEL: define{{.*}} void @_Z3fn1i
122 // X86: [[array:%[^ ]+]] = alloca [3 x i32]
123 // AMDGCN: [[alloca:%[^ ]+]] = alloca [3 x i32], align 4, addrspace(5)
124 // AMDGCN: [[array:%[^ ]+]] ={{.*}} addrspacecast ptr addrspace(5) [[alloca]] to ptr
125 // CHECK: getelementptr inbounds [3 x i32], ptr [[array]], i{{32|64}} 0
126 // CHECK-NEXT: store i32 1, ptr
127 // CHECK-NEXT: getelementptr
129 // CHECK-NEXT: getelementptr
133 // CHECK-NEXT: getelementptr
134 // CHECK-NEXT: getelementptr inbounds [3 x i32], ptr
135 // CHECK-NEXT: store ptr
136 // CHECK-NEXT: getelementptr
137 // CHECK-NEXT: store i{{32|64}} 3
138 std::initializer_list
<int> intlist
{1, 2, i
};
142 // CHECK-LABEL: define{{.*}} void @_Z3fn2v
143 void target(std::initializer_list
<destroyme1
>);
144 // objects should be destroyed before dm2, after call returns
145 // CHECK: call void @_Z6targetSt16initializer_listI10destroyme1E
146 target({ destroyme1(), destroyme1() });
147 // CHECK: call void @_ZN10destroyme1D1Ev
149 // CHECK: call void @_ZN10destroyme2D1Ev
153 // CHECK-LABEL: define{{.*}} void @_Z3fn3v
154 // objects should be destroyed after dm2
155 auto list
= { destroyme1(), destroyme1() };
157 // CHECK: call void @_ZN10destroyme2D1Ev
158 // CHECK: call void @_ZN10destroyme1D1Ev
162 // CHECK-LABEL: define{{.*}} void @_Z3fn4v
163 void target(std::initializer_list
<witharg1
>);
164 // objects should be destroyed before dm2, after call returns
165 // CHECK: call void @_ZN8witharg1C1ERK10destroyme1
166 // CHECK: call void @_Z6targetSt16initializer_listI8witharg1E
167 target({ witharg1(destroyme1()), witharg1(destroyme1()) });
168 // CHECK: call void @_ZN8witharg1D1Ev
169 // CHECK: call void @_ZN10destroyme1D1Ev
171 // CHECK: call void @_ZN10destroyme2D1Ev
175 // CHECK-LABEL: define{{.*}} void @_Z3fn5v
176 // temps should be destroyed before dm2
177 // objects should be destroyed after dm2
178 // CHECK: call void @_ZN8witharg1C1ERK10destroyme1
179 auto list
= { witharg1(destroyme1()), witharg1(destroyme1()) };
180 // CHECK: call void @_ZN10destroyme1D1Ev
182 // CHECK: call void @_ZN10destroyme2D1Ev
183 // CHECK: call void @_ZN8witharg1D1Ev
187 // CHECK-LABEL: define{{.*}} void @_Z3fn6v
188 void target(const wantslist1
&);
189 // objects should be destroyed before dm2, after call returns
190 // CHECK: call void @_ZN10wantslist1C1ESt16initializer_listI10destroyme1E
191 // CHECK: call void @_Z6targetRK10wantslist1
192 target({ destroyme1(), destroyme1() });
193 // CHECK: call void @_ZN10wantslist1D1Ev
194 // CHECK: call void @_ZN10destroyme1D1Ev
196 // CHECK: call void @_ZN10destroyme2D1Ev
199 // CHECK-LABEL: define{{.*}} void @_Z3fn7v
200 // temps should be destroyed before dm2
201 // object should be destroyed after dm2
202 // CHECK: call void @_ZN10wantslist1C1ESt16initializer_listI10destroyme1E
203 wantslist1 wl
= { destroyme1(), destroyme1() };
204 // CHECK: call void @_ZN10destroyme1D1Ev
206 // CHECK: call void @_ZN10destroyme2D1Ev
207 // CHECK: call void @_ZN10wantslist1D1Ev
211 // CHECK-LABEL: define{{.*}} void @_Z3fn8v
212 void target(std::initializer_list
<std::initializer_list
<destroyme1
>>);
213 // objects should be destroyed before dm2, after call returns
214 // CHECK: call void @_Z6targetSt16initializer_listIS_I10destroyme1EE
215 std::initializer_list
<destroyme1
> inner
;
216 target({ inner
, { destroyme1() } });
217 // CHECK: call void @_ZN10destroyme1D1Ev
218 // Only one destroy loop, since only one inner init list is directly inited.
219 // CHECK-NOT: call void @_ZN10destroyme1D1Ev
221 // CHECK: call void @_ZN10destroyme2D1Ev
225 // CHECK-LABEL: define{{.*}} void @_Z3fn9v
226 // objects should be destroyed after dm2
227 std::initializer_list
<destroyme1
> inner
;
228 std::initializer_list
<std::initializer_list
<destroyme1
>> list
=
229 { inner
, { destroyme1() } };
231 // CHECK: call void @_ZN10destroyme2D1Ev
232 // CHECK: call void @_ZN10destroyme1D1Ev
233 // Only one destroy loop, since only one inner init list is directly inited.
234 // CHECK-NOT: call void @_ZN10destroyme1D1Ev
239 // CHECK-LABEL: define{{.*}} void @_Z4fn10i
240 // CHECK: alloca [3 x i32]
241 // CHECK-X86: call noalias nonnull align 16 ptr @_Znw{{[jm]}}
242 // CHECK-AMDGPU: call noalias nonnull align 8 ptr @_Znw{{[jm]}}
243 // CHECK: store i32 %
244 // CHECK: store i32 2
245 // CHECK: store i32 3
247 (void) new std::initializer_list
<int> {i
, 2, 3};
251 // CHECK-LABEL: define{{.*}} void @_Z4fn11v
252 (void) new std::initializer_list
<destroyme1
> {destroyme1(), destroyme1()};
253 // CHECK: call void @_ZN10destroyme1D1Ev
255 // CHECK: call void @_ZN10destroyme2D1Ev
270 map(std::initializer_list
<pair
>);
273 map m
{ {1, 2}, {3, 4} };
276 namespace rdar13325066
{
279 // CHECK-LABEL: define{{.*}} void @_ZN12rdar133250664loopERNS_1XES1_
280 void loop(X
&x1
, X
&x2
) {
284 // CHECK: call void @_ZN12rdar133250661XD1Ev
287 // CHECK: call void @_ZN12rdar133250661XD1Ev
291 for (X x
: { x1
, x2
}) { }
302 // CHECK-LABEL: define{{.*}} void @_ZN5dtors1fEv(
304 // CHECK: call void @_ZN5dtors1SC1Ev(
305 // CHECK: call void @_ZN5dtors1SC1Ev(
306 std::initializer_list
<S
>{ S(), S() };
308 // Destruction loop for underlying array.
310 // CHECK: call void @_ZN5dtors1SD1Ev(
313 // CHECK: call void @_ZN5dtors1zEv(
316 // CHECK-NOT: call void @_ZN5dtors1SD1Ev(
319 // CHECK-LABEL: define{{.*}} void @_ZN5dtors1gEv(
321 // CHECK: call void @_ZN5dtors1SC1Ev(
322 // CHECK: call void @_ZN5dtors1SC1Ev(
323 auto x
= std::initializer_list
<S
>{ S(), S() };
325 // Destruction loop for underlying array.
327 // CHECK: call void @_ZN5dtors1SD1Ev(
330 // CHECK: call void @_ZN5dtors1zEv(
333 // CHECK-NOT: call void @_ZN5dtors1SD1Ev(
336 // CHECK-LABEL: define{{.*}} void @_ZN5dtors1hEv(
338 // CHECK: call void @_ZN5dtors1SC1Ev(
339 // CHECK: call void @_ZN5dtors1SC1Ev(
340 std::initializer_list
<S
> x
= { S(), S() };
342 // CHECK-NOT: call void @_ZN5dtors1SD1Ev(
344 // CHECK: call void @_ZN5dtors1zEv(
347 // Destruction loop for underlying array.
349 // CHECK: call void @_ZN5dtors1SD1Ev(
354 namespace partly_constant
{
356 std::initializer_list
<std::initializer_list
<int>> &&il
= { { 1, 2, 3 }, { 4, k
}, { 5, 6, 7, 8 } };
358 // CHECK-NOT: @[[PARTLY_CONSTANT_FIRST]],
359 // CHECK: store ptr {{.*}}@[[PARTLY_CONSTANT_FIRST]]{{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}
360 // CHECK: store i64 3, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i32 0, i32 1)
361 // CHECK-NOT: @[[PARTLY_CONSTANT_FIRST]],
363 // Second init list array (non-constant).
364 // CHECK: store i32 4, ptr {{.*}}@[[PARTLY_CONSTANT_SECOND]]{{.*}}
365 // CHECK: load i32, ptr {{.*}}@_ZN15partly_constant1kE
366 // CHECK: store i32 {{.*}}, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_SECOND]]{{.*}}, i64 1)
369 // CHECK: store ptr {{.*}}@[[PARTLY_CONSTANT_SECOND]]{{.*}}, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 1)
370 // CHECK: store i64 2, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 1, i32 1)
373 // CHECK-NOT: @[[PARTLY_CONSTANT_THIRD]],
374 // CHECK: store ptr {{.*}}@[[PARTLY_CONSTANT_THIRD]]{{.*}}, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 2)
375 // CHECK: store i64 4, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, i64 2, i32 1)
376 // CHECK-NOT: @[[PARTLY_CONSTANT_THIRD]],
379 // CHECK: store ptr {{.*}}@[[PARTLY_CONSTANT_INNER]]{{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_OUTER]]{{.*}}
380 // CHECK: store i64 3, ptr getelementptr inbounds ({{.*}}, ptr {{.*}}@[[PARTLY_CONSTANT_OUTER]]{{.*}}, i32 0, i32 1)
383 // CHECK: store ptr {{.*}}@[[PARTLY_CONSTANT_OUTER]]{{.*}}, ptr {{.*}}@_ZN15partly_constant2ilE{{.*}}, align 8
386 struct A
{ A(); ~A(); };
387 struct B
{ const A
&a
; ~B(); };
388 struct C
{ std::initializer_list
<B
> b
; ~C(); };
390 // CHECK-LABEL: define{{.*}} void @_ZN6nested1gEv(
392 // CHECK: call void @_ZN6nested1AC1Ev(
394 // CHECK: call void @_ZN6nested1AC1Ev(
396 const C
&c
{ { { A() }, { A() } } };
398 // CHECK: call void @_ZN6nested1fEv(
402 // CHECK: call void @_ZN6nested1CD1Ev(
405 // Destroy B[2] array.
406 // FIXME: This isn't technically correct: reverse construction order would
407 // destroy the second B then the second A then the first B then the first A.
408 // CHECK: call void @_ZN6nested1BD1Ev(
413 // CHECK: call void @_ZN6nested1AD1Ev(
415 // CHECK: call void @_ZN6nested1AD1Ev(
423 A(std::initializer_list
<int>);
431 std::initializer_list
<int> a
;
433 std::initializer_list
<double> c
;
438 namespace ArrayOfInitList
{
440 S(std::initializer_list
<int>);
446 struct vector
{ vector(std::initializer_list
<int>); };
447 struct MyClass
{ explicit MyClass(const vector
&v
); };
448 template<int x
> void f() { new MyClass({42, 43}); }
449 template void f
<0>();
450 // CHECK-LABEL: define {{.*}} @_ZN7PR204451fILi0EEEvv(
451 // CHECK: store ptr {{.*}}@[[REFTMP1]]{{.*}}
452 // CHECK: call void @_ZN7PR204456vectorC1ESt16initializer_listIiE(
453 // CHECK: call void @_ZN7PR204457MyClassC1ERKNS_6vectorE(
456 namespace ConstExpr
{
460 constexpr C(int x
) : x(x
) {}
462 void f(std::initializer_list
<C
>);
464 // CHECK-LABEL: _ZN9ConstExpr1gEv
465 // CHECK: store ptr {{.*}}@[[REFTMP2]]{{.*}}
466 // CHECK: call void @_ZN9ConstExpr1fESt16initializer_listINS_1CEE
467 f({C(1), C(2), C(3)});
471 namespace B19773010
{
472 template <class T1
, class T2
> struct pair
{
475 constexpr pair() : first(), second() {}
476 constexpr pair(T1 a
, T2 b
) : first(a
), second(b
) {}
479 enum E
{ ENUM_CONSTANT
};
481 testcase(std::initializer_list
<pair
<const char *, E
>>);
484 // CHECK-LABEL: @_ZN9B197730102f1Ev
485 testcase a
{{"", ENUM_CONSTANT
}};
486 // X86: store ptr @.ref.tmp{{.*}}, ptr %{{.*}}, align 8
487 // AMDGCN: store ptr addrspacecast{{.*}} @.ref.tmp{{.*}}{{.*}}, ptr %{{.*}}, align 8
490 // CHECK-LABEL: @_ZN9B197730102f2Ev
491 // X86: store ptr @_ZGRZN9B197730102f2EvE1p_, ptr getelementptr inbounds (%"class.std::initializer_list.10", ptr @_ZZN9B197730102f2EvE1p, i64 1), align 16
492 // AMDGCN: store ptr addrspacecast{{.*}} @_ZGRZN9B197730102f2EvE1p_{{.*}}, ptr getelementptr inbounds (%"class.std::initializer_list.10", ptr addrspacecast{{.*}}@_ZZN9B197730102f2EvE1p{{.*}}, i64 1), align 8
493 static std::initializer_list
<pair
<const char *, E
>> a
, p
[2] =
494 {a
, {{"", ENUM_CONSTANT
}}};
497 void PR22940_helper(const pair
<void*, int>&) { }
499 // CHECK-LABEL: @_ZN9B197730107PR22940Ev
500 // CHECK: call {{.*}} @_ZN9B197730104pairIPviEC{{.}}Ev(
501 // CHECK: call {{.*}} @_ZN9B1977301014PR22940_helperERKNS_4pairIPviEE(
502 PR22940_helper(pair
<void*, int>());