Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck %s |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 2 | |
Anders Carlsson | 01a79ac | 2009-11-21 23:56:04 +0000 | [diff] [blame] | 3 | // CHECK: @a = global i32 10 |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 4 | int a = 10; |
John McCall | 8878686 | 2010-02-08 21:46:50 +0000 | [diff] [blame] | 5 | // CHECK: @ar = constant i32* @a |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 6 | int &ar = a; |
| 7 | |
| 8 | void f(); |
John McCall | 8878686 | 2010-02-08 21:46:50 +0000 | [diff] [blame] | 9 | // CHECK: @fr = constant void ()* @_Z1fv |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 10 | void (&fr)() = f; |
| 11 | |
| 12 | struct S { int& a; }; |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 13 | // CHECK: @s = global %struct.S { i32* @a } |
Anders Carlsson | 1e5dc6e | 2009-04-11 01:08:03 +0000 | [diff] [blame] | 14 | S s = { a }; |
| 15 | |
Anders Carlsson | 01a79ac | 2009-11-21 23:56:04 +0000 | [diff] [blame] | 16 | // PR5581 |
| 17 | namespace PR5581 { |
| 18 | class C { |
| 19 | public: |
| 20 | enum { e0, e1 }; |
| 21 | unsigned f; |
| 22 | }; |
| 23 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 24 | // CHECK: @_ZN6PR55812g0E = global %"class.PR5581::C" { i32 1 } |
Anders Carlsson | 01a79ac | 2009-11-21 23:56:04 +0000 | [diff] [blame] | 25 | C g0 = { C::e1 }; |
| 26 | } |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 27 | |
| 28 | namespace test2 { |
| 29 | struct A { |
| 30 | static const double d = 1.0; |
| 31 | static const float f = d / 2; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 32 | static int g(); |
| 33 | } a; |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 34 | |
John McCall | 90fb58e | 2010-10-09 02:28:39 +0000 | [diff] [blame] | 35 | // 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 Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 37 | // CHECK: @_ZN5test22t2E = global double* @_ZN5test21A1d |
| 38 | // CHECK: @_ZN5test22t3E = global {{.*}} @_ZN5test21A1g |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 39 | double t0 = A::d; |
| 40 | double t1[] = { A::d, A::f }; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 41 | const double *t2 = &a.d; |
| 42 | int (*t3)() = &a.g; |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 43 | } |
Eli Friedman | b000459 | 2011-10-24 22:25:55 +0000 | [diff] [blame] | 44 | |
| 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 |
| 47 | float PR9558 = reinterpret_cast<const float&>("asd"); |
Richard Smith | 4bb6686 | 2011-12-02 00:30:33 +0000 | [diff] [blame] | 48 | |
| 49 | // An initialized const automatic variable cannot be promoted to a constant |
| 50 | // global if it has a mutable member. |
| 51 | struct MutableMember { |
| 52 | mutable int n; |
| 53 | }; |
| 54 | int writeToMutable() { |
| 55 | // CHECK-NOT: {{.*}}MM{{.*}} = {{.*}}constant |
| 56 | const MutableMember MM = { 0 }; |
| 57 | return ++MM.n; |
| 58 | } |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 59 | |
| 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 |
| 67 | void UnfoldableAddrLabelDiff() { static __int128_t x = (long)&&a-(long)&&b; a:b:return;} |
| 68 | |
| 69 | // But make sure we do fold this. |
Eli Friedman | 0ad93a3 | 2012-01-04 23:41:09 +0000 | [diff] [blame] | 70 | // CHECK: @_ZZ21FoldableAddrLabelDiffvE1x = internal global i64 sub (i64 ptrtoint (i8* blockaddress(@_Z21FoldableAddrLabelDiffv |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 71 | void FoldableAddrLabelDiff() { static long x = (long)&&a-(long)&&b; a:b:return;} |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 72 | |
| 73 | // CHECK: @i = constant i32* bitcast (float* @PR9558 to i32*) |
| 74 | int &i = reinterpret_cast<int&>(PR9558); |
| 75 | |
| 76 | int arr[2]; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 77 | // CHECK: @pastEnd = constant i32* bitcast (i8* getelementptr (i8, i8* bitcast ([2 x i32]* @arr to i8*), i64 8) to i32*) |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 78 | int &pastEnd = arr[2]; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 79 | |
| 80 | struct X { |
| 81 | long n : 8; |
| 82 | }; |
| 83 | long k; |
| 84 | X x = {(long)&k}; |
| 85 | // CHECK: store i8 ptrtoint (i64* @k to i8), i8* getelementptr inbounds (%struct.X, %struct.X* @x, i32 0, i32 0) |