[RISCV] Add shrinkwrap test cases showing gaps in current impl
[llvm-project.git] / clang / test / CodeGenCXX / references.cpp
blobb84cb788d161c3b8e3b810ef817cc8206d1a1881
1 // RUN: not %clang_cc1 -triple x86_64-apple-darwin -verify -emit-llvm -o - %s | FileCheck %s
2 void t1() {
3 // CHECK-LABEL: define{{.*}} void @_Z2t1v
4 // CHECK: [[REFLOAD:%.*]] = load ptr, ptr @a, align 8
5 // CHECK: load i32, ptr [[REFLOAD]], align 4
6 extern int& a;
7 int b = a;
10 void t2(int& a) {
11 // CHECK-LABEL: define{{.*}} void @_Z2t2Ri
12 // CHECK: [[REFLOAD2:%.*]] = load ptr, ptr {{.*}}, align 8
13 // CHECK: load i32, ptr [[REFLOAD2]], align 4
14 int b = a;
17 int g;
18 int& gr = g;
19 int& grr = gr;
20 void t3() {
21 int b = gr;
24 // Test reference binding.
26 struct C { int a; };
27 void f(const bool&);
28 void f(const int&);
29 void f(const _Complex int&);
30 void f(const C&);
32 C aggregate_return();
34 bool& bool_reference_return();
35 int& int_reference_return();
36 _Complex int& complex_int_reference_return();
37 C& aggregate_reference_return();
39 void test_bool() {
40 bool a = true;
41 f(a);
43 f(true);
45 bool_reference_return() = true;
46 a = bool_reference_return();
48 struct { const bool& b; } b = { true };
51 void test_scalar() {
52 int a = 10;
53 f(a);
55 struct { int bitfield : 3; } s = { 3 };
56 f(s.bitfield);
58 f(10);
60 __attribute((vector_size(16))) typedef int vec4;
61 f((vec4){1,2,3,4}[0]);
63 int_reference_return() = 10;
64 a = int_reference_return();
66 struct { const int& a; } agg = { 10 };
69 void test_complex() {
70 _Complex int a = 10i;
71 f(a);
73 f(10i);
75 complex_int_reference_return() = 10i;
76 a = complex_int_reference_return();
78 struct { const _Complex int &a; } agg = { 10i };
81 void test_aggregate() {
82 C c;
83 f(c);
85 f(aggregate_return());
86 aggregate_reference_return().a = 10;
88 c = aggregate_reference_return();
90 struct { const C& a; } agg = { C() };
93 int& reference_return() {
94 return g;
97 int reference_decl() {
98 int& a = g;
99 const int& b = 1;
100 return a+b;
103 struct A {
104 int& b();
107 void f(A* a) {
108 int b = a->b();
111 // PR5122
112 void *foo = 0;
113 void * const & kFoo = foo;
115 struct D : C { D(); ~D(); };
117 void h() {
118 // CHECK: call void @_ZN1DD1Ev
119 const C& c = D();
122 namespace T {
123 struct A {
124 A();
125 ~A();
128 struct B {
129 B();
130 ~B();
131 A f();
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
145 // PR5227.
146 namespace PR5227 {
147 void f(int &a) {
148 (a = 10) = 20;
152 // PR5590
153 struct s0;
154 struct s1 { struct s0 &s0; };
155 void f0(s1 a) { s1 b = a; }
157 // PR6024
158 // CHECK: @_Z2f2v()
159 // CHECK: alloca i32,
160 // CHECK-NEXT: store
161 // CHECK-NEXT: ret
162 const int &f2() { return 0; }
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
169 void test(const int& arg = foo) {
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 };
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();
186 struct P {
187 X first;
190 P getP();
192 // CHECK-LABEL: define{{.*}} void @_ZN2N21fEi
193 // CHECK: call void @_ZN2N24getPEv
194 // CHECK: store i32 17
195 // CHECK: call void @_ZN2N21PD1Ev
196 void f(int i) {
197 const X& xr = getP().first;
198 i = 17;
201 struct SpaceWaster {
202 int i, j;
205 struct ReallyHasX {
206 X x;
209 struct HasX : ReallyHasX { };
211 struct HasXContainer {
212 HasX has;
215 struct Y : SpaceWaster, HasXContainer { };
216 struct Z : SpaceWaster, Y { };
218 Z getZ();
220 // CHECK-LABEL: define{{.*}} void @_ZN2N21gEi
221 // CHECK: call void @_ZN2N24getZEv
222 // CHECK: {{getelementptr inbounds.*i64 16}}
223 // CHECK: store i32 19
224 // CHECK: call void @_ZN2N21ZD1Ev
225 // CHECK: ret void
226 void g(int i) {
227 const X &xr = getZ().has.x;
228 i = 19;
232 namespace N3 {
234 // PR7326
236 struct A {
237 explicit A(int);
238 ~A();
241 // CHECK-LABEL: define internal void @__cxx_global_var_init
242 // CHECK: call void @_ZN2N31AC1Ei(ptr {{[^,]*}} @_ZGRN2N35sA123E_, i32 noundef 123)
243 // CHECK: call i32 @__cxa_atexit
244 // CHECK: ret void
245 const A &sA123 = A(123);
248 namespace N4 {
250 struct A {
251 A();
252 ~A();
255 void f() {
256 // CHECK-LABEL: define{{.*}} void @_ZN2N41fEv
257 // CHECK: call void @_ZN2N41AC1Ev(ptr {{[^,]*}} @_ZGRZN2N41fEvE2ar_)
258 // CHECK: call i32 @__cxa_atexit
259 // CHECK: ret void
260 static const A& ar = A();
265 // PR9494
266 namespace N5 {
267 struct AnyS { bool b; };
268 void f(const bool&);
269 AnyS g();
270 void h() {
271 // CHECK: call i8 @_ZN2N51gEv()
272 // CHECK: call void @_ZN2N51fERKb(ptr
273 f(g().b);
277 // PR9565
278 namespace PR9565 {
279 struct a { int a : 10, b : 10; };
280 // CHECK-LABEL: define{{.*}} void @_ZN6PR95651fEv()
281 void f() {
282 // CHECK: call void @llvm.memcpy
283 a x = { 0, 0 };
284 // CHECK: [[WITH_SEVENTEEN:%[.a-zA-Z0-9]+]] = or i32 [[WITHOUT_SEVENTEEN:%[.a-zA-Z0-9]+]], 17
285 // CHECK: store i32 [[WITH_SEVENTEEN]], ptr [[XA:%[.a-zA-Z0-9]+]]
286 x.a = 17;
287 // CHECK-NEXT: load
288 // CHECK-NEXT: shl
289 // CHECK-NEXT: ashr
290 // CHECK-NEXT: store i32
291 // CHECK-NEXT: store ptr
292 const int &y = x.a;
293 // CHECK-NEXT: load
294 // CHECK-NEXT: and
295 // CHECK-NEXT: or i32 {{.*}}, 19456
296 // CHECK-NEXT: store i32
297 x.b = 19;
298 // CHECK-NEXT: ret void
302 namespace N6 {
303 extern struct x {char& x;}y;
304 int a() { return y.x; }
305 // CHECK-LABEL: define{{.*}} i32 @_ZN2N61aEv
306 // CHECK: [[REFLOAD3:%.*]] = load ptr, ptr @_ZN2N61yE, align 8
307 // CHECK: load i8, ptr [[REFLOAD3]], align 1