blob: deb923a708905ff663603d258405f4075fcf9e4f [file] [log] [blame]
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck %s
Anders Carlsson1e5dc6e2009-04-11 01:08:03 +00002
Anders Carlsson01a79ac2009-11-21 23:56:04 +00003// CHECK: @a = global i32 10
Anders Carlsson1e5dc6e2009-04-11 01:08:03 +00004int a = 10;
John McCall88786862010-02-08 21:46:50 +00005// CHECK: @ar = constant i32* @a
Anders Carlsson1e5dc6e2009-04-11 01:08:03 +00006int &ar = a;
7
8void f();
John McCall88786862010-02-08 21:46:50 +00009// CHECK: @fr = constant void ()* @_Z1fv
Anders Carlsson1e5dc6e2009-04-11 01:08:03 +000010void (&fr)() = f;
11
12struct S { int& a; };
Chris Lattnerc5cbb902011-06-20 04:01:35 +000013// CHECK: @s = global %struct.S { i32* @a }
Anders Carlsson1e5dc6e2009-04-11 01:08:03 +000014S s = { a };
15
Anders Carlsson01a79ac2009-11-21 23:56:04 +000016// PR5581
17namespace PR5581 {
18class C {
19public:
20 enum { e0, e1 };
21 unsigned f;
22};
23
Chris Lattnerc5cbb902011-06-20 04:01:35 +000024// CHECK: @_ZN6PR55812g0E = global %"class.PR5581::C" { i32 1 }
Anders Carlsson01a79ac2009-11-21 23:56:04 +000025C g0 = { C::e1 };
26}
John McCall189d6ef2010-10-09 01:34:31 +000027
28namespace test2 {
29 struct A {
30 static const double d = 1.0;
31 static const float f = d / 2;
Richard Smith1bf9a9e2011-11-12 22:28:03 +000032 static int g();
33 } a;
John McCall189d6ef2010-10-09 01:34:31 +000034
John McCall90fb58e2010-10-09 02:28:39 +000035 // CHECK: @_ZN5test22t0E = global double {{1\.0+e\+0+}}, align 8
36 // CHECK: @_ZN5test22t1E = global [2 x double] [double {{1\.0+e\+0+}}, double {{5\.0+e-0*}}1], align 16
Richard Smith1bf9a9e2011-11-12 22:28:03 +000037 // CHECK: @_ZN5test22t2E = global double* @_ZN5test21A1d
38 // CHECK: @_ZN5test22t3E = global {{.*}} @_ZN5test21A1g
John McCall189d6ef2010-10-09 01:34:31 +000039 double t0 = A::d;
40 double t1[] = { A::d, A::f };
Richard Smith1bf9a9e2011-11-12 22:28:03 +000041 const double *t2 = &a.d;
42 int (*t3)() = &a.g;
John McCall189d6ef2010-10-09 01:34:31 +000043}
Eli Friedmanb0004592011-10-24 22:25:55 +000044
45// We don't expect to fold this in the frontend, but make sure it doesn't crash.
46// CHECK: @PR9558 = global float 0.000000e+0
47float PR9558 = reinterpret_cast<const float&>("asd");
Richard Smith4bb66862011-12-02 00:30:33 +000048
49// An initialized const automatic variable cannot be promoted to a constant
50// global if it has a mutable member.
51struct MutableMember {
52 mutable int n;
53};
54int writeToMutable() {
55 // CHECK-NOT: {{.*}}MM{{.*}} = {{.*}}constant
56 const MutableMember MM = { 0 };
57 return ++MM.n;
58}
Eli Friedman65639282012-01-04 23:13:47 +000059
60// Make sure we don't try to fold this in the frontend; the backend can't
61// handle it.
62// CHECK: @PR11705 = global i128 0
63__int128_t PR11705 = (__int128_t)&PR11705;
64
65// Make sure we don't try to fold this either.
66// CHECK: @_ZZ23UnfoldableAddrLabelDiffvE1x = internal global i128 0
67void UnfoldableAddrLabelDiff() { static __int128_t x = (long)&&a-(long)&&b; a:b:return;}
68
69// But make sure we do fold this.
Eli Friedman0ad93a32012-01-04 23:41:09 +000070// CHECK: @_ZZ21FoldableAddrLabelDiffvE1x = internal global i64 sub (i64 ptrtoint (i8* blockaddress(@_Z21FoldableAddrLabelDiffv
Eli Friedman65639282012-01-04 23:13:47 +000071void FoldableAddrLabelDiff() { static long x = (long)&&a-(long)&&b; a:b:return;}
Richard Smith61e61622012-01-12 06:08:57 +000072
73// CHECK: @i = constant i32* bitcast (float* @PR9558 to i32*)
74int &i = reinterpret_cast<int&>(PR9558);
75
76int arr[2];
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070077// CHECK: @pastEnd = constant i32* bitcast (i8* getelementptr (i8, i8* bitcast ([2 x i32]* @arr to i8*), i64 8) to i32*)
Richard Smith61e61622012-01-12 06:08:57 +000078int &pastEnd = arr[2];
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070079
80struct X {
81 long n : 8;
82};
83long k;
84X x = {(long)&k};
85// CHECK: store i8 ptrtoint (i64* @k to i8), i8* getelementptr inbounds (%struct.X, %struct.X* @x, i32 0, i32 0)