Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / static-data-member.cpp
blob7914105e31e0e6ef4466afaef8ed2c3ec135756d
1 // RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -o - %s | FileCheck %s
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | \
3 // RUN: FileCheck --check-prefix=MACHO %s
5 // CHECK: @_ZN5test11A1aE ={{.*}} constant i32 10, align 4
6 // CHECK: @_ZN5test212_GLOBAL__N_11AIiE1xE = internal global i32 0, align 4
7 // CHECK: @_ZN5test31AIiE1xE = weak_odr global i32 0, comdat, align 4
8 // CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0, comdat($_ZN5test31AIiE1xE)
9 // MACHO: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
10 // MACHO-NOT: comdat
12 // CHECK: _ZN5test51U2k0E ={{.*}} global i32 0
13 // CHECK: _ZN5test51U2k1E ={{.*}} global i32 0
14 // CHECK: _ZN5test51U2k2E ={{.*}} constant i32 76
15 // CHECK-NOT: test51U2k3E
16 // CHECK-NOT: test51U2k4E
18 // PR5564.
19 namespace test1 {
20 struct A {
21 static const int a = 10;
24 const int A::a;
26 struct S {
27 static int i;
30 void f() {
31 int a = S::i;
35 // Test that we don't use guards for initializing template static data
36 // members with internal linkage.
37 namespace test2 {
38 int foo();
40 namespace {
41 template <class T> struct A {
42 static int x;
45 template <class T> int A<T>::x = foo();
46 template struct A<int>;
49 // CHECK-LABEL: define internal void @__cxx_global_var_init()
50 // CHECK: [[TMP:%.*]] = call noundef i32 @_ZN5test23fooEv()
51 // CHECK-NEXT: store i32 [[TMP]], ptr @_ZN5test212_GLOBAL__N_11AIiE1xE, align 4
52 // CHECK-NEXT: ret void
55 // Test that we don't use threadsafe statics when initializing
56 // template static data members.
57 namespace test3 {
58 int foo();
60 template <class T> struct A {
61 static int x;
64 template <class T> int A<T>::x = foo();
65 template struct A<int>;
67 // CHECK-LABEL: define internal void @__cxx_global_var_init.1() {{.*}} comdat($_ZN5test31AIiE1xE)
68 // MACHO-LABEL: define internal void @__cxx_global_var_init.1()
69 // MACHO-NOT: comdat
70 // CHECK: [[GUARDBYTE:%.*]] = load i8, ptr @_ZGVN5test31AIiE1xE
71 // CHECK-NEXT: [[UNINITIALIZED:%.*]] = icmp eq i8 [[GUARDBYTE]], 0
72 // CHECK-NEXT: br i1 [[UNINITIALIZED]]
73 // CHECK: store i8 1, ptr @_ZGVN5test31AIiE1xE
74 // CHECK-NEXT: [[TMP:%.*]] = call noundef i32 @_ZN5test33fooEv()
75 // CHECK-NEXT: store i32 [[TMP]], ptr @_ZN5test31AIiE1xE, align 4
76 // CHECK-NEXT: br label
77 // CHECK: ret void
80 // Test that we can fold member lookup expressions which resolve to static data
81 // members.
82 namespace test4 {
83 struct A {
84 static const int n = 76;
87 int f(A *a) {
88 // CHECK-LABEL: define{{.*}} i32 @_ZN5test41fEPNS_1AE
89 // CHECK: ret i32 76
90 return a->n;
94 // Test that static data members in unions behave properly.
95 namespace test5 {
96 union U {
97 static int k0;
98 static const int k1;
99 static const int k2 = 76;
100 static const int k3;
101 static const int k4 = 81;
103 int U::k0;
104 const int U::k1 = (k0 = 9, 42);
105 const int U::k2;
107 // CHECK: store i32 9, ptr @_ZN5test51U2k0E
108 // CHECK: store i32 {{.*}}, ptr @_ZN5test51U2k1E
109 // CHECK-NOT: store {{.*}} ptr @_ZN5test51U2k2E