blob: 6c89424eb6a7b4b6e7efde2015505d4d02547f79 [file] [log] [blame]
Akira Hatanaka44ebe002013-03-14 19:09:52 +00001//===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002//
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 mips32/64.
11//
12//===----------------------------------------------------------------------===//
13#include "MipsSEISelLowering.h"
14#include "MipsRegisterInfo.h"
15#include "MipsTargetMachine.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
Akira Hatanakaa6bbde52013-04-13 02:13:30 +000018#include "llvm/IR/Intrinsics.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000019#include "llvm/Support/CommandLine.h"
20#include "llvm/Target/TargetInstrInfo.h"
21
22using namespace llvm;
23
24static cl::opt<bool>
25EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
26 cl::desc("MIPS: Enable tail calls."), cl::init(false));
27
Akira Hatanaka63791212013-09-07 00:52:30 +000028static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
29 cl::desc("Expand double precision loads and "
30 "stores to their single precision "
31 "counterparts"));
32
Akira Hatanaka96ca1822013-03-13 00:54:29 +000033MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
34 : MipsTargetLowering(TM) {
35 // Set up the register classes
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000036 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000037
38 if (HasMips64)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000039 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000040
Daniel Sanders36c671e2013-09-27 09:44:59 +000041 if (Subtarget->hasDSP() || Subtarget->hasMSA()) {
42 // Expand all truncating stores and extending loads.
43 unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
44 unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
45
46 for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
47 for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
48 setTruncStoreAction((MVT::SimpleValueType)VT0,
49 (MVT::SimpleValueType)VT1, Expand);
50
51 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
52 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
53 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
54 }
55 }
56
Akira Hatanaka96ca1822013-03-13 00:54:29 +000057 if (Subtarget->hasDSP()) {
58 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
59
60 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
Akira Hatanaka654655f2013-08-14 00:53:38 +000061 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000062
63 // Expand all builtin opcodes.
64 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
65 setOperationAction(Opc, VecTys[i], Expand);
66
Akira Hatanaka2f088222013-04-13 00:55:41 +000067 setOperationAction(ISD::ADD, VecTys[i], Legal);
68 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000069 setOperationAction(ISD::LOAD, VecTys[i], Legal);
70 setOperationAction(ISD::STORE, VecTys[i], Legal);
71 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
72 }
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +000073
74 setTargetDAGCombine(ISD::SHL);
75 setTargetDAGCombine(ISD::SRA);
76 setTargetDAGCombine(ISD::SRL);
Akira Hatanaka68741cc2013-04-30 22:37:26 +000077 setTargetDAGCombine(ISD::SETCC);
78 setTargetDAGCombine(ISD::VSELECT);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000079 }
80
Akira Hatanaka2f088222013-04-13 00:55:41 +000081 if (Subtarget->hasDSPR2())
82 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
83
Jack Carter3a2c2d42013-08-13 20:54:07 +000084 if (Subtarget->hasMSA()) {
Daniel Sandersc65f58a2013-09-11 10:15:48 +000085 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
86 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
87 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
88 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
89 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
90 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
91 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
Daniel Sandersf7456c72013-09-23 13:22:24 +000092
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +000093 setTargetDAGCombine(ISD::AND);
94 setTargetDAGCombine(ISD::SRA);
Daniel Sanderse1d24352013-09-24 12:04:44 +000095 setTargetDAGCombine(ISD::VSELECT);
Daniel Sandersf7456c72013-09-23 13:22:24 +000096 setTargetDAGCombine(ISD::XOR);
Jack Carter3a2c2d42013-08-13 20:54:07 +000097 }
98
Reed Kotlerc03807a2013-08-30 19:40:56 +000099 if (!Subtarget->mipsSEUsesSoftFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000100 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
101
102 // When dealing with single precision only, use libcalls
103 if (!Subtarget->isSingleFloat()) {
Akira Hatanakabfb66242013-08-20 23:38:40 +0000104 if (Subtarget->isFP64bit())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000105 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
106 else
107 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
108 }
109 }
110
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000111 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
112 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
113 setOperationAction(ISD::MULHS, MVT::i32, Custom);
114 setOperationAction(ISD::MULHU, MVT::i32, Custom);
115
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000116 if (HasMips64) {
117 setOperationAction(ISD::MULHS, MVT::i64, Custom);
118 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000119 setOperationAction(ISD::MUL, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000120 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000121
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000122 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
123 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
124
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000125 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
126 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
127 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
128 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000129 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
130 setOperationAction(ISD::LOAD, MVT::i32, Custom);
131 setOperationAction(ISD::STORE, MVT::i32, Custom);
132
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000133 setTargetDAGCombine(ISD::ADDE);
134 setTargetDAGCombine(ISD::SUBE);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000135 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000136
Daniel Sandersce09d072013-08-28 12:14:50 +0000137 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000138 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
139 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
140
Akira Hatanaka63791212013-09-07 00:52:30 +0000141 if (NoDPLoadStore) {
142 setOperationAction(ISD::LOAD, MVT::f64, Custom);
143 setOperationAction(ISD::STORE, MVT::f64, Custom);
144 }
145
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000146 computeRegisterProperties();
147}
148
149const MipsTargetLowering *
150llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
151 return new MipsSETargetLowering(TM);
152}
153
Daniel Sanders7a289d02013-09-23 12:02:46 +0000154// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000155void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000156addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
157 addRegisterClass(Ty, RC);
158
159 // Expand all builtin opcodes.
160 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
161 setOperationAction(Opc, Ty, Expand);
162
163 setOperationAction(ISD::BITCAST, Ty, Legal);
164 setOperationAction(ISD::LOAD, Ty, Legal);
165 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000166 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
167 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000168 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000169
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000170 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000171 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000172 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000173 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000174 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000175 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000176 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000177 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000178 setOperationAction(ISD::SHL, Ty, Legal);
179 setOperationAction(ISD::SRA, Ty, Legal);
180 setOperationAction(ISD::SRL, Ty, Legal);
181 setOperationAction(ISD::SUB, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000182 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000183 setOperationAction(ISD::UREM, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000184 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000185 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000186 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000187
Daniel Sanders015972b2013-10-11 10:00:06 +0000188 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
189 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
190 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
191 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
192 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
193 }
194
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000195 setOperationAction(ISD::SETCC, Ty, Legal);
196 setCondCodeAction(ISD::SETNE, Ty, Expand);
197 setCondCodeAction(ISD::SETGE, Ty, Expand);
198 setCondCodeAction(ISD::SETGT, Ty, Expand);
199 setCondCodeAction(ISD::SETUGE, Ty, Expand);
200 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000201}
202
Daniel Sanders7a289d02013-09-23 12:02:46 +0000203// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000204void MipsSETargetLowering::
205addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000206 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000207
208 // Expand all builtin opcodes.
209 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
210 setOperationAction(Opc, Ty, Expand);
211
212 setOperationAction(ISD::LOAD, Ty, Legal);
213 setOperationAction(ISD::STORE, Ty, Legal);
214 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000215 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000216 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000217 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000218
219 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000220 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000221 setOperationAction(ISD::FADD, Ty, Legal);
222 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000223 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000224 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000225 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000226 setOperationAction(ISD::FMUL, Ty, Legal);
227 setOperationAction(ISD::FRINT, Ty, Legal);
228 setOperationAction(ISD::FSQRT, Ty, Legal);
229 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000230 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000231
232 setOperationAction(ISD::SETCC, Ty, Legal);
233 setCondCodeAction(ISD::SETOGE, Ty, Expand);
234 setCondCodeAction(ISD::SETOGT, Ty, Expand);
235 setCondCodeAction(ISD::SETUGE, Ty, Expand);
236 setCondCodeAction(ISD::SETUGT, Ty, Expand);
237 setCondCodeAction(ISD::SETGE, Ty, Expand);
238 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000239 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000240}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000241
242bool
243MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
244 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
245
246 switch (SVT) {
247 case MVT::i64:
248 case MVT::i32:
249 if (Fast)
250 *Fast = true;
251 return true;
252 default:
253 return false;
254 }
255}
256
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000257SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
258 SelectionDAG &DAG) const {
259 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000260 case ISD::LOAD: return lowerLOAD(Op, DAG);
261 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000262 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
263 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
264 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
265 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
266 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
267 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000268 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
269 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000270 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
271 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000272 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000273 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000274 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000275 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000276 }
277
278 return MipsTargetLowering::LowerOperation(Op, DAG);
279}
280
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000281// selectMADD -
282// Transforms a subgraph in CurDAG if the following pattern is found:
283// (addc multLo, Lo0), (adde multHi, Hi0),
284// where,
285// multHi/Lo: product of multiplication
286// Lo0: initial value of Lo register
287// Hi0: initial value of Hi register
288// Return true if pattern matching was successful.
289static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
290 // ADDENode's second operand must be a flag output of an ADDC node in order
291 // for the matching to be successful.
292 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
293
294 if (ADDCNode->getOpcode() != ISD::ADDC)
295 return false;
296
297 SDValue MultHi = ADDENode->getOperand(0);
298 SDValue MultLo = ADDCNode->getOperand(0);
299 SDNode *MultNode = MultHi.getNode();
300 unsigned MultOpc = MultHi.getOpcode();
301
302 // MultHi and MultLo must be generated by the same node,
303 if (MultLo.getNode() != MultNode)
304 return false;
305
306 // and it must be a multiplication.
307 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
308 return false;
309
310 // MultLo amd MultHi must be the first and second output of MultNode
311 // respectively.
312 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
313 return false;
314
315 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
316 // of the values of MultNode, in which case MultNode will be removed in later
317 // phases.
318 // If there exist users other than ADDENode or ADDCNode, this function returns
319 // here, which will result in MultNode being mapped to a single MULT
320 // instruction node rather than a pair of MULT and MADD instructions being
321 // produced.
322 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
323 return false;
324
Andrew Trickef9de2a2013-05-25 02:42:55 +0000325 SDLoc DL(ADDENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000326
327 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000328 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000329 ADDCNode->getOperand(1),
330 ADDENode->getOperand(1));
331
332 // create MipsMAdd(u) node
333 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
334
335 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
336 MultNode->getOperand(0),// Factor 0
337 MultNode->getOperand(1),// Factor 1
338 ACCIn);
339
340 // replace uses of adde and addc here
341 if (!SDValue(ADDCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000342 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000343 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
344 }
345 if (!SDValue(ADDENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000346 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000347 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
348 }
349
350 return true;
351}
352
353// selectMSUB -
354// Transforms a subgraph in CurDAG if the following pattern is found:
355// (addc Lo0, multLo), (sube Hi0, multHi),
356// where,
357// multHi/Lo: product of multiplication
358// Lo0: initial value of Lo register
359// Hi0: initial value of Hi register
360// Return true if pattern matching was successful.
361static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
362 // SUBENode's second operand must be a flag output of an SUBC node in order
363 // for the matching to be successful.
364 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
365
366 if (SUBCNode->getOpcode() != ISD::SUBC)
367 return false;
368
369 SDValue MultHi = SUBENode->getOperand(1);
370 SDValue MultLo = SUBCNode->getOperand(1);
371 SDNode *MultNode = MultHi.getNode();
372 unsigned MultOpc = MultHi.getOpcode();
373
374 // MultHi and MultLo must be generated by the same node,
375 if (MultLo.getNode() != MultNode)
376 return false;
377
378 // and it must be a multiplication.
379 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
380 return false;
381
382 // MultLo amd MultHi must be the first and second output of MultNode
383 // respectively.
384 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
385 return false;
386
387 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
388 // of the values of MultNode, in which case MultNode will be removed in later
389 // phases.
390 // If there exist users other than SUBENode or SUBCNode, this function returns
391 // here, which will result in MultNode being mapped to a single MULT
392 // instruction node rather than a pair of MULT and MSUB instructions being
393 // produced.
394 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
395 return false;
396
Andrew Trickef9de2a2013-05-25 02:42:55 +0000397 SDLoc DL(SUBENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000398
399 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000400 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000401 SUBCNode->getOperand(0),
402 SUBENode->getOperand(0));
403
404 // create MipsSub(u) node
405 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
406
407 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
408 MultNode->getOperand(0),// Factor 0
409 MultNode->getOperand(1),// Factor 1
410 ACCIn);
411
412 // replace uses of sube and subc here
413 if (!SDValue(SUBCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000414 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000415 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
416 }
417 if (!SDValue(SUBENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000418 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000419 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
420 }
421
422 return true;
423}
424
425static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
426 TargetLowering::DAGCombinerInfo &DCI,
427 const MipsSubtarget *Subtarget) {
428 if (DCI.isBeforeLegalize())
429 return SDValue();
430
431 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
432 selectMADD(N, &DAG))
433 return SDValue(N, 0);
434
435 return SDValue();
436}
437
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000438// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
439//
440// Performs the following transformations:
441// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
442// sign/zero-extension is completely overwritten by the new one performed by
443// the ISD::AND.
444// - Removes redundant zero extensions performed by an ISD::AND.
445static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
446 TargetLowering::DAGCombinerInfo &DCI,
447 const MipsSubtarget *Subtarget) {
448 if (!Subtarget->hasMSA())
449 return SDValue();
450
451 SDValue Op0 = N->getOperand(0);
452 SDValue Op1 = N->getOperand(1);
453 unsigned Op0Opcode = Op0->getOpcode();
454
455 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
456 // where $d + 1 == 2^n and n == 32
457 // or $d + 1 == 2^n and n <= 32 and ZExt
458 // -> (MipsVExtractZExt $a, $b, $c)
459 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
460 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
461 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
462
463 if (!Mask)
464 return SDValue();
465
466 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
467
468 if (Log2IfPositive <= 0)
469 return SDValue(); // Mask+1 is not a power of 2
470
471 SDValue Op0Op2 = Op0->getOperand(2);
472 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
473 unsigned ExtendTySize = ExtendTy.getSizeInBits();
474 unsigned Log2 = Log2IfPositive;
475
476 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
477 Log2 == ExtendTySize) {
478 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
479 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
480 Op0->getVTList(), Ops, Op0->getNumOperands());
481 return Op0;
482 }
483 }
484
485 return SDValue();
486}
487
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000488static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
489 TargetLowering::DAGCombinerInfo &DCI,
490 const MipsSubtarget *Subtarget) {
491 if (DCI.isBeforeLegalize())
492 return SDValue();
493
494 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
495 selectMSUB(N, &DAG))
496 return SDValue(N, 0);
497
498 return SDValue();
499}
500
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000501static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
502 EVT ShiftTy, SelectionDAG &DAG) {
503 // Clear the upper (64 - VT.sizeInBits) bits.
504 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
505
506 // Return 0.
507 if (C == 0)
508 return DAG.getConstant(0, VT);
509
510 // Return x.
511 if (C == 1)
512 return X;
513
514 // If c is power of 2, return (shl x, log2(c)).
515 if (isPowerOf2_64(C))
516 return DAG.getNode(ISD::SHL, DL, VT, X,
517 DAG.getConstant(Log2_64(C), ShiftTy));
518
519 unsigned Log2Ceil = Log2_64_Ceil(C);
520 uint64_t Floor = 1LL << Log2_64(C);
521 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
522
523 // If |c - floor_c| <= |c - ceil_c|,
524 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
525 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
526 if (C - Floor <= Ceil - C) {
527 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
528 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
529 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
530 }
531
532 // If |c - floor_c| > |c - ceil_c|,
533 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
534 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
535 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
536 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
537}
538
539static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
540 const TargetLowering::DAGCombinerInfo &DCI,
541 const MipsSETargetLowering *TL) {
542 EVT VT = N->getValueType(0);
543
544 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
545 if (!VT.isVector())
546 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
547 VT, TL->getScalarShiftAmountTy(VT), DAG);
548
549 return SDValue(N, 0);
550}
551
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000552static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
553 SelectionDAG &DAG,
554 const MipsSubtarget *Subtarget) {
555 // See if this is a vector splat immediate node.
556 APInt SplatValue, SplatUndef;
557 unsigned SplatBitSize;
558 bool HasAnyUndefs;
559 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
560 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
561
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000562 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000563 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000564 EltSize, !Subtarget->isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000565 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000566 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000567 return SDValue();
568
Andrew Trickef9de2a2013-05-25 02:42:55 +0000569 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000570 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
571}
572
573static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
574 TargetLowering::DAGCombinerInfo &DCI,
575 const MipsSubtarget *Subtarget) {
576 EVT Ty = N->getValueType(0);
577
578 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
579 return SDValue();
580
581 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
582}
583
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000584// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
585// constant splats into MipsISD::SHRA_DSP for DSPr2.
586//
587// Performs the following transformations:
588// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
589// sign/zero-extension is completely overwritten by the new one performed by
590// the ISD::SRA and ISD::SHL nodes.
591// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
592// sequence.
593//
594// See performDSPShiftCombine for more information about the transformation
595// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000596static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
597 TargetLowering::DAGCombinerInfo &DCI,
598 const MipsSubtarget *Subtarget) {
599 EVT Ty = N->getValueType(0);
600
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000601 if (Subtarget->hasMSA()) {
602 SDValue Op0 = N->getOperand(0);
603 SDValue Op1 = N->getOperand(1);
604
605 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
606 // where $d + sizeof($c) == 32
607 // or $d + sizeof($c) <= 32 and SExt
608 // -> (MipsVExtractSExt $a, $b, $c)
609 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
610 SDValue Op0Op0 = Op0->getOperand(0);
611 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
612
613 if (!ShAmount)
614 return SDValue();
615
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000616 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
617 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
618 return SDValue();
619
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000620 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
621 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
622
623 if (TotalBits == 32 ||
624 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
625 TotalBits <= 32)) {
626 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
627 Op0Op0->getOperand(2) };
628 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
629 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
630 return Op0Op0;
631 }
632 }
633 }
634
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000635 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
636 return SDValue();
637
638 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
639}
640
641
642static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
643 TargetLowering::DAGCombinerInfo &DCI,
644 const MipsSubtarget *Subtarget) {
645 EVT Ty = N->getValueType(0);
646
647 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
648 return SDValue();
649
650 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
651}
652
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000653static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
654 bool IsV216 = (Ty == MVT::v2i16);
655
656 switch (CC) {
657 case ISD::SETEQ:
658 case ISD::SETNE: return true;
659 case ISD::SETLT:
660 case ISD::SETLE:
661 case ISD::SETGT:
662 case ISD::SETGE: return IsV216;
663 case ISD::SETULT:
664 case ISD::SETULE:
665 case ISD::SETUGT:
666 case ISD::SETUGE: return !IsV216;
667 default: return false;
668 }
669}
670
671static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
672 EVT Ty = N->getValueType(0);
673
674 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
675 return SDValue();
676
677 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
678 return SDValue();
679
Andrew Trickef9de2a2013-05-25 02:42:55 +0000680 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000681 N->getOperand(1), N->getOperand(2));
682}
683
684static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
685 EVT Ty = N->getValueType(0);
686
Daniel Sanders3ce56622013-09-24 12:18:31 +0000687 if (Ty.is128BitVector() && Ty.isInteger()) {
688 // Try the following combines:
689 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
690 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
691 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
692 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
693 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
694 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
695 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
696 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
697 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
698 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
699 // legalizer.
700 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000701
Daniel Sanders3ce56622013-09-24 12:18:31 +0000702 if (Op0->getOpcode() != ISD::SETCC)
703 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000704
Daniel Sanders3ce56622013-09-24 12:18:31 +0000705 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
706 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000707
Daniel Sanders3ce56622013-09-24 12:18:31 +0000708 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
709 Signed = true;
710 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
711 Signed = false;
712 else
713 return SDValue();
714
715 SDValue Op1 = N->getOperand(1);
716 SDValue Op2 = N->getOperand(2);
717 SDValue Op0Op0 = Op0->getOperand(0);
718 SDValue Op0Op1 = Op0->getOperand(1);
719
720 if (Op1 == Op0Op0 && Op2 == Op0Op1)
721 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
722 Ty, Op1, Op2);
723 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
724 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
725 Ty, Op1, Op2);
726 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
727 SDValue SetCC = N->getOperand(0);
728
729 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
730 return SDValue();
731
732 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
733 SetCC.getOperand(0), SetCC.getOperand(1),
734 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
735 }
736
737 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000738}
739
Daniel Sandersf7456c72013-09-23 13:22:24 +0000740static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
741 const MipsSubtarget *Subtarget) {
742 EVT Ty = N->getValueType(0);
743
744 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
745 // Try the following combines:
746 // (xor (or $a, $b), (build_vector allones))
747 // (xor (or $a, $b), (bitcast (build_vector allones)))
748 SDValue Op0 = N->getOperand(0);
749 SDValue Op1 = N->getOperand(1);
750 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000751
752 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
753 NotOp = Op1;
754 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
755 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000756 else
757 return SDValue();
758
759 if (NotOp->getOpcode() == ISD::OR)
760 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
761 NotOp->getOperand(1));
762 }
763
764 return SDValue();
765}
766
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000767SDValue
768MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
769 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000770 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000771
772 switch (N->getOpcode()) {
773 case ISD::ADDE:
774 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000775 case ISD::AND:
776 Val = performANDCombine(N, DAG, DCI, Subtarget);
777 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000778 case ISD::SUBE:
779 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000780 case ISD::MUL:
781 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000782 case ISD::SHL:
783 return performSHLCombine(N, DAG, DCI, Subtarget);
784 case ISD::SRA:
785 return performSRACombine(N, DAG, DCI, Subtarget);
786 case ISD::SRL:
787 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000788 case ISD::VSELECT:
789 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000790 case ISD::XOR:
791 Val = performXORCombine(N, DAG, Subtarget);
792 break;
793 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000794 Val = performSETCCCombine(N, DAG);
795 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000796 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000797
798 if (Val.getNode())
799 return Val;
800
801 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000802}
803
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000804MachineBasicBlock *
805MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
806 MachineBasicBlock *BB) const {
807 switch (MI->getOpcode()) {
808 default:
809 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
810 case Mips::BPOSGE32_PSEUDO:
811 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +0000812 case Mips::SNZ_B_PSEUDO:
813 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
814 case Mips::SNZ_H_PSEUDO:
815 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
816 case Mips::SNZ_W_PSEUDO:
817 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
818 case Mips::SNZ_D_PSEUDO:
819 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
820 case Mips::SNZ_V_PSEUDO:
821 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
822 case Mips::SZ_B_PSEUDO:
823 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
824 case Mips::SZ_H_PSEUDO:
825 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
826 case Mips::SZ_W_PSEUDO:
827 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
828 case Mips::SZ_D_PSEUDO:
829 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
830 case Mips::SZ_V_PSEUDO:
831 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +0000832 case Mips::COPY_FW_PSEUDO:
833 return emitCOPY_FW(MI, BB);
834 case Mips::COPY_FD_PSEUDO:
835 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +0000836 case Mips::INSERT_FW_PSEUDO:
837 return emitINSERT_FW(MI, BB);
838 case Mips::INSERT_FD_PSEUDO:
839 return emitINSERT_FD(MI, BB);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000840 case Mips::FILL_FW_PSEUDO:
841 return emitFILL_FW(MI, BB);
842 case Mips::FILL_FD_PSEUDO:
843 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +0000844 case Mips::FEXP2_W_1_PSEUDO:
845 return emitFEXP2_W_1(MI, BB);
846 case Mips::FEXP2_D_1_PSEUDO:
847 return emitFEXP2_D_1(MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000848 }
849}
850
851bool MipsSETargetLowering::
852isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
853 unsigned NextStackOffset,
854 const MipsFunctionInfo& FI) const {
855 if (!EnableMipsTailCalls)
856 return false;
857
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000858 // Return false if either the callee or caller has a byval argument.
859 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
860 return false;
861
862 // Return true if the callee's argument area is no larger than the
863 // caller's.
864 return NextStackOffset <= FI.getIncomingArgSize();
865}
866
867void MipsSETargetLowering::
868getOpndList(SmallVectorImpl<SDValue> &Ops,
869 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
870 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
871 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
872 // T9 should contain the address of the callee function if
873 // -reloction-model=pic or it is an indirect call.
874 if (IsPICCall || !GlobalOrExternal) {
875 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
876 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
877 } else
878 Ops.push_back(Callee);
879
880 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
881 InternalLinkage, CLI, Callee, Chain);
882}
883
Akira Hatanaka63791212013-09-07 00:52:30 +0000884SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
885 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
886
887 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
888 return MipsTargetLowering::lowerLOAD(Op, DAG);
889
890 // Replace a double precision load with two i32 loads and a buildpair64.
891 SDLoc DL(Op);
892 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
893 EVT PtrVT = Ptr.getValueType();
894
895 // i32 load from lower address.
896 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
897 MachinePointerInfo(), Nd.isVolatile(),
898 Nd.isNonTemporal(), Nd.isInvariant(),
899 Nd.getAlignment());
900
901 // i32 load from higher address.
902 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
903 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
904 MachinePointerInfo(), Nd.isVolatile(),
905 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +0000906 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka63791212013-09-07 00:52:30 +0000907
908 if (!Subtarget->isLittle())
909 std::swap(Lo, Hi);
910
911 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
912 SDValue Ops[2] = {BP, Hi.getValue(1)};
913 return DAG.getMergeValues(Ops, 2, DL);
914}
915
916SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
917 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
918
919 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
920 return MipsTargetLowering::lowerSTORE(Op, DAG);
921
922 // Replace a double precision store with two extractelement64s and i32 stores.
923 SDLoc DL(Op);
924 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
925 EVT PtrVT = Ptr.getValueType();
926 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
927 Val, DAG.getConstant(0, MVT::i32));
928 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
929 Val, DAG.getConstant(1, MVT::i32));
930
931 if (!Subtarget->isLittle())
932 std::swap(Lo, Hi);
933
934 // i32 store to lower address.
935 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
936 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
937 Nd.getTBAAInfo());
938
939 // i32 store to higher address.
940 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
941 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +0000942 Nd.isVolatile(), Nd.isNonTemporal(),
943 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +0000944}
945
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000946SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
947 bool HasLo, bool HasHi,
948 SelectionDAG &DAG) const {
949 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000950 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000951 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
952 Op.getOperand(0), Op.getOperand(1));
953 SDValue Lo, Hi;
954
955 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000956 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000957 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000958 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000959
960 if (!HasLo || !HasHi)
961 return HasLo ? Lo : Hi;
962
963 SDValue Vals[] = { Lo, Hi };
964 return DAG.getMergeValues(Vals, 2, DL);
965}
966
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000967
Andrew Trickef9de2a2013-05-25 02:42:55 +0000968static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000969 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
970 DAG.getConstant(0, MVT::i32));
971 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
972 DAG.getConstant(1, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000973 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000974}
975
Andrew Trickef9de2a2013-05-25 02:42:55 +0000976static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000977 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
978 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000979 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
980}
981
982// This function expands mips intrinsic nodes which have 64-bit input operands
983// or output values.
984//
985// out64 = intrinsic-node in64
986// =>
987// lo = copy (extract-element (in64, 0))
988// hi = copy (extract-element (in64, 1))
989// mips-specific-node
990// v0 = copy lo
991// v1 = copy hi
992// out64 = merge-values (v0, v1)
993//
994static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000995 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000996 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
997 SmallVector<SDValue, 3> Ops;
998 unsigned OpNo = 0;
999
1000 // See if Op has a chain input.
1001 if (HasChainIn)
1002 Ops.push_back(Op->getOperand(OpNo++));
1003
1004 // The next operand is the intrinsic opcode.
1005 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1006
1007 // See if the next operand has type i64.
1008 SDValue Opnd = Op->getOperand(++OpNo), In64;
1009
1010 if (Opnd.getValueType() == MVT::i64)
1011 In64 = initAccumulator(Opnd, DL, DAG);
1012 else
1013 Ops.push_back(Opnd);
1014
1015 // Push the remaining operands.
1016 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1017 Ops.push_back(Op->getOperand(OpNo));
1018
1019 // Add In64 to the end of the list.
1020 if (In64.getNode())
1021 Ops.push_back(In64);
1022
1023 // Scan output.
1024 SmallVector<EVT, 2> ResTys;
1025
1026 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1027 I != E; ++I)
1028 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1029
1030 // Create node.
1031 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1032 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1033
1034 if (!HasChainIn)
1035 return Out;
1036
1037 assert(Val->getValueType(1) == MVT::Other);
1038 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1039 return DAG.getMergeValues(Vals, 2, DL);
1040}
1041
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001042// Lower an MSA copy intrinsic into the specified SelectionDAG node
1043static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1044 SDLoc DL(Op);
1045 SDValue Vec = Op->getOperand(1);
1046 SDValue Idx = Op->getOperand(2);
1047 EVT ResTy = Op->getValueType(0);
1048 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1049
1050 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1051 DAG.getValueType(EltTy));
1052
1053 return Result;
1054}
1055
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001056static SDValue
1057lowerMSASplatImm(SDLoc DL, EVT ResTy, SDValue ImmOp, SelectionDAG &DAG) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001058 EVT ViaVecTy = ResTy;
1059 SmallVector<SDValue, 16> Ops;
1060 SDValue ImmHiOp;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001061
Daniel Sandersf49dd822013-09-24 13:33:07 +00001062 if (ViaVecTy == MVT::v2i64) {
1063 ImmHiOp = DAG.getNode(ISD::SRA, DL, MVT::i32, ImmOp,
1064 DAG.getConstant(31, MVT::i32));
1065 for (unsigned i = 0; i < ViaVecTy.getVectorNumElements(); ++i) {
1066 Ops.push_back(ImmHiOp);
1067 Ops.push_back(ImmOp);
1068 }
1069 ViaVecTy = MVT::v4i32;
1070 } else {
1071 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1072 Ops.push_back(ImmOp);
1073 }
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001074
Daniel Sandersf49dd822013-09-24 13:33:07 +00001075 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, &Ops[0],
1076 Ops.size());
1077
1078 if (ResTy != ViaVecTy)
1079 Result = DAG.getNode(ISD::BITCAST, DL, ResTy, Result);
1080
1081 return Result;
1082}
1083
1084static SDValue
1085lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001086 return lowerMSASplatImm(SDLoc(Op), Op->getValueType(0),
1087 Op->getOperand(ImmOp), DAG);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001088}
1089
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001090SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1091 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001092 SDLoc DL(Op);
1093
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001094 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1095 default:
1096 return SDValue();
1097 case Intrinsic::mips_shilo:
1098 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1099 case Intrinsic::mips_dpau_h_qbl:
1100 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1101 case Intrinsic::mips_dpau_h_qbr:
1102 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1103 case Intrinsic::mips_dpsu_h_qbl:
1104 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1105 case Intrinsic::mips_dpsu_h_qbr:
1106 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1107 case Intrinsic::mips_dpa_w_ph:
1108 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1109 case Intrinsic::mips_dps_w_ph:
1110 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1111 case Intrinsic::mips_dpax_w_ph:
1112 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1113 case Intrinsic::mips_dpsx_w_ph:
1114 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1115 case Intrinsic::mips_mulsa_w_ph:
1116 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1117 case Intrinsic::mips_mult:
1118 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1119 case Intrinsic::mips_multu:
1120 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1121 case Intrinsic::mips_madd:
1122 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1123 case Intrinsic::mips_maddu:
1124 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1125 case Intrinsic::mips_msub:
1126 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1127 case Intrinsic::mips_msubu:
1128 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001129 case Intrinsic::mips_addv_b:
1130 case Intrinsic::mips_addv_h:
1131 case Intrinsic::mips_addv_w:
1132 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001133 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1134 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001135 case Intrinsic::mips_addvi_b:
1136 case Intrinsic::mips_addvi_h:
1137 case Intrinsic::mips_addvi_w:
1138 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001139 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1140 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001141 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001142 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1143 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001144 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001145 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1146 lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersce09d072013-08-28 12:14:50 +00001147 case Intrinsic::mips_bnz_b:
1148 case Intrinsic::mips_bnz_h:
1149 case Intrinsic::mips_bnz_w:
1150 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001151 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1152 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001153 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001154 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1155 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001156 case Intrinsic::mips_bsel_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001157 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sanderse1d24352013-09-24 12:04:44 +00001158 Op->getOperand(1), Op->getOperand(2),
1159 Op->getOperand(3));
1160 case Intrinsic::mips_bseli_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001161 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sanderse1d24352013-09-24 12:04:44 +00001162 Op->getOperand(1), Op->getOperand(2),
1163 lowerMSASplatImm(Op, 3, DAG));
Daniel Sandersce09d072013-08-28 12:14:50 +00001164 case Intrinsic::mips_bz_b:
1165 case Intrinsic::mips_bz_h:
1166 case Intrinsic::mips_bz_w:
1167 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001168 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1169 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001170 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001171 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1172 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001173 case Intrinsic::mips_ceq_b:
1174 case Intrinsic::mips_ceq_h:
1175 case Intrinsic::mips_ceq_w:
1176 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001177 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001178 Op->getOperand(2), ISD::SETEQ);
1179 case Intrinsic::mips_ceqi_b:
1180 case Intrinsic::mips_ceqi_h:
1181 case Intrinsic::mips_ceqi_w:
1182 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001183 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001184 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1185 case Intrinsic::mips_cle_s_b:
1186 case Intrinsic::mips_cle_s_h:
1187 case Intrinsic::mips_cle_s_w:
1188 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001189 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001190 Op->getOperand(2), ISD::SETLE);
1191 case Intrinsic::mips_clei_s_b:
1192 case Intrinsic::mips_clei_s_h:
1193 case Intrinsic::mips_clei_s_w:
1194 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001195 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001196 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1197 case Intrinsic::mips_cle_u_b:
1198 case Intrinsic::mips_cle_u_h:
1199 case Intrinsic::mips_cle_u_w:
1200 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001201 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001202 Op->getOperand(2), ISD::SETULE);
1203 case Intrinsic::mips_clei_u_b:
1204 case Intrinsic::mips_clei_u_h:
1205 case Intrinsic::mips_clei_u_w:
1206 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001207 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001208 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1209 case Intrinsic::mips_clt_s_b:
1210 case Intrinsic::mips_clt_s_h:
1211 case Intrinsic::mips_clt_s_w:
1212 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001213 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001214 Op->getOperand(2), ISD::SETLT);
1215 case Intrinsic::mips_clti_s_b:
1216 case Intrinsic::mips_clti_s_h:
1217 case Intrinsic::mips_clti_s_w:
1218 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001219 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001220 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1221 case Intrinsic::mips_clt_u_b:
1222 case Intrinsic::mips_clt_u_h:
1223 case Intrinsic::mips_clt_u_w:
1224 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001225 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001226 Op->getOperand(2), ISD::SETULT);
1227 case Intrinsic::mips_clti_u_b:
1228 case Intrinsic::mips_clti_u_h:
1229 case Intrinsic::mips_clti_u_w:
1230 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001231 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001232 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001233 case Intrinsic::mips_copy_s_b:
1234 case Intrinsic::mips_copy_s_h:
1235 case Intrinsic::mips_copy_s_w:
1236 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001237 case Intrinsic::mips_copy_s_d:
1238 // Don't lower directly into VEXTRACT_SEXT_ELT since i64 might be illegal.
1239 // Instead lower to the generic EXTRACT_VECTOR_ELT node and let the type
1240 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1241 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1242 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001243 case Intrinsic::mips_copy_u_b:
1244 case Intrinsic::mips_copy_u_h:
1245 case Intrinsic::mips_copy_u_w:
1246 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001247 case Intrinsic::mips_copy_u_d:
1248 // Don't lower directly into VEXTRACT_ZEXT_ELT since i64 might be illegal.
1249 // Instead lower to the generic EXTRACT_VECTOR_ELT node and let the type
1250 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1251 //
1252 // Note: When i64 is illegal, this results in copy_s.w instructions instead
1253 // of copy_u.w instructions. This makes no difference to the behaviour
1254 // since i64 is only illegal when the register file is 32-bit.
1255 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1256 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001257 case Intrinsic::mips_div_s_b:
1258 case Intrinsic::mips_div_s_h:
1259 case Intrinsic::mips_div_s_w:
1260 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001261 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1262 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001263 case Intrinsic::mips_div_u_b:
1264 case Intrinsic::mips_div_u_h:
1265 case Intrinsic::mips_div_u_w:
1266 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001267 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1268 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001269 case Intrinsic::mips_fadd_w:
1270 case Intrinsic::mips_fadd_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001271 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1272 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001273 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1274 case Intrinsic::mips_fceq_w:
1275 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001276 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001277 Op->getOperand(2), ISD::SETOEQ);
1278 case Intrinsic::mips_fcle_w:
1279 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001280 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001281 Op->getOperand(2), ISD::SETOLE);
1282 case Intrinsic::mips_fclt_w:
1283 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001284 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001285 Op->getOperand(2), ISD::SETOLT);
1286 case Intrinsic::mips_fcne_w:
1287 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001288 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001289 Op->getOperand(2), ISD::SETONE);
1290 case Intrinsic::mips_fcor_w:
1291 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001292 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001293 Op->getOperand(2), ISD::SETO);
1294 case Intrinsic::mips_fcueq_w:
1295 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001296 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001297 Op->getOperand(2), ISD::SETUEQ);
1298 case Intrinsic::mips_fcule_w:
1299 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001300 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001301 Op->getOperand(2), ISD::SETULE);
1302 case Intrinsic::mips_fcult_w:
1303 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001304 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001305 Op->getOperand(2), ISD::SETULT);
1306 case Intrinsic::mips_fcun_w:
1307 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001308 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001309 Op->getOperand(2), ISD::SETUO);
1310 case Intrinsic::mips_fcune_w:
1311 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001312 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001313 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001314 case Intrinsic::mips_fdiv_w:
1315 case Intrinsic::mips_fdiv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001316 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1317 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001318 case Intrinsic::mips_ffint_u_w:
1319 case Intrinsic::mips_ffint_u_d:
1320 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1321 Op->getOperand(1));
1322 case Intrinsic::mips_ffint_s_w:
1323 case Intrinsic::mips_ffint_s_d:
1324 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1325 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001326 case Intrinsic::mips_fill_b:
1327 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001328 case Intrinsic::mips_fill_w:
1329 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001330 SmallVector<SDValue, 16> Ops;
1331 EVT ResTy = Op->getValueType(0);
1332
1333 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1334 Ops.push_back(Op->getOperand(1));
1335
Daniel Sandersc72593e2013-09-27 13:20:41 +00001336 // If ResTy is v2i64 then the type legalizer will break this node down into
1337 // an equivalent v4i32.
1338 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, &Ops[0], Ops.size());
Daniel Sandersf49dd822013-09-24 13:33:07 +00001339 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001340 case Intrinsic::mips_fexp2_w:
1341 case Intrinsic::mips_fexp2_d: {
1342 EVT ResTy = Op->getValueType(0);
1343 return DAG.getNode(
1344 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1345 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1346 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001347 case Intrinsic::mips_flog2_w:
1348 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001349 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001350 case Intrinsic::mips_fmadd_w:
1351 case Intrinsic::mips_fmadd_d:
1352 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1353 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001354 case Intrinsic::mips_fmul_w:
1355 case Intrinsic::mips_fmul_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001356 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1357 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001358 case Intrinsic::mips_fmsub_w:
1359 case Intrinsic::mips_fmsub_d: {
1360 EVT ResTy = Op->getValueType(0);
1361 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1362 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1363 Op->getOperand(2), Op->getOperand(3)));
1364 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001365 case Intrinsic::mips_frint_w:
1366 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001367 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001368 case Intrinsic::mips_fsqrt_w:
1369 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001370 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001371 case Intrinsic::mips_fsub_w:
1372 case Intrinsic::mips_fsub_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001373 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1374 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001375 case Intrinsic::mips_ftrunc_u_w:
1376 case Intrinsic::mips_ftrunc_u_d:
1377 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1378 Op->getOperand(1));
1379 case Intrinsic::mips_ftrunc_s_w:
1380 case Intrinsic::mips_ftrunc_s_d:
1381 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1382 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001383 case Intrinsic::mips_ilvev_b:
1384 case Intrinsic::mips_ilvev_h:
1385 case Intrinsic::mips_ilvev_w:
1386 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001387 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001388 Op->getOperand(1), Op->getOperand(2));
1389 case Intrinsic::mips_ilvl_b:
1390 case Intrinsic::mips_ilvl_h:
1391 case Intrinsic::mips_ilvl_w:
1392 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001393 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001394 Op->getOperand(1), Op->getOperand(2));
1395 case Intrinsic::mips_ilvod_b:
1396 case Intrinsic::mips_ilvod_h:
1397 case Intrinsic::mips_ilvod_w:
1398 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001399 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001400 Op->getOperand(1), Op->getOperand(2));
1401 case Intrinsic::mips_ilvr_b:
1402 case Intrinsic::mips_ilvr_h:
1403 case Intrinsic::mips_ilvr_w:
1404 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001405 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001406 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001407 case Intrinsic::mips_insert_b:
1408 case Intrinsic::mips_insert_h:
1409 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001410 case Intrinsic::mips_insert_d:
1411 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1412 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001413 case Intrinsic::mips_ldi_b:
1414 case Intrinsic::mips_ldi_h:
1415 case Intrinsic::mips_ldi_w:
1416 case Intrinsic::mips_ldi_d:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001417 return lowerMSASplatImm(Op, 1, DAG);
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001418 case Intrinsic::mips_lsa: {
1419 EVT ResTy = Op->getValueType(0);
1420 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1421 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1422 Op->getOperand(2), Op->getOperand(3)));
1423 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001424 case Intrinsic::mips_maddv_b:
1425 case Intrinsic::mips_maddv_h:
1426 case Intrinsic::mips_maddv_w:
1427 case Intrinsic::mips_maddv_d: {
1428 EVT ResTy = Op->getValueType(0);
1429 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1430 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1431 Op->getOperand(2), Op->getOperand(3)));
1432 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001433 case Intrinsic::mips_max_s_b:
1434 case Intrinsic::mips_max_s_h:
1435 case Intrinsic::mips_max_s_w:
1436 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001437 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1438 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001439 case Intrinsic::mips_max_u_b:
1440 case Intrinsic::mips_max_u_h:
1441 case Intrinsic::mips_max_u_w:
1442 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001443 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1444 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001445 case Intrinsic::mips_maxi_s_b:
1446 case Intrinsic::mips_maxi_s_h:
1447 case Intrinsic::mips_maxi_s_w:
1448 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001449 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1450 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001451 case Intrinsic::mips_maxi_u_b:
1452 case Intrinsic::mips_maxi_u_h:
1453 case Intrinsic::mips_maxi_u_w:
1454 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001455 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1456 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001457 case Intrinsic::mips_min_s_b:
1458 case Intrinsic::mips_min_s_h:
1459 case Intrinsic::mips_min_s_w:
1460 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001461 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1462 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001463 case Intrinsic::mips_min_u_b:
1464 case Intrinsic::mips_min_u_h:
1465 case Intrinsic::mips_min_u_w:
1466 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001467 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1468 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001469 case Intrinsic::mips_mini_s_b:
1470 case Intrinsic::mips_mini_s_h:
1471 case Intrinsic::mips_mini_s_w:
1472 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001473 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1474 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001475 case Intrinsic::mips_mini_u_b:
1476 case Intrinsic::mips_mini_u_h:
1477 case Intrinsic::mips_mini_u_w:
1478 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001479 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1480 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001481 case Intrinsic::mips_mod_s_b:
1482 case Intrinsic::mips_mod_s_h:
1483 case Intrinsic::mips_mod_s_w:
1484 case Intrinsic::mips_mod_s_d:
1485 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1486 Op->getOperand(2));
1487 case Intrinsic::mips_mod_u_b:
1488 case Intrinsic::mips_mod_u_h:
1489 case Intrinsic::mips_mod_u_w:
1490 case Intrinsic::mips_mod_u_d:
1491 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1492 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001493 case Intrinsic::mips_mulv_b:
1494 case Intrinsic::mips_mulv_h:
1495 case Intrinsic::mips_mulv_w:
1496 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001497 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1498 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001499 case Intrinsic::mips_msubv_b:
1500 case Intrinsic::mips_msubv_h:
1501 case Intrinsic::mips_msubv_w:
1502 case Intrinsic::mips_msubv_d: {
1503 EVT ResTy = Op->getValueType(0);
1504 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
1505 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1506 Op->getOperand(2), Op->getOperand(3)));
1507 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001508 case Intrinsic::mips_nlzc_b:
1509 case Intrinsic::mips_nlzc_h:
1510 case Intrinsic::mips_nlzc_w:
1511 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001512 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001513 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001514 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1515 Op->getOperand(1), Op->getOperand(2));
1516 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001517 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001518 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001519 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1520 Op->getOperand(1),
1521 lowerMSASplatImm(Op, 2, DAG));
1522 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001523 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001524 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001525 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
1526 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001527 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001528 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1529 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001530 case Intrinsic::mips_pckev_b:
1531 case Intrinsic::mips_pckev_h:
1532 case Intrinsic::mips_pckev_w:
1533 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001534 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001535 Op->getOperand(1), Op->getOperand(2));
1536 case Intrinsic::mips_pckod_b:
1537 case Intrinsic::mips_pckod_h:
1538 case Intrinsic::mips_pckod_w:
1539 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001540 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001541 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00001542 case Intrinsic::mips_pcnt_b:
1543 case Intrinsic::mips_pcnt_h:
1544 case Intrinsic::mips_pcnt_w:
1545 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001546 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sanders26307182013-09-24 14:20:00 +00001547 case Intrinsic::mips_shf_b:
1548 case Intrinsic::mips_shf_h:
1549 case Intrinsic::mips_shf_w:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001550 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00001551 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001552 case Intrinsic::mips_sll_b:
1553 case Intrinsic::mips_sll_h:
1554 case Intrinsic::mips_sll_w:
1555 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001556 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
1557 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001558 case Intrinsic::mips_slli_b:
1559 case Intrinsic::mips_slli_h:
1560 case Intrinsic::mips_slli_w:
1561 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001562 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
1563 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00001564 case Intrinsic::mips_splati_b:
1565 case Intrinsic::mips_splati_h:
1566 case Intrinsic::mips_splati_w:
1567 case Intrinsic::mips_splati_d:
1568 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1569 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1570 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001571 case Intrinsic::mips_sra_b:
1572 case Intrinsic::mips_sra_h:
1573 case Intrinsic::mips_sra_w:
1574 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001575 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
1576 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001577 case Intrinsic::mips_srai_b:
1578 case Intrinsic::mips_srai_h:
1579 case Intrinsic::mips_srai_w:
1580 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001581 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
1582 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001583 case Intrinsic::mips_srl_b:
1584 case Intrinsic::mips_srl_h:
1585 case Intrinsic::mips_srl_w:
1586 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001587 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
1588 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001589 case Intrinsic::mips_srli_b:
1590 case Intrinsic::mips_srli_h:
1591 case Intrinsic::mips_srli_w:
1592 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001593 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
1594 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001595 case Intrinsic::mips_subv_b:
1596 case Intrinsic::mips_subv_h:
1597 case Intrinsic::mips_subv_w:
1598 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001599 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
1600 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001601 case Intrinsic::mips_subvi_b:
1602 case Intrinsic::mips_subvi_h:
1603 case Intrinsic::mips_subvi_w:
1604 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001605 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
1606 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00001607 case Intrinsic::mips_vshf_b:
1608 case Intrinsic::mips_vshf_h:
1609 case Intrinsic::mips_vshf_w:
1610 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001611 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00001612 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001613 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001614 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
1615 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001616 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001617 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
1618 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001619 }
1620}
1621
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00001622static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1623 SDLoc DL(Op);
1624 SDValue ChainIn = Op->getOperand(0);
1625 SDValue Address = Op->getOperand(2);
1626 SDValue Offset = Op->getOperand(3);
1627 EVT ResTy = Op->getValueType(0);
1628 EVT PtrTy = Address->getValueType(0);
1629
1630 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1631
1632 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
1633 false, false, 16);
1634}
1635
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001636SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
1637 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00001638 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1639 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001640 default:
1641 return SDValue();
1642 case Intrinsic::mips_extp:
1643 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
1644 case Intrinsic::mips_extpdp:
1645 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
1646 case Intrinsic::mips_extr_w:
1647 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
1648 case Intrinsic::mips_extr_r_w:
1649 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
1650 case Intrinsic::mips_extr_rs_w:
1651 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
1652 case Intrinsic::mips_extr_s_h:
1653 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
1654 case Intrinsic::mips_mthlip:
1655 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
1656 case Intrinsic::mips_mulsaq_s_w_ph:
1657 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
1658 case Intrinsic::mips_maq_s_w_phl:
1659 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
1660 case Intrinsic::mips_maq_s_w_phr:
1661 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
1662 case Intrinsic::mips_maq_sa_w_phl:
1663 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
1664 case Intrinsic::mips_maq_sa_w_phr:
1665 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
1666 case Intrinsic::mips_dpaq_s_w_ph:
1667 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
1668 case Intrinsic::mips_dpsq_s_w_ph:
1669 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
1670 case Intrinsic::mips_dpaq_sa_l_w:
1671 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
1672 case Intrinsic::mips_dpsq_sa_l_w:
1673 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
1674 case Intrinsic::mips_dpaqx_s_w_ph:
1675 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
1676 case Intrinsic::mips_dpaqx_sa_w_ph:
1677 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
1678 case Intrinsic::mips_dpsqx_s_w_ph:
1679 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
1680 case Intrinsic::mips_dpsqx_sa_w_ph:
1681 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00001682 case Intrinsic::mips_ld_b:
1683 case Intrinsic::mips_ld_h:
1684 case Intrinsic::mips_ld_w:
1685 case Intrinsic::mips_ld_d:
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00001686 return lowerMSALoadIntr(Op, DAG, Intr);
1687 }
1688}
1689
1690static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1691 SDLoc DL(Op);
1692 SDValue ChainIn = Op->getOperand(0);
1693 SDValue Value = Op->getOperand(2);
1694 SDValue Address = Op->getOperand(3);
1695 SDValue Offset = Op->getOperand(4);
1696 EVT PtrTy = Address->getValueType(0);
1697
1698 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1699
1700 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
1701 false, 16);
1702}
1703
1704SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
1705 SelectionDAG &DAG) const {
1706 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1707 switch (Intr) {
1708 default:
1709 return SDValue();
1710 case Intrinsic::mips_st_b:
1711 case Intrinsic::mips_st_h:
1712 case Intrinsic::mips_st_w:
1713 case Intrinsic::mips_st_d:
Daniel Sandersce09d072013-08-28 12:14:50 +00001714 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001715 }
1716}
1717
Daniel Sanders7a289d02013-09-23 12:02:46 +00001718/// \brief Check if the given BuildVectorSDNode is a splat.
1719/// This method currently relies on DAG nodes being reused when equivalent,
1720/// so it's possible for this to return false even when isConstantSplat returns
1721/// true.
1722static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00001723 unsigned int nOps = N->getNumOperands();
1724 assert(nOps > 1 && "isSplat has 0 or 1 sized build vector");
1725
1726 SDValue Operand0 = N->getOperand(0);
1727
1728 for (unsigned int i = 1; i < nOps; ++i) {
1729 if (N->getOperand(i) != Operand0)
1730 return false;
1731 }
1732
1733 return true;
1734}
1735
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001736// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
1737//
1738// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
1739// choose to sign-extend but we could have equally chosen zero-extend. The
1740// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
1741// result into this node later (possibly changing it to a zero-extend in the
1742// process).
1743SDValue MipsSETargetLowering::
1744lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
1745 SDLoc DL(Op);
1746 EVT ResTy = Op->getValueType(0);
1747 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001748 EVT VecTy = Op0->getValueType(0);
1749
1750 if (!VecTy.is128BitVector())
1751 return SDValue();
1752
1753 if (ResTy.isInteger()) {
1754 SDValue Op1 = Op->getOperand(1);
1755 EVT EltTy = VecTy.getVectorElementType();
1756 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
1757 DAG.getValueType(EltTy));
1758 }
1759
1760 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001761}
1762
Daniel Sandersf49dd822013-09-24 13:33:07 +00001763static bool isConstantOrUndef(const SDValue Op) {
1764 if (Op->getOpcode() == ISD::UNDEF)
1765 return true;
1766 if (dyn_cast<ConstantSDNode>(Op))
1767 return true;
1768 if (dyn_cast<ConstantFPSDNode>(Op))
1769 return true;
1770 return false;
1771}
1772
1773static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
1774 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
1775 if (isConstantOrUndef(Op->getOperand(i)))
1776 return true;
1777 return false;
1778}
1779
Daniel Sanders7a289d02013-09-23 12:02:46 +00001780// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
1781// backend.
1782//
1783// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001784// - Constant splats are legal as-is as long as the SplatBitSize is a power of
1785// 2 less than or equal to 64 and the value fits into a signed 10-bit
1786// immediate
1787// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
1788// is a power of 2 less than or equal to 64 and the value does not fit into a
1789// signed 10-bit immediate
1790// - Non-constant splats are legal as-is.
1791// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
1792// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00001793SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
1794 SelectionDAG &DAG) const {
1795 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
1796 EVT ResTy = Op->getValueType(0);
1797 SDLoc DL(Op);
1798 APInt SplatValue, SplatUndef;
1799 unsigned SplatBitSize;
1800 bool HasAnyUndefs;
1801
1802 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
1803 return SDValue();
1804
1805 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1806 HasAnyUndefs, 8,
Daniel Sandersf49dd822013-09-24 13:33:07 +00001807 !Subtarget->isLittle()) && SplatBitSize <= 64) {
1808 // We can only cope with 8, 16, 32, or 64-bit elements
1809 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
1810 SplatBitSize != 64)
1811 return SDValue();
1812
1813 // If the value fits into a simm10 then we can use ldi.[bhwd]
1814 if (SplatValue.isSignedIntN(10))
1815 return Op;
1816
1817 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00001818
1819 switch (SplatBitSize) {
1820 default:
1821 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00001822 case 8:
1823 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00001824 break;
1825 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001826 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00001827 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00001828 case 32:
1829 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00001830 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00001831 case 64:
1832 // There's no fill.d to fall back on for 64-bit values
1833 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00001834 }
1835
Daniel Sandersf49dd822013-09-24 13:33:07 +00001836 SmallVector<SDValue, 16> Ops;
1837 SDValue Constant = DAG.getConstant(SplatValue.sextOrSelf(32), MVT::i32);
1838
1839 for (unsigned i = 0; i < ViaVecTy.getVectorNumElements(); ++i)
1840 Ops.push_back(Constant);
1841
1842 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Node), ViaVecTy,
1843 &Ops[0], Ops.size());
1844
1845 if (ViaVecTy != ResTy)
1846 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00001847
1848 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00001849 } else if (isSplatVector(Node))
1850 return Op;
1851 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00001852 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
1853 // The resulting code is the same length as the expansion, but it doesn't
1854 // use memory operations
1855 EVT ResTy = Node->getValueType(0);
1856
1857 assert(ResTy.isVector());
1858
1859 unsigned NumElts = ResTy.getVectorNumElements();
1860 SDValue Vector = DAG.getUNDEF(ResTy);
1861 for (unsigned i = 0; i < NumElts; ++i) {
1862 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
1863 Node->getOperand(i),
1864 DAG.getConstant(i, MVT::i32));
1865 }
1866 return Vector;
1867 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00001868
1869 return SDValue();
1870}
1871
Daniel Sanders26307182013-09-24 14:20:00 +00001872// Lower VECTOR_SHUFFLE into SHF (if possible).
1873//
1874// SHF splits the vector into blocks of four elements, then shuffles these
1875// elements according to a <4 x i2> constant (encoded as an integer immediate).
1876//
1877// It is therefore possible to lower into SHF when the mask takes the form:
1878// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
1879// When undef's appear they are treated as if they were whatever value is
1880// necessary in order to fit the above form.
1881//
1882// For example:
1883// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
1884// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
1885// i32 7, i32 6, i32 5, i32 4>
1886// is lowered to:
1887// (SHF_H $w0, $w1, 27)
1888// where the 27 comes from:
1889// 3 + (2 << 2) + (1 << 4) + (0 << 6)
1890static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
1891 SmallVector<int, 16> Indices,
1892 SelectionDAG &DAG) {
1893 int SHFIndices[4] = { -1, -1, -1, -1 };
1894
1895 if (Indices.size() < 4)
1896 return SDValue();
1897
1898 for (unsigned i = 0; i < 4; ++i) {
1899 for (unsigned j = i; j < Indices.size(); j += 4) {
1900 int Idx = Indices[j];
1901
1902 // Convert from vector index to 4-element subvector index
1903 // If an index refers to an element outside of the subvector then give up
1904 if (Idx != -1) {
1905 Idx -= 4 * (j / 4);
1906 if (Idx < 0 || Idx >= 4)
1907 return SDValue();
1908 }
1909
1910 // If the mask has an undef, replace it with the current index.
1911 // Note that it might still be undef if the current index is also undef
1912 if (SHFIndices[i] == -1)
1913 SHFIndices[i] = Idx;
1914
1915 // Check that non-undef values are the same as in the mask. If they
1916 // aren't then give up
1917 if (!(Idx == -1 || Idx == SHFIndices[i]))
1918 return SDValue();
1919 }
1920 }
1921
1922 // Calculate the immediate. Replace any remaining undefs with zero
1923 APInt Imm(32, 0);
1924 for (int i = 3; i >= 0; --i) {
1925 int Idx = SHFIndices[i];
1926
1927 if (Idx == -1)
1928 Idx = 0;
1929
1930 Imm <<= 2;
1931 Imm |= Idx & 0x3;
1932 }
1933
1934 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
1935 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
1936}
1937
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001938// Lower VECTOR_SHUFFLE into ILVEV (if possible).
1939//
1940// ILVEV interleaves the even elements from each vector.
1941//
1942// It is possible to lower into ILVEV when the mask takes the form:
1943// <0, n, 2, n+2, 4, n+4, ...>
1944// where n is the number of elements in the vector.
1945//
1946// When undef's appear in the mask they are treated as if they were whatever
1947// value is necessary in order to fit the above form.
1948static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
1949 SmallVector<int, 16> Indices,
1950 SelectionDAG &DAG) {
1951 assert ((Indices.size() % 2) == 0);
1952 int WsIdx = 0;
1953 int WtIdx = ResTy.getVectorNumElements();
1954
1955 for (unsigned i = 0; i < Indices.size(); i += 2) {
1956 if (Indices[i] != -1 && Indices[i] != WsIdx)
1957 return SDValue();
1958 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
1959 return SDValue();
1960 WsIdx += 2;
1961 WtIdx += 2;
1962 }
1963
1964 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
1965 Op->getOperand(1));
1966}
1967
1968// Lower VECTOR_SHUFFLE into ILVOD (if possible).
1969//
1970// ILVOD interleaves the odd elements from each vector.
1971//
1972// It is possible to lower into ILVOD when the mask takes the form:
1973// <1, n+1, 3, n+3, 5, n+5, ...>
1974// where n is the number of elements in the vector.
1975//
1976// When undef's appear in the mask they are treated as if they were whatever
1977// value is necessary in order to fit the above form.
1978static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
1979 SmallVector<int, 16> Indices,
1980 SelectionDAG &DAG) {
1981 assert ((Indices.size() % 2) == 0);
1982 int WsIdx = 1;
1983 int WtIdx = ResTy.getVectorNumElements() + 1;
1984
1985 for (unsigned i = 0; i < Indices.size(); i += 2) {
1986 if (Indices[i] != -1 && Indices[i] != WsIdx)
1987 return SDValue();
1988 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
1989 return SDValue();
1990 WsIdx += 2;
1991 WtIdx += 2;
1992 }
1993
1994 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
1995 Op->getOperand(1));
1996}
1997
1998// Lower VECTOR_SHUFFLE into ILVL (if possible).
1999//
2000// ILVL interleaves consecutive elements from the left half of each vector.
2001//
2002// It is possible to lower into ILVL when the mask takes the form:
2003// <0, n, 1, n+1, 2, n+2, ...>
2004// where n is the number of elements in the vector.
2005//
2006// When undef's appear in the mask they are treated as if they were whatever
2007// value is necessary in order to fit the above form.
2008static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2009 SmallVector<int, 16> Indices,
2010 SelectionDAG &DAG) {
2011 assert ((Indices.size() % 2) == 0);
2012 int WsIdx = 0;
2013 int WtIdx = ResTy.getVectorNumElements();
2014
2015 for (unsigned i = 0; i < Indices.size(); i += 2) {
2016 if (Indices[i] != -1 && Indices[i] != WsIdx)
2017 return SDValue();
2018 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2019 return SDValue();
2020 WsIdx ++;
2021 WtIdx ++;
2022 }
2023
2024 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2025 Op->getOperand(1));
2026}
2027
2028// Lower VECTOR_SHUFFLE into ILVR (if possible).
2029//
2030// ILVR interleaves consecutive elements from the right half of each vector.
2031//
2032// It is possible to lower into ILVR when the mask takes the form:
2033// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2034// where n is the number of elements in the vector and x is half n.
2035//
2036// When undef's appear in the mask they are treated as if they were whatever
2037// value is necessary in order to fit the above form.
2038static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2039 SmallVector<int, 16> Indices,
2040 SelectionDAG &DAG) {
2041 assert ((Indices.size() % 2) == 0);
2042 unsigned NumElts = ResTy.getVectorNumElements();
2043 int WsIdx = NumElts / 2;
2044 int WtIdx = NumElts + NumElts / 2;
2045
2046 for (unsigned i = 0; i < Indices.size(); i += 2) {
2047 if (Indices[i] != -1 && Indices[i] != WsIdx)
2048 return SDValue();
2049 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2050 return SDValue();
2051 WsIdx ++;
2052 WtIdx ++;
2053 }
2054
2055 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2056 Op->getOperand(1));
2057}
2058
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002059// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2060//
2061// PCKEV copies the even elements of each vector into the result vector.
2062//
2063// It is possible to lower into PCKEV when the mask takes the form:
2064// <0, 2, 4, ..., n, n+2, n+4, ...>
2065// where n is the number of elements in the vector.
2066//
2067// When undef's appear in the mask they are treated as if they were whatever
2068// value is necessary in order to fit the above form.
2069static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2070 SmallVector<int, 16> Indices,
2071 SelectionDAG &DAG) {
2072 assert ((Indices.size() % 2) == 0);
2073 int Idx = 0;
2074
2075 for (unsigned i = 0; i < Indices.size(); ++i) {
2076 if (Indices[i] != -1 && Indices[i] != Idx)
2077 return SDValue();
2078 Idx += 2;
2079 }
2080
2081 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2082 Op->getOperand(1));
2083}
2084
2085// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2086//
2087// PCKOD copies the odd elements of each vector into the result vector.
2088//
2089// It is possible to lower into PCKOD when the mask takes the form:
2090// <1, 3, 5, ..., n+1, n+3, n+5, ...>
2091// where n is the number of elements in the vector.
2092//
2093// When undef's appear in the mask they are treated as if they were whatever
2094// value is necessary in order to fit the above form.
2095static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2096 SmallVector<int, 16> Indices,
2097 SelectionDAG &DAG) {
2098 assert ((Indices.size() % 2) == 0);
2099 int Idx = 1;
2100
2101 for (unsigned i = 0; i < Indices.size(); ++i) {
2102 if (Indices[i] != -1 && Indices[i] != Idx)
2103 return SDValue();
2104 Idx += 2;
2105 }
2106
2107 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2108 Op->getOperand(1));
2109}
2110
Daniel Sanderse5087042013-09-24 14:02:15 +00002111// Lower VECTOR_SHUFFLE into VSHF.
2112//
2113// This mostly consists of converting the shuffle indices in Indices into a
2114// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2115// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2116// if the type is v8i16 and all the indices are less than 8 then the second
2117// operand is unused and can be replaced with anything. We choose to replace it
2118// with the used operand since this reduces the number of instructions overall.
2119static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2120 SmallVector<int, 16> Indices,
2121 SelectionDAG &DAG) {
2122 SmallVector<SDValue, 16> Ops;
2123 SDValue Op0;
2124 SDValue Op1;
2125 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2126 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2127 bool Using1stVec = false;
2128 bool Using2ndVec = false;
2129 SDLoc DL(Op);
2130 int ResTyNumElts = ResTy.getVectorNumElements();
2131
2132 for (int i = 0; i < ResTyNumElts; ++i) {
2133 // Idx == -1 means UNDEF
2134 int Idx = Indices[i];
2135
2136 if (0 <= Idx && Idx < ResTyNumElts)
2137 Using1stVec = true;
2138 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2139 Using2ndVec = true;
2140 }
2141
2142 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2143 ++I)
2144 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2145
2146 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, &Ops[0],
2147 Ops.size());
2148
2149 if (Using1stVec && Using2ndVec) {
2150 Op0 = Op->getOperand(0);
2151 Op1 = Op->getOperand(1);
2152 } else if (Using1stVec)
2153 Op0 = Op1 = Op->getOperand(0);
2154 else if (Using2ndVec)
2155 Op0 = Op1 = Op->getOperand(1);
2156 else
2157 llvm_unreachable("shuffle vector mask references neither vector operand?");
2158
2159 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op0, Op1);
2160}
2161
2162// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2163// indices in the shuffle.
2164SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2165 SelectionDAG &DAG) const {
2166 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2167 EVT ResTy = Op->getValueType(0);
2168
2169 if (!ResTy.is128BitVector())
2170 return SDValue();
2171
2172 int ResTyNumElts = ResTy.getVectorNumElements();
2173 SmallVector<int, 16> Indices;
2174
2175 for (int i = 0; i < ResTyNumElts; ++i)
2176 Indices.push_back(Node->getMaskElt(i));
2177
Daniel Sanders26307182013-09-24 14:20:00 +00002178 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2179 if (Result.getNode())
2180 return Result;
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002181 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2182 if (Result.getNode())
2183 return Result;
2184 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2185 if (Result.getNode())
2186 return Result;
2187 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2188 if (Result.getNode())
2189 return Result;
2190 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2191 if (Result.getNode())
2192 return Result;
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002193 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2194 if (Result.getNode())
2195 return Result;
2196 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2197 if (Result.getNode())
2198 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002199 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2200}
2201
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002202MachineBasicBlock * MipsSETargetLowering::
2203emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2204 // $bb:
2205 // bposge32_pseudo $vr0
2206 // =>
2207 // $bb:
2208 // bposge32 $tbb
2209 // $fbb:
2210 // li $vr2, 0
2211 // b $sink
2212 // $tbb:
2213 // li $vr1, 1
2214 // $sink:
2215 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2216
2217 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2218 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002219 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002220 DebugLoc DL = MI->getDebugLoc();
2221 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2222 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2223 MachineFunction *F = BB->getParent();
2224 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2225 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2226 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2227 F->insert(It, FBB);
2228 F->insert(It, TBB);
2229 F->insert(It, Sink);
2230
2231 // Transfer the remainder of BB and its successor edges to Sink.
2232 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2233 BB->end());
2234 Sink->transferSuccessorsAndUpdatePHIs(BB);
2235
2236 // Add successors.
2237 BB->addSuccessor(FBB);
2238 BB->addSuccessor(TBB);
2239 FBB->addSuccessor(Sink);
2240 TBB->addSuccessor(Sink);
2241
2242 // Insert the real bposge32 instruction to $BB.
2243 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2244
2245 // Fill $FBB.
2246 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2247 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2248 .addReg(Mips::ZERO).addImm(0);
2249 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2250
2251 // Fill $TBB.
2252 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2253 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2254 .addReg(Mips::ZERO).addImm(1);
2255
2256 // Insert phi function to $Sink.
2257 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2258 MI->getOperand(0).getReg())
2259 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2260
2261 MI->eraseFromParent(); // The pseudo instruction is gone now.
2262 return Sink;
2263}
Daniel Sandersce09d072013-08-28 12:14:50 +00002264
2265MachineBasicBlock * MipsSETargetLowering::
2266emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2267 unsigned BranchOp) const{
2268 // $bb:
2269 // vany_nonzero $rd, $ws
2270 // =>
2271 // $bb:
2272 // bnz.b $ws, $tbb
2273 // b $fbb
2274 // $fbb:
2275 // li $rd1, 0
2276 // b $sink
2277 // $tbb:
2278 // li $rd2, 1
2279 // $sink:
2280 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2281
2282 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2283 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2284 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2285 DebugLoc DL = MI->getDebugLoc();
2286 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2287 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2288 MachineFunction *F = BB->getParent();
2289 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2290 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2291 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2292 F->insert(It, FBB);
2293 F->insert(It, TBB);
2294 F->insert(It, Sink);
2295
2296 // Transfer the remainder of BB and its successor edges to Sink.
2297 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2298 BB->end());
2299 Sink->transferSuccessorsAndUpdatePHIs(BB);
2300
2301 // Add successors.
2302 BB->addSuccessor(FBB);
2303 BB->addSuccessor(TBB);
2304 FBB->addSuccessor(Sink);
2305 TBB->addSuccessor(Sink);
2306
2307 // Insert the real bnz.b instruction to $BB.
2308 BuildMI(BB, DL, TII->get(BranchOp))
2309 .addReg(MI->getOperand(1).getReg())
2310 .addMBB(TBB);
2311
2312 // Fill $FBB.
2313 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2314 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2315 .addReg(Mips::ZERO).addImm(0);
2316 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2317
2318 // Fill $TBB.
2319 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2320 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2321 .addReg(Mips::ZERO).addImm(1);
2322
2323 // Insert phi function to $Sink.
2324 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2325 MI->getOperand(0).getReg())
2326 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2327
2328 MI->eraseFromParent(); // The pseudo instruction is gone now.
2329 return Sink;
2330}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002331
2332// Emit the COPY_FW pseudo instruction.
2333//
2334// copy_fw_pseudo $fd, $ws, n
2335// =>
2336// copy_u_w $rt, $ws, $n
2337// mtc1 $rt, $fd
2338//
2339// When n is zero, the equivalent operation can be performed with (potentially)
2340// zero instructions due to register overlaps. This optimization is never valid
2341// for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2342MachineBasicBlock * MipsSETargetLowering::
2343emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2344 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2345 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2346 DebugLoc DL = MI->getDebugLoc();
2347 unsigned Fd = MI->getOperand(0).getReg();
2348 unsigned Ws = MI->getOperand(1).getReg();
2349 unsigned Lane = MI->getOperand(2).getImm();
2350
2351 if (Lane == 0)
2352 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2353 else {
2354 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2355
2356 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(1);
2357 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2358 }
2359
2360 MI->eraseFromParent(); // The pseudo instruction is gone now.
2361 return BB;
2362}
2363
2364// Emit the COPY_FD pseudo instruction.
2365//
2366// copy_fd_pseudo $fd, $ws, n
2367// =>
2368// splati.d $wt, $ws, $n
2369// copy $fd, $wt:sub_64
2370//
2371// When n is zero, the equivalent operation can be performed with (potentially)
2372// zero instructions due to register overlaps. This optimization is always
2373// valid because FR=1 mode which is the only supported mode in MSA.
2374MachineBasicBlock * MipsSETargetLowering::
2375emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
2376 assert(Subtarget->isFP64bit());
2377
2378 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2379 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2380 unsigned Fd = MI->getOperand(0).getReg();
2381 unsigned Ws = MI->getOperand(1).getReg();
2382 unsigned Lane = MI->getOperand(2).getImm() * 2;
2383 DebugLoc DL = MI->getDebugLoc();
2384
2385 if (Lane == 0)
2386 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2387 else {
2388 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2389
2390 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2391 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2392 }
2393
2394 MI->eraseFromParent(); // The pseudo instruction is gone now.
2395 return BB;
2396}
Daniel Sandersa5150702013-09-27 12:31:32 +00002397
2398// Emit the INSERT_FW pseudo instruction.
2399//
2400// insert_fw_pseudo $wd, $wd_in, $n, $fs
2401// =>
2402// subreg_to_reg $wt:sub_lo, $fs
2403// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002404MachineBasicBlock *
2405MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2406 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002407 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2408 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2409 DebugLoc DL = MI->getDebugLoc();
2410 unsigned Wd = MI->getOperand(0).getReg();
2411 unsigned Wd_in = MI->getOperand(1).getReg();
2412 unsigned Lane = MI->getOperand(2).getImm();
2413 unsigned Fs = MI->getOperand(3).getReg();
2414 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2415
2416 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002417 .addImm(0)
2418 .addReg(Fs)
2419 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00002420 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002421 .addReg(Wd_in)
2422 .addImm(Lane)
2423 .addReg(Wt);
Daniel Sandersa5150702013-09-27 12:31:32 +00002424
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002425 MI->eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00002426 return BB;
2427}
2428
2429// Emit the INSERT_FD pseudo instruction.
2430//
2431// insert_fd_pseudo $wd, $fs, n
2432// =>
2433// subreg_to_reg $wt:sub_64, $fs
2434// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002435MachineBasicBlock *
2436MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2437 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002438 assert(Subtarget->isFP64bit());
2439
2440 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2441 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2442 DebugLoc DL = MI->getDebugLoc();
2443 unsigned Wd = MI->getOperand(0).getReg();
2444 unsigned Wd_in = MI->getOperand(1).getReg();
2445 unsigned Lane = MI->getOperand(2).getImm();
2446 unsigned Fs = MI->getOperand(3).getReg();
2447 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2448
2449 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002450 .addImm(0)
2451 .addReg(Fs)
2452 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00002453 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002454 .addReg(Wd_in)
2455 .addImm(Lane)
2456 .addReg(Wt);
2457
2458 MI->eraseFromParent(); // The pseudo instruction is gone now.
2459 return BB;
2460}
2461
2462// Emit the FILL_FW pseudo instruction.
2463//
2464// fill_fw_pseudo $wd, $fs
2465// =>
2466// implicit_def $wt1
2467// insert_subreg $wt2:subreg_lo, $wt1, $fs
2468// splati.w $wd, $wt2[0]
2469MachineBasicBlock *
2470MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
2471 MachineBasicBlock *BB) const {
2472 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2473 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2474 DebugLoc DL = MI->getDebugLoc();
2475 unsigned Wd = MI->getOperand(0).getReg();
2476 unsigned Fs = MI->getOperand(1).getReg();
2477 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2478 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2479
2480 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2481 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2482 .addReg(Wt1)
2483 .addReg(Fs)
2484 .addImm(Mips::sub_lo);
2485 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
2486
2487 MI->eraseFromParent(); // The pseudo instruction is gone now.
2488 return BB;
2489}
2490
2491// Emit the FILL_FD pseudo instruction.
2492//
2493// fill_fd_pseudo $wd, $fs
2494// =>
2495// implicit_def $wt1
2496// insert_subreg $wt2:subreg_64, $wt1, $fs
2497// splati.d $wd, $wt2[0]
2498MachineBasicBlock *
2499MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
2500 MachineBasicBlock *BB) const {
2501 assert(Subtarget->isFP64bit());
2502
2503 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2504 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2505 DebugLoc DL = MI->getDebugLoc();
2506 unsigned Wd = MI->getOperand(0).getReg();
2507 unsigned Fs = MI->getOperand(1).getReg();
2508 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2509 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2510
2511 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2512 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2513 .addReg(Wt1)
2514 .addReg(Fs)
2515 .addImm(Mips::sub_64);
2516 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00002517
2518 MI->eraseFromParent(); // The pseudo instruction is gone now.
2519 return BB;
2520}
Daniel Sandersa9521602013-10-23 10:36:52 +00002521
2522// Emit the FEXP2_W_1 pseudo instructions.
2523//
2524// fexp2_w_1_pseudo $wd, $wt
2525// =>
2526// ldi.w $ws, 1
2527// fexp2.w $wd, $ws, $wt
2528MachineBasicBlock *
2529MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
2530 MachineBasicBlock *BB) const {
2531 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2532 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2533 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
2534 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2535 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2536 DebugLoc DL = MI->getDebugLoc();
2537
2538 // Splat 1.0 into a vector
2539 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
2540 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
2541
2542 // Emit 1.0 * fexp2(Wt)
2543 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
2544 .addReg(Ws2)
2545 .addReg(MI->getOperand(1).getReg());
2546
2547 MI->eraseFromParent(); // The pseudo instruction is gone now.
2548 return BB;
2549}
2550
2551// Emit the FEXP2_D_1 pseudo instructions.
2552//
2553// fexp2_d_1_pseudo $wd, $wt
2554// =>
2555// ldi.d $ws, 1
2556// fexp2.d $wd, $ws, $wt
2557MachineBasicBlock *
2558MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
2559 MachineBasicBlock *BB) const {
2560 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2561 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2562 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
2563 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2564 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2565 DebugLoc DL = MI->getDebugLoc();
2566
2567 // Splat 1.0 into a vector
2568 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
2569 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
2570
2571 // Emit 1.0 * fexp2(Wt)
2572 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
2573 .addReg(Ws2)
2574 .addReg(MI->getOperand(1).getReg());
2575
2576 MI->eraseFromParent(); // The pseudo instruction is gone now.
2577 return BB;
2578}