1 // RUN: %clang_cc1 -fno-elide-constructors -S -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
2 // RUN: %clang_cc1 -fno-elide-constructors -S -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | opt -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 
Func1(int x)8 int Func1(int x) {
9   if (x) {
10     // CHECK: %call = musttail call i32 @_Z3Bari(i32 %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 i32 @_Z3Bazi(i32 %3)
15   }
16 }
17 
Func2(int x)18 int Func2(int x) {
19   {
20     [[clang::musttail]] return Bar(Bar(x));
21   }
22 }
23 
24 // CHECK: %call1 = musttail call i32 @_Z3Bari(i32 %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 
TailFrom(int x)35 int Foo::TailFrom(int x) {
36   [[clang::musttail]] return MemberFunction(x);
37 }
38 
39 // CHECK: %call = musttail call i32 @_ZN3Foo14MemberFunctionEi(%class.Foo* nonnull align 1 dereferenceable(1) %this1, i32 %0)
40 
Func3(int x)41 int Func3(int x) {
42   [[clang::musttail]] return Foo::StaticMethod(x);
43 }
44 
45 // CHECK: %call = musttail call i32 @_ZN3Foo12StaticMethodEi(i32 %0)
46 
Func4(int x)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 i32 @_ZN3Foo12StaticMethodEi(i32 %0)
53 
54 int (Foo::*pmf)(int);
55 
TailFrom2(int x)56 int Foo::TailFrom2(int x) {
57   [[clang::musttail]] return ((*this).*pmf)(x);
58 }
59 
60 // CHECK: %call = musttail call i32 %8(%class.Foo* nonnull align 1 dereferenceable(1) %this.adjusted, i32 %9)
61 
TailFrom3(int x)62 int Foo::TailFrom3(int x) {
63   [[clang::musttail]] return (this->*pmf)(x);
64 }
65 
66 // CHECK: %call = musttail call i32 %8(%class.Foo* nonnull align 1 dereferenceable(1) %this.adjusted, i32 %9)
67 
68 void ReturnsVoid();
69 
Func5()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 
Func6(int x)80 int Func6(int x) {
81   HasTrivialDestructor foo;
82   [[clang::musttail]] return ReturnsInt(x);
83 }
84 
85 // CHECK: %call = musttail call i32 @_Z10ReturnsInti(i32 %0)
86 
87 struct Data {
88   int (*fptr)(Data *);
89 };
90 
Func7(Data * data)91 int Func7(Data *data) {
92   [[clang::musttail]] return data->fptr(data);
93 }
94 
95 // CHECK: %call = musttail call i32 %1(%struct.Data* %2)
96 
97 template <class T>
TemplateFunc(T)98 T TemplateFunc(T) {
99   return 5;
100 }
101 
Func9(int x)102 int Func9(int x) {
103   [[clang::musttail]] return TemplateFunc<int>(x);
104 }
105 
106 // CHECK: %call = musttail call i32 @_Z12TemplateFuncIiET_S0_(i32 %0)
107 
108 template <class T>
Func10(int x)109 int Func10(int x) {
110   T t;
111   [[clang::musttail]] return Bar(x);
112 }
113 
Func11(int x)114 int Func11(int x) {
115   return Func10<int>(x);
116 }
117 
118 // CHECK: %call = musttail call i32 @_Z3Bari(i32 %0)
119 
120 template <class T>
Func12(T x)121 T Func12(T x) {
122   [[clang::musttail]] return ::Bar(x);
123 }
124 
Func13(int x)125 int Func13(int x) {
126   return Func12<int>(x);
127 }
128 
129 // CHECK: %call = musttail call i32 @_Z3Bari(i32 %0)
130 
Func14(int x)131 int Func14(int x) {
132   int vla[x];
133   [[clang::musttail]] return Bar(x);
134 }
135 
136 // CHECK: %call = musttail call i32 @_Z3Bari(i32 %3)
137 
138 void TrivialDestructorParam(HasTrivialDestructor obj);
139 
Func14(HasTrivialDestructor obj)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 };
NonConstMemberFunction(int * i)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(%struct.Struct3* nonnull align 1 dereferenceable(1) %this1, i32* %0)
156 
157 struct HasNonTrivialCopyConstructor {
158   HasNonTrivialCopyConstructor(const HasNonTrivialCopyConstructor &);
159 };
160 HasNonTrivialCopyConstructor ReturnsClassByValue();
TestNonElidableCopyConstructor()161 HasNonTrivialCopyConstructor TestNonElidableCopyConstructor() {
162   [[clang::musttail]] return (((ReturnsClassByValue())));
163 }
164 
165 // CHECK: musttail call void @_Z19ReturnsClassByValuev(%struct.HasNonTrivialCopyConstructor* 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();
TestNonElidableCopyConstructor2()172 HasNonTrivialCopyConstructor2 TestNonElidableCopyConstructor2() {
173   [[clang::musttail]] return (((ReturnsClassByValue2())));
174 }
175 
176 // CHECK: musttail call void @_Z20ReturnsClassByValue2v()
177 
TestFunctionPointer(int x)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 %1)
184 
185 struct LargeWithCopyConstructor {
186   LargeWithCopyConstructor(const LargeWithCopyConstructor &);
187   char data[32];
188 };
189 LargeWithCopyConstructor ReturnsLarge();
TestLargeWithCopyConstructor()190 LargeWithCopyConstructor TestLargeWithCopyConstructor() {
191   [[clang::musttail]] return ReturnsLarge();
192 }
193 
194 // CHECK: define dso_local void @_Z28TestLargeWithCopyConstructorv(%struct.LargeWithCopyConstructor* noalias sret(%struct.LargeWithCopyConstructor) align 1 %agg.result)
195 // CHECK: musttail call void @_Z12ReturnsLargev(%struct.LargeWithCopyConstructor* sret(%struct.LargeWithCopyConstructor) align 1 %agg.result)
196 
197 using IntFunctionType = int();
198 IntFunctionType *ReturnsIntFunction();
TestRValueFunctionPointer()199 int TestRValueFunctionPointer() {
200   [[clang::musttail]] return ReturnsIntFunction()();
201 }
202 
203 // CHECK: musttail call i32 %call()
204 
205 void(FuncWithParens)() {
206   [[clang::musttail]] return FuncWithParens();
207 }
208 
209 // CHECK: musttail call void @_Z14FuncWithParensv()
210 
TestNonCapturingLambda()211 int TestNonCapturingLambda() {
212   auto lambda = []() { return 12; };
213   [[clang::musttail]] return (+lambda)();
214 }
215 
216 // CHECK: %call = call i32 ()* @"_ZZ22TestNonCapturingLambdavENK3$_0cvPFivEEv"(%class.anon* nonnull align 1 dereferenceable(1) %lambda)
217 // CHECK: musttail call i32 %call()
218 
219 class TestVirtual {
220   virtual void TailTo();
221   virtual void TailFrom();
222 };
223 
TailFrom()224 void TestVirtual::TailFrom() {
225   [[clang::musttail]] return TailTo();
226 }
227 
228 // CHECK: musttail call void %1(%class.TestVirtual* nonnull align 8 dereferenceable(8) %this1)
229