John McCall | 466e221 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple=x86_64-apple-darwin10 -emit-llvm -fexceptions %s -o - |FileCheck %s |
Tim Northover | 19ae117 | 2013-08-12 12:51:05 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -triple=x86_64-apple-darwin10 -emit-llvm %s -o - |FileCheck -check-prefix CHECK-NOEXC %s |
Reid Kleckner | 787dc43 | 2015-04-22 19:37:32 +0000 | [diff] [blame] | 3 | // RUN: %clang_cc1 -triple=x86_64-apple-darwin10 -emit-llvm \ |
| 4 | // RUN: -momit-leaf-frame-pointer -mdisable-fp-elim %s -o - \ |
| 5 | // RUN: | FileCheck -check-prefix CHECK-FP %s |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 6 | |
| 7 | struct A { |
| 8 | A(); |
| 9 | ~A(); |
| 10 | }; |
| 11 | |
Anders Carlsson | a18ed9b | 2009-10-08 17:28:59 +0000 | [diff] [blame] | 12 | struct B { B(); ~B(); }; |
| 13 | |
John McCall | 49786a6 | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 14 | struct C { void *field; }; |
| 15 | |
Anders Carlsson | cb86e10 | 2010-02-05 18:38:45 +0000 | [diff] [blame] | 16 | struct D { ~D(); }; |
| 17 | |
Rafael Espindola | b73c973 | 2014-05-22 23:33:27 +0000 | [diff] [blame] | 18 | // CHECK: @__dso_handle = external global i8 |
John McCall | 49786a6 | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 19 | // CHECK: @c = global %struct.C zeroinitializer, align 8 |
| 20 | |
John McCall | 7e9e2b4 | 2010-08-12 07:31:42 +0000 | [diff] [blame] | 21 | // It's okay if we ever implement the IR-generation optimization to remove this. |
David Blaikie | bdf40a6 | 2015-03-13 18:21:46 +0000 | [diff] [blame] | 22 | // CHECK: @_ZN5test3L3varE = internal constant i8* getelementptr inbounds ([7 x i8], [7 x i8]* |
John McCall | 7e9e2b4 | 2010-08-12 07:31:42 +0000 | [diff] [blame] | 23 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 24 | // PR6205: The casts should not require global initializers |
| 25 | // CHECK: @_ZN6PR59741cE = external global %"struct.PR5974::C" |
David Blaikie | bdf40a6 | 2015-03-13 18:21:46 +0000 | [diff] [blame] | 26 | // CHECK: @_ZN6PR59741aE = global %"struct.PR5974::A"* getelementptr inbounds (%"struct.PR5974::C", %"struct.PR5974::C"* @_ZN6PR59741cE, i32 0, i32 0) |
| 27 | // CHECK: @_ZN6PR59741bE = global %"struct.PR5974::B"* bitcast (i8* getelementptr (i8, i8* bitcast (%"struct.PR5974::C"* @_ZN6PR59741cE to i8*), i64 4) to %"struct.PR5974::B"*), align 8 |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 28 | |
Anders Carlsson | a18ed9b | 2009-10-08 17:28:59 +0000 | [diff] [blame] | 29 | // CHECK: call void @_ZN1AC1Ev(%struct.A* @a) |
David Blaikie | bdf40a6 | 2015-03-13 18:21:46 +0000 | [diff] [blame] | 30 | // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1AD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A, %struct.A* @a, i32 0, i32 0), i8* @__dso_handle) |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 31 | A a; |
Anders Carlsson | a18ed9b | 2009-10-08 17:28:59 +0000 | [diff] [blame] | 32 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 33 | // CHECK: call void @_ZN1BC1Ev(%struct.B* @b) |
David Blaikie | bdf40a6 | 2015-03-13 18:21:46 +0000 | [diff] [blame] | 34 | // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.B*)* @_ZN1BD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.B, %struct.B* @b, i32 0, i32 0), i8* @__dso_handle) |
Anders Carlsson | a18ed9b | 2009-10-08 17:28:59 +0000 | [diff] [blame] | 35 | B b; |
John McCall | 49786a6 | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 36 | |
| 37 | // PR6205: this should not require a global initializer |
| 38 | // CHECK-NOT: call void @_ZN1CC1Ev(%struct.C* @c) |
| 39 | C c; |
| 40 | |
David Blaikie | bdf40a6 | 2015-03-13 18:21:46 +0000 | [diff] [blame] | 41 | // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.D*)* @_ZN1DD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.D, %struct.D* @d, i32 0, i32 0), i8* @__dso_handle) |
Anders Carlsson | cb86e10 | 2010-02-05 18:38:45 +0000 | [diff] [blame] | 42 | D d; |
| 43 | |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 44 | // <rdar://problem/7458115> |
| 45 | namespace test1 { |
| 46 | int f(); |
| 47 | const int x = f(); // This has side-effects and gets emitted immediately. |
| 48 | const int y = x - 1; // This gets deferred. |
| 49 | const int z = ~y; // This also gets deferred, but gets "undeferred" before y. |
| 50 | int test() { return z; } |
Stephen Lin | 4362261 | 2013-08-15 06:47:53 +0000 | [diff] [blame] | 51 | // CHECK-LABEL: define i32 @_ZN5test14testEv() |
John McCall | 45d4947 | 2010-07-30 04:56:58 +0000 | [diff] [blame] | 52 | |
| 53 | // All of these initializers end up delayed, so we check them later. |
| 54 | } |
| 55 | |
| 56 | // <rdar://problem/8246444> |
| 57 | namespace test2 { |
| 58 | struct allocator { allocator(); ~allocator(); }; |
| 59 | struct A { A(const allocator &a = allocator()); ~A(); }; |
| 60 | |
| 61 | A a; |
| 62 | // CHECK: call void @_ZN5test29allocatorC1Ev( |
| 63 | // CHECK: invoke void @_ZN5test21AC1ERKNS_9allocatorE( |
| 64 | // CHECK: call void @_ZN5test29allocatorD1Ev( |
| 65 | // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test21AD1Ev {{.*}} @_ZN5test21aE |
| 66 | } |
| 67 | |
John McCall | 7e9e2b4 | 2010-08-12 07:31:42 +0000 | [diff] [blame] | 68 | namespace test3 { |
| 69 | // Tested at the beginning of the file. |
| 70 | const char * const var = "string"; |
| 71 | extern const char * const var; |
| 72 | |
| 73 | const char *test() { return var; } |
| 74 | } |
| 75 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 76 | namespace test4 { |
Douglas Gregor | fa918f6 | 2011-07-01 21:54:36 +0000 | [diff] [blame] | 77 | struct A { |
| 78 | A(); |
| 79 | }; |
| 80 | extern int foo(); |
| 81 | |
| 82 | // This needs an initialization function and guard variables. |
David Blaikie | a953f28 | 2015-02-27 21:19:58 +0000 | [diff] [blame] | 83 | // CHECK: load i8, i8* bitcast (i64* @_ZGVN5test41xE |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 84 | // CHECK: [[CALL:%.*]] = call i32 @_ZN5test43fooEv |
| 85 | // CHECK-NEXT: store i32 [[CALL]], i32* @_ZN5test41xE |
| 86 | // CHECK-NEXT: store i64 1, i64* @_ZGVN5test41xE |
Douglas Gregor | fa918f6 | 2011-07-01 21:54:36 +0000 | [diff] [blame] | 87 | __attribute__((weak)) int x = foo(); |
| 88 | } |
| 89 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 90 | namespace PR5974 { |
| 91 | struct A { int a; }; |
| 92 | struct B { int b; }; |
| 93 | struct C : A, B { int c; }; |
| 94 | |
| 95 | extern C c; |
| 96 | |
| 97 | // These should not require global initializers. |
| 98 | A* a = &c; |
| 99 | B* b = &c; |
| 100 | } |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 101 | |
John McCall | 32f44bd | 2011-04-12 01:01:22 +0000 | [diff] [blame] | 102 | // PR9570: the indirect field shouldn't crash IR gen. |
| 103 | namespace test5 { |
John McCall | b4744a3 | 2011-04-12 01:15:45 +0000 | [diff] [blame] | 104 | static union { |
John McCall | 32f44bd | 2011-04-12 01:01:22 +0000 | [diff] [blame] | 105 | unsigned bar[4096] __attribute__((aligned(128))); |
| 106 | }; |
| 107 | } |
| 108 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 109 | namespace std { struct type_info; } |
| 110 | |
| 111 | namespace test6 { |
| 112 | struct A { virtual ~A(); }; |
| 113 | struct B : A {}; |
| 114 | extern A *p; |
| 115 | |
| 116 | // We must emit a dynamic initializer for 'q', because it could throw. |
| 117 | B *const q = &dynamic_cast<B&>(*p); |
| 118 | // CHECK: call void @__cxa_bad_cast() |
| 119 | // CHECK: store {{.*}} @_ZN5test6L1qE |
| 120 | |
| 121 | // We don't need to emit 'r' at all, because it has internal linkage, is |
| 122 | // unused, and its initialization has no side-effects. |
| 123 | B *const r = dynamic_cast<B*>(p); |
| 124 | // CHECK-NOT: call void @__cxa_bad_cast() |
| 125 | // CHECK-NOT: store {{.*}} @_ZN5test6L1rE |
| 126 | |
| 127 | // This can throw, so we need to emit it. |
| 128 | const std::type_info *const s = &typeid(*p); |
| 129 | // CHECK: store {{.*}} @_ZN5test6L1sE |
| 130 | |
| 131 | // This can't throw, so we don't. |
| 132 | const std::type_info *const t = &typeid(p); |
| 133 | // CHECK-NOT: @_ZN5test6L1tE |
| 134 | |
Richard Smith | a33e4fe | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 135 | extern B *volatile v; |
| 136 | // CHECK: store {{.*}} @_ZN5test6L1wE |
| 137 | B *const w = dynamic_cast<B*>(v); |
| 138 | |
| 139 | // CHECK: load volatile |
| 140 | // CHECK: store {{.*}} @_ZN5test6L1xE |
| 141 | const int x = *(volatile int*)0x1234; |
| 142 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 143 | namespace { |
| 144 | int a = int(); |
| 145 | volatile int b = int(); |
| 146 | int c = a; |
| 147 | int d = b; |
| 148 | // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1aE |
| 149 | // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1bE |
| 150 | // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1cE |
| 151 | // CHECK: load volatile {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1bE |
| 152 | // CHECK: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1dE |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | namespace test7 { |
| 157 | struct A { A(); }; |
| 158 | struct B { ~B(); int n; }; |
Richard Smith | a33e4fe | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 159 | struct C { C() = default; C(const C&); int n; }; |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 160 | struct D {}; |
| 161 | |
| 162 | // CHECK: call void @_ZN5test71AC1Ev({{.*}}@_ZN5test7L1aE) |
| 163 | const A a = A(); |
| 164 | |
| 165 | // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test71BD1Ev{{.*}} @_ZN5test7L2b1E |
| 166 | // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test71BD1Ev{{.*}} @_ZGRN5test72b2E |
| 167 | // CHECK: call void @_ZN5test71BD1Ev( |
| 168 | // CHECK: store {{.*}} @_ZN5test7L2b3E |
| 169 | const B b1 = B(); |
| 170 | const B &b2 = B(); |
| 171 | const int b3 = B().n; |
| 172 | |
| 173 | // CHECK-NOT: @_ZN5test7L2c1E |
| 174 | // CHECK: @_ZN5test7L2c2E |
Richard Smith | a33e4fe | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 175 | // CHECK-NOT: @_ZN5test7L2c3E |
| 176 | // CHECK: @_ZN5test7L2c4E |
| 177 | const C c1 = C(); |
| 178 | const C c2 = static_cast<const C&>(C()); |
| 179 | const int c3 = C().n; |
| 180 | const int c4 = C(C()).n; |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 181 | |
| 182 | // CHECK-NOT: @_ZN5test7L1dE |
| 183 | const D d = D(); |
| 184 | |
| 185 | // CHECK: store {{.*}} @_ZN5test71eE |
| 186 | int f(), e = f(); |
| 187 | } |
| 188 | |
John McCall | a97f329 | 2011-04-12 01:46:54 +0000 | [diff] [blame] | 189 | |
John McCall | 45d4947 | 2010-07-30 04:56:58 +0000 | [diff] [blame] | 190 | // At the end of the file, we check that y is initialized before z. |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 191 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 192 | // CHECK: define internal void [[TEST1_Z_INIT:@.*]]() |
David Blaikie | a953f28 | 2015-02-27 21:19:58 +0000 | [diff] [blame] | 193 | // CHECK: load i32, i32* @_ZN5test1L1yE |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 194 | // CHECK-NEXT: xor |
| 195 | // CHECK-NEXT: store i32 {{.*}}, i32* @_ZN5test1L1zE |
| 196 | // CHECK: define internal void [[TEST1_Y_INIT:@.*]]() |
David Blaikie | a953f28 | 2015-02-27 21:19:58 +0000 | [diff] [blame] | 197 | // CHECK: load i32, i32* @_ZN5test1L1xE |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 198 | // CHECK-NEXT: sub |
| 199 | // CHECK-NEXT: store i32 {{.*}}, i32* @_ZN5test1L1yE |
| 200 | |
Reid Kleckner | 787dc43 | 2015-04-22 19:37:32 +0000 | [diff] [blame] | 201 | // CHECK: define internal void @_GLOBAL__sub_I_global_init.cpp() #{{[0-9]+}} section "__TEXT,__StaticInit,regular,pure_instructions" { |
John McCall | 70013b6 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 202 | // CHECK: call void [[TEST1_Y_INIT]] |
| 203 | // CHECK: call void [[TEST1_Z_INIT]] |
Anders Carlsson | 13f7c7d | 2010-06-09 01:42:52 +0000 | [diff] [blame] | 204 | |
John McCall | 466e221 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 205 | // rdar://problem/8090834: this should be nounwind |
Nico Weber | bdc9698 | 2014-05-06 20:32:45 +0000 | [diff] [blame] | 206 | // CHECK-NOEXC: define internal void @_GLOBAL__sub_I_global_init.cpp() [[NUW:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" { |
Bill Wendling | c33fc4c | 2013-02-20 07:22:19 +0000 | [diff] [blame] | 207 | |
Reid Kleckner | 787dc43 | 2015-04-22 19:37:32 +0000 | [diff] [blame] | 208 | // CHECK-NOEXC: attributes [[NUW]] = { nounwind{{.*}} } |
| 209 | |
| 210 | // PR21811: attach the appropriate attribute to the global init function |
| 211 | // CHECK-FP: define internal void @_GLOBAL__sub_I_global_init.cpp() [[NUX:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" { |
| 212 | // CHECK-FP: attributes [[NUX]] = { nounwind {{.*}}"no-frame-pointer-elim-non-leaf"{{.*}} } |