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