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