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