xref: /aosp_15_r20/external/llvm/lib/Target/Mips/MipsCCState.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===---- MipsCCState.cpp - CCState with Mips specific extensions ---------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker 
10*9880d681SAndroid Build Coastguard Worker #include "MipsCCState.h"
11*9880d681SAndroid Build Coastguard Worker #include "MipsSubtarget.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker using namespace llvm;
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker /// This function returns true if CallSym is a long double emulation routine.
isF128SoftLibCall(const char * CallSym)17*9880d681SAndroid Build Coastguard Worker static bool isF128SoftLibCall(const char *CallSym) {
18*9880d681SAndroid Build Coastguard Worker   const char *const LibCalls[] = {
19*9880d681SAndroid Build Coastguard Worker       "__addtf3",      "__divtf3",     "__eqtf2",       "__extenddftf2",
20*9880d681SAndroid Build Coastguard Worker       "__extendsftf2", "__fixtfdi",    "__fixtfsi",     "__fixtfti",
21*9880d681SAndroid Build Coastguard Worker       "__fixunstfdi",  "__fixunstfsi", "__fixunstfti",  "__floatditf",
22*9880d681SAndroid Build Coastguard Worker       "__floatsitf",   "__floattitf",  "__floatunditf", "__floatunsitf",
23*9880d681SAndroid Build Coastguard Worker       "__floatuntitf", "__getf2",      "__gttf2",       "__letf2",
24*9880d681SAndroid Build Coastguard Worker       "__lttf2",       "__multf3",     "__netf2",       "__powitf2",
25*9880d681SAndroid Build Coastguard Worker       "__subtf3",      "__trunctfdf2", "__trunctfsf2",  "__unordtf2",
26*9880d681SAndroid Build Coastguard Worker       "ceill",         "copysignl",    "cosl",          "exp2l",
27*9880d681SAndroid Build Coastguard Worker       "expl",          "floorl",       "fmal",          "fmodl",
28*9880d681SAndroid Build Coastguard Worker       "log10l",        "log2l",        "logl",          "nearbyintl",
29*9880d681SAndroid Build Coastguard Worker       "powl",          "rintl",        "roundl",        "sinl",
30*9880d681SAndroid Build Coastguard Worker       "sqrtl",         "truncl"};
31*9880d681SAndroid Build Coastguard Worker 
32*9880d681SAndroid Build Coastguard Worker   // Check that LibCalls is sorted alphabetically.
33*9880d681SAndroid Build Coastguard Worker   auto Comp = [](const char *S1, const char *S2) { return strcmp(S1, S2) < 0; };
34*9880d681SAndroid Build Coastguard Worker   assert(std::is_sorted(std::begin(LibCalls), std::end(LibCalls), Comp));
35*9880d681SAndroid Build Coastguard Worker   return std::binary_search(std::begin(LibCalls), std::end(LibCalls),
36*9880d681SAndroid Build Coastguard Worker                             CallSym, Comp);
37*9880d681SAndroid Build Coastguard Worker }
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker /// This function returns true if Ty is fp128, {f128} or i128 which was
40*9880d681SAndroid Build Coastguard Worker /// originally a fp128.
originalTypeIsF128(Type * Ty,const SDNode * CallNode)41*9880d681SAndroid Build Coastguard Worker static bool originalTypeIsF128(Type *Ty, const SDNode *CallNode) {
42*9880d681SAndroid Build Coastguard Worker   if (Ty->isFP128Ty())
43*9880d681SAndroid Build Coastguard Worker     return true;
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker   if (Ty->isStructTy() && Ty->getStructNumElements() == 1 &&
46*9880d681SAndroid Build Coastguard Worker       Ty->getStructElementType(0)->isFP128Ty())
47*9880d681SAndroid Build Coastguard Worker     return true;
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker   const ExternalSymbolSDNode *ES =
50*9880d681SAndroid Build Coastguard Worker       dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker   // If the Ty is i128 and the function being called is a long double emulation
53*9880d681SAndroid Build Coastguard Worker   // routine, then the original type is f128.
54*9880d681SAndroid Build Coastguard Worker   return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
55*9880d681SAndroid Build Coastguard Worker }
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker MipsCCState::SpecialCallingConvType
getSpecialCallingConvForCallee(const SDNode * Callee,const MipsSubtarget & Subtarget)58*9880d681SAndroid Build Coastguard Worker MipsCCState::getSpecialCallingConvForCallee(const SDNode *Callee,
59*9880d681SAndroid Build Coastguard Worker                                             const MipsSubtarget &Subtarget) {
60*9880d681SAndroid Build Coastguard Worker   MipsCCState::SpecialCallingConvType SpecialCallingConv = NoSpecialCallingConv;
61*9880d681SAndroid Build Coastguard Worker   if (Subtarget.inMips16HardFloat()) {
62*9880d681SAndroid Build Coastguard Worker     if (const GlobalAddressSDNode *G =
63*9880d681SAndroid Build Coastguard Worker             dyn_cast<const GlobalAddressSDNode>(Callee)) {
64*9880d681SAndroid Build Coastguard Worker       llvm::StringRef Sym = G->getGlobal()->getName();
65*9880d681SAndroid Build Coastguard Worker       Function *F = G->getGlobal()->getParent()->getFunction(Sym);
66*9880d681SAndroid Build Coastguard Worker       if (F && F->hasFnAttribute("__Mips16RetHelper")) {
67*9880d681SAndroid Build Coastguard Worker         SpecialCallingConv = Mips16RetHelperConv;
68*9880d681SAndroid Build Coastguard Worker       }
69*9880d681SAndroid Build Coastguard Worker     }
70*9880d681SAndroid Build Coastguard Worker   }
71*9880d681SAndroid Build Coastguard Worker   return SpecialCallingConv;
72*9880d681SAndroid Build Coastguard Worker }
73*9880d681SAndroid Build Coastguard Worker 
PreAnalyzeCallResultForF128(const SmallVectorImpl<ISD::InputArg> & Ins,const TargetLowering::CallLoweringInfo & CLI)74*9880d681SAndroid Build Coastguard Worker void MipsCCState::PreAnalyzeCallResultForF128(
75*9880d681SAndroid Build Coastguard Worker     const SmallVectorImpl<ISD::InputArg> &Ins,
76*9880d681SAndroid Build Coastguard Worker     const TargetLowering::CallLoweringInfo &CLI) {
77*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0; i < Ins.size(); ++i) {
78*9880d681SAndroid Build Coastguard Worker     OriginalArgWasF128.push_back(
79*9880d681SAndroid Build Coastguard Worker         originalTypeIsF128(CLI.RetTy, CLI.Callee.getNode()));
80*9880d681SAndroid Build Coastguard Worker     OriginalArgWasFloat.push_back(CLI.RetTy->isFloatingPointTy());
81*9880d681SAndroid Build Coastguard Worker   }
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker /// Identify lowered values that originated from f128 arguments and record
85*9880d681SAndroid Build Coastguard Worker /// this for use by RetCC_MipsN.
PreAnalyzeReturnForF128(const SmallVectorImpl<ISD::OutputArg> & Outs)86*9880d681SAndroid Build Coastguard Worker void MipsCCState::PreAnalyzeReturnForF128(
87*9880d681SAndroid Build Coastguard Worker     const SmallVectorImpl<ISD::OutputArg> &Outs) {
88*9880d681SAndroid Build Coastguard Worker   const MachineFunction &MF = getMachineFunction();
89*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0; i < Outs.size(); ++i) {
90*9880d681SAndroid Build Coastguard Worker     OriginalArgWasF128.push_back(
91*9880d681SAndroid Build Coastguard Worker         originalTypeIsF128(MF.getFunction()->getReturnType(), nullptr));
92*9880d681SAndroid Build Coastguard Worker     OriginalArgWasFloat.push_back(
93*9880d681SAndroid Build Coastguard Worker         MF.getFunction()->getReturnType()->isFloatingPointTy());
94*9880d681SAndroid Build Coastguard Worker   }
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker 
97*9880d681SAndroid Build Coastguard Worker /// Identify lowered values that originated from f128 arguments and record
98*9880d681SAndroid Build Coastguard Worker /// this.
PreAnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> & Outs,std::vector<TargetLowering::ArgListEntry> & FuncArgs,const SDNode * CallNode)99*9880d681SAndroid Build Coastguard Worker void MipsCCState::PreAnalyzeCallOperands(
100*9880d681SAndroid Build Coastguard Worker     const SmallVectorImpl<ISD::OutputArg> &Outs,
101*9880d681SAndroid Build Coastguard Worker     std::vector<TargetLowering::ArgListEntry> &FuncArgs,
102*9880d681SAndroid Build Coastguard Worker     const SDNode *CallNode) {
103*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0; i < Outs.size(); ++i) {
104*9880d681SAndroid Build Coastguard Worker     OriginalArgWasF128.push_back(
105*9880d681SAndroid Build Coastguard Worker         originalTypeIsF128(FuncArgs[Outs[i].OrigArgIndex].Ty, CallNode));
106*9880d681SAndroid Build Coastguard Worker     OriginalArgWasFloat.push_back(
107*9880d681SAndroid Build Coastguard Worker         FuncArgs[Outs[i].OrigArgIndex].Ty->isFloatingPointTy());
108*9880d681SAndroid Build Coastguard Worker     CallOperandIsFixed.push_back(Outs[i].IsFixed);
109*9880d681SAndroid Build Coastguard Worker   }
110*9880d681SAndroid Build Coastguard Worker }
111*9880d681SAndroid Build Coastguard Worker 
112*9880d681SAndroid Build Coastguard Worker /// Identify lowered values that originated from f128 arguments and record
113*9880d681SAndroid Build Coastguard Worker /// this.
PreAnalyzeFormalArgumentsForF128(const SmallVectorImpl<ISD::InputArg> & Ins)114*9880d681SAndroid Build Coastguard Worker void MipsCCState::PreAnalyzeFormalArgumentsForF128(
115*9880d681SAndroid Build Coastguard Worker     const SmallVectorImpl<ISD::InputArg> &Ins) {
116*9880d681SAndroid Build Coastguard Worker   const MachineFunction &MF = getMachineFunction();
117*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0; i < Ins.size(); ++i) {
118*9880d681SAndroid Build Coastguard Worker     Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
119*9880d681SAndroid Build Coastguard Worker 
120*9880d681SAndroid Build Coastguard Worker     // SRet arguments cannot originate from f128 or {f128} returns so we just
121*9880d681SAndroid Build Coastguard Worker     // push false. We have to handle this specially since SRet arguments
122*9880d681SAndroid Build Coastguard Worker     // aren't mapped to an original argument.
123*9880d681SAndroid Build Coastguard Worker     if (Ins[i].Flags.isSRet()) {
124*9880d681SAndroid Build Coastguard Worker       OriginalArgWasF128.push_back(false);
125*9880d681SAndroid Build Coastguard Worker       OriginalArgWasFloat.push_back(false);
126*9880d681SAndroid Build Coastguard Worker       continue;
127*9880d681SAndroid Build Coastguard Worker     }
128*9880d681SAndroid Build Coastguard Worker 
129*9880d681SAndroid Build Coastguard Worker     assert(Ins[i].getOrigArgIndex() < MF.getFunction()->arg_size());
130*9880d681SAndroid Build Coastguard Worker     std::advance(FuncArg, Ins[i].getOrigArgIndex());
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker     OriginalArgWasF128.push_back(
133*9880d681SAndroid Build Coastguard Worker         originalTypeIsF128(FuncArg->getType(), nullptr));
134*9880d681SAndroid Build Coastguard Worker     OriginalArgWasFloat.push_back(FuncArg->getType()->isFloatingPointTy());
135*9880d681SAndroid Build Coastguard Worker   }
136*9880d681SAndroid Build Coastguard Worker }
137