diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -228,6 +228,9 @@ /// If -fpcc-struct-return or -freg-struct-return is specified. ENUM_CODEGENOPT(StructReturnConvention, StructReturnConventionKind, 2, SRCK_Default) + /// If -fcomplex-ppc-gnu-abi for ppc32 +ENUM_CODEGENOPT(ComplexInRegABI, bool, 1,false) + CODEGENOPT(RelaxAll , 1, 0) ///< Relax all machine code instructions. CODEGENOPT(RelaxedAliasing , 1, 0) ///< Set when -fno-strict-aliasing is enabled. CODEGENOPT(StructPathTBAA , 1, 0) ///< Whether or not to use struct-path TBAA. diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2095,6 +2095,9 @@ HelpText<"Form fused FP ops (e.g. FMAs)">, Values<"fast,on,off,fast-honor-pragmas">; +def fcomplex_ppc_gnu_abi : Flag<["-"], "fcomplex-ppc-gnu-abi">, Group, Flags<[CC1Option]>, + DocBrief<"Follow the GNU ABI, store Complex values in GPR instead of stack for PowerPC-32.">, + HelpText<"Store Complex values in GPR instead of Stack for PowerPC-32.">; defm strict_float_cast_overflow : BoolFOption<"strict-float-cast-overflow", CodeGenOpts<"StrictFloatCastOverflow">, DefaultTrue, NegFlag( - CGT, SoftFloatABI, RetSmallStructInRegABI)) {} + CGT, SoftFloatABI, RetSmallStructInRegABI, + ComplexInRegABI)) {} static bool isStructReturnInRegABI(const llvm::Triple &Triple, const CodeGenOptions &Opts); @@ -337,6 +344,32 @@ return CharUnits::fromQuantity(4); } +ABIArgInfo PPC32_SVR4_ABIInfo::handleComplex(QualType Ty) const{ + + uint64_t TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity(); + llvm::Type* ElemTy; + unsigned SizeRegs; + + if (TyAlign <= 4) { + ElemTy = llvm::Type::getInt32Ty(getVMContext()); + SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; + } else { + ElemTy = llvm::Type::getInt64Ty(getVMContext()); + SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; + } + return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); +} + +ABIArgInfo PPC32_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const{ + + Ty = useFirstFieldIfTransparentUnion(Ty); + + if (isComplexInRegABI && Ty->isAnyComplexType()) { + return handleComplex(Ty); + } + return DefaultABIInfo::classifyArgumentType(Ty); +} + ABIArgInfo PPC32_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { uint64_t Size; @@ -362,6 +395,10 @@ } } + if(isComplexInRegABI && RetTy->isAnyComplexType()) { + return handleComplex(RetTy); + } + return DefaultABIInfo::classifyReturnType(RetTy); } @@ -956,8 +993,11 @@ CodeGen::createPPC32TargetCodeGenInfo(CodeGenModule &CGM, bool SoftFloatABI) { bool RetSmallStructInRegABI = PPC32TargetCodeGenInfo::isStructReturnInRegABI( CGM.getTriple(), CGM.getCodeGenOpts()); + bool isComplexInRegABI = CGM.getCodeGenOpts().getComplexInRegABI(); + return std::make_unique(CGM.getTypes(), SoftFloatABI, - RetSmallStructInRegABI); + RetSmallStructInRegABI, + isComplexInRegABI); } std::unique_ptr diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5415,6 +5415,15 @@ } } + if(Arg *A = Args.getLastArg(options::OPT_fcomplex_ppc_gnu_abi)){ + if(!TC.getTriple().isPPC32()) { + D.Diag(diag::err_drv_unsupported_opt_for_target) + << A->getSpelling() << RawTriple.str(); + } else { + CmdArgs.push_back("-fcomplex-ppc-gnu-abi"); + } + } + if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) CmdArgs.push_back("-fdefault-calling-conv=stdcall"); diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1929,6 +1929,10 @@ } } + if(Args.getLastArg(OPT_fcomplex_ppc_gnu_abi)){ + Opts.setComplexInRegABI(true); + } + if (Arg *A = Args.getLastArg(OPT_mxcoff_roptr)) { if (!T.isOSAIX()) Diags.Report(diag::err_drv_unsupported_opt_for_target) diff --git a/clang/test/CodeGen/PowerPC/ppc32-complex-gnu-abi.c b/clang/test/CodeGen/PowerPC/ppc32-complex-gnu-abi.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/PowerPC/ppc32-complex-gnu-abi.c @@ -0,0 +1,119 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 2 +// RUN: %clang -target ppc32 -emit-llvm -S %s -o - | FileCheck -check-prefixes=CHECK-DEF %s +// +// RUN: %clang -target ppc32 -emit-llvm -S -fcomplex-ppc-gnu-abi %s -o - | FileCheck -check-prefixes=CHECK-GNU %s + + +// CHECK-DEF-LABEL: define dso_local i64 @foo1 +// CHECK-DEF-SAME: (ptr noundef byval({ float, float }) align 4 [[TMP0:%.*]]) #[[ATTR0:[0-9]+]] { +// CHECK-DEF-NEXT: [[TMP2:%.*]] = alloca { float, float }, align 4 +// CHECK-DEF-NEXT: [[TMP3:%.*]] = getelementptr inbounds { float, float }, ptr [[TMP0]], i32 0, i32 0 +// CHECK-DEF-NEXT: [[TMP4:%.*]] = load float, ptr [[TMP3]], align 4 +// CHECK-DEF-NEXT: [[TMP5:%.*]] = getelementptr inbounds { float, float }, ptr [[TMP0]], i32 0, i32 1 +// CHECK-DEF-NEXT: [[TMP6:%.*]] = load float, ptr [[TMP5]], align 4 +// CHECK-DEF-NEXT: [[TMP7:%.*]] = getelementptr inbounds { float, float }, ptr [[TMP2]], i32 0, i32 0 +// CHECK-DEF-NEXT: [[TMP8:%.*]] = getelementptr inbounds { float, float }, ptr [[TMP2]], i32 0, i32 1 +// CHECK-DEF-NEXT: store float [[TMP4]], ptr [[TMP7]], align 4 +// CHECK-DEF-NEXT: store float [[TMP6]], ptr [[TMP8]], align 4 +// CHECK-DEF-NEXT: [[TMP9:%.*]] = load i64, ptr [[TMP2]], align 4 +// CHECK-DEF-NEXT: ret i64 [[TMP9]] +// +// CHECK-GNU-LABEL: define dso_local i64 @foo1 +// CHECK-GNU-SAME: ([2 x i32] noundef [[TMP0:%.*]]) #[[ATTR0:[0-9]+]] { +// CHECK-GNU-NEXT: [[TMP2:%.*]] = alloca { float, float }, align 4 +// CHECK-GNU-NEXT: [[TMP3:%.*]] = alloca { float, float }, align 4 +// CHECK-GNU-NEXT: store [2 x i32] [[TMP0]], ptr [[TMP3]], align 4 +// CHECK-GNU-NEXT: [[TMP4:%.*]] = getelementptr inbounds { float, float }, ptr [[TMP3]], i32 0, i32 0 +// CHECK-GNU-NEXT: [[TMP5:%.*]] = load float, ptr [[TMP4]], align 4 +// CHECK-GNU-NEXT: [[TMP6:%.*]] = getelementptr inbounds { float, float }, ptr [[TMP3]], i32 0, i32 1 +// CHECK-GNU-NEXT: [[TMP7:%.*]] = load float, ptr [[TMP6]], align 4 +// CHECK-GNU-NEXT: [[TMP8:%.*]] = getelementptr inbounds { float, float }, ptr [[TMP2]], i32 0, i32 0 +// CHECK-GNU-NEXT: [[TMP9:%.*]] = getelementptr inbounds { float, float }, ptr [[TMP2]], i32 0, i32 1 +// CHECK-GNU-NEXT: store float [[TMP5]], ptr [[TMP8]], align 4 +// CHECK-GNU-NEXT: store float [[TMP7]], ptr [[TMP9]], align 4 +// CHECK-GNU-NEXT: [[TMP10:%.*]] = load i64, ptr [[TMP2]], align 4 +// CHECK-GNU-NEXT: ret i64 [[TMP10]] +// +_Complex float foo1(_Complex float x) { + return x; +} + +// CHECK-DEF-LABEL: define dso_local void @foo2 +// CHECK-DEF-SAME: (ptr noalias sret({ double, double }) align 8 [[TMP0:%.*]], ptr noundef byval({ double, double }) align 8 [[TMP1:%.*]]) #[[ATTR0]] { +// CHECK-DEF-NEXT: [[TMP3:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP1]], i32 0, i32 0 +// CHECK-DEF-NEXT: [[TMP4:%.*]] = load double, ptr [[TMP3]], align 8 +// CHECK-DEF-NEXT: [[TMP5:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP1]], i32 0, i32 1 +// CHECK-DEF-NEXT: [[TMP6:%.*]] = load double, ptr [[TMP5]], align 8 +// CHECK-DEF-NEXT: [[TMP7:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP0]], i32 0, i32 0 +// CHECK-DEF-NEXT: [[TMP8:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP0]], i32 0, i32 1 +// CHECK-DEF-NEXT: store double [[TMP4]], ptr [[TMP7]], align 8 +// CHECK-DEF-NEXT: store double [[TMP6]], ptr [[TMP8]], align 8 +// CHECK-DEF-NEXT: [[TMP9:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP0]], i32 0, i32 0 +// CHECK-DEF-NEXT: [[TMP10:%.*]] = load double, ptr [[TMP9]], align 8 +// CHECK-DEF-NEXT: [[TMP11:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP0]], i32 0, i32 1 +// CHECK-DEF-NEXT: [[TMP12:%.*]] = load double, ptr [[TMP11]], align 8 +// CHECK-DEF-NEXT: [[TMP13:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP0]], i32 0, i32 0 +// CHECK-DEF-NEXT: [[TMP14:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP0]], i32 0, i32 1 +// CHECK-DEF-NEXT: store double [[TMP10]], ptr [[TMP13]], align 8 +// CHECK-DEF-NEXT: store double [[TMP12]], ptr [[TMP14]], align 8 +// CHECK-DEF-NEXT: ret void +// +// CHECK-GNU-LABEL: define dso_local [2 x i64] @foo2 +// CHECK-GNU-SAME: ([2 x i64] noundef [[TMP0:%.*]]) #[[ATTR0]] { +// CHECK-GNU-NEXT: [[TMP2:%.*]] = alloca { double, double }, align 8 +// CHECK-GNU-NEXT: [[TMP3:%.*]] = alloca { double, double }, align 8 +// CHECK-GNU-NEXT: store [2 x i64] [[TMP0]], ptr [[TMP3]], align 8 +// CHECK-GNU-NEXT: [[TMP4:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP3]], i32 0, i32 0 +// CHECK-GNU-NEXT: [[TMP5:%.*]] = load double, ptr [[TMP4]], align 8 +// CHECK-GNU-NEXT: [[TMP6:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP3]], i32 0, i32 1 +// CHECK-GNU-NEXT: [[TMP7:%.*]] = load double, ptr [[TMP6]], align 8 +// CHECK-GNU-NEXT: [[TMP8:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP2]], i32 0, i32 0 +// CHECK-GNU-NEXT: [[TMP9:%.*]] = getelementptr inbounds { double, double }, ptr [[TMP2]], i32 0, i32 1 +// CHECK-GNU-NEXT: store double [[TMP5]], ptr [[TMP8]], align 8 +// CHECK-GNU-NEXT: store double [[TMP7]], ptr [[TMP9]], align 8 +// CHECK-GNU-NEXT: [[TMP10:%.*]] = load [2 x i64], ptr [[TMP2]], align 8 +// CHECK-GNU-NEXT: ret [2 x i64] [[TMP10]] +// +_Complex double foo2(_Complex double x) { + return x; +} + +// CHECK-DEF-LABEL: define dso_local void @foo3 +// CHECK-DEF-SAME: (ptr noalias sret({ ppc_fp128, ppc_fp128 }) align 16 [[TMP0:%.*]], ptr noundef byval({ ppc_fp128, ppc_fp128 }) align 16 [[TMP1:%.*]]) #[[ATTR0]] { +// CHECK-DEF-NEXT: [[TMP3:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP1]], i32 0, i32 0 +// CHECK-DEF-NEXT: [[TMP4:%.*]] = load ppc_fp128, ptr [[TMP3]], align 16 +// CHECK-DEF-NEXT: [[TMP5:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP1]], i32 0, i32 1 +// CHECK-DEF-NEXT: [[TMP6:%.*]] = load ppc_fp128, ptr [[TMP5]], align 16 +// CHECK-DEF-NEXT: [[TMP7:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP0]], i32 0, i32 0 +// CHECK-DEF-NEXT: [[TMP8:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP0]], i32 0, i32 1 +// CHECK-DEF-NEXT: store ppc_fp128 [[TMP4]], ptr [[TMP7]], align 16 +// CHECK-DEF-NEXT: store ppc_fp128 [[TMP6]], ptr [[TMP8]], align 16 +// CHECK-DEF-NEXT: [[TMP9:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP0]], i32 0, i32 0 +// CHECK-DEF-NEXT: [[TMP10:%.*]] = load ppc_fp128, ptr [[TMP9]], align 16 +// CHECK-DEF-NEXT: [[TMP11:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP0]], i32 0, i32 1 +// CHECK-DEF-NEXT: [[TMP12:%.*]] = load ppc_fp128, ptr [[TMP11]], align 16 +// CHECK-DEF-NEXT: [[TMP13:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP0]], i32 0, i32 0 +// CHECK-DEF-NEXT: [[TMP14:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP0]], i32 0, i32 1 +// CHECK-DEF-NEXT: store ppc_fp128 [[TMP10]], ptr [[TMP13]], align 16 +// CHECK-DEF-NEXT: store ppc_fp128 [[TMP12]], ptr [[TMP14]], align 16 +// CHECK-DEF-NEXT: ret void +// +// CHECK-GNU-LABEL: define dso_local [4 x i64] @foo3 +// CHECK-GNU-SAME: ([4 x i64] noundef [[TMP0:%.*]]) #[[ATTR0]] { +// CHECK-GNU-NEXT: [[TMP2:%.*]] = alloca { ppc_fp128, ppc_fp128 }, align 16 +// CHECK-GNU-NEXT: [[TMP3:%.*]] = alloca { ppc_fp128, ppc_fp128 }, align 16 +// CHECK-GNU-NEXT: store [4 x i64] [[TMP0]], ptr [[TMP3]], align 16 +// CHECK-GNU-NEXT: [[TMP4:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP3]], i32 0, i32 0 +// CHECK-GNU-NEXT: [[TMP5:%.*]] = load ppc_fp128, ptr [[TMP4]], align 16 +// CHECK-GNU-NEXT: [[TMP6:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP3]], i32 0, i32 1 +// CHECK-GNU-NEXT: [[TMP7:%.*]] = load ppc_fp128, ptr [[TMP6]], align 16 +// CHECK-GNU-NEXT: [[TMP8:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP2]], i32 0, i32 0 +// CHECK-GNU-NEXT: [[TMP9:%.*]] = getelementptr inbounds { ppc_fp128, ppc_fp128 }, ptr [[TMP2]], i32 0, i32 1 +// CHECK-GNU-NEXT: store ppc_fp128 [[TMP5]], ptr [[TMP8]], align 16 +// CHECK-GNU-NEXT: store ppc_fp128 [[TMP7]], ptr [[TMP9]], align 16 +// CHECK-GNU-NEXT: [[TMP10:%.*]] = load [4 x i64], ptr [[TMP2]], align 16 +// CHECK-GNU-NEXT: ret [4 x i64] [[TMP10]] +// +_Complex long double foo3(_Complex long double x) { + return x; +}