xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/static-data-member.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -o - %s | FileCheck %s
2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | \
3*0a6a1f1dSLionel Sambuc // RUN: FileCheck --check-prefix=MACHO %s
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc // CHECK: @_ZN5test11A1aE = constant i32 10, align 4
6f4a2713aSLionel Sambuc // CHECK: @_ZN5test212_GLOBAL__N_11AIiE1xE = internal global i32 0, align 4
7*0a6a1f1dSLionel Sambuc // CHECK: @_ZN5test31AIiE1xE = weak_odr global i32 0, comdat, align 4
8*0a6a1f1dSLionel Sambuc // CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0, comdat($_ZN5test31AIiE1xE)
9*0a6a1f1dSLionel Sambuc // MACHO: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
10*0a6a1f1dSLionel Sambuc // MACHO-NOT: comdat
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc // CHECK: _ZN5test51U2k0E = global i32 0
13f4a2713aSLionel Sambuc // CHECK: _ZN5test51U2k1E = global i32 0
14f4a2713aSLionel Sambuc // CHECK: _ZN5test51U2k2E = constant i32 76
15f4a2713aSLionel Sambuc // CHECK-NOT: test51U2k3E
16f4a2713aSLionel Sambuc // CHECK-NOT: test51U2k4E
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc // PR5564.
19f4a2713aSLionel Sambuc namespace test1 {
20f4a2713aSLionel Sambuc   struct A {
21f4a2713aSLionel Sambuc     static const int a = 10;
22f4a2713aSLionel Sambuc   };
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc   const int A::a;
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc   struct S {
27f4a2713aSLionel Sambuc     static int i;
28f4a2713aSLionel Sambuc   };
29f4a2713aSLionel Sambuc 
f()30f4a2713aSLionel Sambuc   void f() {
31f4a2713aSLionel Sambuc     int a = S::i;
32f4a2713aSLionel Sambuc   }
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc // Test that we don't use guards for initializing template static data
36f4a2713aSLionel Sambuc // members with internal linkage.
37f4a2713aSLionel Sambuc namespace test2 {
38f4a2713aSLionel Sambuc   int foo();
39f4a2713aSLionel Sambuc 
40f4a2713aSLionel Sambuc   namespace {
41f4a2713aSLionel Sambuc     template <class T> struct A {
42f4a2713aSLionel Sambuc       static int x;
43f4a2713aSLionel Sambuc     };
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc     template <class T> int A<T>::x = foo();
46f4a2713aSLionel Sambuc     template struct A<int>;
47f4a2713aSLionel Sambuc   }
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc   // CHECK-LABEL: define internal void @__cxx_global_var_init()
50f4a2713aSLionel Sambuc   // CHECK:      [[TMP:%.*]] = call i32 @_ZN5test23fooEv()
51f4a2713aSLionel Sambuc   // CHECK-NEXT: store i32 [[TMP]], i32* @_ZN5test212_GLOBAL__N_11AIiE1xE, align 4
52f4a2713aSLionel Sambuc   // CHECK-NEXT: ret void
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc // Test that we don't use threadsafe statics when initializing
56f4a2713aSLionel Sambuc // template static data members.
57f4a2713aSLionel Sambuc namespace test3 {
58f4a2713aSLionel Sambuc   int foo();
59f4a2713aSLionel Sambuc 
60f4a2713aSLionel Sambuc   template <class T> struct A {
61f4a2713aSLionel Sambuc     static int x;
62f4a2713aSLionel Sambuc   };
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc   template <class T> int A<T>::x = foo();
65f4a2713aSLionel Sambuc   template struct A<int>;
66f4a2713aSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define internal void @__cxx_global_var_init1() {{.*}} comdat($_ZN5test31AIiE1xE)
68*0a6a1f1dSLionel Sambuc   // MACHO-LABEL: define internal void @__cxx_global_var_init1()
69*0a6a1f1dSLionel Sambuc   // MACHO-NOT: comdat
70f4a2713aSLionel Sambuc   // CHECK:      [[GUARDBYTE:%.*]] = load i8* bitcast (i64* @_ZGVN5test31AIiE1xE to i8*)
71f4a2713aSLionel Sambuc   // CHECK-NEXT: [[UNINITIALIZED:%.*]] = icmp eq i8 [[GUARDBYTE]], 0
72f4a2713aSLionel Sambuc   // CHECK-NEXT: br i1 [[UNINITIALIZED]]
73f4a2713aSLionel Sambuc   // CHECK:      [[TMP:%.*]] = call i32 @_ZN5test33fooEv()
74f4a2713aSLionel Sambuc   // CHECK-NEXT: store i32 [[TMP]], i32* @_ZN5test31AIiE1xE, align 4
75f4a2713aSLionel Sambuc   // CHECK-NEXT: store i64 1, i64* @_ZGVN5test31AIiE1xE
76f4a2713aSLionel Sambuc   // CHECK-NEXT: br label
77f4a2713aSLionel Sambuc   // CHECK:      ret void
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc // Test that we can fold member lookup expressions which resolve to static data
81f4a2713aSLionel Sambuc // members.
82f4a2713aSLionel Sambuc namespace test4 {
83f4a2713aSLionel Sambuc   struct A {
84f4a2713aSLionel Sambuc     static const int n = 76;
85f4a2713aSLionel Sambuc   };
86f4a2713aSLionel Sambuc 
f(A * a)87f4a2713aSLionel Sambuc   int f(A *a) {
88f4a2713aSLionel Sambuc     // CHECK-LABEL: define i32 @_ZN5test41fEPNS_1AE
89f4a2713aSLionel Sambuc     // CHECK: ret i32 76
90f4a2713aSLionel Sambuc     return a->n;
91f4a2713aSLionel Sambuc   }
92f4a2713aSLionel Sambuc }
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc // Test that static data members in unions behave properly.
95f4a2713aSLionel Sambuc namespace test5 {
96f4a2713aSLionel Sambuc   union U {
97f4a2713aSLionel Sambuc     static int k0;
98f4a2713aSLionel Sambuc     static const int k1;
99f4a2713aSLionel Sambuc     static const int k2 = 76;
100f4a2713aSLionel Sambuc     static const int k3;
101f4a2713aSLionel Sambuc     static const int k4 = 81;
102f4a2713aSLionel Sambuc   };
103f4a2713aSLionel Sambuc   int U::k0;
104f4a2713aSLionel Sambuc   const int U::k1 = (k0 = 9, 42);
105f4a2713aSLionel Sambuc   const int U::k2;
106f4a2713aSLionel Sambuc 
107f4a2713aSLionel Sambuc   // CHECK: store i32 9, i32* @_ZN5test51U2k0E
108f4a2713aSLionel Sambuc   // CHECK: store i32 {{.*}}, i32* @_ZN5test51U2k1E
109f4a2713aSLionel Sambuc   // CHECK-NOT: store {{.*}} i32* @_ZN5test51U2k2E
110f4a2713aSLionel Sambuc }
111