229 lines · cpp
1// RUN: %clang_cc1 -fno-elide-constructors -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | FileCheck %s2// RUN: %clang_cc1 -fno-elide-constructors -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | opt -passes=verify3// FIXME: remove the call to "opt" once the tests are running the Clang verifier automatically again.4 5int Bar(int);6int Baz(int);7 8int Func1(int x) {9 if (x) {10 // CHECK: %call = musttail call noundef i32 @_Z3Bari(i32 noundef %1)11 // CHECK-NEXT: ret i32 %call12 [[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 18int 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 26class Foo {27public: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 35int 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 41int Func3(int x) {42 [[clang::musttail]] return Foo::StaticMethod(x);43}44 45// CHECK: %call = musttail call noundef i32 @_ZN3Foo12StaticMethodEi(i32 noundef %0)46 47int 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 54int (Foo::*pmf)(int);55 56int 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 62int 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 68void ReturnsVoid();69 70void Func5() {71 [[clang::musttail]] return ReturnsVoid();72}73 74// CHECK: musttail call void @_Z11ReturnsVoidv()75 76class HasTrivialDestructor {};77 78int ReturnsInt(int x);79 80int 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 87struct Data {88 int (*fptr)(Data *);89};90 91int Func7(Data *data) {92 [[clang::musttail]] return data->fptr(data);93}94 95// CHECK: %call = musttail call noundef i32 %1(ptr noundef %2)96 97template <class T>98T TemplateFunc(T) {99 return 5;100}101 102int 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 108template <class T>109int Func10(int x) {110 T t;111 [[clang::musttail]] return Bar(x);112}113 114int Func11(int x) {115 return Func10<int>(x);116}117 118// CHECK: %call = musttail call noundef i32 @_Z3Bari(i32 noundef %0)119 120template <class T>121T Func12(T x) {122 [[clang::musttail]] return ::Bar(x);123}124 125int Func13(int x) {126 return Func12<int>(x);127}128 129// CHECK: %call = musttail call noundef i32 @_Z3Bari(i32 noundef %0)130 131int 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 138void TrivialDestructorParam(HasTrivialDestructor obj);139 140void Func14(HasTrivialDestructor obj) {141 [[clang::musttail]] return TrivialDestructorParam(obj);142}143 144// CHECK: musttail call void @_Z22TrivialDestructorParam20HasTrivialDestructor()145 146struct Struct3 {147 void ConstMemberFunction(const int *) const;148 void NonConstMemberFunction(int *i);149};150void 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 157struct HasNonTrivialCopyConstructor {158 HasNonTrivialCopyConstructor(const HasNonTrivialCopyConstructor &);159};160HasNonTrivialCopyConstructor ReturnsClassByValue();161HasNonTrivialCopyConstructor 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 167struct HasNonTrivialCopyConstructor2 {168 // Copy constructor works even if it has extra default params.169 HasNonTrivialCopyConstructor2(const HasNonTrivialCopyConstructor &, int DefaultParam = 5);170};171HasNonTrivialCopyConstructor2 ReturnsClassByValue2();172HasNonTrivialCopyConstructor2 TestNonElidableCopyConstructor2() {173 [[clang::musttail]] return (((ReturnsClassByValue2())));174}175 176// CHECK: musttail call void @_Z20ReturnsClassByValue2v()177 178void 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 185struct LargeWithCopyConstructor {186 LargeWithCopyConstructor(const LargeWithCopyConstructor &);187 char data[32];188};189LargeWithCopyConstructor ReturnsLarge();190LargeWithCopyConstructor 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 197using IntFunctionType = int();198IntFunctionType *ReturnsIntFunction();199int TestRValueFunctionPointer() {200 [[clang::musttail]] return ReturnsIntFunction()();201}202 203// CHECK: musttail call noundef i32 %call()204 205void(FuncWithParens)() {206 [[clang::musttail]] return FuncWithParens();207}208 209// CHECK: musttail call void @_Z14FuncWithParensv()210 211int 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 219class TestVirtual {220 virtual void TailTo();221 virtual void TailFrom();222};223 224void TestVirtual::TailFrom() {225 [[clang::musttail]] return TailTo();226}227 228// CHECK: musttail call void %0(ptr noundef nonnull align 8 dereferenceable(8) %this1)229