305 lines · cpp
1// RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s --check-prefix=PPC2// RUN: %clang_cc1 -mfloat-abi hard -triple armv7-unknown-linux-gnueabi -emit-llvm -o - %s | FileCheck %s --check-prefix=ARM323// RUN: %clang_cc1 -mfloat-abi hard -triple aarch64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s --check-prefix=ARM644// RUN: %clang_cc1 -mfloat-abi hard -triple x86_64-unknown-windows-gnu -emit-llvm -o - %s | FileCheck %s --check-prefix=X645// RUN: %clang_cc1 -mfloat-abi hard -triple aarch64-unknown-windows-msvc -emit-llvm -o - %s | FileCheck %s --check-prefix=WOA646 7#if defined(__x86_64__)8#define CC __attribute__((vectorcall))9#else10#define CC11#endif12 13// Test that C++ classes are correctly classified as homogeneous aggregates.14 15struct Base1 {16 int x;17};18struct Base2 {19 double x;20};21struct Base3 {22 double x;23};24struct D1 : Base1 { // non-homogeneous aggregate25 double y, z;26};27struct D2 : Base2 { // homogeneous aggregate28 double y, z;29};30struct D3 : Base1, Base2 { // non-homogeneous aggregate31 double y, z;32};33struct D4 : Base2, Base3 { // homogeneous aggregate34 double y, z;35};36 37struct I1 : Base2 {};38struct I2 : Base2 {};39struct I3 : Base2 {};40struct D5 : I1, I2, I3 {}; // homogeneous aggregate41 42// PPC: define{{.*}} void @_Z7func_D12D1(ptr dead_on_unwind noalias writable sret(%struct.D1) align 8 %agg.result, [3 x i64] %x.coerce)43// ARM32: define{{.*}} arm_aapcs_vfpcc void @_Z7func_D12D1(ptr dead_on_unwind noalias writable sret(%struct.D1) align 8 %agg.result, [3 x i64] %x.coerce)44// ARM64: define{{.*}} void @_Z7func_D12D1(ptr dead_on_unwind noalias writable sret(%struct.D1) align 8 %agg.result, ptr dead_on_return noundef %x)45// X64: define dso_local x86_vectorcallcc void @"\01_Z7func_D12D1@@24"(ptr dead_on_unwind noalias writable sret(%struct.D1) align 8 %agg.result, ptr dead_on_return noundef %x)46D1 CC func_D1(D1 x) { return x; }47 48// PPC: define{{.*}} [3 x double] @_Z7func_D22D2([3 x double] %x.coerce)49// ARM32: define{{.*}} arm_aapcs_vfpcc %struct.D2 @_Z7func_D22D2(%struct.D2 %x.coerce)50// ARM64: define{{.*}} %struct.D2 @_Z7func_D22D2([3 x double] alignstack(8) %x.coerce)51// X64: define dso_local x86_vectorcallcc %struct.D2 @"\01_Z7func_D22D2@@24"(%struct.D2 inreg %x.coerce)52D2 CC func_D2(D2 x) { return x; }53 54// PPC: define{{.*}} void @_Z7func_D32D3(ptr dead_on_unwind noalias writable sret(%struct.D3) align 8 %agg.result, [4 x i64] %x.coerce)55// ARM32: define{{.*}} arm_aapcs_vfpcc void @_Z7func_D32D3(ptr dead_on_unwind noalias writable sret(%struct.D3) align 8 %agg.result, [4 x i64] %x.coerce)56// ARM64: define{{.*}} void @_Z7func_D32D3(ptr dead_on_unwind noalias writable sret(%struct.D3) align 8 %agg.result, ptr dead_on_return noundef %x)57D3 CC func_D3(D3 x) { return x; }58 59// PPC: define{{.*}} [4 x double] @_Z7func_D42D4([4 x double] %x.coerce)60// ARM32: define{{.*}} arm_aapcs_vfpcc %struct.D4 @_Z7func_D42D4(%struct.D4 %x.coerce)61// ARM64: define{{.*}} %struct.D4 @_Z7func_D42D4([4 x double] alignstack(8) %x.coerce)62D4 CC func_D4(D4 x) { return x; }63 64D5 CC func_D5(D5 x) { return x; }65// PPC: define{{.*}} [3 x double] @_Z7func_D52D5([3 x double] %x.coerce)66// ARM32: define{{.*}} arm_aapcs_vfpcc %struct.D5 @_Z7func_D52D5(%struct.D5 %x.coerce)67 68// The C++ multiple inheritance expansion case is a little more complicated, so69// do some extra checking.70//71// ARM64-LABEL: define{{.*}} %struct.D5 @_Z7func_D52D5([3 x double] alignstack(8) %x.coerce)72// ARM64: store [3 x double] %x.coerce, ptr73 74void call_D5(D5 *p) {75 func_D5(*p);76}77 78// Check the call site.79//80// ARM64-LABEL: define{{.*}} void @_Z7call_D5P2D5(ptr noundef %p)81// ARM64: load [3 x double], ptr82// ARM64: call %struct.D5 @_Z7func_D52D5([3 x double] alignstack(8) %{{.*}})83 84struct Empty { };85struct Float1 { float x; };86struct Float2 { float y; };87struct HVAWithEmptyBase : Float1, Empty, Float2 { float z; };88 89// PPC: define{{.*}} void @_Z15with_empty_base16HVAWithEmptyBase([3 x float] %a.coerce)90// ARM64: define{{.*}} void @_Z15with_empty_base16HVAWithEmptyBase([3 x float] alignstack(8) %a.coerce)91// ARM32: define{{.*}} arm_aapcs_vfpcc void @_Z15with_empty_base16HVAWithEmptyBase(%struct.HVAWithEmptyBase %a.coerce)92void CC with_empty_base(HVAWithEmptyBase a) {}93 94// WOA64: define dso_local void @"?with_empty_base@@YAXUHVAWithEmptyBase@@@Z"([2 x i64] %{{.*}})95// X64: define dso_local x86_vectorcallcc void @"\01_Z15with_empty_base16HVAWithEmptyBase@@16"(%struct.HVAWithEmptyBase inreg %a.coerce)96 97struct HVAWithEmptyBitField : Float1, Float2 {98 int : 0; // Takes no space.99 float z;100};101 102// PPC: define{{.*}} void @_Z19with_empty_bitfield20HVAWithEmptyBitField([3 x float] %a.coerce)103// ARM64: define{{.*}} void @_Z19with_empty_bitfield20HVAWithEmptyBitField([3 x float] alignstack(8) %a.coerce)104// ARM32: define{{.*}} arm_aapcs_vfpcc void @_Z19with_empty_bitfield20HVAWithEmptyBitField(%struct.HVAWithEmptyBitField %a.coerce)105// X64: define dso_local x86_vectorcallcc void @"\01_Z19with_empty_bitfield20HVAWithEmptyBitField@@16"(%struct.HVAWithEmptyBitField inreg %a.coerce)106void CC with_empty_bitfield(HVAWithEmptyBitField a) {}107 108namespace pr47611 {109// MSVC on Arm includes "isCXX14Aggregate" as part of its definition of110// Homogeneous Floating-point Aggregate (HFA). Additionally, it has a different111// handling of C++14 aggregates, which can lead to confusion.112 113// Pod is a trivial HFA.114struct Pod {115 double b[2];116};117// Not an aggregate according to C++14 spec => not HFA according to MSVC.118struct NotCXX14Aggregate {119 NotCXX14Aggregate();120 Pod p;121};122// NotPod is a C++14 aggregate. But not HFA, because it contains123// NotCXX14Aggregate (which itself is not HFA because it's not a C++14124// aggregate).125struct NotPod {126 NotCXX14Aggregate x;127};128struct Empty {};129// A class with a base is returned using the sret calling convetion by MSVC.130struct HasEmptyBase : public Empty {131 double b[2];132};133struct HasPodBase : public Pod {};134// WOA64-LABEL: define dso_local %"struct.pr47611::Pod" @"?copy@pr47611@@YA?AUPod@1@PEAU21@@Z"(ptr noundef %x)135Pod copy(Pod *x) { return *x; } // MSVC: ldp d0,d1,[x0], Clang: ldp d0,d1,[x0]136// WOA64-LABEL: define dso_local void @"?copy@pr47611@@YA?AUNotCXX14Aggregate@1@PEAU21@@Z"(ptr dead_on_unwind inreg noalias writable sret(%"struct.pr47611::NotCXX14Aggregate") align 8 %agg.result, ptr noundef %x)137NotCXX14Aggregate copy(NotCXX14Aggregate *x) { return *x; } // MSVC: stp x8,x9,[x0], Clang: str q0,[x0]138// WOA64-LABEL: define dso_local [2 x i64] @"?copy@pr47611@@YA?AUNotPod@1@PEAU21@@Z"(ptr noundef %x)139NotPod copy(NotPod *x) { return *x; }140// WOA64-LABEL: define dso_local void @"?copy@pr47611@@YA?AUHasEmptyBase@1@PEAU21@@Z"(ptr dead_on_unwind inreg noalias writable sret(%"struct.pr47611::HasEmptyBase") align 8 %agg.result, ptr noundef %x)141HasEmptyBase copy(HasEmptyBase *x) { return *x; }142// WOA64-LABEL: define dso_local void @"?copy@pr47611@@YA?AUHasPodBase@1@PEAU21@@Z"(ptr dead_on_unwind inreg noalias writable sret(%"struct.pr47611::HasPodBase") align 8 %agg.result, ptr noundef %x)143HasPodBase copy(HasPodBase *x) { return *x; }144 145void call_copy_pod(Pod *pod) {146 *pod = copy(pod);147 // WOA64-LABEL: define dso_local void @"?call_copy_pod@pr47611@@YAXPEAUPod@1@@Z"148 // WOA64: %{{.*}} = call %"struct.pr47611::Pod" @"?copy@pr47611@@YA?AUPod@1@PEAU21@@Z"(ptr noundef %{{.*}})149}150 151void call_copy_notcxx14aggregate(NotCXX14Aggregate *notcxx14aggregate) {152 *notcxx14aggregate = copy(notcxx14aggregate);153 // WOA64-LABEL: define dso_local void @"?call_copy_notcxx14aggregate@pr47611@@YAXPEAUNotCXX14Aggregate@1@@Z"154 // WOA64: call void @"?copy@pr47611@@YA?AUNotCXX14Aggregate@1@PEAU21@@Z"(ptr dead_on_unwind inreg writable sret(%"struct.pr47611::NotCXX14Aggregate") align 8 %{{.*}}, ptr noundef %{{.*}})155}156 157void call_copy_notpod(NotPod *notPod) {158 *notPod = copy(notPod);159 // WOA64-LABEL: define dso_local void @"?call_copy_notpod@pr47611@@YAXPEAUNotPod@1@@Z"160 // WOA64: %{{.*}} = call [2 x i64] @"?copy@pr47611@@YA?AUNotPod@1@PEAU21@@Z"(ptr noundef %{{.*}})161}162 163void call_copy_hasemptybase(HasEmptyBase *hasEmptyBase) {164 *hasEmptyBase = copy(hasEmptyBase);165 // WOA64-LABEL: define dso_local void @"?call_copy_hasemptybase@pr47611@@YAXPEAUHasEmptyBase@1@@Z"166 // WOA64: call void @"?copy@pr47611@@YA?AUHasEmptyBase@1@PEAU21@@Z"(ptr dead_on_unwind inreg writable sret(%"struct.pr47611::HasEmptyBase") align 8 %{{.*}}, ptr noundef %{{.*}})167}168 169void call_copy_haspodbase(HasPodBase *hasPodBase) {170 *hasPodBase = copy(hasPodBase);171 // WOA64-LABEL: define dso_local void @"?call_copy_haspodbase@pr47611@@YAXPEAUHasPodBase@1@@Z"172 // WOA64: call void @"?copy@pr47611@@YA?AUHasPodBase@1@PEAU21@@Z"(ptr dead_on_unwind inreg writable sret(%"struct.pr47611::HasPodBase") align 8 %{{.*}}, ptr noundef %{{.*}})173}174} // namespace pr47611175 176namespace protected_member {177struct HFA {178 double x;179 double y;180protected:181 double z;182};183double foo(HFA v) { return v.x + v.y; }184// WOA64: define dso_local noundef double @"?foo@protected_member@@YANUHFA@1@@Z"([3 x double] %{{.*}})185}186namespace private_member {187struct HFA {188 double x;189 double y;190private:191 double z;192};193double foo(HFA v) { return v.x + v.y; }194// WOA64: define dso_local noundef double @"?foo@private_member@@YANUHFA@1@@Z"([3 x double] %{{.*}})195}196namespace polymorphic {197struct NonHFA {198 double x;199 double y;200 double z;201 virtual void f1();202};203double foo(NonHFA v) { return v.x + v.y; }204// WOA64: define dso_local noundef double @"?foo@polymorphic@@YANUNonHFA@1@@Z"(ptr dead_on_return noundef %{{.*}})205}206namespace trivial_copy_assignment {207struct HFA {208 double x;209 double y;210 double z;211 HFA &operator=(const HFA&) = default;212};213double foo(HFA v) { return v.x + v.y; }214// WOA64: define dso_local noundef double @"?foo@trivial_copy_assignment@@YANUHFA@1@@Z"([3 x double] %{{.*}})215}216namespace non_trivial_copy_assignment {217struct NonHFA {218 double x;219 double y;220 double z;221 NonHFA &operator=(const NonHFA&);222};223double foo(NonHFA v) { return v.x + v.y; }224// WOA64: define dso_local noundef double @"?foo@non_trivial_copy_assignment@@YANUNonHFA@1@@Z"(ptr dead_on_return noundef %{{.*}})225}226namespace user_provided_ctor {227struct HFA {228 double x;229 double y;230 double z;231 HFA(int);232};233double foo(HFA v) { return v.x + v.y; }234// WOA64: define dso_local noundef double @"?foo@user_provided_ctor@@YANUHFA@1@@Z"([3 x double] %{{.*}})235}236namespace trivial_dtor {237struct HFA {238 double x;239 double y;240 double z;241 ~HFA() = default;242};243double foo(HFA v) { return v.x + v.y; }244// WOA64: define dso_local noundef double @"?foo@trivial_dtor@@YANUHFA@1@@Z"([3 x double] %{{.*}})245}246namespace non_trivial_dtor {247struct NonHFA {248 double x;249 double y;250 double z;251 ~NonHFA();252};253double foo(NonHFA v) { return v.x + v.y; }254// WOA64: define dso_local noundef double @"?foo@non_trivial_dtor@@YANUNonHFA@1@@Z"(ptr dead_on_return noundef %{{.*}})255}256namespace non_empty_base {257struct non_empty_base { double d; };258struct HFA : non_empty_base {259 double x;260 double y;261 double z;262};263double foo(HFA v) { return v.x + v.y; }264// WOA64: define dso_local noundef double @"?foo@non_empty_base@@YANUHFA@1@@Z"([4 x double] %{{.*}})265}266namespace empty_field {267struct empty { };268struct NonHFA {269 double x;270 double y;271 double z;272 empty e;273};274double foo(NonHFA v) { return v.x + v.y; }275// WOA64: define dso_local noundef double @"?foo@empty_field@@YANUNonHFA@1@@Z"(ptr dead_on_return noundef %{{.*}})276}277namespace non_empty_field {278struct non_empty { double d; };279struct HFA {280 double x;281 double y;282 double z;283 non_empty e;284};285double foo(HFA v) { return v.x + v.y; }286// WOA64: define dso_local noundef double @"?foo@non_empty_field@@YANUHFA@1@@Z"([4 x double] %{{.*}})287}288 289namespace pr62223 {290// HVAs don't follow the normal rules for return values. That means they can291// have base classes, user-defined ctors, and protected/private members.292// (The same restrictions that apply to HVA arguments still apply.)293typedef double V __attribute((ext_vector_type(2)));294struct base { V v; };295struct test : base { test(double); protected: V v2;};296test f(test *x) { return *x; }297// WOA64: define dso_local %"struct.pr62223::test" @"?f@pr62223@@YA?AUtest@1@PEAU21@@Z"(ptr noundef %{{.*}})298 299// The above rule only apples to HVAs, not HFAs.300struct base2 { double v; };301struct test2 : base2 { test2(double); protected: double v2;};302test2 f(test2 *x) { return *x; }303// WOA64: define dso_local void @"?f@pr62223@@YA?AUtest2@1@PEAU21@@Z"(ptr dead_on_unwind inreg noalias writable sret(%"struct.pr62223::test2") align 8 %{{.*}}, ptr noundef %{{.*}})304}305