xref: /llvm-project/clang/test/CodeGenCXX/rvalue-references.cpp (revision 3512721d52b3380ea4d3f5b2419d0b7b072e7797)
1 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
2 
3 
4 struct Spacer { int x; };
5 struct A { double array[2]; };
6 struct B : Spacer, A { };
7 
8 B &getB();
9 
10 // CHECK-LABEL: define{{.*}} nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z4getAv()
11 // CHECK: call noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z4getBv()
12 // CHECK-NEXT: getelementptr inbounds i8, ptr
13 // CHECK-NEXT: ret ptr
getA()14 A &&getA() { return static_cast<A&&>(getB()); }
15 
16 int &getIntLValue();
17 int &&getIntXValue();
18 int getIntPRValue();
19 
20 // CHECK-LABEL: define{{.*}} nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z2f0v()
21 // CHECK: call noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z12getIntLValuev()
22 // CHECK-NEXT: ret ptr
f0()23 int &&f0() { return static_cast<int&&>(getIntLValue()); }
24 
25 // CHECK-LABEL: define{{.*}} nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z2f1v()
26 // CHECK: call noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z12getIntXValuev()
27 // CHECK-NEXT: ret ptr
f1()28 int &&f1() { return static_cast<int&&>(getIntXValue()); }
29 
30 // CHECK-LABEL: define{{.*}} nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z2f2v
31 // CHECK: call noundef i32 @_Z13getIntPRValuev()
32 // CHECK-NEXT: store i32 {{.*}}, ptr
33 // CHECK-NEXT: ret ptr
f2()34 int &&f2() { return static_cast<int&&>(getIntPRValue()); }
35 
36 bool ok;
37 
38 class C
39 {
40    int* state_;
41 
42    C(const C&) = delete;
43    C& operator=(const C&) = delete;
44 public:
C(int state)45   C(int state) : state_(new int(state)) { }
46 
C(C && a)47   C(C&& a) {
48     state_ = a.state_;
49     a.state_ = 0;
50   }
51 
~C()52   ~C() {
53     delete state_;
54     state_ = 0;
55   }
56 };
57 
58 C test();
59 
60 // CHECK-LABEL: define{{.*}} void @_Z15elide_copy_initv
elide_copy_init()61 void elide_copy_init() {
62   ok = false;
63   // CHECK: call void @_Z4testv
64   C a = test();
65   // CHECK-NEXT: call void @_ZN1CD1Ev
66   // CHECK-NEXT: ret void
67 }
68 
69 // CHECK-LABEL: define{{.*}} void @_Z16test_move_returnv
test_move_return()70 C test_move_return() {
71   // CHECK: call void @_ZN1CC1Ei
72   C a1(3);
73   // CHECK: call void @_ZN1CC1Ei
74   C a2(4);
75   if (ok)
76     // CHECK: call void @_ZN1CC1EOS_
77     return a1;
78   // CHECK: call void @_ZN1CC1EOS_
79   return a2;
80   // CHECK: call void @_ZN1CD1Ev
81   // CHECK: call void @_ZN1CD1Ev
82   //CHECK:  ret void
83 }
84 
85 // PR10800: don't crash
86 namespace test1 {
87   int &&move(int&);
88 
89   struct A { A(int); };
90   struct B {
91     A a;
92     B(int i);
93   };
94 
95   // CHECK-LABEL:    define{{.*}} void @_ZN5test11BC2Ei(
96   // CHECK:      [[T0:%.*]] = call noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_ZN5test14moveERi(
97   // CHECK-NEXT: [[T1:%.*]] = load i32, ptr [[T0]]
98   // CHECK-NEXT: call void @_ZN5test11AC1Ei({{.*}}, i32 noundef [[T1]])
99   // CHECK-NEXT: ret void
B(int i)100   B::B(int i) : a(move(i)) {}
101 }
102 
103 // PR11009
104 struct MoveConvertible {
105   operator int&& () const;
106 };
moveConstruct()107 void moveConstruct() {
108   (void)(int)MoveConvertible();
109 }
110