blob: 5a5b78c9d5f97e2a19f0859ac573750544ad2ca0 [file] [log] [blame]
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001//===-- Mips16ISelLowering.h - Mips16 DAG Lowering Interface ----*- C++ -*-===//
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
Akira Hatanaka96ca1822013-03-13 00:54:29 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Subclass of MipsTargetLowering specialized for mips16.
10//
11//===----------------------------------------------------------------------===//
Akira Hatanaka96ca1822013-03-13 00:54:29 +000012#include "Mips16ISelLowering.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000013#include "MCTargetDesc/MipsBaseInfo.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000014#include "Mips16HardFloatInfo.h"
15#include "MipsMachineFunction.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000016#include "MipsRegisterInfo.h"
Reed Kotler783c7942013-05-10 22:25:39 +000017#include "MipsTargetMachine.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000019#include "llvm/CodeGen/TargetInstrInfo.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000020#include "llvm/Support/CommandLine.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000021
22using namespace llvm;
23
Chandler Carruth84e68b22014-04-22 02:41:26 +000024#define DEBUG_TYPE "mips-lower"
25
Akira Hatanaka96ca1822013-03-13 00:54:29 +000026static cl::opt<bool> DontExpandCondPseudos16(
27 "mips16-dont-expand-cond-pseudo",
28 cl::init(false),
Sylvestre Ledru469de192014-08-11 18:04:46 +000029 cl::desc("Don't expand conditional move related "
Akira Hatanaka96ca1822013-03-13 00:54:29 +000030 "pseudos for Mips 16"),
31 cl::Hidden);
32
33namespace {
Benjamin Kramerc4d547d2013-06-13 19:06:52 +000034struct Mips16Libcall {
35 RTLIB::Libcall Libcall;
36 const char *Name;
37
38 bool operator<(const Mips16Libcall &RHS) const {
39 return std::strcmp(Name, RHS.Name) < 0;
40 }
41};
Reed Kotler83f879d2013-08-01 21:17:53 +000042
43struct Mips16IntrinsicHelperType{
44 const char* Name;
45 const char* Helper;
46
47 bool operator<(const Mips16IntrinsicHelperType &RHS) const {
48 return std::strcmp(Name, RHS.Name) < 0;
49 }
50 bool operator==(const Mips16IntrinsicHelperType &RHS) const {
51 return std::strcmp(Name, RHS.Name) == 0;
52 }
53};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000054}
Akira Hatanaka96ca1822013-03-13 00:54:29 +000055
Benjamin Kramerc4d547d2013-06-13 19:06:52 +000056// Libcalls for which no helper is generated. Sorted by name for binary search.
57static const Mips16Libcall HardFloatLibCalls[] = {
58 { RTLIB::ADD_F64, "__mips16_adddf3" },
59 { RTLIB::ADD_F32, "__mips16_addsf3" },
60 { RTLIB::DIV_F64, "__mips16_divdf3" },
61 { RTLIB::DIV_F32, "__mips16_divsf3" },
62 { RTLIB::OEQ_F64, "__mips16_eqdf2" },
63 { RTLIB::OEQ_F32, "__mips16_eqsf2" },
64 { RTLIB::FPEXT_F32_F64, "__mips16_extendsfdf2" },
65 { RTLIB::FPTOSINT_F64_I32, "__mips16_fix_truncdfsi" },
66 { RTLIB::FPTOSINT_F32_I32, "__mips16_fix_truncsfsi" },
67 { RTLIB::SINTTOFP_I32_F64, "__mips16_floatsidf" },
68 { RTLIB::SINTTOFP_I32_F32, "__mips16_floatsisf" },
69 { RTLIB::UINTTOFP_I32_F64, "__mips16_floatunsidf" },
70 { RTLIB::UINTTOFP_I32_F32, "__mips16_floatunsisf" },
71 { RTLIB::OGE_F64, "__mips16_gedf2" },
72 { RTLIB::OGE_F32, "__mips16_gesf2" },
73 { RTLIB::OGT_F64, "__mips16_gtdf2" },
74 { RTLIB::OGT_F32, "__mips16_gtsf2" },
75 { RTLIB::OLE_F64, "__mips16_ledf2" },
76 { RTLIB::OLE_F32, "__mips16_lesf2" },
77 { RTLIB::OLT_F64, "__mips16_ltdf2" },
78 { RTLIB::OLT_F32, "__mips16_ltsf2" },
79 { RTLIB::MUL_F64, "__mips16_muldf3" },
80 { RTLIB::MUL_F32, "__mips16_mulsf3" },
81 { RTLIB::UNE_F64, "__mips16_nedf2" },
82 { RTLIB::UNE_F32, "__mips16_nesf2" },
83 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_dc" }, // No associated libcall.
84 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_df" }, // No associated libcall.
85 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_sc" }, // No associated libcall.
86 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_sf" }, // No associated libcall.
87 { RTLIB::SUB_F64, "__mips16_subdf3" },
88 { RTLIB::SUB_F32, "__mips16_subsf3" },
89 { RTLIB::FPROUND_F64_F32, "__mips16_truncdfsf2" },
90 { RTLIB::UO_F64, "__mips16_unorddf2" },
91 { RTLIB::UO_F32, "__mips16_unordsf2" }
92};
93
Reed Kotler83f879d2013-08-01 21:17:53 +000094static const Mips16IntrinsicHelperType Mips16IntrinsicHelper[] = {
Reed Kotlerbe316cf2013-08-09 21:33:41 +000095 {"__fixunsdfsi", "__mips16_call_stub_2" },
Reed Kotler83f879d2013-08-01 21:17:53 +000096 {"ceil", "__mips16_call_stub_df_2"},
97 {"ceilf", "__mips16_call_stub_sf_1"},
98 {"copysign", "__mips16_call_stub_df_10"},
99 {"copysignf", "__mips16_call_stub_sf_5"},
100 {"cos", "__mips16_call_stub_df_2"},
101 {"cosf", "__mips16_call_stub_sf_1"},
102 {"exp2", "__mips16_call_stub_df_2"},
103 {"exp2f", "__mips16_call_stub_sf_1"},
104 {"floor", "__mips16_call_stub_df_2"},
105 {"floorf", "__mips16_call_stub_sf_1"},
106 {"log2", "__mips16_call_stub_df_2"},
107 {"log2f", "__mips16_call_stub_sf_1"},
108 {"nearbyint", "__mips16_call_stub_df_2"},
109 {"nearbyintf", "__mips16_call_stub_sf_1"},
110 {"rint", "__mips16_call_stub_df_2"},
111 {"rintf", "__mips16_call_stub_sf_1"},
112 {"sin", "__mips16_call_stub_df_2"},
113 {"sinf", "__mips16_call_stub_sf_1"},
114 {"sqrt", "__mips16_call_stub_df_2"},
115 {"sqrtf", "__mips16_call_stub_sf_1"},
116 {"trunc", "__mips16_call_stub_df_2"},
117 {"truncf", "__mips16_call_stub_sf_1"},
118};
119
Eric Christopherb1526602014-09-19 23:30:42 +0000120Mips16TargetLowering::Mips16TargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000121 const MipsSubtarget &STI)
122 : MipsTargetLowering(TM, STI) {
Reed Kotlerfafaa9d2013-03-14 22:02:09 +0000123
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000124 // Set up the register classes
125 addRegisterClass(MVT::i32, &Mips::CPU16RegsRegClass);
126
Eric Christophere8ae3e32015-05-07 23:10:21 +0000127 if (!Subtarget.useSoftFloat())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000128 setMips16HardFloatLibCalls();
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000129
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000130 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
131 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
132 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
133 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
134 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
135 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
136 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
137 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
138 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
139 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
140 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
141 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
142 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
143
Reed Kotler97309af2013-10-08 17:32:33 +0000144 setOperationAction(ISD::ROTR, MVT::i32, Expand);
145 setOperationAction(ISD::ROTR, MVT::i64, Expand);
146 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
147 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
148
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000149 computeRegisterProperties(STI.getRegisterInfo());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000150}
151
152const MipsTargetLowering *
Eric Christopherb1526602014-09-19 23:30:42 +0000153llvm::createMips16TargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000154 const MipsSubtarget &STI) {
155 return new Mips16TargetLowering(TM, STI);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000156}
157
Simon Pilgrim4e0648a2019-06-12 17:14:03 +0000158bool Mips16TargetLowering::allowsMisalignedMemoryAccesses(
159 EVT VT, unsigned, unsigned, MachineMemOperand::Flags, bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000160 return false;
161}
162
163MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000164Mips16TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000165 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000166 switch (MI.getOpcode()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000167 default:
168 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
169 case Mips::SelBeqZ:
170 return emitSel16(Mips::BeqzRxImm16, MI, BB);
171 case Mips::SelBneZ:
172 return emitSel16(Mips::BnezRxImm16, MI, BB);
173 case Mips::SelTBteqZCmpi:
Reed Kotler09e59152013-11-15 02:21:52 +0000174 return emitSeliT16(Mips::Bteqz16, Mips::CmpiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000175 case Mips::SelTBteqZSlti:
Reed Kotler09e59152013-11-15 02:21:52 +0000176 return emitSeliT16(Mips::Bteqz16, Mips::SltiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000177 case Mips::SelTBteqZSltiu:
Reed Kotler09e59152013-11-15 02:21:52 +0000178 return emitSeliT16(Mips::Bteqz16, Mips::SltiuRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000179 case Mips::SelTBtneZCmpi:
Reed Kotler09e59152013-11-15 02:21:52 +0000180 return emitSeliT16(Mips::Btnez16, Mips::CmpiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000181 case Mips::SelTBtneZSlti:
Reed Kotler09e59152013-11-15 02:21:52 +0000182 return emitSeliT16(Mips::Btnez16, Mips::SltiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000183 case Mips::SelTBtneZSltiu:
Reed Kotler09e59152013-11-15 02:21:52 +0000184 return emitSeliT16(Mips::Btnez16, Mips::SltiuRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000185 case Mips::SelTBteqZCmp:
Reed Kotler09e59152013-11-15 02:21:52 +0000186 return emitSelT16(Mips::Bteqz16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000187 case Mips::SelTBteqZSlt:
Reed Kotler09e59152013-11-15 02:21:52 +0000188 return emitSelT16(Mips::Bteqz16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000189 case Mips::SelTBteqZSltu:
Reed Kotler09e59152013-11-15 02:21:52 +0000190 return emitSelT16(Mips::Bteqz16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000191 case Mips::SelTBtneZCmp:
Reed Kotler09e59152013-11-15 02:21:52 +0000192 return emitSelT16(Mips::Btnez16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000193 case Mips::SelTBtneZSlt:
Reed Kotler09e59152013-11-15 02:21:52 +0000194 return emitSelT16(Mips::Btnez16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000195 case Mips::SelTBtneZSltu:
Reed Kotler09e59152013-11-15 02:21:52 +0000196 return emitSelT16(Mips::Btnez16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000197 case Mips::BteqzT8CmpX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000198 return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000199 case Mips::BteqzT8SltX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000200 return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000201 case Mips::BteqzT8SltuX16:
202 // TBD: figure out a way to get this or remove the instruction
203 // altogether.
Reed Kotler09e59152013-11-15 02:21:52 +0000204 return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000205 case Mips::BtnezT8CmpX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000206 return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000207 case Mips::BtnezT8SltX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000208 return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000209 case Mips::BtnezT8SltuX16:
210 // TBD: figure out a way to get this or remove the instruction
211 // altogether.
Reed Kotler09e59152013-11-15 02:21:52 +0000212 return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000213 case Mips::BteqzT8CmpiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000214 Mips::Bteqz16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000215 case Mips::BteqzT8SltiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000216 Mips::Bteqz16, Mips::SltiRxImm16, Mips::SltiRxImmX16, true, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000217 case Mips::BteqzT8SltiuX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000218 Mips::Bteqz16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000219 case Mips::BtnezT8CmpiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000220 Mips::Btnez16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000221 case Mips::BtnezT8SltiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000222 Mips::Btnez16, Mips::SltiRxImm16, Mips::SltiRxImmX16, true, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000223 case Mips::BtnezT8SltiuX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000224 Mips::Btnez16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000225 break;
226 case Mips::SltCCRxRy16:
227 return emitFEXT_CCRX16_ins(Mips::SltRxRy16, MI, BB);
228 break;
229 case Mips::SltiCCRxImmX16:
230 return emitFEXT_CCRXI16_ins
231 (Mips::SltiRxImm16, Mips::SltiRxImmX16, MI, BB);
232 case Mips::SltiuCCRxImmX16:
233 return emitFEXT_CCRXI16_ins
234 (Mips::SltiuRxImm16, Mips::SltiuRxImmX16, MI, BB);
235 case Mips::SltuCCRxRy16:
236 return emitFEXT_CCRX16_ins
237 (Mips::SltuRxRy16, MI, BB);
238 }
239}
240
Daniel Sanders23e98772014-11-02 16:09:29 +0000241bool Mips16TargetLowering::isEligibleForTailCallOptimization(
242 const CCState &CCInfo, unsigned NextStackOffset,
243 const MipsFunctionInfo &FI) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000244 // No tail call optimization for mips16.
245 return false;
246}
247
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000248void Mips16TargetLowering::setMips16HardFloatLibCalls() {
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000249 for (unsigned I = 0; I != array_lengthof(HardFloatLibCalls); ++I) {
250 assert((I == 0 || HardFloatLibCalls[I - 1] < HardFloatLibCalls[I]) &&
251 "Array not sorted!");
252 if (HardFloatLibCalls[I].Libcall != RTLIB::UNKNOWN_LIBCALL)
253 setLibcallName(HardFloatLibCalls[I].Libcall, HardFloatLibCalls[I].Name);
254 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000255
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000256 setLibcallName(RTLIB::O_F64, "__mips16_unorddf2");
257 setLibcallName(RTLIB::O_F32, "__mips16_unordsf2");
258}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000259
260//
261// The Mips16 hard float is a crazy quilt inherited from gcc. I have a much
262// cleaner way to do all of this but it will have to wait until the traditional
263// gcc mechanism is completed.
264//
265// For Pic, in order for Mips16 code to call Mips32 code which according the abi
266// have either arguments or returned values placed in floating point registers,
267// we use a set of helper functions. (This includes functions which return type
268// complex which on Mips are returned in a pair of floating point registers).
269//
270// This is an encoding that we inherited from gcc.
271// In Mips traditional O32, N32 ABI, floating point numbers are passed in
272// floating point argument registers 1,2 only when the first and optionally
273// the second arguments are float (sf) or double (df).
274// For Mips16 we are only concerned with the situations where floating point
275// arguments are being passed in floating point registers by the ABI, because
276// Mips16 mode code cannot execute floating point instructions to load those
277// values and hence helper functions are needed.
278// The possibilities are (), (sf), (sf, sf), (sf, df), (df), (df, sf), (df, df)
279// the helper function suffixs for these are:
280// 0, 1, 5, 9, 2, 6, 10
281// this suffix can then be calculated as follows:
282// for a given argument Arg:
283// Arg1x, Arg2x = 1 : Arg is sf
284// 2 : Arg is df
285// 0: Arg is neither sf or df
286// So this stub is the string for number Arg1x + Arg2x*4.
287// However not all numbers between 0 and 10 are possible, we check anyway and
288// assert if the impossible exists.
289//
290
291unsigned int Mips16TargetLowering::getMips16HelperFunctionStubNumber
292 (ArgListTy &Args) const {
293 unsigned int resultNum = 0;
294 if (Args.size() >= 1) {
295 Type *t = Args[0].Ty;
296 if (t->isFloatTy()) {
297 resultNum = 1;
298 }
299 else if (t->isDoubleTy()) {
300 resultNum = 2;
301 }
302 }
303 if (resultNum) {
304 if (Args.size() >=2) {
305 Type *t = Args[1].Ty;
306 if (t->isFloatTy()) {
307 resultNum += 4;
308 }
309 else if (t->isDoubleTy()) {
310 resultNum += 8;
311 }
312 }
313 }
314 return resultNum;
315}
316
317//
Eric Christopher9b270d42014-09-29 21:57:52 +0000318// Prefixes are attached to stub numbers depending on the return type.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000319// return type: float sf_
320// double df_
321// single complex sc_
322// double complext dc_
323// others NO PREFIX
324//
325//
326// The full name of a helper function is__mips16_call_stub +
327// return type dependent prefix + stub number
328//
Eric Christopher9b270d42014-09-29 21:57:52 +0000329// FIXME: This is something that probably should be in a different source file
330// and perhaps done differently but my main purpose is to not waste runtime
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000331// on something that we can enumerate in the source. Another possibility is
332// to have a python script to generate these mapping tables. This will do
333// for now. There are a whole series of helper function mapping arrays, one
334// for each return type class as outlined above. There there are 11 possible
Eric Christopher9b270d42014-09-29 21:57:52 +0000335// entries. Ones with 0 are ones which should never be selected.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000336//
337// All the arrays are similar except for ones which return neither
Eric Christopher9b270d42014-09-29 21:57:52 +0000338// sf, df, sc, dc, in which we only care about ones which have sf or df as a
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000339// first parameter.
340//
341#define P_ "__mips16_call_stub_"
342#define MAX_STUB_NUMBER 10
343#define T1 P "1", P "2", 0, 0, P "5", P "6", 0, 0, P "9", P "10"
344#define T P "0" , T1
345#define P P_
346static char const * vMips16Helper[MAX_STUB_NUMBER+1] =
Craig Topper062a2ba2014-04-25 05:30:21 +0000347 {nullptr, T1 };
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000348#undef P
349#define P P_ "sf_"
350static char const * sfMips16Helper[MAX_STUB_NUMBER+1] =
351 { T };
352#undef P
353#define P P_ "df_"
354static char const * dfMips16Helper[MAX_STUB_NUMBER+1] =
355 { T };
356#undef P
357#define P P_ "sc_"
358static char const * scMips16Helper[MAX_STUB_NUMBER+1] =
359 { T };
360#undef P
361#define P P_ "dc_"
362static char const * dcMips16Helper[MAX_STUB_NUMBER+1] =
363 { T };
364#undef P
365#undef P_
366
367
368const char* Mips16TargetLowering::
369 getMips16HelperFunction
370 (Type* RetTy, ArgListTy &Args, bool &needHelper) const {
371 const unsigned int stubNum = getMips16HelperFunctionStubNumber(Args);
372#ifndef NDEBUG
373 const unsigned int maxStubNum = 10;
374 assert(stubNum <= maxStubNum);
375 const bool validStubNum[maxStubNum+1] =
376 {true, true, true, false, false, true, true, false, false, true, true};
377 assert(validStubNum[stubNum]);
378#endif
379 const char *result;
380 if (RetTy->isFloatTy()) {
381 result = sfMips16Helper[stubNum];
382 }
383 else if (RetTy ->isDoubleTy()) {
384 result = dfMips16Helper[stubNum];
James Y Knight62df5ee2019-01-10 16:07:20 +0000385 } else if (StructType *SRetTy = dyn_cast<StructType>(RetTy)) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000386 // check if it's complex
James Y Knight62df5ee2019-01-10 16:07:20 +0000387 if (SRetTy->getNumElements() == 2) {
388 if ((SRetTy->getElementType(0)->isFloatTy()) &&
389 (SRetTy->getElementType(1)->isFloatTy())) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000390 result = scMips16Helper[stubNum];
James Y Knight62df5ee2019-01-10 16:07:20 +0000391 } else if ((SRetTy->getElementType(0)->isDoubleTy()) &&
392 (SRetTy->getElementType(1)->isDoubleTy())) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000393 result = dcMips16Helper[stubNum];
James Y Knight62df5ee2019-01-10 16:07:20 +0000394 } else {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000395 llvm_unreachable("Uncovered condition");
396 }
James Y Knight62df5ee2019-01-10 16:07:20 +0000397 } else {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000398 llvm_unreachable("Uncovered condition");
399 }
James Y Knight62df5ee2019-01-10 16:07:20 +0000400 } else {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000401 if (stubNum == 0) {
402 needHelper = false;
403 return "";
404 }
405 result = vMips16Helper[stubNum];
406 }
407 needHelper = true;
408 return result;
409}
410
411void Mips16TargetLowering::
412getOpndList(SmallVectorImpl<SDValue> &Ops,
413 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
414 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +0000415 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
416 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000417 SelectionDAG &DAG = CLI.DAG;
Akira Hatanakaaf4211a2013-09-28 00:12:32 +0000418 MachineFunction &MF = DAG.getMachineFunction();
419 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Craig Topper062a2ba2014-04-25 05:30:21 +0000420 const char* Mips16HelperFunction = nullptr;
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000421 bool NeedMips16Helper = false;
422
Eric Christopher1c29a652014-07-18 22:55:25 +0000423 if (Subtarget.inMips16HardFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000424 //
425 // currently we don't have symbols tagged with the mips16 or mips32
426 // qualifier so we will assume that we don't know what kind it is.
427 // and generate the helper
428 //
429 bool LookupHelper = true;
430 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(CLI.Callee)) {
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000431 Mips16Libcall Find = { RTLIB::UNKNOWN_LIBCALL, S->getSymbol() };
432
Benjamin Kramer502b9e12014-04-12 16:15:53 +0000433 if (std::binary_search(std::begin(HardFloatLibCalls),
434 std::end(HardFloatLibCalls), Find))
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000435 LookupHelper = false;
Reed Kotler83f879d2013-08-01 21:17:53 +0000436 else {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000437 const char *Symbol = S->getSymbol();
438 Mips16IntrinsicHelperType IntrinsicFind = { Symbol, "" };
439 const Mips16HardFloatInfo::FuncSignature *Signature =
440 Mips16HardFloatInfo::findFuncSignature(Symbol);
441 if (!IsPICCall && (Signature && (FuncInfo->StubsNeeded.find(Symbol) ==
442 FuncInfo->StubsNeeded.end()))) {
443 FuncInfo->StubsNeeded[Symbol] = Signature;
444 //
445 // S2 is normally saved if the stub is for a function which
446 // returns a float or double value and is not otherwise. This is
447 // because more work is required after the function the stub
448 // is calling completes, and so the stub cannot directly return
449 // and the stub has no stack space to store the return address so
450 // S2 is used for that purpose.
451 // In order to take advantage of not saving S2, we need to also
452 // optimize the call in the stub and this requires some further
453 // functionality in MipsAsmPrinter which we don't have yet.
454 // So for now we always save S2. The optimization will be done
455 // in a follow-on patch.
456 //
Reed Kotlerd2da8102014-02-19 22:11:45 +0000457 if (1 || (Signature->RetSig != Mips16HardFloatInfo::NoFPRet))
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000458 FuncInfo->setSaveS2();
459 }
Reed Kotler83f879d2013-08-01 21:17:53 +0000460 // one more look at list of intrinsics
Benjamin Kramer502b9e12014-04-12 16:15:53 +0000461 const Mips16IntrinsicHelperType *Helper =
Fangrui Songdc8de602019-06-21 05:40:31 +0000462 llvm::lower_bound(Mips16IntrinsicHelper, IntrinsicFind);
Benjamin Kramer502b9e12014-04-12 16:15:53 +0000463 if (Helper != std::end(Mips16IntrinsicHelper) &&
464 *Helper == IntrinsicFind) {
465 Mips16HelperFunction = Helper->Helper;
Reed Kotler83f879d2013-08-01 21:17:53 +0000466 NeedMips16Helper = true;
467 LookupHelper = false;
468 }
469
470 }
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000471 } else if (GlobalAddressSDNode *G =
472 dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
473 Mips16Libcall Find = { RTLIB::UNKNOWN_LIBCALL,
474 G->getGlobal()->getName().data() };
475
Benjamin Kramer502b9e12014-04-12 16:15:53 +0000476 if (std::binary_search(std::begin(HardFloatLibCalls),
477 std::end(HardFloatLibCalls), Find))
Reed Kotler0fed8d42013-05-21 00:50:30 +0000478 LookupHelper = false;
Reed Kotler0fed8d42013-05-21 00:50:30 +0000479 }
Saleem Abdulrasool9f664c12014-05-17 21:50:01 +0000480 if (LookupHelper)
481 Mips16HelperFunction =
482 getMips16HelperFunction(CLI.RetTy, CLI.getArgs(), NeedMips16Helper);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000483 }
484
485 SDValue JumpTarget = Callee;
486
487 // T9 should contain the address of the callee function if
Daniel Sanders9a4f2c52015-01-24 14:35:11 +0000488 // -relocation-model=pic or it is an indirect call.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000489 if (IsPICCall || !GlobalOrExternal) {
490 unsigned V0Reg = Mips::V0;
491 if (NeedMips16Helper) {
492 RegsToPass.push_front(std::make_pair(V0Reg, Callee));
Mehdi Amini44ede332015-07-09 02:09:04 +0000493 JumpTarget = DAG.getExternalSymbol(Mips16HelperFunction,
494 getPointerTy(DAG.getDataLayout()));
Akira Hatanakaaf4211a2013-09-28 00:12:32 +0000495 ExternalSymbolSDNode *S = cast<ExternalSymbolSDNode>(JumpTarget);
Daniel Sanders9a4f2c52015-01-24 14:35:11 +0000496 JumpTarget = getAddrGlobal(S, CLI.DL, JumpTarget.getValueType(), DAG,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +0000497 MipsII::MO_GOT, Chain,
498 FuncInfo->callPtrInfo(S->getSymbol()));
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000499 } else
500 RegsToPass.push_front(std::make_pair((unsigned)Mips::T9, Callee));
501 }
502
503 Ops.push_back(JumpTarget);
504
505 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
Sasa Stankovic7072a792014-10-01 08:22:21 +0000506 InternalLinkage, IsCallReloc, CLI, Callee,
507 Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000508}
509
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000510MachineBasicBlock *
511Mips16TargetLowering::emitSel16(unsigned Opc, MachineInstr &MI,
512 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000513 if (DontExpandCondPseudos16)
514 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000515 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000516 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000517 // To "insert" a SELECT_CC instruction, we actually have to insert the
518 // diamond control-flow pattern. The incoming instruction knows the
519 // destination vreg to set, the condition code register to branch on, the
520 // true/false values to select between, and a branch opcode to use.
521 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000522 MachineFunction::iterator It = ++BB->getIterator();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000523
524 // thisMBB:
525 // ...
526 // TrueVal = ...
527 // setcc r1, r2, r3
528 // bNE r1, r0, copy1MBB
529 // fallthrough --> copy0MBB
530 MachineBasicBlock *thisMBB = BB;
531 MachineFunction *F = BB->getParent();
532 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
533 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
534 F->insert(It, copy0MBB);
535 F->insert(It, sinkMBB);
536
537 // Transfer the remainder of BB and its successor edges to sinkMBB.
538 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000539 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000540 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
541
542 // Next, add the true and fallthrough blocks as its successors.
543 BB->addSuccessor(copy0MBB);
544 BB->addSuccessor(sinkMBB);
545
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000546 BuildMI(BB, DL, TII->get(Opc))
547 .addReg(MI.getOperand(3).getReg())
548 .addMBB(sinkMBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000549
550 // copy0MBB:
551 // %FalseValue = ...
552 // # fallthrough to sinkMBB
553 BB = copy0MBB;
554
555 // Update machine-CFG edges
556 BB->addSuccessor(sinkMBB);
557
558 // sinkMBB:
559 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
560 // ...
561 BB = sinkMBB;
562
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000563 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
564 .addReg(MI.getOperand(1).getReg())
565 .addMBB(thisMBB)
566 .addReg(MI.getOperand(2).getReg())
567 .addMBB(copy0MBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000568
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000569 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000570 return BB;
571}
572
Eric Christopher96e72c62015-01-29 23:27:36 +0000573MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000574Mips16TargetLowering::emitSelT16(unsigned Opc1, unsigned Opc2, MachineInstr &MI,
Eric Christopher96e72c62015-01-29 23:27:36 +0000575 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000576 if (DontExpandCondPseudos16)
577 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000578 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000579 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000580 // To "insert" a SELECT_CC instruction, we actually have to insert the
581 // diamond control-flow pattern. The incoming instruction knows the
582 // destination vreg to set, the condition code register to branch on, the
583 // true/false values to select between, and a branch opcode to use.
584 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000585 MachineFunction::iterator It = ++BB->getIterator();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000586
587 // thisMBB:
588 // ...
589 // TrueVal = ...
590 // setcc r1, r2, r3
591 // bNE r1, r0, copy1MBB
592 // fallthrough --> copy0MBB
593 MachineBasicBlock *thisMBB = BB;
594 MachineFunction *F = BB->getParent();
595 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
596 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
597 F->insert(It, copy0MBB);
598 F->insert(It, sinkMBB);
599
600 // Transfer the remainder of BB and its successor edges to sinkMBB.
601 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000602 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000603 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
604
605 // Next, add the true and fallthrough blocks as its successors.
606 BB->addSuccessor(copy0MBB);
607 BB->addSuccessor(sinkMBB);
608
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000609 BuildMI(BB, DL, TII->get(Opc2))
610 .addReg(MI.getOperand(3).getReg())
611 .addReg(MI.getOperand(4).getReg());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000612 BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
613
614 // copy0MBB:
615 // %FalseValue = ...
616 // # fallthrough to sinkMBB
617 BB = copy0MBB;
618
619 // Update machine-CFG edges
620 BB->addSuccessor(sinkMBB);
621
622 // sinkMBB:
623 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
624 // ...
625 BB = sinkMBB;
626
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000627 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
628 .addReg(MI.getOperand(1).getReg())
629 .addMBB(thisMBB)
630 .addReg(MI.getOperand(2).getReg())
631 .addMBB(copy0MBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000632
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000633 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000634 return BB;
635
636}
637
Eric Christopher96e72c62015-01-29 23:27:36 +0000638MachineBasicBlock *
639Mips16TargetLowering::emitSeliT16(unsigned Opc1, unsigned Opc2,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000640 MachineInstr &MI,
Eric Christopher96e72c62015-01-29 23:27:36 +0000641 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000642 if (DontExpandCondPseudos16)
643 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000644 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000645 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000646 // To "insert" a SELECT_CC instruction, we actually have to insert the
647 // diamond control-flow pattern. The incoming instruction knows the
648 // destination vreg to set, the condition code register to branch on, the
649 // true/false values to select between, and a branch opcode to use.
650 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000651 MachineFunction::iterator It = ++BB->getIterator();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000652
653 // thisMBB:
654 // ...
655 // TrueVal = ...
656 // setcc r1, r2, r3
657 // bNE r1, r0, copy1MBB
658 // fallthrough --> copy0MBB
659 MachineBasicBlock *thisMBB = BB;
660 MachineFunction *F = BB->getParent();
661 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
662 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
663 F->insert(It, copy0MBB);
664 F->insert(It, sinkMBB);
665
666 // Transfer the remainder of BB and its successor edges to sinkMBB.
667 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000668 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000669 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
670
671 // Next, add the true and fallthrough blocks as its successors.
672 BB->addSuccessor(copy0MBB);
673 BB->addSuccessor(sinkMBB);
674
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000675 BuildMI(BB, DL, TII->get(Opc2))
676 .addReg(MI.getOperand(3).getReg())
677 .addImm(MI.getOperand(4).getImm());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000678 BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
679
680 // copy0MBB:
681 // %FalseValue = ...
682 // # fallthrough to sinkMBB
683 BB = copy0MBB;
684
685 // Update machine-CFG edges
686 BB->addSuccessor(sinkMBB);
687
688 // sinkMBB:
689 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
690 // ...
691 BB = sinkMBB;
692
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000693 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
694 .addReg(MI.getOperand(1).getReg())
695 .addMBB(thisMBB)
696 .addReg(MI.getOperand(2).getReg())
697 .addMBB(copy0MBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000698
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000699 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000700 return BB;
701
702}
703
Eric Christopher96e72c62015-01-29 23:27:36 +0000704MachineBasicBlock *
705Mips16TargetLowering::emitFEXT_T8I816_ins(unsigned BtOpc, unsigned CmpOpc,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000706 MachineInstr &MI,
Eric Christopher96e72c62015-01-29 23:27:36 +0000707 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000708 if (DontExpandCondPseudos16)
709 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000710 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0c476112019-08-15 19:22:08 +0000711 Register regX = MI.getOperand(0).getReg();
712 Register regY = MI.getOperand(1).getReg();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000713 MachineBasicBlock *target = MI.getOperand(2).getMBB();
714 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(CmpOpc))
715 .addReg(regX)
716 .addReg(regY);
717 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(BtOpc)).addMBB(target);
718 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000719 return BB;
720}
721
722MachineBasicBlock *Mips16TargetLowering::emitFEXT_T8I8I16_ins(
Eric Christopher96e72c62015-01-29 23:27:36 +0000723 unsigned BtOpc, unsigned CmpiOpc, unsigned CmpiXOpc, bool ImmSigned,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000724 MachineInstr &MI, MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000725 if (DontExpandCondPseudos16)
726 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000727 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0c476112019-08-15 19:22:08 +0000728 Register regX = MI.getOperand(0).getReg();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000729 int64_t imm = MI.getOperand(1).getImm();
730 MachineBasicBlock *target = MI.getOperand(2).getMBB();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000731 unsigned CmpOpc;
732 if (isUInt<8>(imm))
733 CmpOpc = CmpiOpc;
Reed Kotlerce510832013-06-09 23:23:46 +0000734 else if ((!ImmSigned && isUInt<16>(imm)) ||
735 (ImmSigned && isInt<16>(imm)))
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000736 CmpOpc = CmpiXOpc;
737 else
738 llvm_unreachable("immediate field not usable");
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000739 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(CmpOpc)).addReg(regX).addImm(imm);
740 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(BtOpc)).addMBB(target);
741 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000742 return BB;
743}
744
745static unsigned Mips16WhichOp8uOr16simm
746 (unsigned shortOp, unsigned longOp, int64_t Imm) {
747 if (isUInt<8>(Imm))
748 return shortOp;
749 else if (isInt<16>(Imm))
750 return longOp;
751 else
752 llvm_unreachable("immediate field not usable");
753}
754
Eric Christopher96e72c62015-01-29 23:27:36 +0000755MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000756Mips16TargetLowering::emitFEXT_CCRX16_ins(unsigned SltOpc, MachineInstr &MI,
Eric Christopher96e72c62015-01-29 23:27:36 +0000757 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000758 if (DontExpandCondPseudos16)
759 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000760 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0c476112019-08-15 19:22:08 +0000761 Register CC = MI.getOperand(0).getReg();
762 Register regX = MI.getOperand(1).getReg();
763 Register regY = MI.getOperand(2).getReg();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000764 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SltOpc))
765 .addReg(regX)
766 .addReg(regY);
767 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(Mips::MoveR3216), CC)
768 .addReg(Mips::T8);
769 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000770 return BB;
771}
772
Eric Christopher96e72c62015-01-29 23:27:36 +0000773MachineBasicBlock *
774Mips16TargetLowering::emitFEXT_CCRXI16_ins(unsigned SltiOpc, unsigned SltiXOpc,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000775 MachineInstr &MI,
Eric Christopher96e72c62015-01-29 23:27:36 +0000776 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000777 if (DontExpandCondPseudos16)
778 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000779 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0c476112019-08-15 19:22:08 +0000780 Register CC = MI.getOperand(0).getReg();
781 Register regX = MI.getOperand(1).getReg();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000782 int64_t Imm = MI.getOperand(2).getImm();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000783 unsigned SltOpc = Mips16WhichOp8uOr16simm(SltiOpc, SltiXOpc, Imm);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000784 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SltOpc)).addReg(regX).addImm(Imm);
785 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(Mips::MoveR3216), CC)
786 .addReg(Mips::T8);
787 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000788 return BB;
789
790}