xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/cxx11-thread-local.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc int f();
4f4a2713aSLionel Sambuc int g();
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc // CHECK: @a = thread_local global i32 0
7f4a2713aSLionel Sambuc thread_local int a = f();
8f4a2713aSLionel Sambuc extern thread_local int b;
9f4a2713aSLionel Sambuc // CHECK: @c = global i32 0
10f4a2713aSLionel Sambuc int c = b;
11f4a2713aSLionel Sambuc // CHECK: @_ZL1d = internal thread_local global i32 0
12f4a2713aSLionel Sambuc static thread_local int d = g();
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc struct U { static thread_local int m; };
15f4a2713aSLionel Sambuc // CHECK: @_ZN1U1mE = thread_local global i32 0
16f4a2713aSLionel Sambuc thread_local int U::m = f();
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc template<typename T> struct V { static thread_local int m; };
19f4a2713aSLionel Sambuc template<typename T> thread_local int V<T>::m = g();
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc // CHECK: @e = global i32 0
22f4a2713aSLionel Sambuc int e = V<int>::m;
23f4a2713aSLionel Sambuc 
24*0a6a1f1dSLionel Sambuc // CHECK: @_ZN1VIiE1mE = linkonce_odr thread_local global i32 0
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc // CHECK: @_ZZ1fvE1n = internal thread_local global i32 0
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc // CHECK: @_ZGVZ1fvE1n = internal thread_local global i8 0
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc // CHECK: @_ZZ8tls_dtorvE1s = internal thread_local global
31f4a2713aSLionel Sambuc // CHECK: @_ZGVZ8tls_dtorvE1s = internal thread_local global i8 0
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc // CHECK: @_ZZ8tls_dtorvE1t = internal thread_local global
34f4a2713aSLionel Sambuc // CHECK: @_ZGVZ8tls_dtorvE1t = internal thread_local global i8 0
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc // CHECK: @_ZZ8tls_dtorvE1u = internal thread_local global
37f4a2713aSLionel Sambuc // CHECK: @_ZGVZ8tls_dtorvE1u = internal thread_local global i8 0
38*0a6a1f1dSLionel Sambuc // CHECK: @_ZGRZ8tls_dtorvE1u_ = internal thread_local global
39f4a2713aSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc // CHECK: @_ZGVN1VIiE1mE = linkonce_odr thread_local global i64 0
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc // CHECK: @__tls_guard = internal thread_local global i8 0
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc // CHECK: @llvm.global_ctors = appending global {{.*}} @[[GLOBAL_INIT:[^ ]*]]
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc // CHECK: @_ZTH1a = alias void ()* @__tls_init
47*0a6a1f1dSLionel Sambuc // CHECK: @_ZTHL1d = internal alias void ()* @__tls_init
48f4a2713aSLionel Sambuc // CHECK: @_ZTHN1U1mE = alias void ()* @__tls_init
49*0a6a1f1dSLionel Sambuc // CHECK: @_ZTHN1VIiE1mE = linkonce_odr alias void ()* @__tls_init
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc // Individual variable initialization functions:
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc // CHECK: define {{.*}} @[[A_INIT:.*]]()
55f4a2713aSLionel Sambuc // CHECK: call i32 @_Z1fv()
56f4a2713aSLionel Sambuc // CHECK-NEXT: store i32 {{.*}}, i32* @a, align 4
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc // CHECK-LABEL: define i32 @_Z1fv()
f()59f4a2713aSLionel Sambuc int f() {
60f4a2713aSLionel Sambuc   // CHECK: %[[GUARD:.*]] = load i8* @_ZGVZ1fvE1n, align 1
61f4a2713aSLionel Sambuc   // CHECK: %[[NEED_INIT:.*]] = icmp eq i8 %[[GUARD]], 0
62f4a2713aSLionel Sambuc   // CHECK: br i1 %[[NEED_INIT]]
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc   // CHECK: %[[CALL:.*]] = call i32 @_Z1gv()
65f4a2713aSLionel Sambuc   // CHECK: store i32 %[[CALL]], i32* @_ZZ1fvE1n, align 4
66f4a2713aSLionel Sambuc   // CHECK: store i8 1, i8* @_ZGVZ1fvE1n
67f4a2713aSLionel Sambuc   // CHECK: br label
68f4a2713aSLionel Sambuc   static thread_local int n = g();
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc   // CHECK: load i32* @_ZZ1fvE1n, align 4
71f4a2713aSLionel Sambuc   return n;
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc // CHECK: define {{.*}} @[[C_INIT:.*]]()
75f4a2713aSLionel Sambuc // CHECK: call i32* @_ZTW1b()
76f4a2713aSLionel Sambuc // CHECK-NEXT: load i32* %{{.*}}, align 4
77f4a2713aSLionel Sambuc // CHECK-NEXT: store i32 %{{.*}}, i32* @c, align 4
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc // CHECK-LABEL: define weak_odr hidden i32* @_ZTW1b()
80f4a2713aSLionel Sambuc // CHECK: br i1 icmp ne (void ()* @_ZTH1b, void ()* null),
81f4a2713aSLionel Sambuc // not null:
82f4a2713aSLionel Sambuc // CHECK: call void @_ZTH1b()
83f4a2713aSLionel Sambuc // CHECK: br label
84f4a2713aSLionel Sambuc // finally:
85f4a2713aSLionel Sambuc // CHECK: ret i32* @b
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc // CHECK: define {{.*}} @[[D_INIT:.*]]()
88f4a2713aSLionel Sambuc // CHECK: call i32 @_Z1gv()
89f4a2713aSLionel Sambuc // CHECK-NEXT: store i32 %{{.*}}, i32* @_ZL1d, align 4
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc // CHECK: define {{.*}} @[[U_M_INIT:.*]]()
92f4a2713aSLionel Sambuc // CHECK: call i32 @_Z1fv()
93f4a2713aSLionel Sambuc // CHECK-NEXT: store i32 %{{.*}}, i32* @_ZN1U1mE, align 4
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc // CHECK: define {{.*}} @[[E_INIT:.*]]()
96f4a2713aSLionel Sambuc // CHECK: call i32* @_ZTWN1VIiE1mE()
97f4a2713aSLionel Sambuc // CHECK-NEXT: load i32* %{{.*}}, align 4
98f4a2713aSLionel Sambuc // CHECK-NEXT: store i32 %{{.*}}, i32* @e, align 4
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc // CHECK-LABEL: define weak_odr hidden i32* @_ZTWN1VIiE1mE()
101f4a2713aSLionel Sambuc // CHECK: call void @_ZTHN1VIiE1mE()
102f4a2713aSLionel Sambuc // CHECK: ret i32* @_ZN1VIiE1mE
103f4a2713aSLionel Sambuc 
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc struct S { S(); ~S(); };
106f4a2713aSLionel Sambuc struct T { ~T(); };
107f4a2713aSLionel Sambuc 
108f4a2713aSLionel Sambuc // CHECK-LABEL: define void @_Z8tls_dtorv()
tls_dtor()109f4a2713aSLionel Sambuc void tls_dtor() {
110f4a2713aSLionel Sambuc   // CHECK: load i8* @_ZGVZ8tls_dtorvE1s
111f4a2713aSLionel Sambuc   // CHECK: call void @_ZN1SC1Ev(%struct.S* @_ZZ8tls_dtorvE1s)
112f4a2713aSLionel Sambuc   // CHECK: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZZ8tls_dtorvE1s{{.*}} @__dso_handle
113f4a2713aSLionel Sambuc   // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1s
114f4a2713aSLionel Sambuc   static thread_local S s;
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc   // CHECK: load i8* @_ZGVZ8tls_dtorvE1t
117f4a2713aSLionel Sambuc   // CHECK-NOT: _ZN1T
118f4a2713aSLionel Sambuc   // CHECK: call i32 @__cxa_thread_atexit({{.*}}@_ZN1TD1Ev {{.*}}@_ZZ8tls_dtorvE1t{{.*}} @__dso_handle
119f4a2713aSLionel Sambuc   // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1t
120f4a2713aSLionel Sambuc   static thread_local T t;
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc   // CHECK: load i8* @_ZGVZ8tls_dtorvE1u
123*0a6a1f1dSLionel Sambuc   // CHECK: call void @_ZN1SC1Ev(%struct.S* @_ZGRZ8tls_dtorvE1u_)
124*0a6a1f1dSLionel Sambuc   // CHECK: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZGRZ8tls_dtorvE1u_{{.*}} @__dso_handle
125f4a2713aSLionel Sambuc   // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1u
126f4a2713aSLionel Sambuc   static thread_local const S &u = S();
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc // CHECK: declare i32 @__cxa_thread_atexit(void (i8*)*, i8*, i8*)
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc // CHECK: define {{.*}} @_Z7PR15991v(
PR15991()132f4a2713aSLionel Sambuc int PR15991() {
133f4a2713aSLionel Sambuc   thread_local int n;
134f4a2713aSLionel Sambuc   auto l = [] { return n; };
135f4a2713aSLionel Sambuc   return l();
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc struct PR19254 {
139*0a6a1f1dSLionel Sambuc   static thread_local int n;
140*0a6a1f1dSLionel Sambuc   int f();
141*0a6a1f1dSLionel Sambuc };
142*0a6a1f1dSLionel Sambuc // CHECK: define {{.*}} @_ZN7PR192541fEv(
f()143*0a6a1f1dSLionel Sambuc int PR19254::f() {
144*0a6a1f1dSLionel Sambuc   // CHECK: call void @_ZTHN7PR192541nE(
145*0a6a1f1dSLionel Sambuc   return this->n;
146*0a6a1f1dSLionel Sambuc }
147*0a6a1f1dSLionel Sambuc 
148*0a6a1f1dSLionel Sambuc namespace {
149*0a6a1f1dSLionel Sambuc thread_local int anon_i{1};
150*0a6a1f1dSLionel Sambuc }
set_anon_i()151*0a6a1f1dSLionel Sambuc void set_anon_i() {
152*0a6a1f1dSLionel Sambuc   anon_i = 2;
153*0a6a1f1dSLionel Sambuc }
154*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define internal i32* @_ZTWN12_GLOBAL__N_16anon_iE()
155*0a6a1f1dSLionel Sambuc 
156f4a2713aSLionel Sambuc // CHECK: define {{.*}} @[[V_M_INIT:.*]]()
157f4a2713aSLionel Sambuc // CHECK: load i8* bitcast (i64* @_ZGVN1VIiE1mE to i8*)
158f4a2713aSLionel Sambuc // CHECK: %[[V_M_INITIALIZED:.*]] = icmp eq i8 %{{.*}}, 0
159f4a2713aSLionel Sambuc // CHECK: br i1 %[[V_M_INITIALIZED]],
160f4a2713aSLionel Sambuc // need init:
161f4a2713aSLionel Sambuc // CHECK: call i32 @_Z1gv()
162f4a2713aSLionel Sambuc // CHECK: store i32 %{{.*}}, i32* @_ZN1VIiE1mE, align 4
163f4a2713aSLionel Sambuc // CHECK: store i64 1, i64* @_ZGVN1VIiE1mE
164f4a2713aSLionel Sambuc // CHECK: br label
165f4a2713aSLionel Sambuc 
166f4a2713aSLionel Sambuc // CHECK: define {{.*}}@[[GLOBAL_INIT:.*]]()
167f4a2713aSLionel Sambuc // CHECK: call void @[[C_INIT]]()
168f4a2713aSLionel Sambuc // CHECK: call void @[[E_INIT]]()
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc 
171f4a2713aSLionel Sambuc // CHECK: define {{.*}}@__tls_init()
172f4a2713aSLionel Sambuc // CHECK: load i8* @__tls_guard
173f4a2713aSLionel Sambuc // CHECK: %[[NEED_TLS_INIT:.*]] = icmp eq i8 %{{.*}}, 0
174f4a2713aSLionel Sambuc // CHECK: store i8 1, i8* @__tls_guard
175f4a2713aSLionel Sambuc // CHECK: br i1 %[[NEED_TLS_INIT]],
176f4a2713aSLionel Sambuc // init:
177f4a2713aSLionel Sambuc // CHECK: call void @[[A_INIT]]()
178f4a2713aSLionel Sambuc // CHECK: call void @[[D_INIT]]()
179f4a2713aSLionel Sambuc // CHECK: call void @[[U_M_INIT]]()
180f4a2713aSLionel Sambuc // CHECK: call void @[[V_M_INIT]]()
181f4a2713aSLionel Sambuc 
182f4a2713aSLionel Sambuc 
183f4a2713aSLionel Sambuc // CHECK: define weak_odr hidden i32* @_ZTW1a() {
184f4a2713aSLionel Sambuc // CHECK:   call void @_ZTH1a()
185f4a2713aSLionel Sambuc // CHECK:   ret i32* @a
186f4a2713aSLionel Sambuc // CHECK: }
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc 
189f4a2713aSLionel Sambuc // CHECK: declare extern_weak void @_ZTH1b()
190f4a2713aSLionel Sambuc 
191f4a2713aSLionel Sambuc 
192*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define internal i32* @_ZTWL1d()
193f4a2713aSLionel Sambuc // CHECK: call void @_ZTHL1d()
194f4a2713aSLionel Sambuc // CHECK: ret i32* @_ZL1d
195f4a2713aSLionel Sambuc 
196f4a2713aSLionel Sambuc // CHECK-LABEL: define weak_odr hidden i32* @_ZTWN1U1mE()
197f4a2713aSLionel Sambuc // CHECK: call void @_ZTHN1U1mE()
198f4a2713aSLionel Sambuc // CHECK: ret i32* @_ZN1U1mE
199