• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 %s -triple=x86_64-pc-linuxs -emit-llvm -o - | FileCheck %s
2 
3 // CHECK: @_ZZ1hvE1i = internal global i32 0, align 4
4 // CHECK: @base_req = global [4 x i8] c"foo\00", align 1
5 // CHECK: @base_req_uchar = global [4 x i8] c"bar\00", align 1
6 
7 // CHECK: @_ZZN5test31BC1EvE1u = internal global { i8, [3 x i8] } { i8 97, [3 x i8] undef }, align 4
8 
9 // CHECK: @_ZZ2h2vE1i = linkonce_odr global i32 0, comdat, align 4
10 // CHECK: @_ZGVZ2h2vE1i = linkonce_odr global i64 0, comdat, align 8{{$}}
11 // CHECK: @_ZZN5test1L6getvarEiE3var = internal constant [4 x i32] [i32 1, i32 0, i32 2, i32 4], align 16
12 // CHECK: @_ZZN5test414useStaticLocalEvE3obj = linkonce_odr global %"struct.test4::HasVTable" zeroinitializer, comdat, align 8
13 
14 struct A {
15   A();
16   ~A();
17 };
18 
f()19 void f() {
20   // CHECK: load atomic i8, i8* bitcast (i64* @_ZGVZ1fvE1a to i8*) acquire, align 8
21   // CHECK: call i32 @__cxa_guard_acquire
22   // CHECK: call void @_ZN1AC1Ev
23   // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1AD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A, %struct.A* @_ZZ1fvE1a, i32 0, i32 0), i8* @__dso_handle)
24   // CHECK: call void @__cxa_guard_release
25   static A a;
26 }
27 
g()28 void g() {
29   // CHECK: call i8* @_Znwm(i64 1)
30   // CHECK: call void @_ZN1AC1Ev(
31   static A& a = *new A;
32 }
33 
34 int a();
h()35 void h() {
36   static const int i = a();
37 }
38 
39 // CHECK: define linkonce_odr void @_Z2h2v() {{.*}} comdat {
h2()40 inline void h2() {
41   static int i = a();
42 }
43 
h3()44 void h3() {
45   h2();
46 }
47 
48 // PR6980: this shouldn't crash
49 namespace test0 {
50   struct A { A(); };
51   __attribute__((noreturn)) int throw_exception();
52 
test()53   void test() {
54     throw_exception();
55     static A r;
56   }
57 }
58 
59 namespace test1 {
60   // CHECK-LABEL: define internal i32 @_ZN5test1L6getvarEi(
getvar(int index)61   static inline int getvar(int index) {
62     static const int var[] = { 1, 0, 2, 4 };
63     return var[index];
64   }
65 
test()66   void test() { (void) getvar(2); }
67 }
68 
69 // Make sure we emit the initializer correctly for the following:
70 char base_req[] = { "foo" };
71 unsigned char base_req_uchar[] = { "bar" };
72 
73 namespace union_static_local {
74   // CHECK-LABEL: define internal void @_ZZN18union_static_local4testEvEN1c4mainEv
75   // CHECK: call void @_ZN18union_static_local1fEPNS_1xE(%"union.union_static_local::x"* bitcast ({ [2 x i8*] }* @_ZZN18union_static_local4testEvE3foo to %"union.union_static_local::x"*))
76   union x { long double y; const char *x[2]; };
77   void f(union x*);
test()78   void test() {
79     static union x foo = { .x = { "a", "b" } };
80     struct c {
81       static void main() {
82         f(&foo);
83       }
84     };
85     c::main();
86   }
87 }
88 
89 // rdar://problem/11091093
90 //   Static variables should be consistent across constructor
91 //   or destructor variants.
92 namespace test2 {
93   struct A {
94     A();
95     ~A();
96   };
97 
98   struct B : virtual A {
99     B();
100     ~B();
101   };
102 
103   // If we ever implement this as a delegate ctor call, just change
104   // this to take variadic arguments or something.
105   extern int foo();
B()106   B::B() {
107     static int x = foo();
108   }
109   // CHECK-LABEL: define void @_ZN5test21BC2Ev
110   // CHECK:   load atomic i8, i8* bitcast (i64* @_ZGVZN5test21BC1EvE1x to i8*) acquire,
111   // CHECK:   call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BC1EvE1x)
112   // CHECK:   [[T0:%.*]] = call i32 @_ZN5test23fooEv()
113   // CHECK:   store i32 [[T0]], i32* @_ZZN5test21BC1EvE1x,
114   // CHECK:   call void @__cxa_guard_release(i64* @_ZGVZN5test21BC1EvE1x)
115 
116   // CHECK-LABEL: define void @_ZN5test21BC1Ev
117   // CHECK:   load atomic i8, i8* bitcast (i64* @_ZGVZN5test21BC1EvE1x to i8*) acquire,
118   // CHECK:   call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BC1EvE1x)
119   // CHECK:   [[T0:%.*]] = call i32 @_ZN5test23fooEv()
120   // CHECK:   store i32 [[T0]], i32* @_ZZN5test21BC1EvE1x,
121   // CHECK:   call void @__cxa_guard_release(i64* @_ZGVZN5test21BC1EvE1x)
122 
123   // This is just for completeness, because we actually emit this
124   // using a delegate dtor call.
~B()125   B::~B() {
126     static int y = foo();
127   }
128   // CHECK-LABEL: define void @_ZN5test21BD2Ev(
129   // CHECK:   load atomic i8, i8* bitcast (i64* @_ZGVZN5test21BD1EvE1y to i8*) acquire,
130   // CHECK:   call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BD1EvE1y)
131   // CHECK:   [[T0:%.*]] = call i32 @_ZN5test23fooEv()
132   // CHECK:   store i32 [[T0]], i32* @_ZZN5test21BD1EvE1y,
133   // CHECK:   call void @__cxa_guard_release(i64* @_ZGVZN5test21BD1EvE1y)
134 
135   // CHECK-LABEL: define void @_ZN5test21BD1Ev(
136   // CHECK:   call void @_ZN5test21BD2Ev(
137 }
138 
139 // This shouldn't error out.
140 namespace test3 {
141   struct A {
142     A();
143     ~A();
144   };
145 
146   struct B : virtual A {
147     B();
148     ~B();
149   };
150 
B()151   B::B() {
152     union U { char x; int i; };
153     static U u = { 'a' };
154   }
155   // CHECK-LABEL: define void @_ZN5test31BC2Ev(
156   // CHECK-LABEL: define void @_ZN5test31BC1Ev(
157 }
158 
159 // We forgot to set the comdat when replacing the global with a different type.
160 namespace test4 {
161 struct HasVTable {
162   virtual void f();
163 };
useStaticLocal()164 inline HasVTable &useStaticLocal() {
165   static HasVTable obj;
166   return obj;
167 }
useit()168 void useit() {
169   useStaticLocal();
170 }
171 // CHECK: define linkonce_odr dereferenceable(8) %"struct.test4::HasVTable"* @_ZN5test414useStaticLocalEv()
172 // CHECK: ret %"struct.test4::HasVTable"* @_ZZN5test414useStaticLocalEvE3obj
173 }
174