blob: ef48c850a1b866ec7c4e3023bec296984b4882a6 [file] [log] [blame]
Daniel Sanders0456c152014-11-07 14:24:31 +00001//===---- MipsCCState.cpp - CCState with Mips specific extensions ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Daniel Sanders0456c152014-11-07 14:24:31 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "MipsCCState.h"
10#include "MipsSubtarget.h"
11#include "llvm/IR/Module.h"
12
13using namespace llvm;
14
15/// This function returns true if CallSym is a long double emulation routine.
16static bool isF128SoftLibCall(const char *CallSym) {
17 const char *const LibCalls[] = {
18 "__addtf3", "__divtf3", "__eqtf2", "__extenddftf2",
19 "__extendsftf2", "__fixtfdi", "__fixtfsi", "__fixtfti",
20 "__fixunstfdi", "__fixunstfsi", "__fixunstfti", "__floatditf",
21 "__floatsitf", "__floattitf", "__floatunditf", "__floatunsitf",
22 "__floatuntitf", "__getf2", "__gttf2", "__letf2",
23 "__lttf2", "__multf3", "__netf2", "__powitf2",
24 "__subtf3", "__trunctfdf2", "__trunctfsf2", "__unordtf2",
25 "ceill", "copysignl", "cosl", "exp2l",
Stefan Maksimovic285c0f42018-10-12 08:18:38 +000026 "expl", "floorl", "fmal", "fmaxl",
27 "fmodl", "log10l", "log2l", "logl",
28 "nearbyintl", "powl", "rintl", "roundl",
29 "sinl", "sqrtl", "truncl"};
Daniel Sanders0456c152014-11-07 14:24:31 +000030
Daniel Sanders0456c152014-11-07 14:24:31 +000031 // Check that LibCalls is sorted alphabetically.
Craig Toppera8334512015-10-17 21:32:26 +000032 auto Comp = [](const char *S1, const char *S2) { return strcmp(S1, S2) < 0; };
33 assert(std::is_sorted(std::begin(LibCalls), std::end(LibCalls), Comp));
34 return std::binary_search(std::begin(LibCalls), std::end(LibCalls),
35 CallSym, Comp);
Daniel Sanders0456c152014-11-07 14:24:31 +000036}
37
38/// This function returns true if Ty is fp128, {f128} or i128 which was
39/// originally a fp128.
Simon Dardis70f79252017-04-26 11:10:38 +000040static bool originalTypeIsF128(const Type *Ty, const char *Func) {
Daniel Sanders0456c152014-11-07 14:24:31 +000041 if (Ty->isFP128Ty())
42 return true;
43
44 if (Ty->isStructTy() && Ty->getStructNumElements() == 1 &&
45 Ty->getStructElementType(0)->isFP128Ty())
46 return true;
47
Daniel Sanders0456c152014-11-07 14:24:31 +000048 // If the Ty is i128 and the function being called is a long double emulation
49 // routine, then the original type is f128.
Simon Dardis70f79252017-04-26 11:10:38 +000050 return (Func && Ty->isIntegerTy(128) && isF128SoftLibCall(Func));
Daniel Sanders0456c152014-11-07 14:24:31 +000051}
52
Simon Dardis212cccb2017-06-09 14:37:08 +000053/// Return true if the original type was vXfXX.
54static bool 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.
62static bool originalTypeIsVectorFloat(const Type * Ty) {
63 if (Ty->isVectorTy() && Ty->isFPOrFPVectorTy())
64 return true;
65
66 return false;
67}
68
Daniel Sanders0456c152014-11-07 14:24:31 +000069MipsCCState::SpecialCallingConvType
70MipsCCState::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
86void MipsCCState::PreAnalyzeCallResultForF128(
87 const SmallVectorImpl<ISD::InputArg> &Ins,
Simon Dardis70f79252017-04-26 11:10:38 +000088 const Type *RetTy, const char *Call) {
Daniel Sandersc43cda82014-11-07 16:54:21 +000089 for (unsigned i = 0; i < Ins.size(); ++i) {
Daniel Sanders0456c152014-11-07 14:24:31 +000090 OriginalArgWasF128.push_back(
Simon Dardis70f79252017-04-26 11:10:38 +000091 originalTypeIsF128(RetTy, Call));
92 OriginalArgWasFloat.push_back(RetTy->isFloatingPointTy());
Daniel Sandersc43cda82014-11-07 16:54:21 +000093 }
Daniel Sanders0456c152014-11-07 14:24:31 +000094}
95
Simon Dardis212cccb2017-06-09 14:37:08 +000096/// Identify lowered values that originated from f128 or float arguments and
97/// record this for use by RetCC_MipsN.
Daniel Sanders0456c152014-11-07 14:24:31 +000098void MipsCCState::PreAnalyzeReturnForF128(
99 const SmallVectorImpl<ISD::OutputArg> &Outs) {
100 const MachineFunction &MF = getMachineFunction();
Daniel Sandersc43cda82014-11-07 16:54:21 +0000101 for (unsigned i = 0; i < Outs.size(); ++i) {
Daniel Sanders0456c152014-11-07 14:24:31 +0000102 OriginalArgWasF128.push_back(
Matthias Braunf1caa282017-12-15 22:22:58 +0000103 originalTypeIsF128(MF.getFunction().getReturnType(), nullptr));
Daniel Sandersc43cda82014-11-07 16:54:21 +0000104 OriginalArgWasFloat.push_back(
Matthias Braunf1caa282017-12-15 22:22:58 +0000105 MF.getFunction().getReturnType()->isFloatingPointTy());
Daniel Sandersc43cda82014-11-07 16:54:21 +0000106 }
Daniel Sanders0456c152014-11-07 14:24:31 +0000107}
108
Simon Dardis212cccb2017-06-09 14:37:08 +0000109/// Identify lower values that originated from vXfXX and record
Daniel Sanders0456c152014-11-07 14:24:31 +0000110/// this.
Simon Dardis212cccb2017-06-09 14:37:08 +0000111void MipsCCState::PreAnalyzeCallResultForVectorFloat(
112 const SmallVectorImpl<ISD::InputArg> &Ins, const Type *RetTy) {
113 for (unsigned i = 0; i < Ins.size(); ++i) {
114 OriginalRetWasFloatVector.push_back(originalTypeIsVectorFloat(RetTy));
115 }
116}
117
118/// Identify lowered values that originated from vXfXX arguments and record
119/// this.
120void MipsCCState::PreAnalyzeReturnForVectorFloat(
121 const SmallVectorImpl<ISD::OutputArg> &Outs) {
122 for (unsigned i = 0; i < Outs.size(); ++i) {
123 ISD::OutputArg Out = Outs[i];
124 OriginalRetWasFloatVector.push_back(
125 originalEVTTypeIsVectorFloat(Out.ArgVT));
126 }
127}
128
129/// Identify lowered values that originated from f128, float and sret to vXfXX
130/// arguments and record this.
Daniel Sanders0456c152014-11-07 14:24:31 +0000131void MipsCCState::PreAnalyzeCallOperands(
132 const SmallVectorImpl<ISD::OutputArg> &Outs,
133 std::vector<TargetLowering::ArgListEntry> &FuncArgs,
Simon Dardis70f79252017-04-26 11:10:38 +0000134 const char *Func) {
Daniel Sanders0456c152014-11-07 14:24:31 +0000135 for (unsigned i = 0; i < Outs.size(); ++i) {
Simon Dardis212cccb2017-06-09 14:37:08 +0000136 TargetLowering::ArgListEntry FuncArg = FuncArgs[Outs[i].OrigArgIndex];
137
138 OriginalArgWasF128.push_back(originalTypeIsF128(FuncArg.Ty, Func));
139 OriginalArgWasFloat.push_back(FuncArg.Ty->isFloatingPointTy());
140 OriginalArgWasFloatVector.push_back(FuncArg.Ty->isVectorTy());
Daniel Sanders0456c152014-11-07 14:24:31 +0000141 CallOperandIsFixed.push_back(Outs[i].IsFixed);
142 }
143}
144
Simon Dardis212cccb2017-06-09 14:37:08 +0000145/// Identify lowered values that originated from f128, float and vXfXX arguments
146/// and record this.
Daniel Sanders0456c152014-11-07 14:24:31 +0000147void MipsCCState::PreAnalyzeFormalArgumentsForF128(
148 const SmallVectorImpl<ISD::InputArg> &Ins) {
149 const MachineFunction &MF = getMachineFunction();
150 for (unsigned i = 0; i < Ins.size(); ++i) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000151 Function::const_arg_iterator FuncArg = MF.getFunction().arg_begin();
Daniel Sanders0456c152014-11-07 14:24:31 +0000152
153 // SRet arguments cannot originate from f128 or {f128} returns so we just
154 // push false. We have to handle this specially since SRet arguments
155 // aren't mapped to an original argument.
156 if (Ins[i].Flags.isSRet()) {
157 OriginalArgWasF128.push_back(false);
Daniel Sanders87f9b88b2014-11-10 15:57:53 +0000158 OriginalArgWasFloat.push_back(false);
Simon Dardis212cccb2017-06-09 14:37:08 +0000159 OriginalArgWasFloatVector.push_back(false);
Daniel Sanders0456c152014-11-07 14:24:31 +0000160 continue;
161 }
162
Matthias Braunf1caa282017-12-15 22:22:58 +0000163 assert(Ins[i].getOrigArgIndex() < MF.getFunction().arg_size());
Andrew Trick05938a52015-02-16 18:10:47 +0000164 std::advance(FuncArg, Ins[i].getOrigArgIndex());
Daniel Sanders0456c152014-11-07 14:24:31 +0000165
166 OriginalArgWasF128.push_back(
167 originalTypeIsF128(FuncArg->getType(), nullptr));
Daniel Sandersc43cda82014-11-07 16:54:21 +0000168 OriginalArgWasFloat.push_back(FuncArg->getType()->isFloatingPointTy());
Simon Dardis212cccb2017-06-09 14:37:08 +0000169
170 // The MIPS vector ABI exhibits a corner case of sorts or quirk; if the
171 // first argument is actually an SRet pointer to a vector, then the next
172 // argument slot is $a2.
173 OriginalArgWasFloatVector.push_back(FuncArg->getType()->isVectorTy());
Daniel Sanders0456c152014-11-07 14:24:31 +0000174 }
175}