John McCall | 044cc54 | 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 |
| 2 | // RUN: %clang_cc1 -triple=x86_64-apple-darwin10 -emit-llvm %s -o - |FileCheck -check-prefix NOEXC %s |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 3 | |
| 4 | struct A { |
| 5 | A(); |
| 6 | ~A(); |
| 7 | }; |
| 8 | |
Anders Carlsson | 74d644a | 2009-10-08 17:28:59 +0000 | [diff] [blame] | 9 | struct B { B(); ~B(); }; |
| 10 | |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 11 | struct C { void *field; }; |
| 12 | |
Anders Carlsson | 2ca4f63 | 2010-02-05 18:38:45 +0000 | [diff] [blame] | 13 | struct D { ~D(); }; |
| 14 | |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 15 | // CHECK: @__dso_handle = external unnamed_addr global i8 |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 16 | // CHECK: @c = global %struct.C zeroinitializer, align 8 |
| 17 | |
John McCall | 6d31122 | 2010-08-12 07:31:42 +0000 | [diff] [blame] | 18 | // It's okay if we ever implement the IR-generation optimization to remove this. |
| 19 | // CHECK: @_ZN5test3L3varE = internal constant i8* getelementptr inbounds ([7 x i8]* |
| 20 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 21 | // PR6205: The casts should not require global initializers |
| 22 | // CHECK: @_ZN6PR59741cE = external global %"struct.PR5974::C" |
Anders Carlsson | eb9d81d | 2011-04-17 21:56:13 +0000 | [diff] [blame] | 23 | // CHECK: @_ZN6PR59741aE = global %"struct.PR5974::A"* getelementptr inbounds (%"struct.PR5974::C"* @_ZN6PR59741cE, i32 0, i32 0) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 24 | // CHECK: @_ZN6PR59741bE = global %"struct.PR5974::B"* bitcast (i8* getelementptr (i8* bitcast (%"struct.PR5974::C"* @_ZN6PR59741cE to i8*), i64 4) to %"struct.PR5974::B"*), align 8 |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 25 | |
Anders Carlsson | 74d644a | 2009-10-08 17:28:59 +0000 | [diff] [blame] | 26 | // CHECK: call void @_ZN1AC1Ev(%struct.A* @a) |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 27 | // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1AD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A* @a, i32 0, i32 0), i8* @__dso_handle) |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 28 | A a; |
Anders Carlsson | 74d644a | 2009-10-08 17:28:59 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 30 | // CHECK: call void @_ZN1BC1Ev(%struct.B* @b) |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 31 | // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.B*)* @_ZN1BD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.B* @b, i32 0, i32 0), i8* @__dso_handle) |
Anders Carlsson | 74d644a | 2009-10-08 17:28:59 +0000 | [diff] [blame] | 32 | B b; |
John McCall | fb8b69a | 2010-02-02 08:02:49 +0000 | [diff] [blame] | 33 | |
| 34 | // PR6205: this should not require a global initializer |
| 35 | // CHECK-NOT: call void @_ZN1CC1Ev(%struct.C* @c) |
| 36 | C c; |
| 37 | |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 38 | // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.D*)* @_ZN1DD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.D* @d, i32 0, i32 0), i8* @__dso_handle) |
Anders Carlsson | 2ca4f63 | 2010-02-05 18:38:45 +0000 | [diff] [blame] | 39 | D d; |
| 40 | |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 41 | // <rdar://problem/7458115> |
| 42 | namespace test1 { |
| 43 | int f(); |
| 44 | const int x = f(); // This has side-effects and gets emitted immediately. |
| 45 | const int y = x - 1; // This gets deferred. |
| 46 | const int z = ~y; // This also gets deferred, but gets "undeferred" before y. |
| 47 | int test() { return z; } |
John McCall | 39dad53 | 2010-08-03 22:46:07 +0000 | [diff] [blame] | 48 | // CHECK: define i32 @_ZN5test14testEv() |
John McCall | 85aca0f | 2010-07-30 04:56:58 +0000 | [diff] [blame] | 49 | |
| 50 | // All of these initializers end up delayed, so we check them later. |
| 51 | } |
| 52 | |
| 53 | // <rdar://problem/8246444> |
| 54 | namespace test2 { |
| 55 | struct allocator { allocator(); ~allocator(); }; |
| 56 | struct A { A(const allocator &a = allocator()); ~A(); }; |
| 57 | |
| 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 |
| 63 | } |
| 64 | |
John McCall | 6d31122 | 2010-08-12 07:31:42 +0000 | [diff] [blame] | 65 | namespace test3 { |
| 66 | // Tested at the beginning of the file. |
| 67 | const char * const var = "string"; |
| 68 | extern const char * const var; |
| 69 | |
| 70 | const char *test() { return var; } |
| 71 | } |
| 72 | |
Richard Smith | 8ae4ec2 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 73 | namespace test4 { |
Douglas Gregor | e67d151 | 2011-07-01 21:54:36 +0000 | [diff] [blame] | 74 | struct A { |
| 75 | A(); |
| 76 | }; |
| 77 | extern int foo(); |
| 78 | |
| 79 | // This needs an initialization function and guard variables. |
Richard Smith | 8ae4ec2 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 80 | // CHECK: load i8* bitcast (i64* @_ZGVN5test41xE |
| 81 | // CHECK: [[CALL:%.*]] = call i32 @_ZN5test43fooEv |
| 82 | // CHECK-NEXT: store i32 [[CALL]], i32* @_ZN5test41xE |
| 83 | // CHECK-NEXT: store i64 1, i64* @_ZGVN5test41xE |
Douglas Gregor | e67d151 | 2011-07-01 21:54:36 +0000 | [diff] [blame] | 84 | __attribute__((weak)) int x = foo(); |
| 85 | } |
| 86 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 87 | namespace PR5974 { |
| 88 | struct A { int a; }; |
| 89 | struct B { int b; }; |
| 90 | struct C : A, B { int c; }; |
| 91 | |
| 92 | extern C c; |
| 93 | |
| 94 | // These should not require global initializers. |
| 95 | A* a = &c; |
| 96 | B* b = &c; |
| 97 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 98 | |
John McCall | 26fbc72 | 2011-04-12 01:01:22 +0000 | [diff] [blame] | 99 | // PR9570: the indirect field shouldn't crash IR gen. |
| 100 | namespace test5 { |
John McCall | 1d1a679 | 2011-04-12 01:15:45 +0000 | [diff] [blame] | 101 | static union { |
John McCall | 26fbc72 | 2011-04-12 01:01:22 +0000 | [diff] [blame] | 102 | unsigned bar[4096] __attribute__((aligned(128))); |
| 103 | }; |
| 104 | } |
| 105 | |
Richard Smith | 8ae4ec2 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 106 | namespace std { struct type_info; } |
| 107 | |
| 108 | namespace test6 { |
| 109 | struct A { virtual ~A(); }; |
| 110 | struct B : A {}; |
| 111 | extern A *p; |
| 112 | |
| 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 |
| 117 | |
| 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 |
| 123 | |
| 124 | // This can throw, so we need to emit it. |
| 125 | const std::type_info *const s = &typeid(*p); |
| 126 | // CHECK: store {{.*}} @_ZN5test6L1sE |
| 127 | |
| 128 | // This can't throw, so we don't. |
| 129 | const std::type_info *const t = &typeid(p); |
| 130 | // CHECK-NOT: @_ZN5test6L1tE |
| 131 | |
Richard Smith | 60b7038 | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 132 | extern B *volatile v; |
| 133 | // CHECK: store {{.*}} @_ZN5test6L1wE |
| 134 | B *const w = dynamic_cast<B*>(v); |
| 135 | |
| 136 | // CHECK: load volatile |
| 137 | // CHECK: store {{.*}} @_ZN5test6L1xE |
| 138 | const int x = *(volatile int*)0x1234; |
| 139 | |
Richard Smith | 8ae4ec2 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 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 |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | namespace test7 { |
| 154 | struct A { A(); }; |
| 155 | struct B { ~B(); int n; }; |
Richard Smith | 60b7038 | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 156 | struct C { C() = default; C(const C&); int n; }; |
Richard Smith | 8ae4ec2 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 157 | struct D {}; |
| 158 | |
| 159 | // CHECK: call void @_ZN5test71AC1Ev({{.*}}@_ZN5test7L1aE) |
| 160 | const A a = A(); |
| 161 | |
| 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; |
| 169 | |
| 170 | // CHECK-NOT: @_ZN5test7L2c1E |
| 171 | // CHECK: @_ZN5test7L2c2E |
Richard Smith | 60b7038 | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 172 | // CHECK-NOT: @_ZN5test7L2c3E |
| 173 | // CHECK: @_ZN5test7L2c4E |
| 174 | const C c1 = C(); |
| 175 | const C c2 = static_cast<const C&>(C()); |
| 176 | const int c3 = C().n; |
| 177 | const int c4 = C(C()).n; |
Richard Smith | 8ae4ec2 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 178 | |
| 179 | // CHECK-NOT: @_ZN5test7L1dE |
| 180 | const D d = D(); |
| 181 | |
| 182 | // CHECK: store {{.*}} @_ZN5test71eE |
| 183 | int f(), e = f(); |
| 184 | } |
| 185 | |
John McCall | 99ace16 | 2011-04-12 01:46:54 +0000 | [diff] [blame] | 186 | |
John McCall | 85aca0f | 2010-07-30 04:56:58 +0000 | [diff] [blame] | 187 | // At the end of the file, we check that y is initialized before z. |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 188 | |
Richard Smith | 8ae4ec2 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 189 | // CHECK: define internal void [[TEST1_Z_INIT:@.*]]() |
| 190 | // CHECK: load i32* @_ZN5test1L1yE |
| 191 | // CHECK-NEXT: xor |
| 192 | // CHECK-NEXT: store i32 {{.*}}, i32* @_ZN5test1L1zE |
| 193 | // CHECK: define internal void [[TEST1_Y_INIT:@.*]]() |
| 194 | // CHECK: load i32* @_ZN5test1L1xE |
| 195 | // CHECK-NEXT: sub |
| 196 | // CHECK-NEXT: store i32 {{.*}}, i32* @_ZN5test1L1yE |
| 197 | |
Anders Carlsson | 9df792c | 2010-06-09 01:42:52 +0000 | [diff] [blame] | 198 | // CHECK: define internal void @_GLOBAL__I_a() section "__TEXT,__StaticInit,regular,pure_instructions" { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 199 | // CHECK: call void [[TEST1_Y_INIT]] |
| 200 | // CHECK: call void [[TEST1_Z_INIT]] |
Anders Carlsson | 9df792c | 2010-06-09 01:42:52 +0000 | [diff] [blame] | 201 | |
John McCall | 044cc54 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 202 | // rdar://problem/8090834: this should be nounwind |
Bill Wendling | 8992457 | 2013-02-27 00:06:04 +0000 | [diff] [blame] | 203 | // CHECK-NOEXC: define internal void @_GLOBAL__I_a() [[NUW:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" { |
Bill Wendling | f7a9da0 | 2013-02-20 07:22:19 +0000 | [diff] [blame] | 204 | |
Bill Wendling | 8992457 | 2013-02-27 00:06:04 +0000 | [diff] [blame] | 205 | // CHECK-NOEXC: attributes [[NUW]] = { nounwind } |