xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/atomicinit.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -emit-llvm -O1 -o - -triple=i686-apple-darwin9 -std=c++11 | FileCheck %s
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc // CHECK-DAG: @PR22043 = global i32 0, align 4
4*0a6a1f1dSLionel Sambuc typedef _Atomic(int) AtomicInt;
5*0a6a1f1dSLionel Sambuc AtomicInt PR22043 = AtomicInt();
6*0a6a1f1dSLionel Sambuc 
7*0a6a1f1dSLionel Sambuc // CHECK-DAG: @_ZN7PR180978constant1aE = global { i16, i8 } { i16 1, i8 6 }, align 4
8*0a6a1f1dSLionel Sambuc // CHECK-DAG: @_ZN7PR180978constant1bE = global { i16, i8 } { i16 2, i8 6 }, align 4
9*0a6a1f1dSLionel Sambuc // CHECK-DAG: @_ZN7PR180978constant1cE = global { i16, i8 } { i16 3, i8 6 }, align 4
10*0a6a1f1dSLionel Sambuc // CHECK-DAG: @_ZN7PR180978constant1yE = global { { i16, i8 }, i32 } { { i16, i8 } { i16 4, i8 6 }, i32 5 }, align 4
11*0a6a1f1dSLionel Sambuc 
12f4a2713aSLionel Sambuc struct A {
13f4a2713aSLionel Sambuc   _Atomic(int) i;
14f4a2713aSLionel Sambuc   A(int j);
15f4a2713aSLionel Sambuc   void v(int j);
16f4a2713aSLionel Sambuc };
17f4a2713aSLionel Sambuc // Storing to atomic values should be atomic
18f4a2713aSLionel Sambuc // CHECK: store atomic i32
v(int j)19f4a2713aSLionel Sambuc void A::v(int j) { i = j; }
20f4a2713aSLionel Sambuc // Initialising atomic values should not be atomic
21f4a2713aSLionel Sambuc // CHECK-NOT: store atomic
A(int j)22f4a2713aSLionel Sambuc A::A(int j) : i(j) {}
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc struct B {
25f4a2713aSLionel Sambuc   int i;
BB26f4a2713aSLionel Sambuc   B(int x) : i(x) {}
27f4a2713aSLionel Sambuc };
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc _Atomic(B) b;
30f4a2713aSLionel Sambuc 
31f4a2713aSLionel Sambuc // CHECK-LABEL: define void @_Z11atomic_initR1Ai
atomic_init(A & a,int i)32f4a2713aSLionel Sambuc void atomic_init(A& a, int i) {
33f4a2713aSLionel Sambuc   // CHECK-NOT: atomic
34f4a2713aSLionel Sambuc   // CHECK: tail call void @_ZN1BC1Ei
35f4a2713aSLionel Sambuc   __c11_atomic_init(&b, B(i));
36f4a2713aSLionel Sambuc   // CHECK-NEXT: ret void
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc // CHECK-LABEL: define void @_Z16atomic_init_boolPU7_Atomicbb
atomic_init_bool(_Atomic (bool)* ab,bool b)40f4a2713aSLionel Sambuc void atomic_init_bool(_Atomic(bool) *ab, bool b) {
41f4a2713aSLionel Sambuc   // CHECK-NOT: atomic
42f4a2713aSLionel Sambuc   // CHECK: {{zext i1.*to i8}}
43f4a2713aSLionel Sambuc   // CHECK-NEXT: store i8
44f4a2713aSLionel Sambuc   __c11_atomic_init(ab, b);
45f4a2713aSLionel Sambuc   // CHECK-NEXT: ret void
46f4a2713aSLionel Sambuc }
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc struct AtomicBoolMember {
49f4a2713aSLionel Sambuc   _Atomic(bool) ab;
50f4a2713aSLionel Sambuc   AtomicBoolMember(bool b);
51f4a2713aSLionel Sambuc };
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc // CHECK-LABEL: define void @_ZN16AtomicBoolMemberC2Eb
54f4a2713aSLionel Sambuc // CHECK: {{zext i1.*to i8}}
55f4a2713aSLionel Sambuc // CHECK-NEXT: store i8
56f4a2713aSLionel Sambuc // CHECK-NEXT: ret void
AtomicBoolMember(bool b)57f4a2713aSLionel Sambuc AtomicBoolMember::AtomicBoolMember(bool b) : ab(b) { }
58f4a2713aSLionel Sambuc 
59*0a6a1f1dSLionel Sambuc namespace PR18097 {
60*0a6a1f1dSLionel Sambuc   namespace dynamic {
61*0a6a1f1dSLionel Sambuc     struct X {
62*0a6a1f1dSLionel Sambuc       X(int);
63*0a6a1f1dSLionel Sambuc       short n;
64*0a6a1f1dSLionel Sambuc       char c;
65*0a6a1f1dSLionel Sambuc     };
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc     // CHECK-LABEL: define {{.*}} @__cxx_global_var_init
68*0a6a1f1dSLionel Sambuc     // CHECK: call void @_ZN7PR180977dynamic1XC1Ei({{.*}}* @_ZN7PR180977dynamic1aE, i32 1)
69*0a6a1f1dSLionel Sambuc     _Atomic(X) a = X(1);
70*0a6a1f1dSLionel Sambuc 
71*0a6a1f1dSLionel Sambuc     // CHECK-LABEL: define {{.*}} @__cxx_global_var_init
72*0a6a1f1dSLionel Sambuc     // CHECK: call void @_ZN7PR180977dynamic1XC1Ei({{.*}}* @_ZN7PR180977dynamic1bE, i32 2)
73*0a6a1f1dSLionel Sambuc     _Atomic(X) b(X(2));
74*0a6a1f1dSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc     // CHECK-LABEL: define {{.*}} @__cxx_global_var_init
76*0a6a1f1dSLionel Sambuc     // CHECK: call void @_ZN7PR180977dynamic1XC1Ei({{.*}}* @_ZN7PR180977dynamic1cE, i32 3)
_Atomic(X)77*0a6a1f1dSLionel Sambuc     _Atomic(X) c{X(3)};
78*0a6a1f1dSLionel Sambuc 
79*0a6a1f1dSLionel Sambuc     struct Y {
80*0a6a1f1dSLionel Sambuc       _Atomic(X) a;
81*0a6a1f1dSLionel Sambuc       _Atomic(int) b;
82*0a6a1f1dSLionel Sambuc     };
83*0a6a1f1dSLionel Sambuc     // CHECK-LABEL: define {{.*}} @__cxx_global_var_init
84*0a6a1f1dSLionel Sambuc     // CHECK: call void @_ZN7PR180977dynamic1XC1Ei({{.*}}* getelementptr inbounds ({{.*}}* @_ZN7PR180977dynamic1yE, i32 0, i32 0), i32 4)
85*0a6a1f1dSLionel Sambuc     // CHECK: store i32 5, i32* getelementptr inbounds ({{.*}}* @_ZN7PR180977dynamic1yE, i32 0, i32 1)
86*0a6a1f1dSLionel Sambuc     Y y = { X(4), 5 };
87*0a6a1f1dSLionel Sambuc   }
88*0a6a1f1dSLionel Sambuc 
89*0a6a1f1dSLionel Sambuc   // CHECKs at top of file.
90*0a6a1f1dSLionel Sambuc   namespace constant {
91*0a6a1f1dSLionel Sambuc     struct X {
XPR18097::constant::X92*0a6a1f1dSLionel Sambuc       constexpr X(int n) : n(n) {}
93*0a6a1f1dSLionel Sambuc       short n;
94*0a6a1f1dSLionel Sambuc       char c = 6;
95*0a6a1f1dSLionel Sambuc     };
96*0a6a1f1dSLionel Sambuc     _Atomic(X) a = X(1);
97*0a6a1f1dSLionel Sambuc     _Atomic(X) b(X(2));
_Atomic(X)98*0a6a1f1dSLionel Sambuc     _Atomic(X) c{X(3)};
99*0a6a1f1dSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc     struct Y {
101*0a6a1f1dSLionel Sambuc       _Atomic(X) a;
102*0a6a1f1dSLionel Sambuc       _Atomic(int) b;
103*0a6a1f1dSLionel Sambuc     };
104*0a6a1f1dSLionel Sambuc     Y y = { X(4), 5 };
105*0a6a1f1dSLionel Sambuc   }
106*0a6a1f1dSLionel Sambuc }
107