Index: lib/Basic/Targets.cpp =================================================================== --- lib/Basic/Targets.cpp +++ lib/Basic/Targets.cpp @@ -7014,6 +7014,10 @@ : LinuxTargetInfo(Triple) { LongDoubleFormat = &llvm::APFloat::IEEEquad; } + + bool useFloat128ManglingForLongDouble() const override { + return true; + } }; } // end anonymous namespace Index: lib/CodeGen/TargetInfo.cpp =================================================================== --- lib/CodeGen/TargetInfo.cpp +++ lib/CodeGen/TargetInfo.cpp @@ -1975,8 +1975,18 @@ (ET == getContext().LongDoubleTy && getTarget().getTriple().isOSNaCl())) Lo = Hi = SSE; - else if (ET == getContext().LongDoubleTy) - Current = ComplexX87; + else if (ET == getContext().LongDoubleTy) { + // AMD64 ABI says that long double should be f80 and + // complex long double should be ComplexX87. + // However, it also defines __float128 as f128 in SSE, + // complex float and complex double like structure. + // So complex __float128 should be like structure, + // which is also implemented by gcc. + if (auto ResType = CGT.ConvertType(ET)) + Current = ResType->isFP128Ty() ? Memory : ComplexX87; + else + Current = ComplexX87; + } // If this complex type crosses an eightbyte boundary then it // should be split. @@ -2518,6 +2528,12 @@ ABIArgInfo X86_64ABIInfo:: classifyReturnType(QualType RetTy) const { + // TODO: Simplify classify to handle f128 according to AMD64 ABI. + if (auto ResType = CGT.ConvertType(RetTy)) { + if (ResType->isFP128Ty()) + return ABIArgInfo::getDirect(ResType); + } + // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the // classification algorithm. X86_64ABIInfo::Class Lo, Hi; @@ -2650,6 +2666,15 @@ bool isNamedArg) const { + // TODO: Simplify classifyArgumentType to handle f128 according to AMD64 ABI. + if (auto ResType = CGT.ConvertType(Ty)) { + if (ResType->isFP128Ty()) { + neededInt = 0; + neededSSE = 1; + return ABIArgInfo::getDirect(ResType); + } + } + Ty = useFirstFieldIfTransparentUnion(Ty); X86_64ABIInfo::Class Lo, Hi; Index: test/CodeGen/x86-long-double.cpp =================================================================== --- /dev/null +++ test/CodeGen/x86-long-double.cpp @@ -0,0 +1,22 @@ +// RUN: %clang -target x86_64-linux-android -emit-llvm -O -S -o - %s \ +// RUN: | FileCheck %s --check-prefix=A64 +// RUN: %clang -target x86_64-linux-gnu -emit-llvm -O -S -o - %s \ +// RUN: | FileCheck %s --check-prefix=G64 +// RUN: %clang -target powerpc64-linux-gnu -emit-llvm -O -S -o - %s \ +// RUN: | FileCheck %s --check-prefix=P64 +// RUN: %clang -target i686-linux-android -emit-llvm -O -S -o - %s \ +// RUN: | FileCheck %s --check-prefix=A32 +// RUN: %clang -target i686-linux-gnu -emit-llvm -O -S -o - %s \ +// RUN: | FileCheck %s --check-prefix=G32 +// RUN: %clang -target powerpc-linux-gnu -emit-llvm -O -S -o - %s \ +// RUN: | FileCheck %s --check-prefix=P32 + +// Check mangled name of long double. +// Android's gcc and llvm use fp128 for long double. +void test(long, float, double, long double, long double _Complex) { } +// A64: define void @_Z4testlfdgCg(i64, float, double, fp128, { fp128, fp128 }* +// G64: define void @_Z4testlfdeCe(i64, float, double, x86_fp80, { x86_fp80, x86_fp80 }* +// P64: define void @_Z4testlfdgCg(i64, float, double, ppc_fp128, ppc_fp128 {{.*}}, ppc_fp128 +// A32: define void @_Z4testlfdeCe(i32, float, double, double, { double, double }* +// G32: define void @_Z4testlfdeCe(i32, float, double, x86_fp80, { x86_fp80, x86_fp80 }* +// P32: define void @_Z4testlfdgCg(i32, float, double, ppc_fp128, { ppc_fp128, ppc_fp128 }* Index: test/CodeGen/x86_64-fp128.c =================================================================== --- /dev/null +++ test/CodeGen/x86_64-fp128.c @@ -0,0 +1,116 @@ +// RUN: %clang -target x86_64-linux-android -emit-llvm -O -S -o - %s \ +// RUN: | FileCheck %s --check-prefix=ANDROID --check-prefix=CHECK +// RUN: %clang -target x86_64-linux-gnu -emit-llvm -O -S -o - %s \ +// RUN: | FileCheck %s --check-prefix=GNU --check-prefix=CHECK +// RUN: %clang -target x86_64 -emit-llvm -O -S -o - %s \ +// RUN: | FileCheck %s --check-prefix=GNU --check-prefix=CHECK + +// Android uses fp128 for long double but other x86_64 targets use x86_fp80. + +long double dataLD = 1.0L; +// ANDROID: @dataLD = global fp128 0xL00000000000000003FFF000000000000, align 16 +// GNU: @dataLD = global x86_fp80 0xK3FFF8000000000000000, align 16 + +long double _Complex dataLDC = {1.0L, 1.0L}; +// ANDROID: @dataLDC = global { fp128, fp128 } { fp128 0xL00000000000000003FFF000000000000, fp128 0xL00000000000000003FFF000000000000 }, align 16 +// GNU: @dataLDC = global { x86_fp80, x86_fp80 } { x86_fp80 0xK3FFF8000000000000000, x86_fp80 0xK3FFF8000000000000000 }, align 16 + +long double TestLD(long double x) { + return x * x; +// ANDROID: define fp128 @TestLD(fp128 %x) +// GNU: define x86_fp80 @TestLD(x86_fp80 %x) +} + +long double _Complex TestLDC(long double _Complex x) { + return x * x; +// ANDROID: define void @TestLDC({ fp128, fp128 }* {{.*}}, { fp128, fp128 }* {{.*}} %x) +// GNU: define { x86_fp80, x86_fp80 } @TestLDC({ x86_fp80, x86_fp80 }* {{.*}} %x) +} + +typedef __builtin_va_list va_list; + +int TestGetVarInt(va_list ap) { + return __builtin_va_arg(ap, int); +// Since int can be passed in memory or in register there is a branch and a phi. +// CHECK: define i32 @TestGetVarInt( +// CHECK: br +// CHECK: load {{.*}} %overflow_arg_area_p, +// CHECK: = phi +// CHECK: ret i32 +} + +double TestGetVarDouble(va_list ap) { + return __builtin_va_arg(ap, double); +// Since double can be passed in memory or in register there is a branch and a phi. +// CHECK: define double @TestGetVarDouble( +// CHECK: br +// CHECK: load {{.*}} %overflow_arg_area_p, +// CHECK: = phi +// CHECK: ret double +} + +long double TestGetVarLD(va_list ap) { + return __builtin_va_arg(ap, long double); +// fp128 can be passed in memory or in register, but x86_fp80 is in memory. +// ANDROID: define fp128 @TestGetVarLD( +// GNU: define x86_fp80 @TestGetVarLD( +// ANDROID: br +// GNU-NOT: br +// CHECK: load {{.*}} %overflow_arg_area_p, +// ANDROID: = phi +// GNU-NOT: = phi +// ANDROID: ret fp128 +// GNU: ret x86_fp80 +} + +long double _Complex TestGetVarLDC(va_list ap) { + return __builtin_va_arg(ap, long double _Complex); +// Pair of fp128 or x86_fp80 are passed as struct in memory. +// ANDROID: define void @TestGetVarLDC({ fp128, fp128 }* {{.*}}, %struct.__va_list_tag* +// GNU: define { x86_fp80, x86_fp80 } @TestGetVarLDC( +// CHECK-NOT: br +// CHECK: load {{.*}} %overflow_arg_area_p, +// CHECK-NOT: phi +// ANDROID: ret void +// GNU: ret { x86_fp80, x86_fp80 } +} + +void TestVarArg(const char *s, ...); + +void TestPassVarInt(int x) { + TestVarArg("A", x); +// CHECK: define void @TestPassVarInt(i32 %x) +// CHECK: call {{.*}} @TestVarArg(i8* {{.*}}, i32 %x) +} + +void TestPassVarFloat(float x) { + TestVarArg("A", x); +// CHECK: define void @TestPassVarFloat(float %x) +// CHECK: call {{.*}} @TestVarArg(i8* {{.*}}, double % +} + +void TestPassVarDouble(double x) { + TestVarArg("A", x); +// CHECK: define void @TestPassVarDouble(double %x) +// CHECK: call {{.*}} @TestVarArg(i8* {{.*}}, double %x +} + +void TestPassVarLD(long double x) { + TestVarArg("A", x); +// ANDROID: define void @TestPassVarLD(fp128 %x) +// ANDROID: call {{.*}} @TestVarArg(i8* {{.*}}, fp128 %x +// GNU: define void @TestPassVarLD(x86_fp80 %x) +// GNU: call {{.*}} @TestVarArg(i8* {{.*}}, x86_fp80 %x +} + +void TestPassVarLDC(long double _Complex x) { + TestVarArg("A", x); +// ANDROID: define void @TestPassVarLDC({ fp128, fp128 }* {{.*}} %x) +// ANDROID: store fp128 %x.{{.*}}, fp128* % +// ANDROID-NEXT: store fp128 %x.{{.*}}, fp128* % +// ANDROID-NEXT: call {{.*}} @TestVarArg(i8* {{.*}}, { fp128, fp128 }* {{.*}} % +// GNU: define void @TestPassVarLDC({ x86_fp80, x86_fp80 }* {{.*}} %x) +// GNU: store x86_fp80 %x.{{.*}}, x86_fp80* % +// GNU-NEXT: store x86_fp80 %x.{{.*}}, x86_fp80* % +// GNGNU-NEXT: call {{.*}} @TestVarArg(i8* {{.*}}, { x86_fp80, x86_fp80 }* {{.*}} % +}