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 \
4 // RUN: -momit-leaf-frame-pointer -mdisable-fp-elim %s -o - \
5 // RUN: | FileCheck -check-prefix CHECK-FP %s
12 struct B { B(); ~B(); };
14 struct C { void *field; };
18 // CHECK: @__dso_handle = external global i8
19 // CHECK: @c = global %struct.C zeroinitializer, align 8
21 // It's okay if we ever implement the IR-generation optimization to remove this.
22 // CHECK: @_ZN5test3L3varE = internal constant i8* getelementptr inbounds ([7 x i8], [7 x i8]*
24 // PR6205: The casts should not require global initializers
25 // CHECK: @_ZN6PR59741cE = external global %"struct.PR5974::C"
26 // CHECK: @_ZN6PR59741aE = global %"struct.PR5974::A"* getelementptr inbounds (%"struct.PR5974::C", %"struct.PR5974::C"* @_ZN6PR59741cE, i32 0, i32 0)
27 // 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
29 // CHECK: call void @_ZN1AC1Ev(%struct.A* @a)
30 // 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)
33 // CHECK: call void @_ZN1BC1Ev(%struct.B* @b)
34 // 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)
37 // PR6205: this should not require a global initializer
38 // CHECK-NOT: call void @_ZN1CC1Ev(%struct.C* @c)
41 // 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)
44 // <rdar://problem/7458115>
47 const int x = f(); // This has side-effects and gets emitted immediately.
48 const int y = x - 1; // This gets deferred.
49 const int z = ~y; // This also gets deferred, but gets "undeferred" before y.
50 int test() { return z; }
51 // CHECK-LABEL: define i32 @_ZN5test14testEv()
53 // All of these initializers end up delayed, so we check them later.
56 // <rdar://problem/8246444>
58 struct allocator { allocator(); ~allocator(); };
59 struct A { A(const allocator &a = allocator()); ~A(); };
62 // CHECK: call void @_ZN5test29allocatorC1Ev(
63 // CHECK: invoke void @_ZN5test21AC1ERKNS_9allocatorE(
64 // CHECK: call void @_ZN5test29allocatorD1Ev(
65 // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test21AD1Ev {{.*}} @_ZN5test21aE
69 // Tested at the beginning of the file.
70 const char * const var = "string";
71 extern const char * const var;
73 const char *test() { return var; }
82 // This needs an initialization function and guard variables.
83 // CHECK: load i8, i8* bitcast (i64* @_ZGVN5test41xE
84 // CHECK: [[CALL:%.*]] = call i32 @_ZN5test43fooEv
85 // CHECK-NEXT: store i32 [[CALL]], i32* @_ZN5test41xE
86 // CHECK-NEXT: store i64 1, i64* @_ZGVN5test41xE
87 __attribute__((weak)) int x = foo();
93 struct C : A, B { int c; };
97 // These should not require global initializers.
102 // PR9570: the indirect field shouldn't crash IR gen.
105 unsigned bar[4096] __attribute__((aligned(128)));
109 namespace std { struct type_info; }
112 struct A { virtual ~A(); };
116 // We must emit a dynamic initializer for 'q', because it could throw.
117 B *const q = &dynamic_cast<B&>(*p);
118 // CHECK: call void @__cxa_bad_cast()
119 // CHECK: store {{.*}} @_ZN5test6L1qE
121 // We don't need to emit 'r' at all, because it has internal linkage, is
122 // unused, and its initialization has no side-effects.
123 B *const r = dynamic_cast<B*>(p);
124 // CHECK-NOT: call void @__cxa_bad_cast()
125 // CHECK-NOT: store {{.*}} @_ZN5test6L1rE
127 // This can throw, so we need to emit it.
128 const std::type_info *const s = &typeid(*p);
129 // CHECK: store {{.*}} @_ZN5test6L1sE
131 // This can't throw, so we don't.
132 const std::type_info *const t = &typeid(p);
133 // CHECK-NOT: @_ZN5test6L1tE
135 extern B *volatile v;
136 // CHECK: store {{.*}} @_ZN5test6L1wE
137 B *const w = dynamic_cast<B*>(v);
139 // CHECK: load volatile
140 // CHECK: store {{.*}} @_ZN5test6L1xE
141 const int x = *(volatile int*)0x1234;
145 volatile int b = int();
148 // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1aE
149 // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1bE
150 // CHECK-NOT: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1cE
151 // CHECK: load volatile {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1bE
152 // CHECK: store {{.*}} @_ZN5test6{{[A-Za-z0-9_]*}}1dE
158 struct B { ~B(); int n; };
159 struct C { C() = default; C(const C&); int n; };
162 // CHECK: call void @_ZN5test71AC1Ev({{.*}}@_ZN5test7L1aE)
165 // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test71BD1Ev{{.*}} @_ZN5test7L2b1E
166 // CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test71BD1Ev{{.*}} @_ZGRN5test72b2E
167 // CHECK: call void @_ZN5test71BD1Ev(
168 // CHECK: store {{.*}} @_ZN5test7L2b3E
171 const int b3 = B().n;
173 // CHECK-NOT: @_ZN5test7L2c1E
174 // CHECK: @_ZN5test7L2c2E
175 // CHECK-NOT: @_ZN5test7L2c3E
176 // CHECK: @_ZN5test7L2c4E
178 const C c2 = static_cast<const C&>(C());
179 const int c3 = C().n;
180 const int c4 = C(C()).n;
182 // CHECK-NOT: @_ZN5test7L1dE
185 // CHECK: store {{.*}} @_ZN5test71eE
190 // At the end of the file, we check that y is initialized before z.
192 // CHECK: define internal void [[TEST1_Z_INIT:@.*]]()
193 // CHECK: load i32, i32* @_ZN5test1L1yE
195 // CHECK-NEXT: store i32 {{.*}}, i32* @_ZN5test1L1zE
196 // CHECK: define internal void [[TEST1_Y_INIT:@.*]]()
197 // CHECK: load i32, i32* @_ZN5test1L1xE
199 // CHECK-NEXT: store i32 {{.*}}, i32* @_ZN5test1L1yE
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]]
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" {
208 // CHECK-NOEXC: attributes [[NUW]] = { nounwind{{.*}} }
210 // PR21811: attach the appropriate attribute to the global init function
211 // CHECK-FP: define internal void @_GLOBAL__sub_I_global_init.cpp() [[NUX:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" {
212 // CHECK-FP: attributes [[NUX]] = { nounwind {{.*}}"no-frame-pointer-elim-non-leaf"{{.*}} }