Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / static-member-variable-explicit-specialization.cpp
blob46c4c4d39176982aa086281d71031b91298e2a06
1 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -o - | FileCheck --check-prefix=ELF --check-prefix=ALL %s
2 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck --check-prefix=MACHO --check-prefix=ALL %s
3 // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -fdeclspec -DSELECTANY -o - | FileCheck --check-prefix=ELF-SELECTANY %s
5 #ifdef SELECTANY
6 struct S {
7 S();
8 ~S();
9 };
11 int f();
13 // ELF-SELECTANY: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @selectany }]
14 // ELF-SELECTANY: @llvm.used = appending global [1 x ptr] [ptr @selectany]
15 int __declspec(selectany) selectany = f();
17 #else
19 // ALL: ; ModuleID
21 extern "C" int foo();
23 template<typename T> struct A { static int a; };
24 template<typename T> int A<T>::a = foo();
26 // ALLK-NOT: @_ZN1AIcE1aE
27 template<> int A<char>::a;
29 // ALL: @_ZN1AIbE1aE ={{.*}} global i32 10
30 template<> int A<bool>::a = 10;
32 // ALL: @llvm.global_ctors = appending global [16 x { i32, ptr, ptr }]
34 // ELF: [{ i32, ptr, ptr } { i32 65535, ptr @[[unordered:[^,]*]], ptr @_ZN1AIsE1aE },
35 // MACHO: [{ i32, ptr, ptr } { i32 65535, ptr @[[unordered:[^,]*]], ptr null },
37 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered7:[^,]*]], ptr @_Z1xIsE },
38 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered7:[^,]*]], ptr null },
40 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered10:[^,]*]], ptr @_ZN2ns1aIiE1iE },
41 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered10:[^,]*]], ptr null },
43 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered11:[^,]*]], ptr @_ZN2ns1b1iIiEE },
44 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered11:[^,]*]], ptr null },
46 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered14:[^,]*]], ptr @_ZN1AIvE1aE },
47 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered14:[^,]*]], ptr null },
49 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered15:[^,]*]], ptr @_Z1xIcE },
50 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered15:[^,]*]], ptr null },
52 // ALL: { i32, ptr, ptr } { i32 65535, ptr @[[unordered16:[^,]*]], ptr null },
54 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered19:[^,]*]], ptr @_ZN3FibILi2EE1aE },
55 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered19:[^,]*]], ptr null },
57 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered18:[^,]*]], ptr @_ZN3FibILi3EE1aE },
58 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered18:[^,]*]], ptr null },
60 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered20:[^,]*]], ptr @_ZN3FibILi4EE1aE },
61 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered20:[^,]*]], ptr null },
63 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered17:[^,]*]], ptr @_ZN3FibILi5EE1aE },
64 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered17:[^,]*]], ptr null },
66 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered23:[^,]*]], ptr @_ZN4Fib2ILi2EE1aE },
67 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered23:[^,]*]], ptr null },
69 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered24:[^,]*]], ptr @_ZN4Fib2ILi3EE1aE },
70 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered24:[^,]*]], ptr null },
72 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered22:[^,]*]], ptr @_ZN4Fib2ILi4EE1aE },
73 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered22:[^,]*]], ptr null },
75 // ELF: { i32, ptr, ptr } { i32 65535, ptr @[[unordered21:[^,]*]], ptr @_ZN4Fib2ILi5EE1aE },
76 // MACHO: { i32, ptr, ptr } { i32 65535, ptr @[[unordered21:[^,]*]], ptr null },
78 // ALL: { i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_static_member_variable_explicit_specialization.cpp, ptr null }]
80 /// llvm.used ensures SHT_INIT_ARRAY in a section group cannot be GCed.
81 // ELF: @llvm.used = appending global [14 x ptr] [ptr @_ZN1AIsE1aE, ptr @_Z1xIsE, ptr @_ZN2ns1aIiE1iE, ptr @_ZN2ns1b1iIiEE, ptr @_ZN1AIvE1aE, ptr @_Z1xIcE, ptr @_ZN3FibILi5EE1aE, ptr @_ZN3FibILi3EE1aE, ptr @_ZN3FibILi2EE1aE, ptr @_ZN3FibILi4EE1aE, ptr @_ZN4Fib2ILi5EE1aE, ptr @_ZN4Fib2ILi4EE1aE, ptr @_ZN4Fib2ILi2EE1aE, ptr @_ZN4Fib2ILi3EE1aE]
83 template int A<short>::a; // Unordered
84 int b = foo();
85 int c = foo();
86 int d = A<void>::a; // Unordered
88 // An explicit specialization is ordered, and goes in __GLOBAL_sub_I_static_member_variable_explicit_specialization.cpp.
89 template<> struct A<int> { static int a; };
90 int A<int>::a = foo();
92 template<typename T> struct S { static T x; static T y; };
93 template<> int S<int>::x = foo();
94 template<> int S<int>::y = S<int>::x;
96 template<typename T> T x = foo();
97 template short x<short>; // Unordered
98 template<> int x<int> = foo();
99 int e = x<char>; // Unordered
101 namespace ns {
102 template <typename T> struct a {
103 static int i;
105 template<typename T> int a<T>::i = foo();
106 template struct a<int>;
108 struct b {
109 template <typename T> static T i;
111 template<typename T> T b::i = foo();
112 template int b::i<int>;
115 namespace {
116 template<typename T> struct Internal { static int a; };
117 template<typename T> int Internal<T>::a = foo();
119 int *use_internal_a = &Internal<int>::a;
121 template<int n> struct Fib { static int a; };
122 template<> int Fib<0>::a = 0;
123 template<> int Fib<1>::a = 1;
124 template<int n> int Fib<n>::a = Fib<n-2>::a + Fib<n-1>::a;
125 int f = Fib<5>::a;
127 template<int n> struct Fib2 { static int a; };
128 template<> int Fib2<0>::a = 0;
129 template<> int Fib2<1>::a = 1;
130 template<int n> int Fib2<n>::a = Fib2<n-1>::a + Fib2<n-2>::a;
131 int f2 = Fib2<5>::a;
133 #endif
135 // ALL: define internal void @[[unordered]](
136 // ALL: call i32 @foo()
137 // ALL: store {{.*}} @_ZN1AIsE1aE
138 // ALL: ret
140 // ALL: define internal void @[[unordered7]](
141 // ALL: call i32 @foo()
142 // ALL: store {{.*}} @_Z1xIsE
143 // ALL: ret
145 // ALL: define internal void @[[unordered10]](
146 // ALL: call i32 @foo()
147 // ALL: store {{.*}} @_ZN2ns1aIiE1iE
148 // ALL: ret
150 // ALL: define internal void @[[unordered11]](
151 // ALL: call i32 @foo()
152 // ALL: store {{.*}} @_ZN2ns1b1iIiEE
153 // ALL: ret
155 // ALL: define internal void @[[unordered15]](
156 // ALL: call i32 @foo()
157 // ALL: store {{.*}} @_Z1xIcE
158 // ALL: ret
160 // ALL: define internal void @[[unordered16]](
161 // ALL: call i32 @foo()
162 // ALL: store {{.*}} @_ZN12_GLOBAL__N_18InternalIiE1aE
163 // ALL: ret
165 // ALL: define internal void @[[unordered17]](
166 // ALL: store {{.*}} @_ZN3FibILi5EE1aE
167 // ALL: ret
169 // ALL: define internal void @[[unordered18]](
170 // ALL: store {{.*}} @_ZN3FibILi3EE1aE
171 // ALL: ret
173 // ALL: define internal void @[[unordered19]](
174 // ALL: store {{.*}} @_ZN3FibILi2EE1aE
175 // ALL: ret
177 // ALL: define internal void @[[unordered20]](
178 // ALL: store {{.*}} @_ZN3FibILi4EE1aE
179 // ALL: ret
181 // ALL: define internal void @[[unordered21]](
182 // ALL: store {{.*}} @_ZN4Fib2ILi5EE1aE
183 // ALL: ret
185 // ALL: define internal void @[[unordered22]](
186 // ALL: store {{.*}} @_ZN4Fib2ILi4EE1aE
187 // ALL: ret
189 // ALL: define internal void @[[unordered23]](
190 // ALL: store {{.*}} @_ZN4Fib2ILi2EE1aE
191 // ALL: ret
193 // ALL: define internal void @[[unordered24]](
194 // ALL: store {{.*}} @_ZN4Fib2ILi3EE1aE
195 // ALL: ret
197 // ALL: define internal void @_GLOBAL__sub_I_static_member_variable_explicit_specialization.cpp()
198 // We call unique stubs for every ordered dynamic initializer in the TU.
199 // ALL: call
200 // ALL: call
201 // ALL: call
202 // ALL: call
203 // ALL: call
204 // ALL: call
205 // ALL: call
206 // ALL: call
207 // ALL: call
208 // ALL: call
209 // ALL-NOT: call
210 // ALL: ret