| 1 |
// RUN: %clang_cc1 -fms-extensions -fno-threadsafe-statics -emit-llvm %s -o - -mconstructor-aliases -triple=i386-pc-win32 | FileCheck %s |
| 2 |
|
| 3 |
// CHECK: @llvm.global_ctors = appending global [5 x { i32, ptr, ptr }] [ |
| 4 |
// CHECK: { i32, ptr, ptr } { i32 65535, ptr @"??__Eselectany1@@YAXXZ", ptr @"?selectany1@@3US@@A" }, |
| 5 |
// CHECK: { i32, ptr, ptr } { i32 65535, ptr @"??__Eselectany2@@YAXXZ", ptr @"?selectany2@@3US@@A" }, |
| 6 |
// CHECK: { i32, ptr, ptr } { i32 65535, ptr @"??__E?s@?$ExportedTemplate@H@@2US@@A@@YAXXZ", ptr @"?s@?$ExportedTemplate@H@@2US@@A" }, |
| 7 |
// CHECK: { i32, ptr, ptr } { i32 65535, ptr @"??__E?foo@?$B@H@@2VA@@A@@YAXXZ", ptr @"?foo@?$B@H@@2VA@@A" }, |
| 8 |
// CHECK: { i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_microsoft_abi_static_initializers.cpp, ptr null } |
| 9 |
// CHECK: ] |
| 10 |
|
| 11 |
struct S { |
| 12 |
S(); |
| 13 |
~S(); |
| 14 |
}; |
| 15 |
|
| 16 |
S s; |
| 17 |
|
| 18 |
// CHECK: define internal void @"??__Es@@YAXXZ"() |
| 19 |
// CHECK: call x86_thiscallcc noundef ptr @"??0S@@QAE@XZ" |
| 20 |
// CHECK: call i32 @atexit(ptr @"??__Fs@@YAXXZ") |
| 21 |
// CHECK: ret void |
| 22 |
|
| 23 |
// CHECK: define internal void @"??__Fs@@YAXXZ"() |
| 24 |
// CHECK: call x86_thiscallcc void @"??1S@@QAE@XZ" |
| 25 |
// CHECK: ret void |
| 26 |
|
| 27 |
// These globals should have initializers comdat associative with the global. |
| 28 |
// See @llvm.global_ctors above. |
| 29 |
__declspec(selectany) S selectany1; |
| 30 |
__declspec(selectany) S selectany2; |
| 31 |
// CHECK: define linkonce_odr dso_local void @"??__Eselectany1@@YAXXZ"() {{.*}} comdat |
| 32 |
// CHECK-NOT: @"??_Bselectany1 |
| 33 |
// CHECK: call x86_thiscallcc noundef ptr @"??0S@@QAE@XZ" |
| 34 |
// CHECK: ret void |
| 35 |
// CHECK: define linkonce_odr dso_local void @"??__Eselectany2@@YAXXZ"() {{.*}} comdat |
| 36 |
// CHECK-NOT: @"??_Bselectany2 |
| 37 |
// CHECK: call x86_thiscallcc noundef ptr @"??0S@@QAE@XZ" |
| 38 |
// CHECK: ret void |
| 39 |
|
| 40 |
// The implicitly instantiated static data member should have initializer |
| 41 |
// comdat associative with the global. |
| 42 |
template <typename T> struct __declspec(dllexport) ExportedTemplate { |
| 43 |
static S s; |
| 44 |
}; |
| 45 |
template <typename T> S ExportedTemplate<T>::s; |
| 46 |
void useExportedTemplate(ExportedTemplate<int> x) { |
| 47 |
(void)x.s; |
| 48 |
} |
| 49 |
|
| 50 |
void StaticLocal() { |
| 51 |
static S TheS; |
| 52 |
} |
| 53 |
|
| 54 |
// CHECK-LABEL: define dso_local void @"?StaticLocal@@YAXXZ"() |
| 55 |
// CHECK: load i32, ptr @"?$S1@?1??StaticLocal@@YAXXZ@4IA" |
| 56 |
// CHECK: store i32 {{.*}}, ptr @"?$S1@?1??StaticLocal@@YAXXZ@4IA" |
| 57 |
// CHECK: ret |
| 58 |
|
| 59 |
void MultipleStatics() { |
| 60 |
static S S1; |
| 61 |
static S S2; |
| 62 |
static S S3; |
| 63 |
static S S4; |
| 64 |
static S S5; |
| 65 |
static S S6; |
| 66 |
static S S7; |
| 67 |
static S S8; |
| 68 |
static S S9; |
| 69 |
static S S10; |
| 70 |
static S S11; |
| 71 |
static S S12; |
| 72 |
static S S13; |
| 73 |
static S S14; |
| 74 |
static S S15; |
| 75 |
static S S16; |
| 76 |
static S S17; |
| 77 |
static S S18; |
| 78 |
static S S19; |
| 79 |
static S S20; |
| 80 |
static S S21; |
| 81 |
static S S22; |
| 82 |
static S S23; |
| 83 |
static S S24; |
| 84 |
static S S25; |
| 85 |
static S S26; |
| 86 |
static S S27; |
| 87 |
static S S28; |
| 88 |
static S S29; |
| 89 |
static S S30; |
| 90 |
static S S31; |
| 91 |
static S S32; |
| 92 |
static S S33; |
| 93 |
static S S34; |
| 94 |
static S S35; |
| 95 |
} |
| 96 |
// CHECK-LABEL: define dso_local void @"?MultipleStatics@@YAXXZ"() |
| 97 |
// CHECK: load i32, ptr @"?$S1@?1??MultipleStatics@@YAXXZ@4IA" |
| 98 |
// CHECK: and i32 {{.*}}, 1 |
| 99 |
// CHECK: and i32 {{.*}}, 2 |
| 100 |
// CHECK: and i32 {{.*}}, 4 |
| 101 |
// CHECK: and i32 {{.*}}, 8 |
| 102 |
// CHECK: and i32 {{.*}}, 16 |
| 103 |
// ... |
| 104 |
// CHECK: and i32 {{.*}}, -2147483648 |
| 105 |
// CHECK: load i32, ptr @"?$S1@?1??MultipleStatics@@[email protected]" |
| 106 |
// CHECK: and i32 {{.*}}, 1 |
| 107 |
// CHECK: and i32 {{.*}}, 2 |
| 108 |
// CHECK: and i32 {{.*}}, 4 |
| 109 |
// CHECK: ret |
| 110 |
|
| 111 |
// Force WeakODRLinkage by using templates |
| 112 |
class A { |
| 113 |
public: |
| 114 |
A() {} |
| 115 |
~A() {} |
| 116 |
int a; |
| 117 |
}; |
| 118 |
|
| 119 |
template<typename T> |
| 120 |
class B { |
| 121 |
public: |
| 122 |
static A foo; |
| 123 |
}; |
| 124 |
|
| 125 |
template<typename T> A B<T>::foo; |
| 126 |
|
| 127 |
inline S &UnreachableStatic() { |
| 128 |
if (0) { |
| 129 |
static S s; // bit 1 |
| 130 |
return s; |
| 131 |
} |
| 132 |
static S s; // bit 2 |
| 133 |
return s; |
| 134 |
} |
| 135 |
|
| 136 |
// CHECK-LABEL: define linkonce_odr dso_local noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @"?UnreachableStatic@@YAAAUS@@XZ"() {{.*}} comdat |
| 137 |
// CHECK: and i32 {{.*}}, 2 |
| 138 |
// CHECK: or i32 {{.*}}, 2 |
| 139 |
// CHECK: ret |
| 140 |
|
| 141 |
inline S &getS() { |
| 142 |
static S TheS; |
| 143 |
return TheS; |
| 144 |
} |
| 145 |
|
| 146 |
// CHECK-LABEL: define linkonce_odr dso_local noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @"?getS@@YAAAUS@@XZ"() {{.*}} comdat |
| 147 |
// CHECK: load i32, ptr @"??_B?1??getS@@YAAAUS@@XZ@51" |
| 148 |
// CHECK: and i32 {{.*}}, 1 |
| 149 |
// CHECK: icmp eq i32 {{.*}}, 0 |
| 150 |
// CHECK: br i1 |
| 151 |
// init: |
| 152 |
// CHECK: or i32 {{.*}}, 1 |
| 153 |
// CHECK: store i32 {{.*}}, ptr @"??_B?1??getS@@YAAAUS@@XZ@51" |
| 154 |
// CHECK: call x86_thiscallcc noundef ptr @"??0S@@QAE@XZ"(ptr {{[^,]*}} @"?TheS@?1??getS@@YAAAUS@@XZ@4U2@A") |
| 155 |
// CHECK: call i32 @atexit(ptr @"??__FTheS@?1??getS@@YAAAUS@@XZ@YAXXZ") |
| 156 |
// CHECK: br label |
| 157 |
// init.end: |
| 158 |
// CHECK: ret ptr @"?TheS@?1??getS@@YAAAUS@@XZ@4U2@A" |
| 159 |
|
| 160 |
inline int enum_in_function() { |
| 161 |
// CHECK-LABEL: define linkonce_odr dso_local noundef i32 @"?enum_in_function@@YAHXZ"() {{.*}} comdat |
| 162 |
static enum e { foo, bar, baz } x; |
| 163 |
// CHECK: @"?x@?1??enum_in_function@@YAHXZ@4W4e@?1??1@YAHXZ@A" |
| 164 |
static int y; |
| 165 |
// CHECK: @"?y@?1??enum_in_function@@YAHXZ@4HA" |
| 166 |
return x + y; |
| 167 |
}; |
| 168 |
|
| 169 |
struct T { |
| 170 |
enum e { foo, bar, baz }; |
| 171 |
int enum_in_struct() { |
| 172 |
// CHECK-LABEL: define linkonce_odr dso_local x86_thiscallcc noundef i32 @"?enum_in_struct@T@@QAEHXZ"({{.*}}) {{.*}} comdat |
| 173 |
static int x; |
| 174 |
// CHECK: @"?x@?1??enum_in_struct@T@@QAEHXZ@4HA" |
| 175 |
return x++; |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
inline int switch_test(int x) { |
| 180 |
// CHECK-LABEL: define linkonce_odr dso_local noundef i32 @"?switch_test@@YAHH@Z"(i32 noundef %x) {{.*}} comdat |
| 181 |
switch (x) { |
| 182 |
static int a; |
| 183 |
// CHECK: @"?a@?3??switch_test@@YAHH@Z@4HA" |
| 184 |
case 0: |
| 185 |
a++; |
| 186 |
return 1; |
| 187 |
case 1: |
| 188 |
static int b; |
| 189 |
// CHECK: @"?b@?3??switch_test@@YAHH@Z@4HA" |
| 190 |
return b++; |
| 191 |
case 2: { |
| 192 |
static int c; |
| 193 |
// CHECK: @"?c@?4??switch_test@@YAHH@Z@4HA" |
| 194 |
return b + c++; |
| 195 |
} |
| 196 |
}; |
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
int f(); |
| 201 |
inline void switch_test2() { |
| 202 |
// CHECK-LABEL: define linkonce_odr dso_local void @"?switch_test2@@YAXXZ"() {{.*}} comdat |
| 203 |
// CHECK: @"?x@?2??switch_test2@@YAXXZ@4HA" |
| 204 |
switch (1) default: static int x = f(); |
| 205 |
} |
| 206 |
|
| 207 |
namespace DynamicDLLImportInitVSMangling { |
| 208 |
// Failing to pop the ExprEvalContexts when instantiating a dllimport var with |
| 209 |
// dynamic initializer would cause subsequent static local numberings to be |
| 210 |
// incorrect. |
| 211 |
struct NonPOD { NonPOD(); }; |
| 212 |
template <typename T> struct A { static NonPOD x; }; |
| 213 |
template <typename T> NonPOD A<T>::x; |
| 214 |
template struct __declspec(dllimport) A<int>; |
| 215 |
|
| 216 |
inline int switch_test3() { |
| 217 |
// CHECK-LABEL: define linkonce_odr dso_local noundef i32 @"?switch_test3@DynamicDLLImportInitVSMangling@@YAHXZ"() {{.*}} comdat |
| 218 |
static int local; |
| 219 |
// CHECK: @"?local@?1??switch_test3@DynamicDLLImportInitVSMangling@@YAHXZ@4HA" |
| 220 |
return local++; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
void force_usage() { |
| 225 |
UnreachableStatic(); |
| 226 |
getS(); |
| 227 |
(void)B<int>::foo; // (void) - force usage |
| 228 |
enum_in_function(); |
| 229 |
(void)&T::enum_in_struct; |
| 230 |
switch_test(1); |
| 231 |
switch_test2(); |
| 232 |
DynamicDLLImportInitVSMangling::switch_test3(); |
| 233 |
} |
| 234 |
|
| 235 |
// CHECK: define linkonce_odr dso_local void @"??__E?foo@?$B@H@@2VA@@A@@YAXXZ"() {{.*}} comdat |
| 236 |
// CHECK-NOT: and |
| 237 |
// CHECK-NOT: ?_Bfoo@ |
| 238 |
// CHECK: call x86_thiscallcc noundef ptr @"??0A@@QAE@XZ" |
| 239 |
// CHECK: call i32 @atexit(ptr @"??__F?foo@?$B@H@@2VA@@A@@YAXXZ") |
| 240 |
// CHECK: ret void |
| 241 |
|
| 242 |
// CHECK: define linkonce_odr dso_local x86_thiscallcc noundef ptr @"??0A@@QAE@XZ"({{.*}}) {{.*}} comdat |
| 243 |
|
| 244 |
// CHECK: define linkonce_odr dso_local x86_thiscallcc void @"??1A@@QAE@XZ"({{.*}}) {{.*}} comdat |
| 245 |
|
| 246 |
// CHECK: define internal void @"??__F?foo@?$B@H@@2VA@@A@@YAXXZ" |
| 247 |
// CHECK: call x86_thiscallcc void @"??1A@@QAE@XZ"{{.*}}foo |
| 248 |
// CHECK: ret void |
| 249 |
|
| 250 |
// CHECK: define internal void @_GLOBAL__sub_I_microsoft_abi_static_initializers.cpp() |
| 251 |
// CHECK: call void @"??__Es@@YAXXZ"() |
| 252 |
// CHECK: ret void |