1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck %s
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -std=c++98 -o - %s | FileCheck %s
3 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -std=c++11 -o - %s | FileCheck %s
4 
5 // CHECK: @a = global i32 10
6 int a = 10;
7 // CHECK: @ar = constant i32* @a
8 int &ar = a;
9 
10 void f();
11 // CHECK: @fr = constant void ()* @_Z1fv
12 void (&fr)() = f;
13 
14 struct S { int& a; };
15 // CHECK: @s = global %struct.S { i32* @a }
16 S s = { a };
17 
18 // PR5581
19 namespace PR5581 {
20 class C {
21 public:
22   enum { e0, e1 };
23   unsigned f;
24 };
25 
26 // CHECK: @_ZN6PR55812g0E = global %"class.PR5581::C" { i32 1 }
27 C g0 = { C::e1 };
28 }
29 
30 namespace test2 {
31   struct A {
32 #if __cplusplus <= 199711L
33     static const double d = 1.0;
34     static const float f = d / 2;
35 #else
36     static constexpr double d = 1.0;
37     static constexpr float f = d / 2;
38 #endif
39     static int g();
40   } a;
41 
42   // CHECK: @_ZN5test22t0E = global double {{1\.0+e\+0+}}, align 8
43   // CHECK: @_ZN5test22t1E = global [2 x double] [double {{1\.0+e\+0+}}, double {{5\.0+e-0*}}1], align 16
44   // CHECK: @_ZN5test22t2E = global double* @_ZN5test21A1d
45   // CHECK: @_ZN5test22t3E = global {{.*}} @_ZN5test21A1g
46   double t0 = A::d;
47   double t1[] = { A::d, A::f };
48   const double *t2 = &a.d;
49   int (*t3)() = &a.g;
50 }
51 
52 // We don't expect to fold this in the frontend, but make sure it doesn't crash.
53 // CHECK: @PR9558 = global float 0.000000e+0
54 float PR9558 = reinterpret_cast<const float&>("asd");
55 
56 // An initialized const automatic variable cannot be promoted to a constant
57 // global if it has a mutable member.
58 struct MutableMember {
59   mutable int n;
60 };
61 int writeToMutable() {
62   // CHECK-NOT: {{.*}}MM{{.*}} = {{.*}}constant
63   const MutableMember MM = { 0 };
64   return ++MM.n;
65 }
66 
67 // Make sure we don't try to fold this in the frontend; the backend can't
68 // handle it.
69 // CHECK: @PR11705 = global i128 0
70 __int128_t PR11705 = (__int128_t)&PR11705;
71 
72 // Make sure we don't try to fold this either.
73 // CHECK: @_ZZ23UnfoldableAddrLabelDiffvE1x = internal global i128 0
74 void UnfoldableAddrLabelDiff() { static __int128_t x = (long)&&a-(long)&&b; a:b:return;}
75 
76 // But make sure we do fold this.
77 // CHECK: @_ZZ21FoldableAddrLabelDiffvE1x = internal global i64 sub (i64 ptrtoint (i8* blockaddress(@_Z21FoldableAddrLabelDiffv
78 void FoldableAddrLabelDiff() { static long x = (long)&&a-(long)&&b; a:b:return;}
79 
80 // CHECK: @i = constant i32* bitcast (float* @PR9558 to i32*)
81 int &i = reinterpret_cast<int&>(PR9558);
82 
83 int arr[2];
84 // CHECK: @pastEnd = constant i32* bitcast (i8* getelementptr (i8, i8* bitcast ([2 x i32]* @arr to i8*), i64 8) to i32*)
85 int &pastEnd = arr[2];
86 
87 struct X {
88   long n : 8;
89 };
90 long k;
91 X x = {(long)&k};
92 // CHECK: store i8 ptrtoint (i64* @k to i8), i8* getelementptr inbounds (%struct.X, %struct.X* @x, i32 0, i32 0)
93