Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -emit-llvm -o - %s | FileCheck %s |
Anders Carlsson | c8667a8 | 2009-05-19 20:40:02 +0000 | [diff] [blame] | 2 | void t1() { |
Eli Friedman | 2f77b3d | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 3 | // CHECK: define void @_Z2t1v |
| 4 | // CHECK: [[REFLOAD:%.*]] = load i32** @a, align 8 |
| 5 | // CHECK: load i32* [[REFLOAD]], align 4 |
Anders Carlsson | c8667a8 | 2009-05-19 20:40:02 +0000 | [diff] [blame] | 6 | extern int& a; |
| 7 | int b = a; |
| 8 | } |
| 9 | |
| 10 | void t2(int& a) { |
Eli Friedman | 2f77b3d | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 11 | // CHECK: define void @_Z2t2Ri |
| 12 | // CHECK: [[REFLOAD2:%.*]] = load i32** {{.*}}, align 8 |
| 13 | // CHECK: load i32* [[REFLOAD2]], align 4 |
Anders Carlsson | c8667a8 | 2009-05-19 20:40:02 +0000 | [diff] [blame] | 14 | int b = a; |
| 15 | } |
| 16 | |
| 17 | int g; |
| 18 | int& gr = g; |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 19 | int& grr = gr; |
Anders Carlsson | c8667a8 | 2009-05-19 20:40:02 +0000 | [diff] [blame] | 20 | void t3() { |
| 21 | int b = gr; |
| 22 | } |
Anders Carlsson | 4bbab92 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 23 | |
| 24 | // Test reference binding. |
| 25 | |
Anders Carlsson | 68ea78a | 2009-05-27 01:46:48 +0000 | [diff] [blame] | 26 | struct C { int a; }; |
Anders Carlsson | e04d1c7 | 2009-05-20 01:03:17 +0000 | [diff] [blame] | 27 | void f(const bool&); |
Anders Carlsson | 4bbab92 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 28 | void f(const int&); |
| 29 | void f(const _Complex int&); |
| 30 | void f(const C&); |
| 31 | |
Anders Carlsson | 4826568 | 2009-05-27 01:45:47 +0000 | [diff] [blame] | 32 | C aggregate_return(); |
| 33 | |
| 34 | bool& bool_reference_return(); |
| 35 | int& int_reference_return(); |
| 36 | _Complex int& complex_int_reference_return(); |
Anders Carlsson | 68ea78a | 2009-05-27 01:46:48 +0000 | [diff] [blame] | 37 | C& aggregate_reference_return(); |
Eli Friedman | 5df0d42 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 38 | |
Anders Carlsson | e04d1c7 | 2009-05-20 01:03:17 +0000 | [diff] [blame] | 39 | void test_bool() { |
| 40 | bool a = true; |
| 41 | f(a); |
| 42 | |
| 43 | f(true); |
Anders Carlsson | 4826568 | 2009-05-27 01:45:47 +0000 | [diff] [blame] | 44 | |
| 45 | bool_reference_return() = true; |
Anders Carlsson | e9f2f45 | 2009-05-27 03:37:57 +0000 | [diff] [blame] | 46 | a = bool_reference_return(); |
Anders Carlsson | e78ccb4 | 2010-02-03 19:13:55 +0000 | [diff] [blame] | 47 | |
| 48 | struct { const bool& b; } b = { true }; |
Anders Carlsson | e04d1c7 | 2009-05-20 01:03:17 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Anders Carlsson | 4bbab92 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 51 | void test_scalar() { |
| 52 | int a = 10; |
Anders Carlsson | 4bbab92 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 53 | f(a); |
Anders Carlsson | e04d1c7 | 2009-05-20 01:03:17 +0000 | [diff] [blame] | 54 | |
Anders Carlsson | 38d068e | 2009-05-20 01:24:22 +0000 | [diff] [blame] | 55 | struct { int bitfield : 3; } s = { 3 }; |
Anders Carlsson | 9f75e6b | 2009-05-20 01:27:39 +0000 | [diff] [blame] | 56 | f(s.bitfield); |
Anders Carlsson | 38d068e | 2009-05-20 01:24:22 +0000 | [diff] [blame] | 57 | |
Anders Carlsson | e04d1c7 | 2009-05-20 01:03:17 +0000 | [diff] [blame] | 58 | f(10); |
Eli Friedman | 5df0d42 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 59 | |
| 60 | __attribute((vector_size(16))) typedef int vec4; |
| 61 | f((vec4){1,2,3,4}[0]); |
Anders Carlsson | 4826568 | 2009-05-27 01:45:47 +0000 | [diff] [blame] | 62 | |
| 63 | int_reference_return() = 10; |
Anders Carlsson | e9f2f45 | 2009-05-27 03:37:57 +0000 | [diff] [blame] | 64 | a = int_reference_return(); |
Anders Carlsson | e78ccb4 | 2010-02-03 19:13:55 +0000 | [diff] [blame] | 65 | |
| 66 | struct { const int& a; } agg = { 10 }; |
Anders Carlsson | 4bbab92 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void test_complex() { |
| 70 | _Complex int a = 10i; |
Anders Carlsson | 4bbab92 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 71 | f(a); |
Anders Carlsson | 7cd3a64 | 2009-05-20 01:35:03 +0000 | [diff] [blame] | 72 | |
| 73 | f(10i); |
Anders Carlsson | 4826568 | 2009-05-27 01:45:47 +0000 | [diff] [blame] | 74 | |
| 75 | complex_int_reference_return() = 10i; |
Anders Carlsson | e9f2f45 | 2009-05-27 03:37:57 +0000 | [diff] [blame] | 76 | a = complex_int_reference_return(); |
Anders Carlsson | e78ccb4 | 2010-02-03 19:13:55 +0000 | [diff] [blame] | 77 | |
| 78 | struct { const _Complex int &a; } agg = { 10i }; |
Anders Carlsson | 4bbab92 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void test_aggregate() { |
| 82 | C c; |
Anders Carlsson | 4bbab92 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 83 | f(c); |
Eli Friedman | 5df0d42 | 2009-05-20 02:31:19 +0000 | [diff] [blame] | 84 | |
Anders Carlsson | 4826568 | 2009-05-27 01:45:47 +0000 | [diff] [blame] | 85 | f(aggregate_return()); |
Anders Carlsson | 68ea78a | 2009-05-27 01:46:48 +0000 | [diff] [blame] | 86 | aggregate_reference_return().a = 10; |
Anders Carlsson | e70e8f7 | 2009-05-27 16:45:02 +0000 | [diff] [blame] | 87 | |
| 88 | c = aggregate_reference_return(); |
Anders Carlsson | e78ccb4 | 2010-02-03 19:13:55 +0000 | [diff] [blame] | 89 | |
| 90 | struct { const C& a; } agg = { C() }; |
Anders Carlsson | 4bbab92 | 2009-05-20 00:36:58 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Eli Friedman | d54b6ac | 2009-05-27 04:56:12 +0000 | [diff] [blame] | 93 | int& reference_return() { |
| 94 | return g; |
| 95 | } |
Eli Friedman | 4a18784 | 2009-05-27 05:39:06 +0000 | [diff] [blame] | 96 | |
| 97 | int reference_decl() { |
| 98 | int& a = g; |
| 99 | const int& b = 1; |
| 100 | return a+b; |
| 101 | } |
Anders Carlsson | faf8664 | 2009-09-01 21:18:52 +0000 | [diff] [blame] | 102 | |
| 103 | struct A { |
| 104 | int& b(); |
| 105 | }; |
| 106 | |
| 107 | void f(A* a) { |
| 108 | int b = a->b(); |
| 109 | } |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 110 | |
| 111 | // PR5122 |
| 112 | void *foo = 0; |
| 113 | void * const & kFoo = foo; |
| 114 | |
Anders Carlsson | b3f7442 | 2009-10-15 00:51:46 +0000 | [diff] [blame] | 115 | struct D : C { D(); ~D(); }; |
| 116 | |
| 117 | void h() { |
| 118 | // CHECK: call void @_ZN1DD1Ev |
| 119 | const C& c = D(); |
| 120 | } |
| 121 | |
Anders Carlsson | e1b7ea1 | 2009-10-18 23:09:21 +0000 | [diff] [blame] | 122 | namespace T { |
| 123 | struct A { |
| 124 | A(); |
| 125 | ~A(); |
| 126 | }; |
| 127 | |
| 128 | struct B { |
| 129 | B(); |
| 130 | ~B(); |
| 131 | A f(); |
| 132 | }; |
| 133 | |
| 134 | void f() { |
| 135 | // CHECK: call void @_ZN1T1BC1Ev |
| 136 | // CHECK: call void @_ZN1T1B1fEv |
| 137 | // CHECK: call void @_ZN1T1BD1Ev |
| 138 | const A& a = B().f(); |
| 139 | // CHECK: call void @_ZN1T1fEv |
| 140 | f(); |
| 141 | // CHECK: call void @_ZN1T1AD1Ev |
| 142 | } |
| 143 | } |
| 144 | |
Anders Carlsson | 86aa0cd | 2009-10-19 18:28:22 +0000 | [diff] [blame] | 145 | // PR5227. |
| 146 | namespace PR5227 { |
| 147 | void f(int &a) { |
| 148 | (a = 10) = 20; |
| 149 | } |
| 150 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 151 | |
| 152 | // PR5590 |
| 153 | struct s0; |
| 154 | struct s1 { struct s0 &s0; }; |
| 155 | void f0(s1 a) { s1 b = a; } |
Douglas Gregor | 33fd1fc | 2010-03-24 23:14:04 +0000 | [diff] [blame] | 156 | |
| 157 | // PR6024 |
| 158 | // CHECK: @_Z2f2v() |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 159 | // CHECK: alloca i32, |
| 160 | // CHECK-NEXT: store |
| 161 | // CHECK-NEXT: ret |
Douglas Gregor | 33fd1fc | 2010-03-24 23:14:04 +0000 | [diff] [blame] | 162 | const int &f2() { return 0; } |
Chandler Carruth | 761c94e | 2010-05-16 09:32:51 +0000 | [diff] [blame] | 163 | |
| 164 | // Don't constant fold const reference parameters with default arguments to |
| 165 | // their default arguments. |
| 166 | namespace N1 { |
| 167 | const int foo = 1; |
| 168 | // CHECK: @_ZN2N14test |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 169 | void test(const int& arg = foo) { |
Chandler Carruth | 761c94e | 2010-05-16 09:32:51 +0000 | [diff] [blame] | 170 | // Ensure this array is on the stack where we can set values instead of |
| 171 | // being a global constant. |
| 172 | // CHECK: %args_array = alloca |
| 173 | const int* const args_array[] = { &arg }; |
| 174 | } |
| 175 | } |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 176 | |
| 177 | // Bind to subobjects while extending the life of the complete object. |
| 178 | namespace N2 { |
| 179 | class X { |
| 180 | public: |
| 181 | X(const X&); |
| 182 | X &operator=(const X&); |
| 183 | ~X(); |
| 184 | }; |
| 185 | |
| 186 | struct P { |
| 187 | X first; |
| 188 | }; |
| 189 | |
| 190 | P getP(); |
| 191 | |
| 192 | // CHECK: define void @_ZN2N21fEi |
| 193 | // CHECK: call void @_ZN2N24getPEv |
| 194 | // CHECK: getelementptr inbounds |
| 195 | // CHECK: store i32 17 |
| 196 | // CHECK: call void @_ZN2N21PD1Ev |
| 197 | void f(int i) { |
| 198 | const X& xr = getP().first; |
| 199 | i = 17; |
| 200 | } |
| 201 | |
| 202 | struct SpaceWaster { |
| 203 | int i, j; |
| 204 | }; |
| 205 | |
| 206 | struct ReallyHasX { |
| 207 | X x; |
| 208 | }; |
| 209 | |
| 210 | struct HasX : ReallyHasX { }; |
| 211 | |
| 212 | struct HasXContainer { |
| 213 | HasX has; |
| 214 | }; |
| 215 | |
| 216 | struct Y : SpaceWaster, HasXContainer { }; |
| 217 | struct Z : SpaceWaster, Y { }; |
| 218 | |
| 219 | Z getZ(); |
| 220 | |
| 221 | // CHECK: define void @_ZN2N21gEi |
| 222 | // CHECK: call void @_ZN2N24getZEv |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 223 | // CHECK: {{getelementptr inbounds.*i32 0, i32 0}} |
| 224 | // CHECK: {{getelementptr inbounds.*i32 0, i32 0}} |
| 225 | // CHECK: store i32 19 |
| 226 | // CHECK: call void @_ZN2N21ZD1Ev |
| 227 | // CHECK: ret void |
Douglas Gregor | 60dcb84 | 2010-05-20 08:36:28 +0000 | [diff] [blame] | 228 | void g(int i) { |
| 229 | const X &xr = getZ().has.x; |
| 230 | i = 19; |
| 231 | } |
| 232 | } |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 233 | |
| 234 | namespace N3 { |
| 235 | |
| 236 | // PR7326 |
| 237 | |
| 238 | struct A { |
| 239 | explicit A(int); |
| 240 | ~A(); |
| 241 | }; |
| 242 | |
| 243 | // CHECK: define internal void @__cxx_global_var_init |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 244 | // CHECK: call void @_ZN2N31AC1Ei(%"struct.N3::A"* @_ZGRN2N35sA123E, i32 123) |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 245 | // CHECK: call i32 @__cxa_atexit |
| 246 | // CHECK: ret void |
| 247 | const A &sA123 = A(123); |
| 248 | } |
| 249 | |
| 250 | namespace N4 { |
| 251 | |
| 252 | struct A { |
| 253 | A(); |
| 254 | ~A(); |
| 255 | }; |
| 256 | |
| 257 | void f() { |
| 258 | // CHECK: define void @_ZN2N41fEv |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 259 | // CHECK: call void @_ZN2N41AC1Ev(%"struct.N4::A"* @_ZGRZN2N41fEvE2ar) |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 260 | // CHECK: call i32 @__cxa_atexit |
| 261 | // CHECK: ret void |
| 262 | static const A& ar = A(); |
| 263 | |
| 264 | } |
| 265 | } |
| 266 | |
Eli Friedman | 545aa7a | 2011-03-16 22:34:09 +0000 | [diff] [blame] | 267 | // PR9494 |
| 268 | namespace N5 { |
| 269 | struct AnyS { bool b; }; |
| 270 | void f(const bool&); |
| 271 | AnyS g(); |
| 272 | void h() { |
| 273 | // CHECK: call i8 @_ZN2N51gEv() |
| 274 | // CHECK: call void @_ZN2N51fERKb(i8* |
| 275 | f(g().b); |
| 276 | } |
| 277 | } |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 278 | |
| 279 | // PR9565 |
| 280 | namespace PR9565 { |
| 281 | struct a { int a : 10, b : 10; }; |
| 282 | // CHECK: define void @_ZN6PR95651fEv() |
| 283 | void f() { |
| 284 | // CHECK: call void @llvm.memcpy |
| 285 | a x = { 0, 0 }; |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 286 | // CHECK: [[WITH_SEVENTEEN:%[.a-zA-Z0-9]+]] = or i32 [[WITHOUT_SEVENTEEN:%[.a-zA-Z0-9]+]], 17 |
| 287 | // CHECK: store i32 [[WITH_SEVENTEEN]], i32* [[XA:%[.a-zA-Z0-9]+]] |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 288 | x.a = 17; |
| 289 | // CHECK-NEXT: bitcast |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 290 | // CHECK-NEXT: load |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 291 | // CHECK-NEXT: shl |
| 292 | // CHECK-NEXT: ashr |
| 293 | // CHECK-NEXT: store i32 |
| 294 | // CHECK-NEXT: store i32* |
| 295 | const int &y = x.a; |
| 296 | // CHECK-NEXT: bitcast |
| 297 | // CHECK-NEXT: load |
| 298 | // CHECK-NEXT: and |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 299 | // CHECK-NEXT: or i32 {{.*}}, 19456 |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 300 | // CHECK-NEXT: store i32 |
| 301 | x.b = 19; |
| 302 | // CHECK-NEXT: ret void |
| 303 | } |
| 304 | } |
Eli Friedman | 2f77b3d | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 305 | |
| 306 | namespace N6 { |
| 307 | extern struct x {char& x;}y; |
| 308 | int a() { return y.x; } |
| 309 | // CHECK: define i32 @_ZN2N61aEv |
| 310 | // CHECK: [[REFLOAD3:%.*]] = load i8** getelementptr inbounds (%"struct.N6::x"* @_ZN2N61yE, i32 0, i32 0), align 8 |
Eli Friedman | 3238059 | 2011-11-16 02:19:03 +0000 | [diff] [blame] | 311 | // CHECK: load i8* [[REFLOAD3]], align 1 |
Eli Friedman | 2f77b3d | 2011-11-16 00:42:57 +0000 | [diff] [blame] | 312 | } |