blob: 79df622241a08b7f63e2f8ce50f65e018fc5494f [file] [log] [blame]
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001//===-- Mips16ISelLowering.h - Mips16 DAG Lowering Interface ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Subclass of MipsTargetLowering specialized for mips16.
11//
12//===----------------------------------------------------------------------===//
Akira Hatanaka96ca1822013-03-13 00:54:29 +000013#include "Mips16ISelLowering.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000014#include "MCTargetDesc/MipsBaseInfo.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000015#include "Mips16HardFloatInfo.h"
16#include "MipsMachineFunction.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000017#include "MipsRegisterInfo.h"
Reed Kotler783c7942013-05-10 22:25:39 +000018#include "MipsTargetMachine.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000020#include "llvm/CodeGen/TargetInstrInfo.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000021#include "llvm/Support/CommandLine.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000022
23using namespace llvm;
24
Chandler Carruth84e68b22014-04-22 02:41:26 +000025#define DEBUG_TYPE "mips-lower"
26
Akira Hatanaka96ca1822013-03-13 00:54:29 +000027static cl::opt<bool> DontExpandCondPseudos16(
28 "mips16-dont-expand-cond-pseudo",
29 cl::init(false),
Sylvestre Ledru469de192014-08-11 18:04:46 +000030 cl::desc("Don't expand conditional move related "
Akira Hatanaka96ca1822013-03-13 00:54:29 +000031 "pseudos for Mips 16"),
32 cl::Hidden);
33
34namespace {
Benjamin Kramerc4d547d2013-06-13 19:06:52 +000035struct Mips16Libcall {
36 RTLIB::Libcall Libcall;
37 const char *Name;
38
39 bool operator<(const Mips16Libcall &RHS) const {
40 return std::strcmp(Name, RHS.Name) < 0;
41 }
42};
Reed Kotler83f879d2013-08-01 21:17:53 +000043
44struct Mips16IntrinsicHelperType{
45 const char* Name;
46 const char* Helper;
47
48 bool operator<(const Mips16IntrinsicHelperType &RHS) const {
49 return std::strcmp(Name, RHS.Name) < 0;
50 }
51 bool operator==(const Mips16IntrinsicHelperType &RHS) const {
52 return std::strcmp(Name, RHS.Name) == 0;
53 }
54};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000055}
Akira Hatanaka96ca1822013-03-13 00:54:29 +000056
Benjamin Kramerc4d547d2013-06-13 19:06:52 +000057// Libcalls for which no helper is generated. Sorted by name for binary search.
58static const Mips16Libcall HardFloatLibCalls[] = {
59 { RTLIB::ADD_F64, "__mips16_adddf3" },
60 { RTLIB::ADD_F32, "__mips16_addsf3" },
61 { RTLIB::DIV_F64, "__mips16_divdf3" },
62 { RTLIB::DIV_F32, "__mips16_divsf3" },
63 { RTLIB::OEQ_F64, "__mips16_eqdf2" },
64 { RTLIB::OEQ_F32, "__mips16_eqsf2" },
65 { RTLIB::FPEXT_F32_F64, "__mips16_extendsfdf2" },
66 { RTLIB::FPTOSINT_F64_I32, "__mips16_fix_truncdfsi" },
67 { RTLIB::FPTOSINT_F32_I32, "__mips16_fix_truncsfsi" },
68 { RTLIB::SINTTOFP_I32_F64, "__mips16_floatsidf" },
69 { RTLIB::SINTTOFP_I32_F32, "__mips16_floatsisf" },
70 { RTLIB::UINTTOFP_I32_F64, "__mips16_floatunsidf" },
71 { RTLIB::UINTTOFP_I32_F32, "__mips16_floatunsisf" },
72 { RTLIB::OGE_F64, "__mips16_gedf2" },
73 { RTLIB::OGE_F32, "__mips16_gesf2" },
74 { RTLIB::OGT_F64, "__mips16_gtdf2" },
75 { RTLIB::OGT_F32, "__mips16_gtsf2" },
76 { RTLIB::OLE_F64, "__mips16_ledf2" },
77 { RTLIB::OLE_F32, "__mips16_lesf2" },
78 { RTLIB::OLT_F64, "__mips16_ltdf2" },
79 { RTLIB::OLT_F32, "__mips16_ltsf2" },
80 { RTLIB::MUL_F64, "__mips16_muldf3" },
81 { RTLIB::MUL_F32, "__mips16_mulsf3" },
82 { RTLIB::UNE_F64, "__mips16_nedf2" },
83 { RTLIB::UNE_F32, "__mips16_nesf2" },
84 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_dc" }, // No associated libcall.
85 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_df" }, // No associated libcall.
86 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_sc" }, // No associated libcall.
87 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_sf" }, // No associated libcall.
88 { RTLIB::SUB_F64, "__mips16_subdf3" },
89 { RTLIB::SUB_F32, "__mips16_subsf3" },
90 { RTLIB::FPROUND_F64_F32, "__mips16_truncdfsf2" },
91 { RTLIB::UO_F64, "__mips16_unorddf2" },
92 { RTLIB::UO_F32, "__mips16_unordsf2" }
93};
94
Reed Kotler83f879d2013-08-01 21:17:53 +000095static const Mips16IntrinsicHelperType Mips16IntrinsicHelper[] = {
Reed Kotlerbe316cf2013-08-09 21:33:41 +000096 {"__fixunsdfsi", "__mips16_call_stub_2" },
Reed Kotler83f879d2013-08-01 21:17:53 +000097 {"ceil", "__mips16_call_stub_df_2"},
98 {"ceilf", "__mips16_call_stub_sf_1"},
99 {"copysign", "__mips16_call_stub_df_10"},
100 {"copysignf", "__mips16_call_stub_sf_5"},
101 {"cos", "__mips16_call_stub_df_2"},
102 {"cosf", "__mips16_call_stub_sf_1"},
103 {"exp2", "__mips16_call_stub_df_2"},
104 {"exp2f", "__mips16_call_stub_sf_1"},
105 {"floor", "__mips16_call_stub_df_2"},
106 {"floorf", "__mips16_call_stub_sf_1"},
107 {"log2", "__mips16_call_stub_df_2"},
108 {"log2f", "__mips16_call_stub_sf_1"},
109 {"nearbyint", "__mips16_call_stub_df_2"},
110 {"nearbyintf", "__mips16_call_stub_sf_1"},
111 {"rint", "__mips16_call_stub_df_2"},
112 {"rintf", "__mips16_call_stub_sf_1"},
113 {"sin", "__mips16_call_stub_df_2"},
114 {"sinf", "__mips16_call_stub_sf_1"},
115 {"sqrt", "__mips16_call_stub_df_2"},
116 {"sqrtf", "__mips16_call_stub_sf_1"},
117 {"trunc", "__mips16_call_stub_df_2"},
118 {"truncf", "__mips16_call_stub_sf_1"},
119};
120
Eric Christopherb1526602014-09-19 23:30:42 +0000121Mips16TargetLowering::Mips16TargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000122 const MipsSubtarget &STI)
123 : MipsTargetLowering(TM, STI) {
Reed Kotlerfafaa9d2013-03-14 22:02:09 +0000124
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000125 // Set up the register classes
126 addRegisterClass(MVT::i32, &Mips::CPU16RegsRegClass);
127
Eric Christophere8ae3e32015-05-07 23:10:21 +0000128 if (!Subtarget.useSoftFloat())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000129 setMips16HardFloatLibCalls();
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000130
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000131 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
132 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
133 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
134 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
135 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
136 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
137 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
138 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
139 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
140 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
141 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
142 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
143 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
144
Reed Kotler97309af2013-10-08 17:32:33 +0000145 setOperationAction(ISD::ROTR, MVT::i32, Expand);
146 setOperationAction(ISD::ROTR, MVT::i64, Expand);
147 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
148 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
149
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000150 computeRegisterProperties(STI.getRegisterInfo());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000151}
152
153const MipsTargetLowering *
Eric Christopherb1526602014-09-19 23:30:42 +0000154llvm::createMips16TargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000155 const MipsSubtarget &STI) {
156 return new Mips16TargetLowering(TM, STI);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000157}
158
159bool
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000160Mips16TargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
161 unsigned,
162 unsigned,
163 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000164 return false;
165}
166
167MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000168Mips16TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000169 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000170 switch (MI.getOpcode()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000171 default:
172 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
173 case Mips::SelBeqZ:
174 return emitSel16(Mips::BeqzRxImm16, MI, BB);
175 case Mips::SelBneZ:
176 return emitSel16(Mips::BnezRxImm16, MI, BB);
177 case Mips::SelTBteqZCmpi:
Reed Kotler09e59152013-11-15 02:21:52 +0000178 return emitSeliT16(Mips::Bteqz16, Mips::CmpiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000179 case Mips::SelTBteqZSlti:
Reed Kotler09e59152013-11-15 02:21:52 +0000180 return emitSeliT16(Mips::Bteqz16, Mips::SltiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000181 case Mips::SelTBteqZSltiu:
Reed Kotler09e59152013-11-15 02:21:52 +0000182 return emitSeliT16(Mips::Bteqz16, Mips::SltiuRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000183 case Mips::SelTBtneZCmpi:
Reed Kotler09e59152013-11-15 02:21:52 +0000184 return emitSeliT16(Mips::Btnez16, Mips::CmpiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000185 case Mips::SelTBtneZSlti:
Reed Kotler09e59152013-11-15 02:21:52 +0000186 return emitSeliT16(Mips::Btnez16, Mips::SltiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000187 case Mips::SelTBtneZSltiu:
Reed Kotler09e59152013-11-15 02:21:52 +0000188 return emitSeliT16(Mips::Btnez16, Mips::SltiuRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000189 case Mips::SelTBteqZCmp:
Reed Kotler09e59152013-11-15 02:21:52 +0000190 return emitSelT16(Mips::Bteqz16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000191 case Mips::SelTBteqZSlt:
Reed Kotler09e59152013-11-15 02:21:52 +0000192 return emitSelT16(Mips::Bteqz16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000193 case Mips::SelTBteqZSltu:
Reed Kotler09e59152013-11-15 02:21:52 +0000194 return emitSelT16(Mips::Bteqz16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000195 case Mips::SelTBtneZCmp:
Reed Kotler09e59152013-11-15 02:21:52 +0000196 return emitSelT16(Mips::Btnez16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000197 case Mips::SelTBtneZSlt:
Reed Kotler09e59152013-11-15 02:21:52 +0000198 return emitSelT16(Mips::Btnez16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000199 case Mips::SelTBtneZSltu:
Reed Kotler09e59152013-11-15 02:21:52 +0000200 return emitSelT16(Mips::Btnez16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000201 case Mips::BteqzT8CmpX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000202 return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000203 case Mips::BteqzT8SltX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000204 return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000205 case Mips::BteqzT8SltuX16:
206 // TBD: figure out a way to get this or remove the instruction
207 // altogether.
Reed Kotler09e59152013-11-15 02:21:52 +0000208 return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000209 case Mips::BtnezT8CmpX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000210 return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000211 case Mips::BtnezT8SltX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000212 return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000213 case Mips::BtnezT8SltuX16:
214 // TBD: figure out a way to get this or remove the instruction
215 // altogether.
Reed Kotler09e59152013-11-15 02:21:52 +0000216 return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000217 case Mips::BteqzT8CmpiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000218 Mips::Bteqz16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000219 case Mips::BteqzT8SltiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000220 Mips::Bteqz16, Mips::SltiRxImm16, Mips::SltiRxImmX16, true, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000221 case Mips::BteqzT8SltiuX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000222 Mips::Bteqz16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000223 case Mips::BtnezT8CmpiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000224 Mips::Btnez16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000225 case Mips::BtnezT8SltiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000226 Mips::Btnez16, Mips::SltiRxImm16, Mips::SltiRxImmX16, true, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000227 case Mips::BtnezT8SltiuX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000228 Mips::Btnez16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000229 break;
230 case Mips::SltCCRxRy16:
231 return emitFEXT_CCRX16_ins(Mips::SltRxRy16, MI, BB);
232 break;
233 case Mips::SltiCCRxImmX16:
234 return emitFEXT_CCRXI16_ins
235 (Mips::SltiRxImm16, Mips::SltiRxImmX16, MI, BB);
236 case Mips::SltiuCCRxImmX16:
237 return emitFEXT_CCRXI16_ins
238 (Mips::SltiuRxImm16, Mips::SltiuRxImmX16, MI, BB);
239 case Mips::SltuCCRxRy16:
240 return emitFEXT_CCRX16_ins
241 (Mips::SltuRxRy16, MI, BB);
242 }
243}
244
Daniel Sanders23e98772014-11-02 16:09:29 +0000245bool Mips16TargetLowering::isEligibleForTailCallOptimization(
246 const CCState &CCInfo, unsigned NextStackOffset,
247 const MipsFunctionInfo &FI) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000248 // No tail call optimization for mips16.
249 return false;
250}
251
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000252void Mips16TargetLowering::setMips16HardFloatLibCalls() {
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000253 for (unsigned I = 0; I != array_lengthof(HardFloatLibCalls); ++I) {
254 assert((I == 0 || HardFloatLibCalls[I - 1] < HardFloatLibCalls[I]) &&
255 "Array not sorted!");
256 if (HardFloatLibCalls[I].Libcall != RTLIB::UNKNOWN_LIBCALL)
257 setLibcallName(HardFloatLibCalls[I].Libcall, HardFloatLibCalls[I].Name);
258 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000259
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000260 setLibcallName(RTLIB::O_F64, "__mips16_unorddf2");
261 setLibcallName(RTLIB::O_F32, "__mips16_unordsf2");
262}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000263
264//
265// The Mips16 hard float is a crazy quilt inherited from gcc. I have a much
266// cleaner way to do all of this but it will have to wait until the traditional
267// gcc mechanism is completed.
268//
269// For Pic, in order for Mips16 code to call Mips32 code which according the abi
270// have either arguments or returned values placed in floating point registers,
271// we use a set of helper functions. (This includes functions which return type
272// complex which on Mips are returned in a pair of floating point registers).
273//
274// This is an encoding that we inherited from gcc.
275// In Mips traditional O32, N32 ABI, floating point numbers are passed in
276// floating point argument registers 1,2 only when the first and optionally
277// the second arguments are float (sf) or double (df).
278// For Mips16 we are only concerned with the situations where floating point
279// arguments are being passed in floating point registers by the ABI, because
280// Mips16 mode code cannot execute floating point instructions to load those
281// values and hence helper functions are needed.
282// The possibilities are (), (sf), (sf, sf), (sf, df), (df), (df, sf), (df, df)
283// the helper function suffixs for these are:
284// 0, 1, 5, 9, 2, 6, 10
285// this suffix can then be calculated as follows:
286// for a given argument Arg:
287// Arg1x, Arg2x = 1 : Arg is sf
288// 2 : Arg is df
289// 0: Arg is neither sf or df
290// So this stub is the string for number Arg1x + Arg2x*4.
291// However not all numbers between 0 and 10 are possible, we check anyway and
292// assert if the impossible exists.
293//
294
295unsigned int Mips16TargetLowering::getMips16HelperFunctionStubNumber
296 (ArgListTy &Args) const {
297 unsigned int resultNum = 0;
298 if (Args.size() >= 1) {
299 Type *t = Args[0].Ty;
300 if (t->isFloatTy()) {
301 resultNum = 1;
302 }
303 else if (t->isDoubleTy()) {
304 resultNum = 2;
305 }
306 }
307 if (resultNum) {
308 if (Args.size() >=2) {
309 Type *t = Args[1].Ty;
310 if (t->isFloatTy()) {
311 resultNum += 4;
312 }
313 else if (t->isDoubleTy()) {
314 resultNum += 8;
315 }
316 }
317 }
318 return resultNum;
319}
320
321//
Eric Christopher9b270d42014-09-29 21:57:52 +0000322// Prefixes are attached to stub numbers depending on the return type.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000323// return type: float sf_
324// double df_
325// single complex sc_
326// double complext dc_
327// others NO PREFIX
328//
329//
330// The full name of a helper function is__mips16_call_stub +
331// return type dependent prefix + stub number
332//
Eric Christopher9b270d42014-09-29 21:57:52 +0000333// FIXME: This is something that probably should be in a different source file
334// and perhaps done differently but my main purpose is to not waste runtime
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000335// on something that we can enumerate in the source. Another possibility is
336// to have a python script to generate these mapping tables. This will do
337// for now. There are a whole series of helper function mapping arrays, one
338// for each return type class as outlined above. There there are 11 possible
Eric Christopher9b270d42014-09-29 21:57:52 +0000339// entries. Ones with 0 are ones which should never be selected.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000340//
341// All the arrays are similar except for ones which return neither
Eric Christopher9b270d42014-09-29 21:57:52 +0000342// 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 +0000343// first parameter.
344//
345#define P_ "__mips16_call_stub_"
346#define MAX_STUB_NUMBER 10
347#define T1 P "1", P "2", 0, 0, P "5", P "6", 0, 0, P "9", P "10"
348#define T P "0" , T1
349#define P P_
350static char const * vMips16Helper[MAX_STUB_NUMBER+1] =
Craig Topper062a2ba2014-04-25 05:30:21 +0000351 {nullptr, T1 };
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000352#undef P
353#define P P_ "sf_"
354static char const * sfMips16Helper[MAX_STUB_NUMBER+1] =
355 { T };
356#undef P
357#define P P_ "df_"
358static char const * dfMips16Helper[MAX_STUB_NUMBER+1] =
359 { T };
360#undef P
361#define P P_ "sc_"
362static char const * scMips16Helper[MAX_STUB_NUMBER+1] =
363 { T };
364#undef P
365#define P P_ "dc_"
366static char const * dcMips16Helper[MAX_STUB_NUMBER+1] =
367 { T };
368#undef P
369#undef P_
370
371
372const char* Mips16TargetLowering::
373 getMips16HelperFunction
374 (Type* RetTy, ArgListTy &Args, bool &needHelper) const {
375 const unsigned int stubNum = getMips16HelperFunctionStubNumber(Args);
376#ifndef NDEBUG
377 const unsigned int maxStubNum = 10;
378 assert(stubNum <= maxStubNum);
379 const bool validStubNum[maxStubNum+1] =
380 {true, true, true, false, false, true, true, false, false, true, true};
381 assert(validStubNum[stubNum]);
382#endif
383 const char *result;
384 if (RetTy->isFloatTy()) {
385 result = sfMips16Helper[stubNum];
386 }
387 else if (RetTy ->isDoubleTy()) {
388 result = dfMips16Helper[stubNum];
James Y Knight62df5ee2019-01-10 16:07:20 +0000389 } else if (StructType *SRetTy = dyn_cast<StructType>(RetTy)) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000390 // check if it's complex
James Y Knight62df5ee2019-01-10 16:07:20 +0000391 if (SRetTy->getNumElements() == 2) {
392 if ((SRetTy->getElementType(0)->isFloatTy()) &&
393 (SRetTy->getElementType(1)->isFloatTy())) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000394 result = scMips16Helper[stubNum];
James Y Knight62df5ee2019-01-10 16:07:20 +0000395 } else if ((SRetTy->getElementType(0)->isDoubleTy()) &&
396 (SRetTy->getElementType(1)->isDoubleTy())) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000397 result = dcMips16Helper[stubNum];
James Y Knight62df5ee2019-01-10 16:07:20 +0000398 } else {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000399 llvm_unreachable("Uncovered condition");
400 }
James Y Knight62df5ee2019-01-10 16:07:20 +0000401 } else {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000402 llvm_unreachable("Uncovered condition");
403 }
James Y Knight62df5ee2019-01-10 16:07:20 +0000404 } else {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000405 if (stubNum == 0) {
406 needHelper = false;
407 return "";
408 }
409 result = vMips16Helper[stubNum];
410 }
411 needHelper = true;
412 return result;
413}
414
415void Mips16TargetLowering::
416getOpndList(SmallVectorImpl<SDValue> &Ops,
417 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
418 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +0000419 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
420 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000421 SelectionDAG &DAG = CLI.DAG;
Akira Hatanakaaf4211a2013-09-28 00:12:32 +0000422 MachineFunction &MF = DAG.getMachineFunction();
423 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Craig Topper062a2ba2014-04-25 05:30:21 +0000424 const char* Mips16HelperFunction = nullptr;
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000425 bool NeedMips16Helper = false;
426
Eric Christopher1c29a652014-07-18 22:55:25 +0000427 if (Subtarget.inMips16HardFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000428 //
429 // currently we don't have symbols tagged with the mips16 or mips32
430 // qualifier so we will assume that we don't know what kind it is.
431 // and generate the helper
432 //
433 bool LookupHelper = true;
434 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(CLI.Callee)) {
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000435 Mips16Libcall Find = { RTLIB::UNKNOWN_LIBCALL, S->getSymbol() };
436
Benjamin Kramer502b9e12014-04-12 16:15:53 +0000437 if (std::binary_search(std::begin(HardFloatLibCalls),
438 std::end(HardFloatLibCalls), Find))
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000439 LookupHelper = false;
Reed Kotler83f879d2013-08-01 21:17:53 +0000440 else {
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000441 const char *Symbol = S->getSymbol();
442 Mips16IntrinsicHelperType IntrinsicFind = { Symbol, "" };
443 const Mips16HardFloatInfo::FuncSignature *Signature =
444 Mips16HardFloatInfo::findFuncSignature(Symbol);
445 if (!IsPICCall && (Signature && (FuncInfo->StubsNeeded.find(Symbol) ==
446 FuncInfo->StubsNeeded.end()))) {
447 FuncInfo->StubsNeeded[Symbol] = Signature;
448 //
449 // S2 is normally saved if the stub is for a function which
450 // returns a float or double value and is not otherwise. This is
451 // because more work is required after the function the stub
452 // is calling completes, and so the stub cannot directly return
453 // and the stub has no stack space to store the return address so
454 // S2 is used for that purpose.
455 // In order to take advantage of not saving S2, we need to also
456 // optimize the call in the stub and this requires some further
457 // functionality in MipsAsmPrinter which we don't have yet.
458 // So for now we always save S2. The optimization will be done
459 // in a follow-on patch.
460 //
Reed Kotlerd2da8102014-02-19 22:11:45 +0000461 if (1 || (Signature->RetSig != Mips16HardFloatInfo::NoFPRet))
Reed Kotler4cdaa7d2014-02-14 19:16:39 +0000462 FuncInfo->setSaveS2();
463 }
Reed Kotler83f879d2013-08-01 21:17:53 +0000464 // one more look at list of intrinsics
Benjamin Kramer502b9e12014-04-12 16:15:53 +0000465 const Mips16IntrinsicHelperType *Helper =
466 std::lower_bound(std::begin(Mips16IntrinsicHelper),
467 std::end(Mips16IntrinsicHelper), IntrinsicFind);
468 if (Helper != std::end(Mips16IntrinsicHelper) &&
469 *Helper == IntrinsicFind) {
470 Mips16HelperFunction = Helper->Helper;
Reed Kotler83f879d2013-08-01 21:17:53 +0000471 NeedMips16Helper = true;
472 LookupHelper = false;
473 }
474
475 }
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000476 } else if (GlobalAddressSDNode *G =
477 dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
478 Mips16Libcall Find = { RTLIB::UNKNOWN_LIBCALL,
479 G->getGlobal()->getName().data() };
480
Benjamin Kramer502b9e12014-04-12 16:15:53 +0000481 if (std::binary_search(std::begin(HardFloatLibCalls),
482 std::end(HardFloatLibCalls), Find))
Reed Kotler0fed8d42013-05-21 00:50:30 +0000483 LookupHelper = false;
Reed Kotler0fed8d42013-05-21 00:50:30 +0000484 }
Saleem Abdulrasool9f664c12014-05-17 21:50:01 +0000485 if (LookupHelper)
486 Mips16HelperFunction =
487 getMips16HelperFunction(CLI.RetTy, CLI.getArgs(), NeedMips16Helper);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000488 }
489
490 SDValue JumpTarget = Callee;
491
492 // T9 should contain the address of the callee function if
Daniel Sanders9a4f2c52015-01-24 14:35:11 +0000493 // -relocation-model=pic or it is an indirect call.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000494 if (IsPICCall || !GlobalOrExternal) {
495 unsigned V0Reg = Mips::V0;
496 if (NeedMips16Helper) {
497 RegsToPass.push_front(std::make_pair(V0Reg, Callee));
Mehdi Amini44ede332015-07-09 02:09:04 +0000498 JumpTarget = DAG.getExternalSymbol(Mips16HelperFunction,
499 getPointerTy(DAG.getDataLayout()));
Akira Hatanakaaf4211a2013-09-28 00:12:32 +0000500 ExternalSymbolSDNode *S = cast<ExternalSymbolSDNode>(JumpTarget);
Daniel Sanders9a4f2c52015-01-24 14:35:11 +0000501 JumpTarget = getAddrGlobal(S, CLI.DL, JumpTarget.getValueType(), DAG,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +0000502 MipsII::MO_GOT, Chain,
503 FuncInfo->callPtrInfo(S->getSymbol()));
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000504 } else
505 RegsToPass.push_front(std::make_pair((unsigned)Mips::T9, Callee));
506 }
507
508 Ops.push_back(JumpTarget);
509
510 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
Sasa Stankovic7072a792014-10-01 08:22:21 +0000511 InternalLinkage, IsCallReloc, CLI, Callee,
512 Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000513}
514
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000515MachineBasicBlock *
516Mips16TargetLowering::emitSel16(unsigned Opc, MachineInstr &MI,
517 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000518 if (DontExpandCondPseudos16)
519 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000520 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000521 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000522 // To "insert" a SELECT_CC instruction, we actually have to insert the
523 // diamond control-flow pattern. The incoming instruction knows the
524 // destination vreg to set, the condition code register to branch on, the
525 // true/false values to select between, and a branch opcode to use.
526 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000527 MachineFunction::iterator It = ++BB->getIterator();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000528
529 // thisMBB:
530 // ...
531 // TrueVal = ...
532 // setcc r1, r2, r3
533 // bNE r1, r0, copy1MBB
534 // fallthrough --> copy0MBB
535 MachineBasicBlock *thisMBB = BB;
536 MachineFunction *F = BB->getParent();
537 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
538 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
539 F->insert(It, copy0MBB);
540 F->insert(It, sinkMBB);
541
542 // Transfer the remainder of BB and its successor edges to sinkMBB.
543 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000544 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000545 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
546
547 // Next, add the true and fallthrough blocks as its successors.
548 BB->addSuccessor(copy0MBB);
549 BB->addSuccessor(sinkMBB);
550
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000551 BuildMI(BB, DL, TII->get(Opc))
552 .addReg(MI.getOperand(3).getReg())
553 .addMBB(sinkMBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000554
555 // copy0MBB:
556 // %FalseValue = ...
557 // # fallthrough to sinkMBB
558 BB = copy0MBB;
559
560 // Update machine-CFG edges
561 BB->addSuccessor(sinkMBB);
562
563 // sinkMBB:
564 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
565 // ...
566 BB = sinkMBB;
567
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000568 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
569 .addReg(MI.getOperand(1).getReg())
570 .addMBB(thisMBB)
571 .addReg(MI.getOperand(2).getReg())
572 .addMBB(copy0MBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000573
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000574 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000575 return BB;
576}
577
Eric Christopher96e72c62015-01-29 23:27:36 +0000578MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000579Mips16TargetLowering::emitSelT16(unsigned Opc1, unsigned Opc2, MachineInstr &MI,
Eric Christopher96e72c62015-01-29 23:27:36 +0000580 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000581 if (DontExpandCondPseudos16)
582 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000583 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000584 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000585 // To "insert" a SELECT_CC instruction, we actually have to insert the
586 // diamond control-flow pattern. The incoming instruction knows the
587 // destination vreg to set, the condition code register to branch on, the
588 // true/false values to select between, and a branch opcode to use.
589 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000590 MachineFunction::iterator It = ++BB->getIterator();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000591
592 // thisMBB:
593 // ...
594 // TrueVal = ...
595 // setcc r1, r2, r3
596 // bNE r1, r0, copy1MBB
597 // fallthrough --> copy0MBB
598 MachineBasicBlock *thisMBB = BB;
599 MachineFunction *F = BB->getParent();
600 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
601 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
602 F->insert(It, copy0MBB);
603 F->insert(It, sinkMBB);
604
605 // Transfer the remainder of BB and its successor edges to sinkMBB.
606 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000607 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000608 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
609
610 // Next, add the true and fallthrough blocks as its successors.
611 BB->addSuccessor(copy0MBB);
612 BB->addSuccessor(sinkMBB);
613
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000614 BuildMI(BB, DL, TII->get(Opc2))
615 .addReg(MI.getOperand(3).getReg())
616 .addReg(MI.getOperand(4).getReg());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000617 BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
618
619 // copy0MBB:
620 // %FalseValue = ...
621 // # fallthrough to sinkMBB
622 BB = copy0MBB;
623
624 // Update machine-CFG edges
625 BB->addSuccessor(sinkMBB);
626
627 // sinkMBB:
628 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
629 // ...
630 BB = sinkMBB;
631
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000632 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
633 .addReg(MI.getOperand(1).getReg())
634 .addMBB(thisMBB)
635 .addReg(MI.getOperand(2).getReg())
636 .addMBB(copy0MBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000637
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000638 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000639 return BB;
640
641}
642
Eric Christopher96e72c62015-01-29 23:27:36 +0000643MachineBasicBlock *
644Mips16TargetLowering::emitSeliT16(unsigned Opc1, unsigned Opc2,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000645 MachineInstr &MI,
Eric Christopher96e72c62015-01-29 23:27:36 +0000646 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000647 if (DontExpandCondPseudos16)
648 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000649 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000650 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000651 // To "insert" a SELECT_CC instruction, we actually have to insert the
652 // diamond control-flow pattern. The incoming instruction knows the
653 // destination vreg to set, the condition code register to branch on, the
654 // true/false values to select between, and a branch opcode to use.
655 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000656 MachineFunction::iterator It = ++BB->getIterator();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000657
658 // thisMBB:
659 // ...
660 // TrueVal = ...
661 // setcc r1, r2, r3
662 // bNE r1, r0, copy1MBB
663 // fallthrough --> copy0MBB
664 MachineBasicBlock *thisMBB = BB;
665 MachineFunction *F = BB->getParent();
666 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
667 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
668 F->insert(It, copy0MBB);
669 F->insert(It, sinkMBB);
670
671 // Transfer the remainder of BB and its successor edges to sinkMBB.
672 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000673 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000674 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
675
676 // Next, add the true and fallthrough blocks as its successors.
677 BB->addSuccessor(copy0MBB);
678 BB->addSuccessor(sinkMBB);
679
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000680 BuildMI(BB, DL, TII->get(Opc2))
681 .addReg(MI.getOperand(3).getReg())
682 .addImm(MI.getOperand(4).getImm());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000683 BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
684
685 // copy0MBB:
686 // %FalseValue = ...
687 // # fallthrough to sinkMBB
688 BB = copy0MBB;
689
690 // Update machine-CFG edges
691 BB->addSuccessor(sinkMBB);
692
693 // sinkMBB:
694 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
695 // ...
696 BB = sinkMBB;
697
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000698 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
699 .addReg(MI.getOperand(1).getReg())
700 .addMBB(thisMBB)
701 .addReg(MI.getOperand(2).getReg())
702 .addMBB(copy0MBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000703
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000704 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000705 return BB;
706
707}
708
Eric Christopher96e72c62015-01-29 23:27:36 +0000709MachineBasicBlock *
710Mips16TargetLowering::emitFEXT_T8I816_ins(unsigned BtOpc, unsigned CmpOpc,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000711 MachineInstr &MI,
Eric Christopher96e72c62015-01-29 23:27:36 +0000712 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000713 if (DontExpandCondPseudos16)
714 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000715 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000716 unsigned regX = MI.getOperand(0).getReg();
717 unsigned regY = MI.getOperand(1).getReg();
718 MachineBasicBlock *target = MI.getOperand(2).getMBB();
719 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(CmpOpc))
720 .addReg(regX)
721 .addReg(regY);
722 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(BtOpc)).addMBB(target);
723 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000724 return BB;
725}
726
727MachineBasicBlock *Mips16TargetLowering::emitFEXT_T8I8I16_ins(
Eric Christopher96e72c62015-01-29 23:27:36 +0000728 unsigned BtOpc, unsigned CmpiOpc, unsigned CmpiXOpc, bool ImmSigned,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000729 MachineInstr &MI, MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000730 if (DontExpandCondPseudos16)
731 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000732 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000733 unsigned regX = MI.getOperand(0).getReg();
734 int64_t imm = MI.getOperand(1).getImm();
735 MachineBasicBlock *target = MI.getOperand(2).getMBB();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000736 unsigned CmpOpc;
737 if (isUInt<8>(imm))
738 CmpOpc = CmpiOpc;
Reed Kotlerce510832013-06-09 23:23:46 +0000739 else if ((!ImmSigned && isUInt<16>(imm)) ||
740 (ImmSigned && isInt<16>(imm)))
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000741 CmpOpc = CmpiXOpc;
742 else
743 llvm_unreachable("immediate field not usable");
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000744 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(CmpOpc)).addReg(regX).addImm(imm);
745 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(BtOpc)).addMBB(target);
746 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000747 return BB;
748}
749
750static unsigned Mips16WhichOp8uOr16simm
751 (unsigned shortOp, unsigned longOp, int64_t Imm) {
752 if (isUInt<8>(Imm))
753 return shortOp;
754 else if (isInt<16>(Imm))
755 return longOp;
756 else
757 llvm_unreachable("immediate field not usable");
758}
759
Eric Christopher96e72c62015-01-29 23:27:36 +0000760MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000761Mips16TargetLowering::emitFEXT_CCRX16_ins(unsigned SltOpc, MachineInstr &MI,
Eric Christopher96e72c62015-01-29 23:27:36 +0000762 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000763 if (DontExpandCondPseudos16)
764 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000765 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000766 unsigned CC = MI.getOperand(0).getReg();
767 unsigned regX = MI.getOperand(1).getReg();
768 unsigned regY = MI.getOperand(2).getReg();
769 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SltOpc))
770 .addReg(regX)
771 .addReg(regY);
772 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(Mips::MoveR3216), CC)
773 .addReg(Mips::T8);
774 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000775 return BB;
776}
777
Eric Christopher96e72c62015-01-29 23:27:36 +0000778MachineBasicBlock *
779Mips16TargetLowering::emitFEXT_CCRXI16_ins(unsigned SltiOpc, unsigned SltiXOpc,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000780 MachineInstr &MI,
Eric Christopher96e72c62015-01-29 23:27:36 +0000781 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000782 if (DontExpandCondPseudos16)
783 return BB;
Eric Christopher96e72c62015-01-29 23:27:36 +0000784 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000785 unsigned CC = MI.getOperand(0).getReg();
786 unsigned regX = MI.getOperand(1).getReg();
787 int64_t Imm = MI.getOperand(2).getImm();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000788 unsigned SltOpc = Mips16WhichOp8uOr16simm(SltiOpc, SltiXOpc, Imm);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000789 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SltOpc)).addReg(regX).addImm(Imm);
790 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(Mips::MoveR3216), CC)
791 .addReg(Mips::T8);
792 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000793 return BB;
794
795}