blob: 91d18eaa1854877fd572d4c890374354190fe134 [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//===----------------------------------------------------------------------===//
13#define DEBUG_TYPE "mips-lower"
14#include "Mips16ISelLowering.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000015#include "MCTargetDesc/MipsBaseInfo.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"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Target/TargetInstrInfo.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000021
22using namespace llvm;
23
Akira Hatanaka96ca1822013-03-13 00:54:29 +000024static cl::opt<bool> DontExpandCondPseudos16(
25 "mips16-dont-expand-cond-pseudo",
26 cl::init(false),
27 cl::desc("Dont expand conditional move related "
28 "pseudos for Mips 16"),
29 cl::Hidden);
30
31namespace {
Benjamin Kramerc4d547d2013-06-13 19:06:52 +000032struct Mips16Libcall {
33 RTLIB::Libcall Libcall;
34 const char *Name;
35
36 bool operator<(const Mips16Libcall &RHS) const {
37 return std::strcmp(Name, RHS.Name) < 0;
38 }
39};
Reed Kotler83f879d2013-08-01 21:17:53 +000040
41struct Mips16IntrinsicHelperType{
42 const char* Name;
43 const char* Helper;
44
45 bool operator<(const Mips16IntrinsicHelperType &RHS) const {
46 return std::strcmp(Name, RHS.Name) < 0;
47 }
48 bool operator==(const Mips16IntrinsicHelperType &RHS) const {
49 return std::strcmp(Name, RHS.Name) == 0;
50 }
51};
Akira Hatanaka96ca1822013-03-13 00:54:29 +000052}
53
Benjamin Kramerc4d547d2013-06-13 19:06:52 +000054// Libcalls for which no helper is generated. Sorted by name for binary search.
55static const Mips16Libcall HardFloatLibCalls[] = {
56 { RTLIB::ADD_F64, "__mips16_adddf3" },
57 { RTLIB::ADD_F32, "__mips16_addsf3" },
58 { RTLIB::DIV_F64, "__mips16_divdf3" },
59 { RTLIB::DIV_F32, "__mips16_divsf3" },
60 { RTLIB::OEQ_F64, "__mips16_eqdf2" },
61 { RTLIB::OEQ_F32, "__mips16_eqsf2" },
62 { RTLIB::FPEXT_F32_F64, "__mips16_extendsfdf2" },
63 { RTLIB::FPTOSINT_F64_I32, "__mips16_fix_truncdfsi" },
64 { RTLIB::FPTOSINT_F32_I32, "__mips16_fix_truncsfsi" },
65 { RTLIB::SINTTOFP_I32_F64, "__mips16_floatsidf" },
66 { RTLIB::SINTTOFP_I32_F32, "__mips16_floatsisf" },
67 { RTLIB::UINTTOFP_I32_F64, "__mips16_floatunsidf" },
68 { RTLIB::UINTTOFP_I32_F32, "__mips16_floatunsisf" },
69 { RTLIB::OGE_F64, "__mips16_gedf2" },
70 { RTLIB::OGE_F32, "__mips16_gesf2" },
71 { RTLIB::OGT_F64, "__mips16_gtdf2" },
72 { RTLIB::OGT_F32, "__mips16_gtsf2" },
73 { RTLIB::OLE_F64, "__mips16_ledf2" },
74 { RTLIB::OLE_F32, "__mips16_lesf2" },
75 { RTLIB::OLT_F64, "__mips16_ltdf2" },
76 { RTLIB::OLT_F32, "__mips16_ltsf2" },
77 { RTLIB::MUL_F64, "__mips16_muldf3" },
78 { RTLIB::MUL_F32, "__mips16_mulsf3" },
79 { RTLIB::UNE_F64, "__mips16_nedf2" },
80 { RTLIB::UNE_F32, "__mips16_nesf2" },
81 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_dc" }, // No associated libcall.
82 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_df" }, // No associated libcall.
83 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_sc" }, // No associated libcall.
84 { RTLIB::UNKNOWN_LIBCALL, "__mips16_ret_sf" }, // No associated libcall.
85 { RTLIB::SUB_F64, "__mips16_subdf3" },
86 { RTLIB::SUB_F32, "__mips16_subsf3" },
87 { RTLIB::FPROUND_F64_F32, "__mips16_truncdfsf2" },
88 { RTLIB::UO_F64, "__mips16_unorddf2" },
89 { RTLIB::UO_F32, "__mips16_unordsf2" }
90};
91
Reed Kotler83f879d2013-08-01 21:17:53 +000092static const Mips16IntrinsicHelperType Mips16IntrinsicHelper[] = {
Reed Kotlerbe316cf2013-08-09 21:33:41 +000093 {"__fixunsdfsi", "__mips16_call_stub_2" },
Reed Kotler83f879d2013-08-01 21:17:53 +000094 {"ceil", "__mips16_call_stub_df_2"},
95 {"ceilf", "__mips16_call_stub_sf_1"},
96 {"copysign", "__mips16_call_stub_df_10"},
97 {"copysignf", "__mips16_call_stub_sf_5"},
98 {"cos", "__mips16_call_stub_df_2"},
99 {"cosf", "__mips16_call_stub_sf_1"},
100 {"exp2", "__mips16_call_stub_df_2"},
101 {"exp2f", "__mips16_call_stub_sf_1"},
102 {"floor", "__mips16_call_stub_df_2"},
103 {"floorf", "__mips16_call_stub_sf_1"},
104 {"log2", "__mips16_call_stub_df_2"},
105 {"log2f", "__mips16_call_stub_sf_1"},
106 {"nearbyint", "__mips16_call_stub_df_2"},
107 {"nearbyintf", "__mips16_call_stub_sf_1"},
108 {"rint", "__mips16_call_stub_df_2"},
109 {"rintf", "__mips16_call_stub_sf_1"},
110 {"sin", "__mips16_call_stub_df_2"},
111 {"sinf", "__mips16_call_stub_sf_1"},
112 {"sqrt", "__mips16_call_stub_df_2"},
113 {"sqrtf", "__mips16_call_stub_sf_1"},
114 {"trunc", "__mips16_call_stub_df_2"},
115 {"truncf", "__mips16_call_stub_sf_1"},
116};
117
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000118Mips16TargetLowering::Mips16TargetLowering(MipsTargetMachine &TM)
119 : MipsTargetLowering(TM) {
Reed Kotlerfafaa9d2013-03-14 22:02:09 +0000120 //
121 // set up as if mips32 and then revert so we can test the mechanism
122 // for switching
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +0000123 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
Reed Kotlerfafaa9d2013-03-14 22:02:09 +0000124 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
125 computeRegisterProperties();
126 clearRegisterClasses();
127
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000128 // Set up the register classes
129 addRegisterClass(MVT::i32, &Mips::CPU16RegsRegClass);
130
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000131 if (Subtarget->inMips16HardFloat())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000132 setMips16HardFloatLibCalls();
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000133
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000134 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
135 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
136 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
137 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
138 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
139 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
140 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
141 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
142 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
143 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
144 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
145 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
146 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
147
Reed Kotler97309af2013-10-08 17:32:33 +0000148 setOperationAction(ISD::ROTR, MVT::i32, Expand);
149 setOperationAction(ISD::ROTR, MVT::i64, Expand);
150 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
151 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
152
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000153 computeRegisterProperties();
154}
155
156const MipsTargetLowering *
157llvm::createMips16TargetLowering(MipsTargetMachine &TM) {
158 return new Mips16TargetLowering(TM);
159}
160
161bool
Matt Arsenault25793a32014-02-05 23:15:53 +0000162Mips16TargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
163 unsigned,
164 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000165 return false;
166}
167
168MachineBasicBlock *
169Mips16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
170 MachineBasicBlock *BB) const {
171 switch (MI->getOpcode()) {
172 default:
173 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
174 case Mips::SelBeqZ:
175 return emitSel16(Mips::BeqzRxImm16, MI, BB);
176 case Mips::SelBneZ:
177 return emitSel16(Mips::BnezRxImm16, MI, BB);
178 case Mips::SelTBteqZCmpi:
Reed Kotler09e59152013-11-15 02:21:52 +0000179 return emitSeliT16(Mips::Bteqz16, Mips::CmpiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000180 case Mips::SelTBteqZSlti:
Reed Kotler09e59152013-11-15 02:21:52 +0000181 return emitSeliT16(Mips::Bteqz16, Mips::SltiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000182 case Mips::SelTBteqZSltiu:
Reed Kotler09e59152013-11-15 02:21:52 +0000183 return emitSeliT16(Mips::Bteqz16, Mips::SltiuRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000184 case Mips::SelTBtneZCmpi:
Reed Kotler09e59152013-11-15 02:21:52 +0000185 return emitSeliT16(Mips::Btnez16, Mips::CmpiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000186 case Mips::SelTBtneZSlti:
Reed Kotler09e59152013-11-15 02:21:52 +0000187 return emitSeliT16(Mips::Btnez16, Mips::SltiRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000188 case Mips::SelTBtneZSltiu:
Reed Kotler09e59152013-11-15 02:21:52 +0000189 return emitSeliT16(Mips::Btnez16, Mips::SltiuRxImmX16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000190 case Mips::SelTBteqZCmp:
Reed Kotler09e59152013-11-15 02:21:52 +0000191 return emitSelT16(Mips::Bteqz16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000192 case Mips::SelTBteqZSlt:
Reed Kotler09e59152013-11-15 02:21:52 +0000193 return emitSelT16(Mips::Bteqz16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000194 case Mips::SelTBteqZSltu:
Reed Kotler09e59152013-11-15 02:21:52 +0000195 return emitSelT16(Mips::Bteqz16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000196 case Mips::SelTBtneZCmp:
Reed Kotler09e59152013-11-15 02:21:52 +0000197 return emitSelT16(Mips::Btnez16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000198 case Mips::SelTBtneZSlt:
Reed Kotler09e59152013-11-15 02:21:52 +0000199 return emitSelT16(Mips::Btnez16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000200 case Mips::SelTBtneZSltu:
Reed Kotler09e59152013-11-15 02:21:52 +0000201 return emitSelT16(Mips::Btnez16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000202 case Mips::BteqzT8CmpX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000203 return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000204 case Mips::BteqzT8SltX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000205 return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000206 case Mips::BteqzT8SltuX16:
207 // TBD: figure out a way to get this or remove the instruction
208 // altogether.
Reed Kotler09e59152013-11-15 02:21:52 +0000209 return emitFEXT_T8I816_ins(Mips::Bteqz16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000210 case Mips::BtnezT8CmpX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000211 return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::CmpRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000212 case Mips::BtnezT8SltX16:
Reed Kotler09e59152013-11-15 02:21:52 +0000213 return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::SltRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000214 case Mips::BtnezT8SltuX16:
215 // TBD: figure out a way to get this or remove the instruction
216 // altogether.
Reed Kotler09e59152013-11-15 02:21:52 +0000217 return emitFEXT_T8I816_ins(Mips::Btnez16, Mips::SltuRxRy16, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000218 case Mips::BteqzT8CmpiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000219 Mips::Bteqz16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000220 case Mips::BteqzT8SltiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000221 Mips::Bteqz16, Mips::SltiRxImm16, Mips::SltiRxImmX16, true, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000222 case Mips::BteqzT8SltiuX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000223 Mips::Bteqz16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000224 case Mips::BtnezT8CmpiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000225 Mips::Btnez16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000226 case Mips::BtnezT8SltiX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000227 Mips::Btnez16, Mips::SltiRxImm16, Mips::SltiRxImmX16, true, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000228 case Mips::BtnezT8SltiuX16: return emitFEXT_T8I8I16_ins(
Reed Kotler09e59152013-11-15 02:21:52 +0000229 Mips::Btnez16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, false, MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000230 break;
231 case Mips::SltCCRxRy16:
232 return emitFEXT_CCRX16_ins(Mips::SltRxRy16, MI, BB);
233 break;
234 case Mips::SltiCCRxImmX16:
235 return emitFEXT_CCRXI16_ins
236 (Mips::SltiRxImm16, Mips::SltiRxImmX16, MI, BB);
237 case Mips::SltiuCCRxImmX16:
238 return emitFEXT_CCRXI16_ins
239 (Mips::SltiuRxImm16, Mips::SltiuRxImmX16, MI, BB);
240 case Mips::SltuCCRxRy16:
241 return emitFEXT_CCRX16_ins
242 (Mips::SltuRxRy16, MI, BB);
243 }
244}
245
246bool Mips16TargetLowering::
247isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
248 unsigned NextStackOffset,
249 const MipsFunctionInfo& FI) const {
250 // No tail call optimization for mips16.
251 return false;
252}
253
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000254void Mips16TargetLowering::setMips16HardFloatLibCalls() {
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000255 for (unsigned I = 0; I != array_lengthof(HardFloatLibCalls); ++I) {
256 assert((I == 0 || HardFloatLibCalls[I - 1] < HardFloatLibCalls[I]) &&
257 "Array not sorted!");
258 if (HardFloatLibCalls[I].Libcall != RTLIB::UNKNOWN_LIBCALL)
259 setLibcallName(HardFloatLibCalls[I].Libcall, HardFloatLibCalls[I].Name);
260 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000261
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000262 setLibcallName(RTLIB::O_F64, "__mips16_unorddf2");
263 setLibcallName(RTLIB::O_F32, "__mips16_unordsf2");
264}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000265
266//
267// The Mips16 hard float is a crazy quilt inherited from gcc. I have a much
268// cleaner way to do all of this but it will have to wait until the traditional
269// gcc mechanism is completed.
270//
271// For Pic, in order for Mips16 code to call Mips32 code which according the abi
272// have either arguments or returned values placed in floating point registers,
273// we use a set of helper functions. (This includes functions which return type
274// complex which on Mips are returned in a pair of floating point registers).
275//
276// This is an encoding that we inherited from gcc.
277// In Mips traditional O32, N32 ABI, floating point numbers are passed in
278// floating point argument registers 1,2 only when the first and optionally
279// the second arguments are float (sf) or double (df).
280// For Mips16 we are only concerned with the situations where floating point
281// arguments are being passed in floating point registers by the ABI, because
282// Mips16 mode code cannot execute floating point instructions to load those
283// values and hence helper functions are needed.
284// The possibilities are (), (sf), (sf, sf), (sf, df), (df), (df, sf), (df, df)
285// the helper function suffixs for these are:
286// 0, 1, 5, 9, 2, 6, 10
287// this suffix can then be calculated as follows:
288// for a given argument Arg:
289// Arg1x, Arg2x = 1 : Arg is sf
290// 2 : Arg is df
291// 0: Arg is neither sf or df
292// So this stub is the string for number Arg1x + Arg2x*4.
293// However not all numbers between 0 and 10 are possible, we check anyway and
294// assert if the impossible exists.
295//
296
297unsigned int Mips16TargetLowering::getMips16HelperFunctionStubNumber
298 (ArgListTy &Args) const {
299 unsigned int resultNum = 0;
300 if (Args.size() >= 1) {
301 Type *t = Args[0].Ty;
302 if (t->isFloatTy()) {
303 resultNum = 1;
304 }
305 else if (t->isDoubleTy()) {
306 resultNum = 2;
307 }
308 }
309 if (resultNum) {
310 if (Args.size() >=2) {
311 Type *t = Args[1].Ty;
312 if (t->isFloatTy()) {
313 resultNum += 4;
314 }
315 else if (t->isDoubleTy()) {
316 resultNum += 8;
317 }
318 }
319 }
320 return resultNum;
321}
322
323//
324// prefixs are attached to stub numbers depending on the return type .
325// return type: float sf_
326// double df_
327// single complex sc_
328// double complext dc_
329// others NO PREFIX
330//
331//
332// The full name of a helper function is__mips16_call_stub +
333// return type dependent prefix + stub number
334//
335//
336// This is something that probably should be in a different source file and
337// perhaps done differently but my main purpose is to not waste runtime
338// on something that we can enumerate in the source. Another possibility is
339// to have a python script to generate these mapping tables. This will do
340// for now. There are a whole series of helper function mapping arrays, one
341// for each return type class as outlined above. There there are 11 possible
342// entries. Ones with 0 are ones which should never be selected
343//
344// All the arrays are similar except for ones which return neither
345// sf, df, sc, dc, in which only care about ones which have sf or df as a
346// first parameter.
347//
348#define P_ "__mips16_call_stub_"
349#define MAX_STUB_NUMBER 10
350#define T1 P "1", P "2", 0, 0, P "5", P "6", 0, 0, P "9", P "10"
351#define T P "0" , T1
352#define P P_
353static char const * vMips16Helper[MAX_STUB_NUMBER+1] =
354 {0, T1 };
355#undef P
356#define P P_ "sf_"
357static char const * sfMips16Helper[MAX_STUB_NUMBER+1] =
358 { T };
359#undef P
360#define P P_ "df_"
361static char const * dfMips16Helper[MAX_STUB_NUMBER+1] =
362 { T };
363#undef P
364#define P P_ "sc_"
365static char const * scMips16Helper[MAX_STUB_NUMBER+1] =
366 { T };
367#undef P
368#define P P_ "dc_"
369static char const * dcMips16Helper[MAX_STUB_NUMBER+1] =
370 { T };
371#undef P
372#undef P_
373
374
375const char* Mips16TargetLowering::
376 getMips16HelperFunction
377 (Type* RetTy, ArgListTy &Args, bool &needHelper) const {
378 const unsigned int stubNum = getMips16HelperFunctionStubNumber(Args);
379#ifndef NDEBUG
380 const unsigned int maxStubNum = 10;
381 assert(stubNum <= maxStubNum);
382 const bool validStubNum[maxStubNum+1] =
383 {true, true, true, false, false, true, true, false, false, true, true};
384 assert(validStubNum[stubNum]);
385#endif
386 const char *result;
387 if (RetTy->isFloatTy()) {
388 result = sfMips16Helper[stubNum];
389 }
390 else if (RetTy ->isDoubleTy()) {
391 result = dfMips16Helper[stubNum];
392 }
393 else if (RetTy->isStructTy()) {
394 // check if it's complex
395 if (RetTy->getNumContainedTypes() == 2) {
396 if ((RetTy->getContainedType(0)->isFloatTy()) &&
397 (RetTy->getContainedType(1)->isFloatTy())) {
398 result = scMips16Helper[stubNum];
399 }
400 else if ((RetTy->getContainedType(0)->isDoubleTy()) &&
401 (RetTy->getContainedType(1)->isDoubleTy())) {
402 result = dcMips16Helper[stubNum];
403 }
404 else {
405 llvm_unreachable("Uncovered condition");
406 }
407 }
408 else {
409 llvm_unreachable("Uncovered condition");
410 }
411 }
412 else {
413 if (stubNum == 0) {
414 needHelper = false;
415 return "";
416 }
417 result = vMips16Helper[stubNum];
418 }
419 needHelper = true;
420 return result;
421}
422
423void Mips16TargetLowering::
424getOpndList(SmallVectorImpl<SDValue> &Ops,
425 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
426 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
427 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
428 SelectionDAG &DAG = CLI.DAG;
Akira Hatanakaaf4211a2013-09-28 00:12:32 +0000429 MachineFunction &MF = DAG.getMachineFunction();
430 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000431 const char* Mips16HelperFunction = 0;
432 bool NeedMips16Helper = false;
433
Simon Atanasyanfde102c2013-12-25 17:00:27 +0000434 if (Subtarget->inMips16HardFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000435 //
436 // currently we don't have symbols tagged with the mips16 or mips32
437 // qualifier so we will assume that we don't know what kind it is.
438 // and generate the helper
439 //
440 bool LookupHelper = true;
441 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(CLI.Callee)) {
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000442 Mips16Libcall Find = { RTLIB::UNKNOWN_LIBCALL, S->getSymbol() };
443
444 if (std::binary_search(HardFloatLibCalls, array_endof(HardFloatLibCalls),
445 Find))
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000446 LookupHelper = false;
Reed Kotler83f879d2013-08-01 21:17:53 +0000447 else {
448 Mips16IntrinsicHelperType IntrinsicFind = {S->getSymbol(), ""};
449 // one more look at list of intrinsics
450 if (std::binary_search(Mips16IntrinsicHelper,
451 array_endof(Mips16IntrinsicHelper),
452 IntrinsicFind)) {
453 const Mips16IntrinsicHelperType *h =(std::find(Mips16IntrinsicHelper,
454 array_endof(Mips16IntrinsicHelper),
455 IntrinsicFind));
456 Mips16HelperFunction = h->Helper;
457 NeedMips16Helper = true;
458 LookupHelper = false;
459 }
460
461 }
Benjamin Kramerc4d547d2013-06-13 19:06:52 +0000462 } else if (GlobalAddressSDNode *G =
463 dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
464 Mips16Libcall Find = { RTLIB::UNKNOWN_LIBCALL,
465 G->getGlobal()->getName().data() };
466
467 if (std::binary_search(HardFloatLibCalls, array_endof(HardFloatLibCalls),
468 Find))
Reed Kotler0fed8d42013-05-21 00:50:30 +0000469 LookupHelper = false;
Reed Kotler0fed8d42013-05-21 00:50:30 +0000470 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000471 if (LookupHelper) Mips16HelperFunction =
472 getMips16HelperFunction(CLI.RetTy, CLI.Args, NeedMips16Helper);
473
474 }
475
476 SDValue JumpTarget = Callee;
477
478 // T9 should contain the address of the callee function if
479 // -reloction-model=pic or it is an indirect call.
480 if (IsPICCall || !GlobalOrExternal) {
481 unsigned V0Reg = Mips::V0;
482 if (NeedMips16Helper) {
483 RegsToPass.push_front(std::make_pair(V0Reg, Callee));
484 JumpTarget = DAG.getExternalSymbol(Mips16HelperFunction, getPointerTy());
Akira Hatanakaaf4211a2013-09-28 00:12:32 +0000485 ExternalSymbolSDNode *S = cast<ExternalSymbolSDNode>(JumpTarget);
486 JumpTarget = getAddrGlobal(S, JumpTarget.getValueType(), DAG,
487 MipsII::MO_GOT, Chain,
488 FuncInfo->callPtrInfo(S->getSymbol()));
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000489 } else
490 RegsToPass.push_front(std::make_pair((unsigned)Mips::T9, Callee));
491 }
492
493 Ops.push_back(JumpTarget);
494
495 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
496 InternalLinkage, CLI, Callee, Chain);
497}
498
499MachineBasicBlock *Mips16TargetLowering::
500emitSel16(unsigned Opc, MachineInstr *MI, MachineBasicBlock *BB) const {
501 if (DontExpandCondPseudos16)
502 return BB;
503 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
504 DebugLoc DL = MI->getDebugLoc();
505 // To "insert" a SELECT_CC instruction, we actually have to insert the
506 // diamond control-flow pattern. The incoming instruction knows the
507 // destination vreg to set, the condition code register to branch on, the
508 // true/false values to select between, and a branch opcode to use.
509 const BasicBlock *LLVM_BB = BB->getBasicBlock();
510 MachineFunction::iterator It = BB;
511 ++It;
512
513 // thisMBB:
514 // ...
515 // TrueVal = ...
516 // setcc r1, r2, r3
517 // bNE r1, r0, copy1MBB
518 // fallthrough --> copy0MBB
519 MachineBasicBlock *thisMBB = BB;
520 MachineFunction *F = BB->getParent();
521 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
522 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
523 F->insert(It, copy0MBB);
524 F->insert(It, sinkMBB);
525
526 // Transfer the remainder of BB and its successor edges to sinkMBB.
527 sinkMBB->splice(sinkMBB->begin(), BB,
528 llvm::next(MachineBasicBlock::iterator(MI)),
529 BB->end());
530 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
531
532 // Next, add the true and fallthrough blocks as its successors.
533 BB->addSuccessor(copy0MBB);
534 BB->addSuccessor(sinkMBB);
535
536 BuildMI(BB, DL, TII->get(Opc)).addReg(MI->getOperand(3).getReg())
537 .addMBB(sinkMBB);
538
539 // copy0MBB:
540 // %FalseValue = ...
541 // # fallthrough to sinkMBB
542 BB = copy0MBB;
543
544 // Update machine-CFG edges
545 BB->addSuccessor(sinkMBB);
546
547 // sinkMBB:
548 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
549 // ...
550 BB = sinkMBB;
551
552 BuildMI(*BB, BB->begin(), DL,
553 TII->get(Mips::PHI), MI->getOperand(0).getReg())
554 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
555 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
556
557 MI->eraseFromParent(); // The pseudo instruction is gone now.
558 return BB;
559}
560
561MachineBasicBlock *Mips16TargetLowering::emitSelT16
562 (unsigned Opc1, unsigned Opc2,
563 MachineInstr *MI, MachineBasicBlock *BB) const {
564 if (DontExpandCondPseudos16)
565 return BB;
566 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
567 DebugLoc DL = MI->getDebugLoc();
568 // To "insert" a SELECT_CC instruction, we actually have to insert the
569 // diamond control-flow pattern. The incoming instruction knows the
570 // destination vreg to set, the condition code register to branch on, the
571 // true/false values to select between, and a branch opcode to use.
572 const BasicBlock *LLVM_BB = BB->getBasicBlock();
573 MachineFunction::iterator It = BB;
574 ++It;
575
576 // thisMBB:
577 // ...
578 // TrueVal = ...
579 // setcc r1, r2, r3
580 // bNE r1, r0, copy1MBB
581 // fallthrough --> copy0MBB
582 MachineBasicBlock *thisMBB = BB;
583 MachineFunction *F = BB->getParent();
584 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
585 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
586 F->insert(It, copy0MBB);
587 F->insert(It, sinkMBB);
588
589 // Transfer the remainder of BB and its successor edges to sinkMBB.
590 sinkMBB->splice(sinkMBB->begin(), BB,
591 llvm::next(MachineBasicBlock::iterator(MI)),
592 BB->end());
593 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
594
595 // Next, add the true and fallthrough blocks as its successors.
596 BB->addSuccessor(copy0MBB);
597 BB->addSuccessor(sinkMBB);
598
599 BuildMI(BB, DL, TII->get(Opc2)).addReg(MI->getOperand(3).getReg())
600 .addReg(MI->getOperand(4).getReg());
601 BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
602
603 // copy0MBB:
604 // %FalseValue = ...
605 // # fallthrough to sinkMBB
606 BB = copy0MBB;
607
608 // Update machine-CFG edges
609 BB->addSuccessor(sinkMBB);
610
611 // sinkMBB:
612 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
613 // ...
614 BB = sinkMBB;
615
616 BuildMI(*BB, BB->begin(), DL,
617 TII->get(Mips::PHI), MI->getOperand(0).getReg())
618 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
619 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
620
621 MI->eraseFromParent(); // The pseudo instruction is gone now.
622 return BB;
623
624}
625
626MachineBasicBlock *Mips16TargetLowering::emitSeliT16
627 (unsigned Opc1, unsigned Opc2,
628 MachineInstr *MI, MachineBasicBlock *BB) const {
629 if (DontExpandCondPseudos16)
630 return BB;
631 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
632 DebugLoc DL = MI->getDebugLoc();
633 // To "insert" a SELECT_CC instruction, we actually have to insert the
634 // diamond control-flow pattern. The incoming instruction knows the
635 // destination vreg to set, the condition code register to branch on, the
636 // true/false values to select between, and a branch opcode to use.
637 const BasicBlock *LLVM_BB = BB->getBasicBlock();
638 MachineFunction::iterator It = BB;
639 ++It;
640
641 // thisMBB:
642 // ...
643 // TrueVal = ...
644 // setcc r1, r2, r3
645 // bNE r1, r0, copy1MBB
646 // fallthrough --> copy0MBB
647 MachineBasicBlock *thisMBB = BB;
648 MachineFunction *F = BB->getParent();
649 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
650 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
651 F->insert(It, copy0MBB);
652 F->insert(It, sinkMBB);
653
654 // Transfer the remainder of BB and its successor edges to sinkMBB.
655 sinkMBB->splice(sinkMBB->begin(), BB,
656 llvm::next(MachineBasicBlock::iterator(MI)),
657 BB->end());
658 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
659
660 // Next, add the true and fallthrough blocks as its successors.
661 BB->addSuccessor(copy0MBB);
662 BB->addSuccessor(sinkMBB);
663
664 BuildMI(BB, DL, TII->get(Opc2)).addReg(MI->getOperand(3).getReg())
665 .addImm(MI->getOperand(4).getImm());
666 BuildMI(BB, DL, TII->get(Opc1)).addMBB(sinkMBB);
667
668 // copy0MBB:
669 // %FalseValue = ...
670 // # fallthrough to sinkMBB
671 BB = copy0MBB;
672
673 // Update machine-CFG edges
674 BB->addSuccessor(sinkMBB);
675
676 // sinkMBB:
677 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
678 // ...
679 BB = sinkMBB;
680
681 BuildMI(*BB, BB->begin(), DL,
682 TII->get(Mips::PHI), MI->getOperand(0).getReg())
683 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
684 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
685
686 MI->eraseFromParent(); // The pseudo instruction is gone now.
687 return BB;
688
689}
690
691MachineBasicBlock
692 *Mips16TargetLowering::emitFEXT_T8I816_ins(unsigned BtOpc, unsigned CmpOpc,
693 MachineInstr *MI,
694 MachineBasicBlock *BB) const {
695 if (DontExpandCondPseudos16)
696 return BB;
697 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
698 unsigned regX = MI->getOperand(0).getReg();
699 unsigned regY = MI->getOperand(1).getReg();
700 MachineBasicBlock *target = MI->getOperand(2).getMBB();
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000701 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(CmpOpc)).addReg(regX)
702 .addReg(regY);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000703 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(BtOpc)).addMBB(target);
704 MI->eraseFromParent(); // The pseudo instruction is gone now.
705 return BB;
706}
707
708MachineBasicBlock *Mips16TargetLowering::emitFEXT_T8I8I16_ins(
Reed Kotlerce510832013-06-09 23:23:46 +0000709 unsigned BtOpc, unsigned CmpiOpc, unsigned CmpiXOpc, bool ImmSigned,
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000710 MachineInstr *MI, MachineBasicBlock *BB) const {
711 if (DontExpandCondPseudos16)
712 return BB;
713 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
714 unsigned regX = MI->getOperand(0).getReg();
715 int64_t imm = MI->getOperand(1).getImm();
716 MachineBasicBlock *target = MI->getOperand(2).getMBB();
717 unsigned CmpOpc;
718 if (isUInt<8>(imm))
719 CmpOpc = CmpiOpc;
Reed Kotlerce510832013-06-09 23:23:46 +0000720 else if ((!ImmSigned && isUInt<16>(imm)) ||
721 (ImmSigned && isInt<16>(imm)))
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000722 CmpOpc = CmpiXOpc;
723 else
724 llvm_unreachable("immediate field not usable");
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000725 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(CmpOpc)).addReg(regX)
726 .addImm(imm);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000727 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(BtOpc)).addMBB(target);
728 MI->eraseFromParent(); // The pseudo instruction is gone now.
729 return BB;
730}
731
732static unsigned Mips16WhichOp8uOr16simm
733 (unsigned shortOp, unsigned longOp, int64_t Imm) {
734 if (isUInt<8>(Imm))
735 return shortOp;
736 else if (isInt<16>(Imm))
737 return longOp;
738 else
739 llvm_unreachable("immediate field not usable");
740}
741
742MachineBasicBlock *Mips16TargetLowering::emitFEXT_CCRX16_ins(
743 unsigned SltOpc,
744 MachineInstr *MI, MachineBasicBlock *BB) const {
745 if (DontExpandCondPseudos16)
746 return BB;
747 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
748 unsigned CC = MI->getOperand(0).getReg();
749 unsigned regX = MI->getOperand(1).getReg();
750 unsigned regY = MI->getOperand(2).getReg();
751 BuildMI(*BB, MI, MI->getDebugLoc(),
752 TII->get(SltOpc)).addReg(regX).addReg(regY);
753 BuildMI(*BB, MI, MI->getDebugLoc(),
754 TII->get(Mips::MoveR3216), CC).addReg(Mips::T8);
755 MI->eraseFromParent(); // The pseudo instruction is gone now.
756 return BB;
757}
758
759MachineBasicBlock *Mips16TargetLowering::emitFEXT_CCRXI16_ins(
760 unsigned SltiOpc, unsigned SltiXOpc,
761 MachineInstr *MI, MachineBasicBlock *BB )const {
762 if (DontExpandCondPseudos16)
763 return BB;
764 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
765 unsigned CC = MI->getOperand(0).getReg();
766 unsigned regX = MI->getOperand(1).getReg();
767 int64_t Imm = MI->getOperand(2).getImm();
768 unsigned SltOpc = Mips16WhichOp8uOr16simm(SltiOpc, SltiXOpc, Imm);
769 BuildMI(*BB, MI, MI->getDebugLoc(),
770 TII->get(SltOpc)).addReg(regX).addImm(Imm);
771 BuildMI(*BB, MI, MI->getDebugLoc(),
772 TII->get(Mips::MoveR3216), CC).addReg(Mips::T8);
773 MI->eraseFromParent(); // The pseudo instruction is gone now.
774 return BB;
775
776}