Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / global-init.cpp
blob52039a5208223875c919bd4048f816514b8f8f7e
1 // RUN: %clang_cc1 %std_cxx98-14 -triple=x86_64-apple-darwin10 -emit-llvm -fexceptions %s -o - |FileCheck %s --check-prefixes=CHECK,PRE17
2 // RUN: %clang_cc1 %std_cxx98-14 -triple=x86_64-apple-darwin10 -emit-llvm %s -o - |FileCheck %s --check-prefixes=CHECK-NOEXC,PRE17
3 // RUN: %clang_cc1 %std_cxx98-14 -triple=x86_64-apple-darwin10 -emit-llvm -mframe-pointer=non-leaf %s -o - \
4 // RUN: | FileCheck -check-prefix CHECK-FP %s
5 // RUN: %clang_cc1 %std_cxx98-14 -triple=x86_64-apple-darwin10 -emit-llvm %s -o - -fno-builtin \
6 // RUN: | FileCheck -check-prefix CHECK-NOBUILTIN %s
7 // RUN: %clang_cc1 %std_cxx17- -triple=x86_64-apple-darwin10 -emit-llvm -fexceptions %s -o - | FileCheck %s
9 struct A {
10 A();
11 ~A();
14 struct B { B(); ~B(); };
16 struct C { void *field; };
18 struct D { ~D(); };
20 // CHECK: @__dso_handle = external hidden global i8
21 // CHECK: @c ={{.*}} global %struct.C zeroinitializer, align 8
23 // PR6205: The casts should not require global initializers
24 // CHECK: @_ZN6PR59741cE = external global %"struct.PR5974::C"
25 // CHECK: @_ZN6PR59741aE ={{.*}} global ptr @_ZN6PR59741cE
26 // CHECK: @_ZN6PR59741bE ={{.*}} global ptr getelementptr (i8, ptr @_ZN6PR59741cE, i64 4), align 8
28 // CHECK: call void @_ZN1AC1Ev(ptr {{[^,]*}} @a)
29 // CHECK: call i32 @__cxa_atexit(ptr @_ZN1AD1Ev, ptr @a, ptr @__dso_handle)
30 A a;
32 // CHECK: call void @_ZN1BC1Ev(ptr {{[^,]*}} @b)
33 // CHECK: call i32 @__cxa_atexit(ptr @_ZN1BD1Ev, ptr @b, ptr @__dso_handle)
34 B b;
36 // PR6205: this should not require a global initializer
37 // CHECK-NOT: call void @_ZN1CC1Ev(ptr @c)
38 C c;
40 // CHECK: call i32 @__cxa_atexit(ptr @_ZN1DD1Ev, ptr @d, ptr @__dso_handle)
41 D d;
43 namespace test1 {
44 int f();
45 const int x = f(); // This has side-effects and gets emitted immediately.
46 const int y = x - 1; // This gets deferred.
47 const int z = ~y; // This also gets deferred, but gets "undeferred" before y.
48 int test() { return z; }
49 // CHECK-LABEL: define{{.*}} i32 @_ZN5test14testEv()
51 // All of these initializers end up delayed, so we check them later.
54 namespace test2 {
55 struct allocator { allocator(); ~allocator(); };
56 struct A { A(const allocator &a = allocator()); ~A(); };
58 A a;
59 // CHECK: call void @_ZN5test29allocatorC1Ev(
60 // CHECK: invoke void @_ZN5test21AC1ERKNS_9allocatorE(
61 // CHECK: call void @_ZN5test29allocatorD1Ev(
62 // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test21AD1Ev, {{.*}} @_ZN5test21aE
65 namespace test3 {
66 // Tested at the beginning of the file.
67 const char * const var = "string";
68 extern const char * const var;
70 const char *test() { return var; }
73 namespace test4 {
74 struct A {
75 A();
77 extern int foo();
79 // This needs an initialization function and guard variables.
80 // CHECK: load i8, ptr @_ZGVN5test41xE
81 // CHECK: store i8 1, ptr @_ZGVN5test41xE
82 // CHECK-NEXT: [[CALL:%.*]] = call noundef i32 @_ZN5test43fooEv
83 // CHECK-NEXT: store i32 [[CALL]], ptr @_ZN5test41xE
84 __attribute__((weak)) int x = foo();
87 namespace PR5974 {
88 struct A { int a; };
89 struct B { int b; };
90 struct C : A, B { int c; };
92 extern C c;
94 // These should not require global initializers.
95 A* a = &c;
96 B* b = &c;
99 // PR9570: the indirect field shouldn't crash IR gen.
100 namespace test5 {
101 static union {
102 unsigned bar[4096] __attribute__((aligned(128)));
106 namespace std { struct type_info; }
108 namespace test6 {
109 struct A { virtual ~A(); };
110 struct B : A {};
111 extern A *p;
113 // We must emit a dynamic initializer for 'q', because it could throw.
114 B *const q = &dynamic_cast<B&>(*p);
115 // CHECK: call void @__cxa_bad_cast()
116 // CHECK: store {{.*}} @_ZN5test6L1qE
118 // We don't need to emit 'r' at all, because it has internal linkage, is
119 // unused, and its initialization has no side-effects.
120 B *const r = dynamic_cast<B*>(p);
121 // CHECK-NOT: call void @__cxa_bad_cast()
122 // CHECK-NOT: store {{.*}} @_ZN5test6L1rE
124 // This can throw, so we need to emit it.
125 const std::type_info *const s = &typeid(*p);
126 // CHECK: store {{.*}} @_ZN5test6L1sE
128 // This can't throw, so we don't.
129 const std::type_info *const t = &typeid(p);
130 // CHECK-NOT: @_ZN5test6L1tE
132 extern B *volatile v;
133 // CHECK: store {{.*}} @_ZN5test6L1wE
134 B *const w = dynamic_cast<B*>(v);
136 // CHECK: load volatile
137 // CHECK: store {{.*}} @_ZN5test6L1xE
138 const int x = *(volatile int*)0x1234;
140 namespace {
141 int a = int();
142 volatile int b = int();
143 int c = a;
144 int d = b;
145 // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1aE
146 // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1bE
147 // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1cE
148 // CHECK: load volatile {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1bE
149 // CHECK: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1dE
153 namespace test7 {
154 struct A { A(); };
155 struct B { ~B(); int n; };
156 struct C { C() = default; C(const C&); int n; };
157 struct D {};
159 // CHECK: call void @_ZN5test71AC1Ev({{.*}}@_ZN5test7L1aE)
160 const A a = A();
162 // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test71BD1Ev{{.*}} @_ZN5test7L2b1E
163 // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test71BD1Ev{{.*}} @_ZGRN5test72b2E
164 // CHECK: call void @_ZN5test71BD1Ev(
165 // CHECK: store {{.*}} @_ZN5test7L2b3E
166 const B b1 = B();
167 const B &b2 = B();
168 const int b3 = B().n;
170 // CHECK-NOT: @_ZN5test7L2c1E
171 // PRE17: call void @llvm.memset{{.*}} @_ZN5test7L2c1E
172 // CHECK-NOT: @_ZN5test7L2c1E
173 // CHECK: @_ZN5test7L2c2E
174 // CHECK-NOT: @_ZN5test7L2c3E
175 // PRE17: @_ZN5test7L2c4E
176 const C c1 = C();
177 const C c2 = static_cast<const C&>(C());
178 const int c3 = C().n;
179 const int c4 = C(C()).n;
181 // CHECK-NOT: @_ZN5test7L1dE
182 const D d = D();
184 // CHECK: store {{.*}} @_ZN5test71eE
185 int f(), e = f();
189 // At the end of the file, we check that y is initialized before z.
191 // CHECK: define internal void [[TEST1_Z_INIT:@.*]]()
192 // CHECK: load i32, ptr @_ZN5test1L1yE
193 // CHECK-NEXT: xor
194 // CHECK-NEXT: store i32 {{.*}}, ptr @_ZN5test1L1zE
195 // CHECK: define internal void [[TEST1_Y_INIT:@.*]]()
196 // CHECK: load i32, ptr @_ZN5test1L1xE
197 // CHECK-NEXT: sub
198 // CHECK-NEXT: store i32 {{.*}}, ptr @_ZN5test1L1yE
200 // CHECK: define internal void @_GLOBAL__sub_I_global_init.cpp() #{{[0-9]+}} section "__TEXT,__StaticInit,regular,pure_instructions" {
201 // CHECK: call void [[TEST1_Y_INIT]]
202 // CHECK: call void [[TEST1_Z_INIT]]
204 // this should be nounwind
205 // CHECK-NOEXC: define internal void @_GLOBAL__sub_I_global_init.cpp() [[NUW:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" {
206 // CHECK-NOEXC: attributes [[NUW]] = { noinline nounwind{{.*}} }
208 // Make sure we mark global initializers with the no-builtins attribute.
209 // CHECK-NOBUILTIN: define internal void @_GLOBAL__sub_I_global_init.cpp() [[NUW:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" {
210 // CHECK-NOBUILTIN: attributes [[NUW]] = { noinline nounwind{{.*}}"no-builtins"{{.*}} }
212 // PR21811: attach the appropriate attribute to the global init function
213 // CHECK-FP: define internal void @_GLOBAL__sub_I_global_init.cpp() [[NUX:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" {
214 // CHECK-FP: attributes [[NUX]] = { noinline nounwind {{.*}}"frame-pointer"="non-leaf"{{.*}} }