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