504 lines · cpp
1// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=i386-pc-linux | FileCheck -check-prefix LINUX %s2// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=i386-pc-win32 -mconstructor-aliases -fno-rtti | FileCheck -check-prefix WIN32 --check-prefix WIN %s3// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=thumb-pc-win32 -mconstructor-aliases -fno-rtti | FileCheck -check-prefix WOA --check-prefix WIN %s4// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=x86_64-pc-win32 -mconstructor-aliases -fno-rtti | FileCheck -check-prefix WIN64 --check-prefix WIN %s5// RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple=aarch64-windows-msvc -mconstructor-aliases -fno-rtti | FileCheck -check-prefix WOA64 --check-prefix WIN %s6 7struct Empty {};8 9struct EmptyWithCtor {10 EmptyWithCtor() {}11};12 13struct Small {14 int x;15};16 17// This is a C++11 trivial and standard-layout struct but not a C++03 POD.18struct SmallCpp11NotCpp03Pod : Empty {19 int x;20};21 22struct SmallWithCtor {23 SmallWithCtor() {}24 int x;25};26 27struct Multibyte {28 char a, b, c, d;29};30 31struct Packed {32 short a;33 int b;34 short c;35};36 37struct SmallWithDtor {38 SmallWithDtor();39 ~SmallWithDtor();40 int x;41};42 43struct SmallWithVftable {44 int x;45 virtual void foo();46};47 48struct Medium {49 int x, y;50};51 52struct MediumWithCopyCtor {53 MediumWithCopyCtor();54 MediumWithCopyCtor(const struct MediumWithCopyCtor &);55 int x, y;56};57 58struct Big {59 int a, b, c, d, e, f;60};61 62struct BigWithDtor {63 BigWithDtor();64 ~BigWithDtor();65 int a, b, c, d, e, f;66};67 68struct BaseNoByval : Small {69 int bb;70};71 72struct SmallWithPrivate {73private:74 int i;75};76 77struct SmallWithSmallWithPrivate {78 SmallWithPrivate p;79};80 81// WIN32: declare dso_local void @"{{.*take_bools_and_chars.*}}"82// WIN32: (ptr inalloca(<{ i8, [3 x i8], i8, [3 x i8], %struct.SmallWithDtor, i8, [3 x i8], i8, [3 x i8], i32, i8, [3 x i8] }>)83void take_bools_and_chars(char a, char b, SmallWithDtor c, char d, bool e, int f, bool g);84void call_bools_and_chars() {85 take_bools_and_chars('A', 'B', SmallWithDtor(), 'D', true, 13, false);86}87 88// Returning structs that fit into a register.89Small small_return() { return Small(); }90// LINUX-LABEL: define{{.*}} void @_Z12small_returnv(ptr dead_on_unwind noalias writable sret(%struct.Small) align 4 %agg.result)91// WIN32: define dso_local i32 @"?small_return@@YA?AUSmall@@XZ"()92// WIN64: define dso_local i32 @"?small_return@@YA?AUSmall@@XZ"()93// WOA64: define dso_local i32 @"?small_return@@YA?AUSmall@@XZ"()94 95Medium medium_return() { return Medium(); }96// LINUX-LABEL: define{{.*}} void @_Z13medium_returnv(ptr dead_on_unwind noalias writable sret(%struct.Medium) align 4 %agg.result)97// WIN32: define dso_local i64 @"?medium_return@@YA?AUMedium@@XZ"()98// WIN64: define dso_local i64 @"?medium_return@@YA?AUMedium@@XZ"()99// WOA64: define dso_local i64 @"?medium_return@@YA?AUMedium@@XZ"()100 101// Returning structs that fit into a register but are not POD.102SmallCpp11NotCpp03Pod small_non_pod_return() { return SmallCpp11NotCpp03Pod(); }103// LINUX-LABEL: define{{.*}} void @_Z20small_non_pod_returnv(ptr dead_on_unwind noalias writable sret(%struct.SmallCpp11NotCpp03Pod) align 4 %agg.result)104// WIN32: define dso_local void @"?small_non_pod_return@@YA?AUSmallCpp11NotCpp03Pod@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.SmallCpp11NotCpp03Pod) align 4 %agg.result)105// WIN64: define dso_local void @"?small_non_pod_return@@YA?AUSmallCpp11NotCpp03Pod@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.SmallCpp11NotCpp03Pod) align 4 %agg.result)106// WOA64: define dso_local void @"?small_non_pod_return@@YA?AUSmallCpp11NotCpp03Pod@@XZ"(ptr dead_on_unwind inreg noalias writable sret(%struct.SmallCpp11NotCpp03Pod) align 4 %agg.result)107 108SmallWithCtor small_with_ctor_return() { return SmallWithCtor(); }109// LINUX-LABEL: define{{.*}} void @_Z22small_with_ctor_returnv(ptr dead_on_unwind noalias writable sret(%struct.SmallWithCtor) align 4 %agg.result)110// WIN32: define dso_local void @"?small_with_ctor_return@@YA?AUSmallWithCtor@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.SmallWithCtor) align 4 %agg.result)111// WIN64: define dso_local void @"?small_with_ctor_return@@YA?AUSmallWithCtor@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.SmallWithCtor) align 4 %agg.result)112// FIXME: The 'sret' mark here doesn't seem to be enough to convince LLVM to113// preserve the hidden sret pointer in R0 across the function.114// WOA: define dso_local arm_aapcs_vfpcc void @"?small_with_ctor_return@@YA?AUSmallWithCtor@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.SmallWithCtor) align 4 %agg.result)115// WOA64: define dso_local void @"?small_with_ctor_return@@YA?AUSmallWithCtor@@XZ"(ptr dead_on_unwind inreg noalias writable sret(%struct.SmallWithCtor) align 4 %agg.result)116 117SmallWithDtor small_with_dtor_return() { return SmallWithDtor(); }118// LINUX-LABEL: define{{.*}} void @_Z22small_with_dtor_returnv(ptr dead_on_unwind noalias writable sret(%struct.SmallWithDtor) align 4 %agg.result)119// WIN32: define dso_local void @"?small_with_dtor_return@@YA?AUSmallWithDtor@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.SmallWithDtor) align 4 %agg.result)120// WIN64: define dso_local void @"?small_with_dtor_return@@YA?AUSmallWithDtor@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.SmallWithDtor) align 4 %agg.result)121// WOA64: define dso_local void @"?small_with_dtor_return@@YA?AUSmallWithDtor@@XZ"(ptr dead_on_unwind inreg noalias writable sret(%struct.SmallWithDtor) align 4 %agg.result)122 123SmallWithVftable small_with_vftable_return() { return SmallWithVftable(); }124// LINUX-LABEL: define{{.*}} void @_Z25small_with_vftable_returnv(ptr dead_on_unwind noalias writable sret(%struct.SmallWithVftable) align 4 %agg.result)125// WIN32: define dso_local void @"?small_with_vftable_return@@YA?AUSmallWithVftable@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.SmallWithVftable) align 4 %agg.result)126// WIN64: define dso_local void @"?small_with_vftable_return@@YA?AUSmallWithVftable@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.SmallWithVftable) align 8 %agg.result)127// WOA64: define dso_local void @"?small_with_vftable_return@@YA?AUSmallWithVftable@@XZ"(ptr dead_on_unwind inreg noalias writable sret(%struct.SmallWithVftable) align 8 %agg.result)128 129MediumWithCopyCtor medium_with_copy_ctor_return() { return MediumWithCopyCtor(); }130// LINUX-LABEL: define{{.*}} void @_Z28medium_with_copy_ctor_returnv(ptr dead_on_unwind noalias writable sret(%struct.MediumWithCopyCtor) align 4 %agg.result)131// WIN32: define dso_local void @"?medium_with_copy_ctor_return@@YA?AUMediumWithCopyCtor@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.MediumWithCopyCtor) align 4 %agg.result)132// WIN64: define dso_local void @"?medium_with_copy_ctor_return@@YA?AUMediumWithCopyCtor@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.MediumWithCopyCtor) align 4 %agg.result)133// WOA: define dso_local arm_aapcs_vfpcc void @"?medium_with_copy_ctor_return@@YA?AUMediumWithCopyCtor@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.MediumWithCopyCtor) align 4 %agg.result)134// WOA64: define dso_local void @"?medium_with_copy_ctor_return@@YA?AUMediumWithCopyCtor@@XZ"(ptr dead_on_unwind inreg noalias writable sret(%struct.MediumWithCopyCtor) align 4 %agg.result)135 136// Returning a large struct that doesn't fit into a register.137Big big_return() { return Big(); }138// LINUX-LABEL: define{{.*}} void @_Z10big_returnv(ptr dead_on_unwind noalias writable sret(%struct.Big) align 4 %agg.result)139// WIN32: define dso_local void @"?big_return@@YA?AUBig@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.Big) align 4 %agg.result)140// WIN64: define dso_local void @"?big_return@@YA?AUBig@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.Big) align 4 %agg.result)141// WOA64: define dso_local void @"?big_return@@YA?AUBig@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.Big) align 4 %agg.result)142 143 144void small_arg(Small s) {}145// LINUX-LABEL: define{{.*}} void @_Z9small_arg5Small(i32 %s.0)146// WIN32: define dso_local void @"?small_arg@@YAXUSmall@@@Z"(i32 %s.0)147// WIN64: define dso_local void @"?small_arg@@YAXUSmall@@@Z"(i32 %s.coerce)148// WOA: define dso_local arm_aapcs_vfpcc void @"?small_arg@@YAXUSmall@@@Z"([1 x i32] %s.coerce)149 150void medium_arg(Medium s) {}151// LINUX-LABEL: define{{.*}} void @_Z10medium_arg6Medium(i32 %s.0, i32 %s.1)152// WIN32: define dso_local void @"?medium_arg@@YAXUMedium@@@Z"(i32 %s.0, i32 %s.1)153// WIN64: define dso_local void @"?medium_arg@@YAXUMedium@@@Z"(i64 %s.coerce)154// WOA: define dso_local arm_aapcs_vfpcc void @"?medium_arg@@YAXUMedium@@@Z"([2 x i32] %s.coerce)155 156void base_no_byval_arg(BaseNoByval s) {}157// LINUX-LABEL: define{{.*}} void @_Z17base_no_byval_arg11BaseNoByval(ptr noundef byval(%struct.BaseNoByval) align 4 %s)158// WIN32: define dso_local void @"?base_no_byval_arg@@YAXUBaseNoByval@@@Z"(i32 %s.0, i32 %s.1)159// WIN64: define dso_local void @"?base_no_byval_arg@@YAXUBaseNoByval@@@Z"(i64 %s.coerce)160// WOA: define dso_local arm_aapcs_vfpcc void @"?base_no_byval_arg@@YAXUBaseNoByval@@@Z"([2 x i32] %s.coerce)161 162void small_arg_with_ctor(SmallWithCtor s) {}163// LINUX-LABEL: define{{.*}} void @_Z19small_arg_with_ctor13SmallWithCtor(ptr noundef byval(%struct.SmallWithCtor) align 4 %s)164// WIN32: define dso_local void @"?small_arg_with_ctor@@YAXUSmallWithCtor@@@Z"(i32 %s.0)165// WIN64: define dso_local void @"?small_arg_with_ctor@@YAXUSmallWithCtor@@@Z"(i32 %s.coerce)166// WOA: define dso_local arm_aapcs_vfpcc void @"?small_arg_with_ctor@@YAXUSmallWithCtor@@@Z"([1 x i32] %s.coerce)167 168// FIXME: We could coerce to a series of i32s here if we wanted to.169void multibyte_arg(Multibyte s) {}170// LINUX-LABEL: define{{.*}} void @_Z13multibyte_arg9Multibyte(ptr noundef byval(%struct.Multibyte) align 4 %s)171// WIN32: define dso_local void @"?multibyte_arg@@YAXUMultibyte@@@Z"(ptr noundef byval(%struct.Multibyte) align 4 %s)172// WIN64: define dso_local void @"?multibyte_arg@@YAXUMultibyte@@@Z"(i32 %s.coerce)173// WOA: define dso_local arm_aapcs_vfpcc void @"?multibyte_arg@@YAXUMultibyte@@@Z"([1 x i32] %s.coerce)174 175void packed_arg(Packed s) {}176// LINUX-LABEL: define{{.*}} void @_Z10packed_arg6Packed(ptr noundef byval(%struct.Packed) align 4 %s)177// WIN32: define dso_local void @"?packed_arg@@YAXUPacked@@@Z"(ptr noundef byval(%struct.Packed) align 4 %s)178// WIN64: define dso_local void @"?packed_arg@@YAXUPacked@@@Z"(ptr dead_on_return noundef %s)179 180// Test that dtors are invoked in the callee.181void small_arg_with_dtor(SmallWithDtor s) {}182// WIN32: define dso_local void @"?small_arg_with_dtor@@YAXUSmallWithDtor@@@Z"(ptr inalloca(<{ %struct.SmallWithDtor }>) %0) {{.*}} {183// WIN32: call x86_thiscallcc void @"??1SmallWithDtor@@QAE@XZ"184// WIN32: }185// WIN64: define dso_local void @"?small_arg_with_dtor@@YAXUSmallWithDtor@@@Z"(i32 %s.coerce) {{.*}} {186// WIN64: call void @"??1SmallWithDtor@@QEAA@XZ"187// WIN64: }188// WOA64: define dso_local void @"?small_arg_with_dtor@@YAXUSmallWithDtor@@@Z"(i64 %s.coerce) {{.*}} {189// WOA64: call void @"??1SmallWithDtor@@QEAA@XZ"(ptr {{[^,]*}} %s)190// WOA64: }191 192// FIXME: MSVC incompatible!193// WOA: define dso_local arm_aapcs_vfpcc void @"?small_arg_with_dtor@@YAXUSmallWithDtor@@@Z"(ptr dead_on_return noundef %s) {{.*}} {194// WOA: call arm_aapcs_vfpcc void @"??1SmallWithDtor@@QAA@XZ"(ptr {{[^,]*}} %s)195// WOA: }196 197 198// Test that the eligible non-aggregate is passed directly, but returned199// indirectly on ARM64 Windows.200// WOA64: define dso_local void @"?small_arg_with_private_member@@YA?AUSmallWithPrivate@@U1@@Z"(ptr dead_on_unwind inreg noalias writable sret(%struct.SmallWithPrivate) align 4 %agg.result, i64 %s.coerce) {{.*}} {201SmallWithPrivate small_arg_with_private_member(SmallWithPrivate s) { return s; }202 203// WOA64: define dso_local i32 @"?small_arg_with_small_struct_with_private_member@@YA?AUSmallWithSmallWithPrivate@@U1@@Z"(i64 %s.coerce) {{.*}} {204// WIN64: define dso_local i32 @"?small_arg_with_small_struct_with_private_member@@YA?AUSmallWithSmallWithPrivate@@U1@@Z"(i32 %s.coerce) {{.*}} {205SmallWithSmallWithPrivate small_arg_with_small_struct_with_private_member(SmallWithSmallWithPrivate s) { return s; }206 207void call_small_arg_with_dtor() {208 small_arg_with_dtor(SmallWithDtor());209}210// WIN64-LABEL: define dso_local void @"?call_small_arg_with_dtor@@YAXXZ"()211// WIN64: call noundef ptr @"??0SmallWithDtor@@QEAA@XZ"212// WIN64: call void @"?small_arg_with_dtor@@YAXUSmallWithDtor@@@Z"(i32 %{{.*}})213// WIN64: ret void214 215// Test that references aren't destroyed in the callee.216void ref_small_arg_with_dtor(const SmallWithDtor &s) { }217// WIN32: define dso_local void @"?ref_small_arg_with_dtor@@YAXABUSmallWithDtor@@@Z"(ptr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %s) {{.*}} {218// WIN32-NOT: call x86_thiscallcc void @"??1SmallWithDtor@@QAE@XZ"219// WIN32: }220// WIN64-LABEL: define dso_local void @"?ref_small_arg_with_dtor@@YAXAEBUSmallWithDtor@@@Z"(ptr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %s)221 222void big_arg_with_dtor(BigWithDtor s) {}223// WIN64-LABEL: define dso_local void @"?big_arg_with_dtor@@YAXUBigWithDtor@@@Z"(ptr dead_on_return noundef %s)224// WIN64: call void @"??1BigWithDtor@@QEAA@XZ"225// WIN64: }226 227void call_big_arg_with_dtor() {228 big_arg_with_dtor(BigWithDtor());229}230// We can elide the copy of the temporary in the caller, because this object is231// larger than 8 bytes and is passed indirectly.232// WIN64-LABEL: define dso_local void @"?call_big_arg_with_dtor@@YAXXZ"()233// WIN64: call noundef ptr @"??0BigWithDtor@@QEAA@XZ"234// WIN64: call void @"?big_arg_with_dtor@@YAXUBigWithDtor@@@Z"(ptr dead_on_return noundef %{{.*}})235// WIN64-NOT: call void @"??1BigWithDtor@@QEAA@XZ"236// WIN64: ret void237 238// Test that temporaries passed by reference are destroyed in the caller.239void temporary_ref_with_dtor() {240 ref_small_arg_with_dtor(SmallWithDtor());241}242// WIN32: define dso_local void @"?temporary_ref_with_dtor@@YAXXZ"() {{.*}} {243// WIN32: call x86_thiscallcc noundef ptr @"??0SmallWithDtor@@QAE@XZ"244// WIN32: call void @"?ref_small_arg_with_dtor@@YAXABUSmallWithDtor@@@Z"245// WIN32: call x86_thiscallcc void @"??1SmallWithDtor@@QAE@XZ"246// WIN32: }247 248void takes_two_by_val_with_dtor(SmallWithDtor a, SmallWithDtor b);249void eh_cleanup_arg_with_dtor() {250 takes_two_by_val_with_dtor(SmallWithDtor(), SmallWithDtor());251}252// When exceptions are off, we don't have any cleanups. See253// microsoft-abi-exceptions.cpp for these cleanups.254// WIN32: define dso_local void @"?eh_cleanup_arg_with_dtor@@YAXXZ"() {{.*}} {255// WIN32: call x86_thiscallcc noundef ptr @"??0SmallWithDtor@@QAE@XZ"256// WIN32: call x86_thiscallcc noundef ptr @"??0SmallWithDtor@@QAE@XZ"257// WIN32: call void @"?takes_two_by_val_with_dtor@@YAXUSmallWithDtor@@0@Z"258// WIN32-NOT: call x86_thiscallcc void @"??1SmallWithDtor@@QAE@XZ"259// WIN32: }260 261void small_arg_with_vftable(SmallWithVftable s) {}262// LINUX-LABEL: define{{.*}} void @_Z22small_arg_with_vftable16SmallWithVftable(ptr dead_on_return noundef %s)263// WIN32: define dso_local void @"?small_arg_with_vftable@@YAXUSmallWithVftable@@@Z"(ptr inalloca(<{ %struct.SmallWithVftable }>) %0)264// WIN64: define dso_local void @"?small_arg_with_vftable@@YAXUSmallWithVftable@@@Z"(ptr dead_on_return noundef %s)265// WOA64: define dso_local void @"?small_arg_with_vftable@@YAXUSmallWithVftable@@@Z"(ptr dead_on_return noundef %s)266 267void medium_arg_with_copy_ctor(MediumWithCopyCtor s) {}268// LINUX-LABEL: define{{.*}} void @_Z25medium_arg_with_copy_ctor18MediumWithCopyCtor(ptr dead_on_return noundef %s)269// WIN32: define dso_local void @"?medium_arg_with_copy_ctor@@YAXUMediumWithCopyCtor@@@Z"(ptr inalloca(<{ %struct.MediumWithCopyCtor }>) %0)270// WIN64: define dso_local void @"?medium_arg_with_copy_ctor@@YAXUMediumWithCopyCtor@@@Z"(ptr dead_on_return noundef %s)271// WOA: define dso_local arm_aapcs_vfpcc void @"?medium_arg_with_copy_ctor@@YAXUMediumWithCopyCtor@@@Z"(ptr dead_on_return noundef %s)272// WOA64: define dso_local void @"?medium_arg_with_copy_ctor@@YAXUMediumWithCopyCtor@@@Z"(ptr dead_on_return noundef %s)273 274void big_arg(Big s) {}275// LINUX-LABEL: define{{.*}} void @_Z7big_arg3Big(ptr noundef byval(%struct.Big) align 4 %s)276// WIN32: define dso_local void @"?big_arg@@YAXUBig@@@Z"(ptr noundef byval(%struct.Big) align 4 %s)277// WIN64: define dso_local void @"?big_arg@@YAXUBig@@@Z"(ptr dead_on_return noundef %s)278 279// PR27607: We would attempt to load i32 value out of the reference instead of280// just loading the pointer from the struct during argument expansion.281struct RefField {282 RefField(int &x);283 int &x;284};285void takes_ref_field(RefField s) {}286// LINUX-LABEL: define{{.*}} void @_Z15takes_ref_field8RefField(ptr noundef byval(%struct.RefField) align 4 %s)287// WIN32: define dso_local void @"?takes_ref_field@@YAXURefField@@@Z"(ptr %s.0)288// WIN64: define dso_local void @"?takes_ref_field@@YAXURefField@@@Z"(i64 %s.coerce)289 290void pass_ref_field() {291 int x;292 takes_ref_field(RefField(x));293}294// LINUX-LABEL: define{{.*}} void @_Z14pass_ref_fieldv()295// LINUX: call void @_Z15takes_ref_field8RefField(ptr noundef byval(%struct.RefField) align 4 %{{.*}})296// WIN32-LABEL: define dso_local void @"?pass_ref_field@@YAXXZ"()297// WIN32: call void @"?takes_ref_field@@YAXURefField@@@Z"(ptr %{{.*}})298// WIN64-LABEL: define dso_local void @"?pass_ref_field@@YAXXZ"()299// WIN64: call void @"?takes_ref_field@@YAXURefField@@@Z"(i64 %{{.*}})300 301class Class {302 public:303 Small thiscall_method_small() { return Small(); }304 // LINUX: define {{.*}} void @_ZN5Class21thiscall_method_smallEv(ptr dead_on_unwind noalias writable sret(%struct.Small) align 4 %agg.result, ptr {{[^,]*}} %this)305 // WIN32: define {{.*}} x86_thiscallcc void @"?thiscall_method_small@Class@@QAE?AUSmall@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind noalias writable sret(%struct.Small) align 4 %agg.result)306 // WIN64: define linkonce_odr dso_local void @"?thiscall_method_small@Class@@QEAA?AUSmall@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind noalias writable sret(%struct.Small) align 4 %agg.result)307 // WOA64: define linkonce_odr dso_local void @"?thiscall_method_small@Class@@QEAA?AUSmall@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind inreg noalias writable sret(%struct.Small) align 4 %agg.result)308 309 SmallWithCtor thiscall_method_small_with_ctor() { return SmallWithCtor(); }310 // LINUX: define {{.*}} void @_ZN5Class31thiscall_method_small_with_ctorEv(ptr dead_on_unwind noalias writable sret(%struct.SmallWithCtor) align 4 %agg.result, ptr {{[^,]*}} %this)311 // WIN32: define {{.*}} x86_thiscallcc void @"?thiscall_method_small_with_ctor@Class@@QAE?AUSmallWithCtor@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind noalias writable sret(%struct.SmallWithCtor) align 4 %agg.result)312 // WIN64: define linkonce_odr dso_local void @"?thiscall_method_small_with_ctor@Class@@QEAA?AUSmallWithCtor@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind noalias writable sret(%struct.SmallWithCtor) align 4 %agg.result)313 // WOA64: define linkonce_odr dso_local void @"?thiscall_method_small_with_ctor@Class@@QEAA?AUSmallWithCtor@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind inreg noalias writable sret(%struct.SmallWithCtor) align 4 %agg.result)314 315 Small __cdecl cdecl_method_small() { return Small(); }316 // LINUX: define {{.*}} void @_ZN5Class18cdecl_method_smallEv(ptr dead_on_unwind noalias writable sret(%struct.Small) align 4 %agg.result, ptr {{[^,]*}} %this)317 // WIN32: define {{.*}} void @"?cdecl_method_small@Class@@QAA?AUSmall@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind noalias writable sret(%struct.Small) align 4 %agg.result)318 // WIN64: define linkonce_odr dso_local void @"?cdecl_method_small@Class@@QEAA?AUSmall@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind noalias writable sret(%struct.Small) align 4 %agg.result)319 320 Big __cdecl cdecl_method_big() { return Big(); }321 // LINUX: define {{.*}} void @_ZN5Class16cdecl_method_bigEv(ptr dead_on_unwind noalias writable sret(%struct.Big) align 4 %agg.result, ptr {{[^,]*}} %this)322 // WIN32: define {{.*}} void @"?cdecl_method_big@Class@@QAA?AUBig@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind noalias writable sret(%struct.Big) align 4 %agg.result)323 // WIN64: define linkonce_odr dso_local void @"?cdecl_method_big@Class@@QEAA?AUBig@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind noalias writable sret(%struct.Big) align 4 %agg.result)324 // WOA64: define linkonce_odr dso_local void @"?cdecl_method_big@Class@@QEAA?AUBig@@XZ"(ptr {{[^,]*}} %this, ptr dead_on_unwind inreg noalias writable sret(%struct.Big) align 4 %agg.result)325 326 void thiscall_method_arg(Empty s) {}327 // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE5Empty(ptr {{[^,]*}} %this)328 // WIN32: define {{.*}} void @"?thiscall_method_arg@Class@@QAEXUEmpty@@@Z"(ptr {{[^,]*}} %this, ptr noundef byval(%struct.Empty) align 4 %s)329 // WIN64: define linkonce_odr dso_local void @"?thiscall_method_arg@Class@@QEAAXUEmpty@@@Z"(ptr {{[^,]*}} %this, i8 %s.coerce)330 331 void thiscall_method_arg(EmptyWithCtor s) {}332 // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE13EmptyWithCtor(ptr {{[^,]*}} %this)333 // WIN32: define {{.*}} void @"?thiscall_method_arg@Class@@QAEXUEmptyWithCtor@@@Z"(ptr {{[^,]*}} %this, ptr noundef byval(%struct.EmptyWithCtor) align 4 %s)334 // WIN64: define linkonce_odr dso_local void @"?thiscall_method_arg@Class@@QEAAXUEmptyWithCtor@@@Z"(ptr {{[^,]*}} %this, i8 %s.coerce)335 336 void thiscall_method_arg(Small s) {}337 // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE5Small(ptr {{[^,]*}} %this, i32 %s.0)338 // WIN32: define {{.*}} void @"?thiscall_method_arg@Class@@QAEXUSmall@@@Z"(ptr {{[^,]*}} %this, i32 %s.0)339 // WIN64: define linkonce_odr dso_local void @"?thiscall_method_arg@Class@@QEAAXUSmall@@@Z"(ptr {{[^,]*}} %this, i32 %s.coerce)340 341 void thiscall_method_arg(SmallWithCtor s) {}342 // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE13SmallWithCtor(ptr {{[^,]*}} %this, ptr noundef byval(%struct.SmallWithCtor) align 4 %s)343 // WIN32: define {{.*}} void @"?thiscall_method_arg@Class@@QAEXUSmallWithCtor@@@Z"(ptr {{[^,]*}} %this, i32 %s.0)344 // WIN64: define linkonce_odr dso_local void @"?thiscall_method_arg@Class@@QEAAXUSmallWithCtor@@@Z"(ptr {{[^,]*}} %this, i32 %s.coerce)345 346 void thiscall_method_arg(Big s) {}347 // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE3Big(ptr {{[^,]*}} %this, ptr noundef byval(%struct.Big) align 4 %s)348 // WIN32: define {{.*}} void @"?thiscall_method_arg@Class@@QAEXUBig@@@Z"(ptr {{[^,]*}} %this, ptr noundef byval(%struct.Big) align 4 %s)349 // WIN64: define linkonce_odr dso_local void @"?thiscall_method_arg@Class@@QEAAXUBig@@@Z"(ptr {{[^,]*}} %this, ptr dead_on_return noundef %s)350};351 352void use_class() {353 Class c;354 c.thiscall_method_small();355 c.thiscall_method_small_with_ctor();356 357 c.cdecl_method_small();358 c.cdecl_method_big();359 360 c.thiscall_method_arg(Empty());361 c.thiscall_method_arg(EmptyWithCtor());362 c.thiscall_method_arg(Small());363 c.thiscall_method_arg(SmallWithCtor());364 c.thiscall_method_arg(Big());365}366 367struct X {368 X();369 ~X();370};371void g(X) {372}373// WIN32: define dso_local void @"?g@@YAXUX@@@Z"(ptr inalloca(<{ %struct.X, [3 x i8] }>) %0) {{.*}} {374// WIN32: call x86_thiscallcc void @"??1X@@QAE@XZ"(ptr {{.*}})375// WIN32: }376void f() {377 g(X());378}379// WIN32: define dso_local void @"?f@@YAXXZ"() {{.*}} {380// WIN32-NOT: call {{.*}} @"??1X@@QAE@XZ"381// WIN32: }382 383 384namespace test2 {385// We used to crash on this due to the mixture of POD byval and non-trivial386// byval.387 388struct NonTrivial {389 NonTrivial();390 NonTrivial(const NonTrivial &o);391 ~NonTrivial();392 int a;393};394struct POD { int b; };395 396int foo(NonTrivial a, POD b);397void bar() {398 POD b;399 b.b = 13;400 int c = foo(NonTrivial(), b);401}402// WIN32-LABEL: define dso_local void @"?bar@test2@@YAXXZ"() {{.*}} {403// WIN32: %[[argmem:[^ ]*]] = alloca inalloca [[argmem_ty:<{ %"struct.test2::NonTrivial", %"struct.test2::POD" }>]]404// WIN32: getelementptr inbounds nuw [[argmem_ty]], ptr %[[argmem]], i32 0, i32 1405// WIN32: call void @llvm.memcpy406// WIN32: getelementptr inbounds nuw [[argmem_ty]], ptr %[[argmem]], i32 0, i32 0407// WIN32: call x86_thiscallcc noundef ptr @"??0NonTrivial@test2@@QAE@XZ"408// WIN32: call noundef i32 @"?foo@test2@@YAHUNonTrivial@1@UPOD@1@@Z"(ptr inalloca([[argmem_ty]]) %argmem)409// WIN32: ret void410// WIN32: }411 412}413 414namespace test3 {415 416// Check that we padded the inalloca struct to a multiple of 4.417struct NonTrivial {418 NonTrivial();419 NonTrivial(const NonTrivial &o);420 ~NonTrivial();421 int a;422};423void foo(NonTrivial a, bool b) { }424// WIN32-LABEL: define dso_local void @"?foo@test3@@YAXUNonTrivial@1@_N@Z"(ptr inalloca(<{ %"struct.test3::NonTrivial", i8, [3 x i8] }>) %0)425 426}427 428// We would crash here because the later definition of ForwardDeclare1 results429// in a different IR type for the value we want to store. However, the alloca's430// type will use the argument type selected by fn1.431struct ForwardDeclare1;432 433typedef void (*FnPtr1)(ForwardDeclare1);434void fn1(FnPtr1 a, SmallWithDtor b) { }435 436struct ForwardDeclare1 {};437 438void fn2(FnPtr1 a, SmallWithDtor b) { fn1(a, b); };439// WIN32-LABEL: define dso_local void @"?fn2@@YAXP6AXUForwardDeclare1@@@ZUSmallWithDtor@@@Z"440// WIN32: %[[a:[^ ]*]] = getelementptr inbounds nuw [[argmem_ty:<{ ptr, %struct.SmallWithDtor }>]], ptr %{{.*}}, i32 0, i32 0441// WIN32: %[[argmem:[^ ]*]] = alloca inalloca [[argmem_ty:<{ ptr, %struct.SmallWithDtor }>]]442// WIN32: %[[gep1:[^ ]*]] = getelementptr inbounds nuw [[argmem_ty]], ptr %[[argmem]], i32 0, i32 1443// WIN32: call void @llvm.memcpy.p0.p0.i32(ptr align 4 %[[gep1]], ptr align 4 {{.*}}, i32 4, i1 false)444// WIN32: %[[a2:[^ ]*]] = load ptr, ptr %[[a]], align 4445// WIN32: %[[gep2:[^ ]*]] = getelementptr inbounds nuw [[argmem_ty]], ptr %[[argmem]], i32 0, i32 0446// WIN32: store ptr %[[a2]], ptr %[[gep2]], align 4447// WIN32: call void @"?fn1@@YAXP6AXUForwardDeclare1@@@ZUSmallWithDtor@@@Z"(ptr inalloca([[argmem_ty]]) %[[argmem]])448 449namespace pr30293 {450// Virtual methods living in a secondary vtable take ptr as their 'this'451// parameter because the 'this' parameter on entry points to the secondary452// vptr. We used to have a bug where we didn't apply this rule consistently,453// and it would cause assertion failures when used with inalloca.454struct A {455 virtual void f();456};457struct B {458 virtual void __cdecl h(SmallWithDtor);459};460struct C final : A, B {461 void g();462 void __cdecl h(SmallWithDtor);463 void f();464};465void C::g() { return h(SmallWithDtor()); }466 467// WIN32-LABEL: define dso_local x86_thiscallcc void @"?g@C@pr30293@@QAEXXZ"(ptr {{[^,]*}} %this)468// WIN32: call x86_thiscallcc noundef ptr @"??0SmallWithDtor@@QAE@XZ"469// WIN32: call void @"?h@C@pr30293@@UAAXUSmallWithDtor@@@Z"(ptr inalloca(<{ ptr, %struct.SmallWithDtor }>) %{{[^,)]*}})470// WIN32: declare dso_local void @"?h@C@pr30293@@UAAXUSmallWithDtor@@@Z"(ptr inalloca(<{ ptr, %struct.SmallWithDtor }>))471 472// WIN64-LABEL: define dso_local void @"?g@C@pr30293@@QEAAXXZ"(ptr {{[^,]*}} %this)473// WIN64: declare dso_local void @"?h@C@pr30293@@UEAAXUSmallWithDtor@@@Z"(ptr noundef, i32)474}475 476namespace protected_member_of_member {477struct field { protected: int i; };478struct t1 {479 field f;480};481extern const t1& v1;482t1 f1() { return v1; }483// WIN: define dso_local {{.*}}i32 @"?f1@protected_member_of_member@@YA?AUt1@1@XZ"()484}485 486namespace default_member_initializer {487struct t1 {488 int i = 3;489};490extern const t1& v1;491t1 f1() { return v1; }492// WIN: define dso_local {{.*}}i32 @"?f1@default_member_initializer@@YA?AUt1@1@XZ"()493}494 495namespace defaulted_copy_ctor {496struct t1 {497 int i;498 t1(const t1&) = default;499};500extern const t1& v1;501t1 f1() { return v1; }502// WIN: define dso_local {{.*}}i32 @"?f1@defaulted_copy_ctor@@YA?AUt1@1@XZ"()503}504