xref: /llvm-project/clang/test/CodeGenCXX/atomicinit.cpp (revision 94473f4db6a6f5f12d7c4081455b5b596094eac5)
1 // RUN: %clang_cc1 -fno-inline-functions %s -emit-llvm -O1 -o - -triple=i686-apple-darwin9 -std=c++11 | FileCheck %s
2 // RUN: %clang_cc1 -fno-inline-functions %s -emit-llvm -O1 -o - -triple=i686-apple-darwin9 -std=c++11 -fexperimental-new-constant-interpreter | FileCheck %s
3 
4 // CHECK-DAG: @PR22043 ={{.*}} local_unnamed_addr global i32 0, align 4
5 typedef _Atomic(int) AtomicInt;
6 AtomicInt PR22043 = AtomicInt();
7 
8 // CHECK-DAG: @_ZN7PR180978constant1aE ={{.*}} local_unnamed_addr global { i16, i8 } { i16 1, i8 6 }, align 4
9 // CHECK-DAG: @_ZN7PR180978constant1bE ={{.*}} local_unnamed_addr global { i16, i8 } { i16 2, i8 6 }, align 4
10 // CHECK-DAG: @_ZN7PR180978constant1cE ={{.*}} local_unnamed_addr global { i16, i8 } { i16 3, i8 6 }, align 4
11 // CHECK-DAG: @_ZN7PR180978constant1yE ={{.*}} local_unnamed_addr global { { i16, i8 }, i32 } { { i16, i8 } { i16 4, i8 6 }, i32 5 }, align 4
12 // CHECK-DAG: @_ZN7PR180978constant1zE ={{.*}} global i32 0, align 4
13 // CHECK-DAG: @_ZN7PR180978constant2y2E ={{.*}} global %"struct.PR18097::constant::Y" zeroinitializer
14 // CHECK-DAG: @_ZN7PR180978constant1gE ={{.*}} global %"struct.PR18097::constant::Struct0" zeroinitializer
15 // CHECK-DAG: @_ZN7PR180978constantL1xE = internal {{.*}} constant { i16, i8 } { i16 1, i8 6 }
16 
17 struct A {
18   _Atomic(int) i;
19   A(int j);
20   void v(int j);
21 };
22 // Storing to atomic values should be atomic
23 // CHECK: store atomic i32 {{.*}} seq_cst, align 4
24 void A::v(int j) { i = j; }
25 // Initialising atomic values should not be atomic
26 // CHECK-NOT: store atomic
27 A::A(int j) : i(j) {}
28 
29 struct B {
30   int i;
31   B(int x) : i(x) {}
32 };
33 
34 _Atomic(B) b;
35 
36 // CHECK-LABEL: define{{.*}} void @_Z11atomic_initR1Ai
37 void atomic_init(A& a, int i) {
38   // CHECK-NOT: atomic
39   // CHECK: call void @_ZN1BC1Ei
40   __c11_atomic_init(&b, B(i));
41   // CHECK-NEXT: ret void
42 }
43 
44 // CHECK-LABEL: define{{.*}} void @_Z16atomic_init_boolPU7_Atomicbb
45 void atomic_init_bool(_Atomic(bool) *ab, bool b) {
46   // CHECK-NOT: atomic
47   // CHECK: {{zext i1.*to i8}}
48   // CHECK-NEXT: store i8
49   __c11_atomic_init(ab, b);
50   // CHECK-NEXT: ret void
51 }
52 
53 struct AtomicBoolMember {
54   _Atomic(bool) ab;
55   AtomicBoolMember(bool b);
56 };
57 
58 // CHECK-LABEL: define{{.*}} void @_ZN16AtomicBoolMemberC2Eb
59 // CHECK: zext i1 {{.*}} to i8
60 // CHECK: store i8
61 // CHECK-NEXT: ret void
62 AtomicBoolMember::AtomicBoolMember(bool b) : ab(b) { }
63 
64 namespace PR18097 {
65   namespace dynamic {
66     struct X {
67       X(int);
68       short n;
69       char c;
70     };
71 
72     // CHECK-LABEL: define {{.*}} @__cxx_global_var_init
73     // CHECK: call void @_ZN7PR180977dynamic1XC1Ei(ptr noundef {{[^,]*}} @_ZN7PR180977dynamic1aE, i32 noundef 1)
74     _Atomic(X) a = X(1);
75 
76     // CHECK-LABEL: define {{.*}} @__cxx_global_var_init
77     // CHECK: call void @_ZN7PR180977dynamic1XC1Ei(ptr noundef {{[^,]*}} @_ZN7PR180977dynamic1bE, i32 noundef 2)
78     _Atomic(X) b(X(2));
79 
80     // CHECK-LABEL: define {{.*}} @__cxx_global_var_init
81     // CHECK: call void @_ZN7PR180977dynamic1XC1Ei(ptr noundef {{[^,]*}} @_ZN7PR180977dynamic1cE, i32 noundef 3)
82     _Atomic(X) c{X(3)};
83 
84     struct Y {
85       _Atomic(X) a;
86       _Atomic(int) b;
87     };
88     // CHECK-LABEL: define {{.*}} @__cxx_global_var_init
89     // CHECK: call void @_ZN7PR180977dynamic1XC1Ei(ptr {{[^,]*}} @_ZN7PR180977dynamic1yE, i32 noundef 4)
90     // CHECK: store i32 5, ptr getelementptr inbounds nuw (i8, ptr @_ZN7PR180977dynamic1yE, i32 4)
91     Y y = { X(4), 5 };
92   }
93 
94   // CHECKs at top of file.
95   namespace constant {
96     struct X {
97       constexpr X(int n) : n(n) {}
98       short n;
99       char c = 6;
100     };
101     _Atomic(X) a = X(1);
102     _Atomic(X) b(X(2));
103     _Atomic(X) c{X(3)};
104 
105     struct Y {
106       _Atomic(X) a;
107       _Atomic(int) b;
108     };
109     Y y = { X(4), 5 };
110 
111     // CHECK-LABEL: define {{.*}} @__cxx_global_var_init
112     // CHECK: tail call void @llvm.memcpy.p0.p0.i32(ptr{{.*}} @_ZN7PR180978constant2y2E, ptr{{.*}} @_ZN7PR180978constantL1xE, i32 3, i1 false)
113     // CHECK: %0 = load i32, ptr @_ZN7PR180978constant1zE
114     // CHECK: store i32 %0, ptr getelementptr inbounds nuw (i8, ptr @_ZN7PR180978constant2y2E, i32 4)
115     int z;
116     constexpr X x{1};
117     Y y2 = { x, z };
118 
119     typedef union {
120       unsigned int f0;
121     } Union0;
122 
123     typedef struct {
124       _Atomic(Union0) f1;
125     } Struct0;
126 
127     Struct0 g = {};
128   }
129 }
130