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