10b57cec5SDimitry Andric //===---- MipsCCState.cpp - CCState with Mips specific extensions ---------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "MipsCCState.h"
100b57cec5SDimitry Andric #include "MipsSubtarget.h"
110b57cec5SDimitry Andric #include "llvm/IR/Module.h"
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric using namespace llvm;
140b57cec5SDimitry Andric
isF128SoftLibCall(const char * CallSym)15*fe6060f1SDimitry Andric bool MipsCCState::isF128SoftLibCall(const char *CallSym) {
160b57cec5SDimitry Andric const char *const LibCalls[] = {
170b57cec5SDimitry Andric "__addtf3", "__divtf3", "__eqtf2", "__extenddftf2",
180b57cec5SDimitry Andric "__extendsftf2", "__fixtfdi", "__fixtfsi", "__fixtfti",
190b57cec5SDimitry Andric "__fixunstfdi", "__fixunstfsi", "__fixunstfti", "__floatditf",
200b57cec5SDimitry Andric "__floatsitf", "__floattitf", "__floatunditf", "__floatunsitf",
210b57cec5SDimitry Andric "__floatuntitf", "__getf2", "__gttf2", "__letf2",
220b57cec5SDimitry Andric "__lttf2", "__multf3", "__netf2", "__powitf2",
230b57cec5SDimitry Andric "__subtf3", "__trunctfdf2", "__trunctfsf2", "__unordtf2",
240b57cec5SDimitry Andric "ceill", "copysignl", "cosl", "exp2l",
250b57cec5SDimitry Andric "expl", "floorl", "fmal", "fmaxl",
260b57cec5SDimitry Andric "fmodl", "log10l", "log2l", "logl",
270b57cec5SDimitry Andric "nearbyintl", "powl", "rintl", "roundl",
280b57cec5SDimitry Andric "sinl", "sqrtl", "truncl"};
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric // Check that LibCalls is sorted alphabetically.
310b57cec5SDimitry Andric auto Comp = [](const char *S1, const char *S2) { return strcmp(S1, S2) < 0; };
325ffd83dbSDimitry Andric assert(llvm::is_sorted(LibCalls, Comp));
335ffd83dbSDimitry Andric return std::binary_search(std::begin(LibCalls), std::end(LibCalls), CallSym,
345ffd83dbSDimitry Andric Comp);
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric /// This function returns true if Ty is fp128, {f128} or i128 which was
380b57cec5SDimitry Andric /// originally a fp128.
originalTypeIsF128(const Type * Ty,const char * Func)39*fe6060f1SDimitry Andric bool MipsCCState::originalTypeIsF128(const Type *Ty, const char *Func) {
400b57cec5SDimitry Andric if (Ty->isFP128Ty())
410b57cec5SDimitry Andric return true;
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric if (Ty->isStructTy() && Ty->getStructNumElements() == 1 &&
440b57cec5SDimitry Andric Ty->getStructElementType(0)->isFP128Ty())
450b57cec5SDimitry Andric return true;
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric // If the Ty is i128 and the function being called is a long double emulation
480b57cec5SDimitry Andric // routine, then the original type is f128.
49*fe6060f1SDimitry Andric // FIXME: This is unsound because these functions could be indirectly called
500b57cec5SDimitry Andric return (Func && Ty->isIntegerTy(128) && isF128SoftLibCall(Func));
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric /// Return true if the original type was vXfXX.
originalEVTTypeIsVectorFloat(EVT Ty)54*fe6060f1SDimitry Andric bool MipsCCState::originalEVTTypeIsVectorFloat(EVT Ty) {
550b57cec5SDimitry Andric if (Ty.isVector() && Ty.getVectorElementType().isFloatingPoint())
560b57cec5SDimitry Andric return true;
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric return false;
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric /// Return true if the original type was vXfXX / vXfXX.
originalTypeIsVectorFloat(const Type * Ty)62*fe6060f1SDimitry Andric bool MipsCCState::originalTypeIsVectorFloat(const Type *Ty) {
630b57cec5SDimitry Andric if (Ty->isVectorTy() && Ty->isFPOrFPVectorTy())
640b57cec5SDimitry Andric return true;
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric return false;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric MipsCCState::SpecialCallingConvType
getSpecialCallingConvForCallee(const SDNode * Callee,const MipsSubtarget & Subtarget)700b57cec5SDimitry Andric MipsCCState::getSpecialCallingConvForCallee(const SDNode *Callee,
710b57cec5SDimitry Andric const MipsSubtarget &Subtarget) {
720b57cec5SDimitry Andric MipsCCState::SpecialCallingConvType SpecialCallingConv = NoSpecialCallingConv;
730b57cec5SDimitry Andric if (Subtarget.inMips16HardFloat()) {
740b57cec5SDimitry Andric if (const GlobalAddressSDNode *G =
750b57cec5SDimitry Andric dyn_cast<const GlobalAddressSDNode>(Callee)) {
760b57cec5SDimitry Andric llvm::StringRef Sym = G->getGlobal()->getName();
770b57cec5SDimitry Andric Function *F = G->getGlobal()->getParent()->getFunction(Sym);
780b57cec5SDimitry Andric if (F && F->hasFnAttribute("__Mips16RetHelper")) {
790b57cec5SDimitry Andric SpecialCallingConv = Mips16RetHelperConv;
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric return SpecialCallingConv;
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric
PreAnalyzeCallResultForF128(const SmallVectorImpl<ISD::InputArg> & Ins,const Type * RetTy,const char * Call)860b57cec5SDimitry Andric void MipsCCState::PreAnalyzeCallResultForF128(
870b57cec5SDimitry Andric const SmallVectorImpl<ISD::InputArg> &Ins,
880b57cec5SDimitry Andric const Type *RetTy, const char *Call) {
890b57cec5SDimitry Andric for (unsigned i = 0; i < Ins.size(); ++i) {
900b57cec5SDimitry Andric OriginalArgWasF128.push_back(
910b57cec5SDimitry Andric originalTypeIsF128(RetTy, Call));
920b57cec5SDimitry Andric OriginalArgWasFloat.push_back(RetTy->isFloatingPointTy());
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric /// Identify lowered values that originated from f128 or float arguments and
970b57cec5SDimitry Andric /// record this for use by RetCC_MipsN.
PreAnalyzeReturnForF128(const SmallVectorImpl<ISD::OutputArg> & Outs)980b57cec5SDimitry Andric void MipsCCState::PreAnalyzeReturnForF128(
990b57cec5SDimitry Andric const SmallVectorImpl<ISD::OutputArg> &Outs) {
1000b57cec5SDimitry Andric const MachineFunction &MF = getMachineFunction();
1010b57cec5SDimitry Andric for (unsigned i = 0; i < Outs.size(); ++i) {
1020b57cec5SDimitry Andric OriginalArgWasF128.push_back(
1030b57cec5SDimitry Andric originalTypeIsF128(MF.getFunction().getReturnType(), nullptr));
1040b57cec5SDimitry Andric OriginalArgWasFloat.push_back(
1050b57cec5SDimitry Andric MF.getFunction().getReturnType()->isFloatingPointTy());
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric /// Identify lower values that originated from vXfXX and record
1100b57cec5SDimitry Andric /// this.
PreAnalyzeCallResultForVectorFloat(const SmallVectorImpl<ISD::InputArg> & Ins,const Type * RetTy)1110b57cec5SDimitry Andric void MipsCCState::PreAnalyzeCallResultForVectorFloat(
1120b57cec5SDimitry Andric const SmallVectorImpl<ISD::InputArg> &Ins, const Type *RetTy) {
1130b57cec5SDimitry Andric for (unsigned i = 0; i < Ins.size(); ++i) {
1140b57cec5SDimitry Andric OriginalRetWasFloatVector.push_back(originalTypeIsVectorFloat(RetTy));
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric /// Identify lowered values that originated from vXfXX arguments and record
1190b57cec5SDimitry Andric /// this.
PreAnalyzeReturnForVectorFloat(const SmallVectorImpl<ISD::OutputArg> & Outs)1200b57cec5SDimitry Andric void MipsCCState::PreAnalyzeReturnForVectorFloat(
1210b57cec5SDimitry Andric const SmallVectorImpl<ISD::OutputArg> &Outs) {
1220b57cec5SDimitry Andric for (unsigned i = 0; i < Outs.size(); ++i) {
1230b57cec5SDimitry Andric ISD::OutputArg Out = Outs[i];
1240b57cec5SDimitry Andric OriginalRetWasFloatVector.push_back(
1250b57cec5SDimitry Andric originalEVTTypeIsVectorFloat(Out.ArgVT));
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric
PreAnalyzeReturnValue(EVT ArgVT)129*fe6060f1SDimitry Andric void MipsCCState::PreAnalyzeReturnValue(EVT ArgVT) {
130*fe6060f1SDimitry Andric OriginalRetWasFloatVector.push_back(originalEVTTypeIsVectorFloat(ArgVT));
131*fe6060f1SDimitry Andric }
132*fe6060f1SDimitry Andric
PreAnalyzeCallOperand(const Type * ArgTy,bool IsFixed,const char * Func)133*fe6060f1SDimitry Andric void MipsCCState::PreAnalyzeCallOperand(const Type *ArgTy, bool IsFixed,
134*fe6060f1SDimitry Andric const char *Func) {
135*fe6060f1SDimitry Andric OriginalArgWasF128.push_back(originalTypeIsF128(ArgTy, Func));
136*fe6060f1SDimitry Andric OriginalArgWasFloat.push_back(ArgTy->isFloatingPointTy());
137*fe6060f1SDimitry Andric OriginalArgWasFloatVector.push_back(ArgTy->isVectorTy());
138*fe6060f1SDimitry Andric CallOperandIsFixed.push_back(IsFixed);
139*fe6060f1SDimitry Andric }
140*fe6060f1SDimitry Andric
1410b57cec5SDimitry Andric /// Identify lowered values that originated from f128, float and sret to vXfXX
1420b57cec5SDimitry Andric /// arguments and record this.
PreAnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> & Outs,std::vector<TargetLowering::ArgListEntry> & FuncArgs,const char * Func)1430b57cec5SDimitry Andric void MipsCCState::PreAnalyzeCallOperands(
1440b57cec5SDimitry Andric const SmallVectorImpl<ISD::OutputArg> &Outs,
1450b57cec5SDimitry Andric std::vector<TargetLowering::ArgListEntry> &FuncArgs,
1460b57cec5SDimitry Andric const char *Func) {
1470b57cec5SDimitry Andric for (unsigned i = 0; i < Outs.size(); ++i) {
1480b57cec5SDimitry Andric TargetLowering::ArgListEntry FuncArg = FuncArgs[Outs[i].OrigArgIndex];
1490b57cec5SDimitry Andric
1500b57cec5SDimitry Andric OriginalArgWasF128.push_back(originalTypeIsF128(FuncArg.Ty, Func));
1510b57cec5SDimitry Andric OriginalArgWasFloat.push_back(FuncArg.Ty->isFloatingPointTy());
1520b57cec5SDimitry Andric OriginalArgWasFloatVector.push_back(FuncArg.Ty->isVectorTy());
1530b57cec5SDimitry Andric CallOperandIsFixed.push_back(Outs[i].IsFixed);
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric
PreAnalyzeFormalArgument(const Type * ArgTy,ISD::ArgFlagsTy Flags)157*fe6060f1SDimitry Andric void MipsCCState::PreAnalyzeFormalArgument(const Type *ArgTy,
158*fe6060f1SDimitry Andric ISD::ArgFlagsTy Flags) {
159*fe6060f1SDimitry Andric // SRet arguments cannot originate from f128 or {f128} returns so we just
160*fe6060f1SDimitry Andric // push false. We have to handle this specially since SRet arguments
161*fe6060f1SDimitry Andric // aren't mapped to an original argument.
162*fe6060f1SDimitry Andric if (Flags.isSRet()) {
163*fe6060f1SDimitry Andric OriginalArgWasF128.push_back(false);
164*fe6060f1SDimitry Andric OriginalArgWasFloat.push_back(false);
165*fe6060f1SDimitry Andric OriginalArgWasFloatVector.push_back(false);
166*fe6060f1SDimitry Andric return;
167*fe6060f1SDimitry Andric }
168*fe6060f1SDimitry Andric
169*fe6060f1SDimitry Andric OriginalArgWasF128.push_back(originalTypeIsF128(ArgTy, nullptr));
170*fe6060f1SDimitry Andric OriginalArgWasFloat.push_back(ArgTy->isFloatingPointTy());
171*fe6060f1SDimitry Andric
172*fe6060f1SDimitry Andric // The MIPS vector ABI exhibits a corner case of sorts or quirk; if the
173*fe6060f1SDimitry Andric // first argument is actually an SRet pointer to a vector, then the next
174*fe6060f1SDimitry Andric // argument slot is $a2.
175*fe6060f1SDimitry Andric OriginalArgWasFloatVector.push_back(ArgTy->isVectorTy());
176*fe6060f1SDimitry Andric }
177*fe6060f1SDimitry Andric
1780b57cec5SDimitry Andric /// Identify lowered values that originated from f128, float and vXfXX arguments
1790b57cec5SDimitry Andric /// and record this.
PreAnalyzeFormalArgumentsForF128(const SmallVectorImpl<ISD::InputArg> & Ins)1800b57cec5SDimitry Andric void MipsCCState::PreAnalyzeFormalArgumentsForF128(
1810b57cec5SDimitry Andric const SmallVectorImpl<ISD::InputArg> &Ins) {
1820b57cec5SDimitry Andric const MachineFunction &MF = getMachineFunction();
1830b57cec5SDimitry Andric for (unsigned i = 0; i < Ins.size(); ++i) {
1840b57cec5SDimitry Andric Function::const_arg_iterator FuncArg = MF.getFunction().arg_begin();
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andric // SRet arguments cannot originate from f128 or {f128} returns so we just
1870b57cec5SDimitry Andric // push false. We have to handle this specially since SRet arguments
1880b57cec5SDimitry Andric // aren't mapped to an original argument.
1890b57cec5SDimitry Andric if (Ins[i].Flags.isSRet()) {
1900b57cec5SDimitry Andric OriginalArgWasF128.push_back(false);
1910b57cec5SDimitry Andric OriginalArgWasFloat.push_back(false);
1920b57cec5SDimitry Andric OriginalArgWasFloatVector.push_back(false);
1930b57cec5SDimitry Andric continue;
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric
1960b57cec5SDimitry Andric assert(Ins[i].getOrigArgIndex() < MF.getFunction().arg_size());
1970b57cec5SDimitry Andric std::advance(FuncArg, Ins[i].getOrigArgIndex());
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andric OriginalArgWasF128.push_back(
2000b57cec5SDimitry Andric originalTypeIsF128(FuncArg->getType(), nullptr));
2010b57cec5SDimitry Andric OriginalArgWasFloat.push_back(FuncArg->getType()->isFloatingPointTy());
2020b57cec5SDimitry Andric
2030b57cec5SDimitry Andric // The MIPS vector ABI exhibits a corner case of sorts or quirk; if the
2040b57cec5SDimitry Andric // first argument is actually an SRet pointer to a vector, then the next
2050b57cec5SDimitry Andric // argument slot is $a2.
2060b57cec5SDimitry Andric OriginalArgWasFloatVector.push_back(FuncArg->getType()->isVectorTy());
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric }
209