Debian Codegraph @

clang/test/CodeGenCXX/attr-musttail.cpp

raw
1 // RUN: %clang_cc1 -fno-elide-constructors -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
2 // RUN: %clang_cc1 -fno-elide-constructors -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | opt -passes=verify
3 // FIXME: remove the call to "opt" once the tests are running the Clang verifier automatically again.
4
5 int Bar(int);
6 int Baz(int);
7
8 int Func1(int x) {
9 if (x) {
10 // CHECK: %call = musttail call noundef i32 @_Z3Bari(i32 noundef %1)
11 // CHECK-NEXT: ret i32 %call
12 [[clang::musttail]] return Bar(x);
13 } else {
14 [[clang::musttail]] return Baz(x); // CHECK: %call1 = musttail call noundef i32 @_Z3Bazi(i32 noundef %3)
15 }
16 }
17
18 int Func2(int x) {
19 {
20 [[clang::musttail]] return Bar(Bar(x));
21 }
22 }
23
24 // CHECK: %call1 = musttail call noundef i32 @_Z3Bari(i32 noundef %call)
25
26 class Foo {
27 public:
28 static int StaticMethod(int x);
29 int MemberFunction(int x);
30 int TailFrom(int x);
31 int TailFrom2(int x);
32 int TailFrom3(int x);
33 };
34
35 int Foo::TailFrom(int x) {
36 [[clang::musttail]] return MemberFunction(x);
37 }
38
39 // CHECK: %call = musttail call noundef i32 @_ZN3Foo14MemberFunctionEi(ptr noundef nonnull align 1 dereferenceable(1) %this1, i32 noundef %0)
40
41 int Func3(int x) {
42 [[clang::musttail]] return Foo::StaticMethod(x);
43 }
44
45 // CHECK: %call = musttail call noundef i32 @_ZN3Foo12StaticMethodEi(i32 noundef %0)
46
47 int Func4(int x) {
48 Foo foo; // Object with trivial destructor.
49 [[clang::musttail]] return foo.StaticMethod(x);
50 }
51
52 // CHECK: %call = musttail call noundef i32 @_ZN3Foo12StaticMethodEi(i32 noundef %0)
53
54 int (Foo::*pmf)(int);
55
56 int Foo::TailFrom2(int x) {
57 [[clang::musttail]] return ((*this).*pmf)(x);
58 }
59
60 // CHECK: %call = musttail call noundef i32 %5(ptr noundef nonnull align 1 dereferenceable(1) %1, i32 noundef %6)
61
62 int Foo::TailFrom3(int x) {
63 [[clang::musttail]] return (this->*pmf)(x);
64 }
65
66 // CHECK: %call = musttail call noundef i32 %5(ptr noundef nonnull align 1 dereferenceable(1) %1, i32 noundef %6)
67
68 void ReturnsVoid();
69
70 void Func5() {
71 [[clang::musttail]] return ReturnsVoid();
72 }
73
74 // CHECK: musttail call void @_Z11ReturnsVoidv()
75
76 class HasTrivialDestructor {};
77
78 int ReturnsInt(int x);
79
80 int Func6(int x) {
81 HasTrivialDestructor foo;
82 [[clang::musttail]] return ReturnsInt(x);
83 }
84
85 // CHECK: %call = musttail call noundef i32 @_Z10ReturnsInti(i32 noundef %0)
86
87 struct Data {
88 int (*fptr)(Data *);
89 };
90
91 int Func7(Data *data) {
92 [[clang::musttail]] return data->fptr(data);
93 }
94
95 // CHECK: %call = musttail call noundef i32 %1(ptr noundef %2)
96
97 template <class T>
98 T TemplateFunc(T) {
99 return 5;
100 }
101
102 int Func9(int x) {
103 [[clang::musttail]] return TemplateFunc<int>(x);
104 }
105
106 // CHECK: %call = musttail call noundef i32 @_Z12TemplateFuncIiET_S0_(i32 noundef %0)
107
108 template <class T>
109 int Func10(int x) {
110 T t;
111 [[clang::musttail]] return Bar(x);
112 }
113
114 int Func11(int x) {
115 return Func10<int>(x);
116 }
117
118 // CHECK: %call = musttail call noundef i32 @_Z3Bari(i32 noundef %0)
119
120 template <class T>
121 T Func12(T x) {
122 [[clang::musttail]] return ::Bar(x);
123 }
124
125 int Func13(int x) {
126 return Func12<int>(x);
127 }
128
129 // CHECK: %call = musttail call noundef i32 @_Z3Bari(i32 noundef %0)
130
131 int Func14(int x) {
132 int vla[x];
133 [[clang::musttail]] return Bar(x);
134 }
135
136 // CHECK: %call = musttail call noundef i32 @_Z3Bari(i32 noundef %3)
137
138 void TrivialDestructorParam(HasTrivialDestructor obj);
139
140 void Func14(HasTrivialDestructor obj) {
141 [[clang::musttail]] return TrivialDestructorParam(obj);
142 }
143
144 // CHECK: musttail call void @_Z22TrivialDestructorParam20HasTrivialDestructor()
145
146 struct Struct3 {
147 void ConstMemberFunction(const int *) const;
148 void NonConstMemberFunction(int *i);
149 };
150 void Struct3::NonConstMemberFunction(int *i) {
151 // The parameters are not identical, but they are compatible.
152 [[clang::musttail]] return ConstMemberFunction(i);
153 }
154
155 // CHECK: musttail call void @_ZNK7Struct319ConstMemberFunctionEPKi(ptr noundef nonnull align 1 dereferenceable(1) %this1, ptr noundef %0)
156
157 struct HasNonTrivialCopyConstructor {
158 HasNonTrivialCopyConstructor(const HasNonTrivialCopyConstructor &);
159 };
160 HasNonTrivialCopyConstructor ReturnsClassByValue();
161 HasNonTrivialCopyConstructor TestNonElidableCopyConstructor() {
162 [[clang::musttail]] return (((ReturnsClassByValue())));
163 }
164
165 // CHECK: musttail call void @_Z19ReturnsClassByValuev(ptr dead_on_unwind writable sret(%struct.HasNonTrivialCopyConstructor) align 1 %agg.result)
166
167 struct HasNonTrivialCopyConstructor2 {
168 // Copy constructor works even if it has extra default params.
169 HasNonTrivialCopyConstructor2(const HasNonTrivialCopyConstructor &, int DefaultParam = 5);
170 };
171 HasNonTrivialCopyConstructor2 ReturnsClassByValue2();
172 HasNonTrivialCopyConstructor2 TestNonElidableCopyConstructor2() {
173 [[clang::musttail]] return (((ReturnsClassByValue2())));
174 }
175
176 // CHECK: musttail call void @_Z20ReturnsClassByValue2v()
177
178 void TestFunctionPointer(int x) {
179 void (*p)(int) = nullptr;
180 [[clang::musttail]] return p(x);
181 }
182
183 // CHECK: musttail call void %0(i32 noundef %1)
184
185 struct LargeWithCopyConstructor {
186 LargeWithCopyConstructor(const LargeWithCopyConstructor &);
187 char data[32];
188 };
189 LargeWithCopyConstructor ReturnsLarge();
190 LargeWithCopyConstructor TestLargeWithCopyConstructor() {
191 [[clang::musttail]] return ReturnsLarge();
192 }
193
194 // CHECK: define dso_local void @_Z28TestLargeWithCopyConstructorv(ptr dead_on_unwind noalias writable sret(%struct.LargeWithCopyConstructor) align 1 %agg.result)
195 // CHECK: musttail call void @_Z12ReturnsLargev(ptr dead_on_unwind writable sret(%struct.LargeWithCopyConstructor) align 1 %agg.result)
196
197 using IntFunctionType = int();
198 IntFunctionType *ReturnsIntFunction();
199 int TestRValueFunctionPointer() {
200 [[clang::musttail]] return ReturnsIntFunction()();
201 }
202
203 // CHECK: musttail call noundef i32 %call()
204
205 void(FuncWithParens)() {
206 [[clang::musttail]] return FuncWithParens();
207 }
208
209 // CHECK: musttail call void @_Z14FuncWithParensv()
210
211 int TestNonCapturingLambda() {
212 auto lambda = []() { return 12; };
213 [[clang::musttail]] return (+lambda)();
214 }
215
216 // CHECK: %call = call noundef ptr @"_ZZ22TestNonCapturingLambdavENK3$_0cvPFivEEv"(ptr noundef nonnull align 1 dereferenceable(1) %lambda)
217 // CHECK: musttail call noundef i32 %call()
218
219 class TestVirtual {
220 virtual void TailTo();
221 virtual void TailFrom();
222 };
223
224 void TestVirtual::TailFrom() {
225 [[clang::musttail]] return TailTo();
226 }
227
228 // CHECK: musttail call void %0(ptr noundef nonnull align 8 dereferenceable(8) %this1)