xref: /llvm-project/llvm/lib/Target/Mips/MipsCCState.cpp (revision 754ed95b6672b9a678a994cc652862a91cdc4406)
1 //===---- MipsCCState.cpp - CCState with Mips specific extensions ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "MipsCCState.h"
10 #include "MipsSubtarget.h"
11 #include "llvm/IR/Module.h"
12 
13 using namespace llvm;
14 
15 bool MipsCCState::isF128SoftLibCall(const char *CallSym) {
16   const char *const LibCalls[] = {
17       "__addtf3",      "__divtf3",     "__eqtf2",       "__extenddftf2",
18       "__extendsftf2", "__fixtfdi",    "__fixtfsi",     "__fixtfti",
19       "__fixunstfdi",  "__fixunstfsi", "__fixunstfti",  "__floatditf",
20       "__floatsitf",   "__floattitf",  "__floatunditf", "__floatunsitf",
21       "__floatuntitf", "__getf2",      "__gttf2",       "__letf2",
22       "__lttf2",       "__multf3",     "__netf2",       "__powitf2",
23       "__subtf3",      "__trunctfdf2", "__trunctfsf2",  "__unordtf2",
24       "ceill",         "copysignl",    "cosl",          "exp2l",
25       "expl",          "floorl",       "fmal",          "fmaxl",
26       "fmodl",         "log10l",       "log2l",         "logl",
27       "nearbyintl",    "powl",         "rintl",         "roundl",
28       "sinl",          "sqrtl",        "truncl"};
29 
30   // Check that LibCalls is sorted alphabetically.
31   auto Comp = [](const char *S1, const char *S2) { return strcmp(S1, S2) < 0; };
32   assert(llvm::is_sorted(LibCalls, Comp));
33   return std::binary_search(std::begin(LibCalls), std::end(LibCalls), CallSym,
34                             Comp);
35 }
36 
37 /// This function returns true if Ty is fp128, {f128} or i128 which was
38 /// originally a fp128.
39 bool MipsCCState::originalTypeIsF128(const Type *Ty, const char *Func) {
40   if (Ty->isFP128Ty())
41     return true;
42 
43   if (Ty->isStructTy() && Ty->getStructNumElements() == 1 &&
44       Ty->getStructElementType(0)->isFP128Ty())
45     return true;
46 
47   // If the Ty is i128 and the function being called is a long double emulation
48   // routine, then the original type is f128.
49   // FIXME: This is unsound because these functions could be indirectly called
50   return (Func && Ty->isIntegerTy(128) && isF128SoftLibCall(Func));
51 }
52 
53 /// Return true if the original type was vXfXX.
54 bool MipsCCState::originalEVTTypeIsVectorFloat(EVT Ty) {
55   if (Ty.isVector() && Ty.getVectorElementType().isFloatingPoint())
56     return true;
57 
58   return false;
59 }
60 
61 /// Return true if the original type was vXfXX / vXfXX.
62 bool MipsCCState::originalTypeIsVectorFloat(const Type *Ty) {
63   if (Ty->isVectorTy() && Ty->isFPOrFPVectorTy())
64     return true;
65 
66   return false;
67 }
68 
69 MipsCCState::SpecialCallingConvType
70 MipsCCState::getSpecialCallingConvForCallee(const SDNode *Callee,
71                                             const MipsSubtarget &Subtarget) {
72   MipsCCState::SpecialCallingConvType SpecialCallingConv = NoSpecialCallingConv;
73   if (Subtarget.inMips16HardFloat()) {
74     if (const GlobalAddressSDNode *G =
75             dyn_cast<const GlobalAddressSDNode>(Callee)) {
76       llvm::StringRef Sym = G->getGlobal()->getName();
77       Function *F = G->getGlobal()->getParent()->getFunction(Sym);
78       if (F && F->hasFnAttribute("__Mips16RetHelper")) {
79         SpecialCallingConv = Mips16RetHelperConv;
80       }
81     }
82   }
83   return SpecialCallingConv;
84 }
85 
86 void MipsCCState::PreAnalyzeCallResultForF128(
87     const SmallVectorImpl<ISD::InputArg> &Ins,
88     const Type *RetTy, const char *Call) {
89   for (unsigned i = 0; i < Ins.size(); ++i) {
90     OriginalArgWasF128.push_back(
91         originalTypeIsF128(RetTy, Call));
92     OriginalArgWasFloat.push_back(RetTy->isFloatingPointTy());
93   }
94 }
95 
96 /// Identify lowered values that originated from f128 or float arguments and
97 /// record this for use by RetCC_MipsN.
98 void MipsCCState::PreAnalyzeCallReturnForF128(
99     const SmallVectorImpl<ISD::OutputArg> &Outs, const Type *RetTy) {
100   for (unsigned i = 0; i < Outs.size(); ++i) {
101     OriginalArgWasF128.push_back(
102         originalTypeIsF128(RetTy, nullptr));
103     OriginalArgWasFloat.push_back(
104         RetTy->isFloatingPointTy());
105   }
106 }
107 
108 /// Identify lower values that originated from vXfXX and record
109 /// this.
110 void MipsCCState::PreAnalyzeCallResultForVectorFloat(
111     const SmallVectorImpl<ISD::InputArg> &Ins, const Type *RetTy) {
112   for (unsigned i = 0; i < Ins.size(); ++i) {
113     OriginalRetWasFloatVector.push_back(originalTypeIsVectorFloat(RetTy));
114   }
115 }
116 
117 /// Identify lowered values that originated from vXfXX arguments and record
118 /// this.
119 void MipsCCState::PreAnalyzeReturnForVectorFloat(
120     const SmallVectorImpl<ISD::OutputArg> &Outs) {
121   for (unsigned i = 0; i < Outs.size(); ++i) {
122     ISD::OutputArg Out = Outs[i];
123     OriginalRetWasFloatVector.push_back(
124         originalEVTTypeIsVectorFloat(Out.ArgVT));
125   }
126 }
127 
128 void MipsCCState::PreAnalyzeReturnValue(EVT ArgVT) {
129   OriginalRetWasFloatVector.push_back(originalEVTTypeIsVectorFloat(ArgVT));
130 }
131 
132 void MipsCCState::PreAnalyzeCallOperand(const Type *ArgTy, bool IsFixed,
133                                         const char *Func) {
134   OriginalArgWasF128.push_back(originalTypeIsF128(ArgTy, Func));
135   OriginalArgWasFloat.push_back(ArgTy->isFloatingPointTy());
136   OriginalArgWasFloatVector.push_back(ArgTy->isVectorTy());
137   CallOperandIsFixed.push_back(IsFixed);
138 }
139 
140 /// Identify lowered values that originated from f128, float and sret to vXfXX
141 /// arguments and record this.
142 void MipsCCState::PreAnalyzeCallOperands(
143     const SmallVectorImpl<ISD::OutputArg> &Outs,
144     std::vector<TargetLowering::ArgListEntry> &FuncArgs,
145     const char *Func) {
146   for (unsigned i = 0; i < Outs.size(); ++i) {
147     TargetLowering::ArgListEntry FuncArg = FuncArgs[Outs[i].OrigArgIndex];
148 
149     OriginalArgWasF128.push_back(originalTypeIsF128(FuncArg.Ty, Func));
150     OriginalArgWasFloat.push_back(FuncArg.Ty->isFloatingPointTy());
151     OriginalArgWasFloatVector.push_back(FuncArg.Ty->isVectorTy());
152     CallOperandIsFixed.push_back(Outs[i].IsFixed);
153   }
154 }
155 
156 void MipsCCState::PreAnalyzeFormalArgument(const Type *ArgTy,
157                                            ISD::ArgFlagsTy Flags) {
158   // SRet arguments cannot originate from f128 or {f128} returns so we just
159   // push false. We have to handle this specially since SRet arguments
160   // aren't mapped to an original argument.
161   if (Flags.isSRet()) {
162     OriginalArgWasF128.push_back(false);
163     OriginalArgWasFloat.push_back(false);
164     OriginalArgWasFloatVector.push_back(false);
165     return;
166   }
167 
168   OriginalArgWasF128.push_back(originalTypeIsF128(ArgTy, nullptr));
169   OriginalArgWasFloat.push_back(ArgTy->isFloatingPointTy());
170 
171   // The MIPS vector ABI exhibits a corner case of sorts or quirk; if the
172   // first argument is actually an SRet pointer to a vector, then the next
173   // argument slot is $a2.
174   OriginalArgWasFloatVector.push_back(ArgTy->isVectorTy());
175 }
176 
177 /// Identify lowered values that originated from f128, float and vXfXX arguments
178 /// and record this.
179 void MipsCCState::PreAnalyzeFormalArgumentsForF128(
180     const SmallVectorImpl<ISD::InputArg> &Ins) {
181   const MachineFunction &MF = getMachineFunction();
182   for (unsigned i = 0; i < Ins.size(); ++i) {
183     Function::const_arg_iterator FuncArg = MF.getFunction().arg_begin();
184 
185     // SRet arguments cannot originate from f128 or {f128} returns so we just
186     // push false. We have to handle this specially since SRet arguments
187     // aren't mapped to an original argument.
188     if (Ins[i].Flags.isSRet()) {
189       OriginalArgWasF128.push_back(false);
190       OriginalArgWasFloat.push_back(false);
191       OriginalArgWasFloatVector.push_back(false);
192       continue;
193     }
194 
195     assert(Ins[i].getOrigArgIndex() < MF.getFunction().arg_size());
196     std::advance(FuncArg, Ins[i].getOrigArgIndex());
197 
198     OriginalArgWasF128.push_back(
199         originalTypeIsF128(FuncArg->getType(), nullptr));
200     OriginalArgWasFloat.push_back(FuncArg->getType()->isFloatingPointTy());
201 
202     // The MIPS vector ABI exhibits a corner case of sorts or quirk; if the
203     // first argument is actually an SRet pointer to a vector, then the next
204     // argument slot is $a2.
205     OriginalArgWasFloatVector.push_back(FuncArg->getType()->isVectorTy());
206   }
207 }
208