blob: 82cb7a84935202f9e53dfbefaff59407258fb328 [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
Reed Kotler1595f362013-04-09 19:46:01 +000036
37 clearRegisterClasses();
38
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000039 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000040
41 if (HasMips64)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000042 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000043
Daniel Sanders36c671e2013-09-27 09:44:59 +000044 if (Subtarget->hasDSP() || Subtarget->hasMSA()) {
45 // Expand all truncating stores and extending loads.
46 unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
47 unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
48
49 for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
50 for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
51 setTruncStoreAction((MVT::SimpleValueType)VT0,
52 (MVT::SimpleValueType)VT1, Expand);
53
54 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
55 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
56 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
57 }
58 }
59
Akira Hatanaka96ca1822013-03-13 00:54:29 +000060 if (Subtarget->hasDSP()) {
61 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
62
63 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
Akira Hatanaka654655f2013-08-14 00:53:38 +000064 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000065
66 // Expand all builtin opcodes.
67 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
68 setOperationAction(Opc, VecTys[i], Expand);
69
Akira Hatanaka2f088222013-04-13 00:55:41 +000070 setOperationAction(ISD::ADD, VecTys[i], Legal);
71 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000072 setOperationAction(ISD::LOAD, VecTys[i], Legal);
73 setOperationAction(ISD::STORE, VecTys[i], Legal);
74 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
75 }
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +000076
77 setTargetDAGCombine(ISD::SHL);
78 setTargetDAGCombine(ISD::SRA);
79 setTargetDAGCombine(ISD::SRL);
Akira Hatanaka68741cc2013-04-30 22:37:26 +000080 setTargetDAGCombine(ISD::SETCC);
81 setTargetDAGCombine(ISD::VSELECT);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000082 }
83
Akira Hatanaka2f088222013-04-13 00:55:41 +000084 if (Subtarget->hasDSPR2())
85 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
86
Jack Carter3a2c2d42013-08-13 20:54:07 +000087 if (Subtarget->hasMSA()) {
Daniel Sandersc65f58a2013-09-11 10:15:48 +000088 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
89 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
90 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
91 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
92 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
93 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
94 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
Daniel Sandersf7456c72013-09-23 13:22:24 +000095
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +000096 setTargetDAGCombine(ISD::AND);
97 setTargetDAGCombine(ISD::SRA);
Daniel Sanderse1d24352013-09-24 12:04:44 +000098 setTargetDAGCombine(ISD::VSELECT);
Daniel Sandersf7456c72013-09-23 13:22:24 +000099 setTargetDAGCombine(ISD::XOR);
Jack Carter3a2c2d42013-08-13 20:54:07 +0000100 }
101
Reed Kotlerc03807a2013-08-30 19:40:56 +0000102 if (!Subtarget->mipsSEUsesSoftFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000103 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
104
105 // When dealing with single precision only, use libcalls
106 if (!Subtarget->isSingleFloat()) {
Akira Hatanakabfb66242013-08-20 23:38:40 +0000107 if (Subtarget->isFP64bit())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000108 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
109 else
110 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
111 }
112 }
113
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000114 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
115 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
116 setOperationAction(ISD::MULHS, MVT::i32, Custom);
117 setOperationAction(ISD::MULHU, MVT::i32, Custom);
118
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000119 if (HasMips64) {
120 setOperationAction(ISD::MULHS, MVT::i64, Custom);
121 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000122 setOperationAction(ISD::MUL, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000123 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000124
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000125 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
126 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
127
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000128 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
129 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
130 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
131 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000132 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
133 setOperationAction(ISD::LOAD, MVT::i32, Custom);
134 setOperationAction(ISD::STORE, MVT::i32, Custom);
135
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000136 setTargetDAGCombine(ISD::ADDE);
137 setTargetDAGCombine(ISD::SUBE);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000138 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000139
Daniel Sandersce09d072013-08-28 12:14:50 +0000140 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000141 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
142 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
143
Akira Hatanaka63791212013-09-07 00:52:30 +0000144 if (NoDPLoadStore) {
145 setOperationAction(ISD::LOAD, MVT::f64, Custom);
146 setOperationAction(ISD::STORE, MVT::f64, Custom);
147 }
148
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000149 computeRegisterProperties();
150}
151
152const MipsTargetLowering *
153llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
154 return new MipsSETargetLowering(TM);
155}
156
Daniel Sanders7a289d02013-09-23 12:02:46 +0000157// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000158void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000159addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
160 addRegisterClass(Ty, RC);
161
162 // Expand all builtin opcodes.
163 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
164 setOperationAction(Opc, Ty, Expand);
165
166 setOperationAction(ISD::BITCAST, Ty, Legal);
167 setOperationAction(ISD::LOAD, Ty, Legal);
168 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000169 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
170 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000171 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000172
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000173 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000174 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000175 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000176 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000177 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000178 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000179 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000180 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000181 setOperationAction(ISD::SHL, Ty, Legal);
182 setOperationAction(ISD::SRA, Ty, Legal);
183 setOperationAction(ISD::SRL, Ty, Legal);
184 setOperationAction(ISD::SUB, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000185 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000186 setOperationAction(ISD::UREM, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000187 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000188 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000189 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000190
191 setOperationAction(ISD::SETCC, Ty, Legal);
192 setCondCodeAction(ISD::SETNE, Ty, Expand);
193 setCondCodeAction(ISD::SETGE, Ty, Expand);
194 setCondCodeAction(ISD::SETGT, Ty, Expand);
195 setCondCodeAction(ISD::SETUGE, Ty, Expand);
196 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000197}
198
Daniel Sanders7a289d02013-09-23 12:02:46 +0000199// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000200void MipsSETargetLowering::
201addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000202 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000203
204 // Expand all builtin opcodes.
205 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
206 setOperationAction(Opc, Ty, Expand);
207
208 setOperationAction(ISD::LOAD, Ty, Legal);
209 setOperationAction(ISD::STORE, Ty, Legal);
210 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000211 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000212 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000213
214 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000215 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000216 setOperationAction(ISD::FADD, Ty, Legal);
217 setOperationAction(ISD::FDIV, Ty, Legal);
218 setOperationAction(ISD::FLOG2, Ty, Legal);
219 setOperationAction(ISD::FMUL, Ty, Legal);
220 setOperationAction(ISD::FRINT, Ty, Legal);
221 setOperationAction(ISD::FSQRT, Ty, Legal);
222 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000223 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000224
225 setOperationAction(ISD::SETCC, Ty, Legal);
226 setCondCodeAction(ISD::SETOGE, Ty, Expand);
227 setCondCodeAction(ISD::SETOGT, Ty, Expand);
228 setCondCodeAction(ISD::SETUGE, Ty, Expand);
229 setCondCodeAction(ISD::SETUGT, Ty, Expand);
230 setCondCodeAction(ISD::SETGE, Ty, Expand);
231 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000232 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000233}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000234
235bool
236MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
237 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
238
239 switch (SVT) {
240 case MVT::i64:
241 case MVT::i32:
242 if (Fast)
243 *Fast = true;
244 return true;
245 default:
246 return false;
247 }
248}
249
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000250SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
251 SelectionDAG &DAG) const {
252 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000253 case ISD::LOAD: return lowerLOAD(Op, DAG);
254 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000255 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
256 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
257 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
258 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
259 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
260 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000261 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
262 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000263 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
264 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000265 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000266 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000267 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000268 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000269 }
270
271 return MipsTargetLowering::LowerOperation(Op, DAG);
272}
273
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000274// selectMADD -
275// Transforms a subgraph in CurDAG if the following pattern is found:
276// (addc multLo, Lo0), (adde multHi, Hi0),
277// where,
278// multHi/Lo: product of multiplication
279// Lo0: initial value of Lo register
280// Hi0: initial value of Hi register
281// Return true if pattern matching was successful.
282static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
283 // ADDENode's second operand must be a flag output of an ADDC node in order
284 // for the matching to be successful.
285 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
286
287 if (ADDCNode->getOpcode() != ISD::ADDC)
288 return false;
289
290 SDValue MultHi = ADDENode->getOperand(0);
291 SDValue MultLo = ADDCNode->getOperand(0);
292 SDNode *MultNode = MultHi.getNode();
293 unsigned MultOpc = MultHi.getOpcode();
294
295 // MultHi and MultLo must be generated by the same node,
296 if (MultLo.getNode() != MultNode)
297 return false;
298
299 // and it must be a multiplication.
300 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
301 return false;
302
303 // MultLo amd MultHi must be the first and second output of MultNode
304 // respectively.
305 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
306 return false;
307
308 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
309 // of the values of MultNode, in which case MultNode will be removed in later
310 // phases.
311 // If there exist users other than ADDENode or ADDCNode, this function returns
312 // here, which will result in MultNode being mapped to a single MULT
313 // instruction node rather than a pair of MULT and MADD instructions being
314 // produced.
315 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
316 return false;
317
Andrew Trickef9de2a2013-05-25 02:42:55 +0000318 SDLoc DL(ADDENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000319
320 // Initialize accumulator.
321 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
322 ADDCNode->getOperand(1),
323 ADDENode->getOperand(1));
324
325 // create MipsMAdd(u) node
326 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
327
328 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
329 MultNode->getOperand(0),// Factor 0
330 MultNode->getOperand(1),// Factor 1
331 ACCIn);
332
333 // replace uses of adde and addc here
334 if (!SDValue(ADDCNode, 0).use_empty()) {
335 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
336 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
337 LoIdx);
338 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
339 }
340 if (!SDValue(ADDENode, 0).use_empty()) {
341 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
342 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
343 HiIdx);
344 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
345 }
346
347 return true;
348}
349
350// selectMSUB -
351// Transforms a subgraph in CurDAG if the following pattern is found:
352// (addc Lo0, multLo), (sube Hi0, multHi),
353// where,
354// multHi/Lo: product of multiplication
355// Lo0: initial value of Lo register
356// Hi0: initial value of Hi register
357// Return true if pattern matching was successful.
358static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
359 // SUBENode's second operand must be a flag output of an SUBC node in order
360 // for the matching to be successful.
361 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
362
363 if (SUBCNode->getOpcode() != ISD::SUBC)
364 return false;
365
366 SDValue MultHi = SUBENode->getOperand(1);
367 SDValue MultLo = SUBCNode->getOperand(1);
368 SDNode *MultNode = MultHi.getNode();
369 unsigned MultOpc = MultHi.getOpcode();
370
371 // MultHi and MultLo must be generated by the same node,
372 if (MultLo.getNode() != MultNode)
373 return false;
374
375 // and it must be a multiplication.
376 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
377 return false;
378
379 // MultLo amd MultHi must be the first and second output of MultNode
380 // respectively.
381 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
382 return false;
383
384 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
385 // of the values of MultNode, in which case MultNode will be removed in later
386 // phases.
387 // If there exist users other than SUBENode or SUBCNode, this function returns
388 // here, which will result in MultNode being mapped to a single MULT
389 // instruction node rather than a pair of MULT and MSUB instructions being
390 // produced.
391 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
392 return false;
393
Andrew Trickef9de2a2013-05-25 02:42:55 +0000394 SDLoc DL(SUBENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000395
396 // Initialize accumulator.
397 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
398 SUBCNode->getOperand(0),
399 SUBENode->getOperand(0));
400
401 // create MipsSub(u) node
402 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
403
404 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
405 MultNode->getOperand(0),// Factor 0
406 MultNode->getOperand(1),// Factor 1
407 ACCIn);
408
409 // replace uses of sube and subc here
410 if (!SDValue(SUBCNode, 0).use_empty()) {
411 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
412 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
413 LoIdx);
414 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
415 }
416 if (!SDValue(SUBENode, 0).use_empty()) {
417 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
418 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
419 HiIdx);
420 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
421 }
422
423 return true;
424}
425
426static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
427 TargetLowering::DAGCombinerInfo &DCI,
428 const MipsSubtarget *Subtarget) {
429 if (DCI.isBeforeLegalize())
430 return SDValue();
431
432 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
433 selectMADD(N, &DAG))
434 return SDValue(N, 0);
435
436 return SDValue();
437}
438
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000439// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
440//
441// Performs the following transformations:
442// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
443// sign/zero-extension is completely overwritten by the new one performed by
444// the ISD::AND.
445// - Removes redundant zero extensions performed by an ISD::AND.
446static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
447 TargetLowering::DAGCombinerInfo &DCI,
448 const MipsSubtarget *Subtarget) {
449 if (!Subtarget->hasMSA())
450 return SDValue();
451
452 SDValue Op0 = N->getOperand(0);
453 SDValue Op1 = N->getOperand(1);
454 unsigned Op0Opcode = Op0->getOpcode();
455
456 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
457 // where $d + 1 == 2^n and n == 32
458 // or $d + 1 == 2^n and n <= 32 and ZExt
459 // -> (MipsVExtractZExt $a, $b, $c)
460 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
461 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
462 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
463
464 if (!Mask)
465 return SDValue();
466
467 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
468
469 if (Log2IfPositive <= 0)
470 return SDValue(); // Mask+1 is not a power of 2
471
472 SDValue Op0Op2 = Op0->getOperand(2);
473 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
474 unsigned ExtendTySize = ExtendTy.getSizeInBits();
475 unsigned Log2 = Log2IfPositive;
476
477 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
478 Log2 == ExtendTySize) {
479 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
480 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
481 Op0->getVTList(), Ops, Op0->getNumOperands());
482 return Op0;
483 }
484 }
485
486 return SDValue();
487}
488
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000489static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
490 TargetLowering::DAGCombinerInfo &DCI,
491 const MipsSubtarget *Subtarget) {
492 if (DCI.isBeforeLegalize())
493 return SDValue();
494
495 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
496 selectMSUB(N, &DAG))
497 return SDValue(N, 0);
498
499 return SDValue();
500}
501
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000502static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
503 EVT ShiftTy, SelectionDAG &DAG) {
504 // Clear the upper (64 - VT.sizeInBits) bits.
505 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
506
507 // Return 0.
508 if (C == 0)
509 return DAG.getConstant(0, VT);
510
511 // Return x.
512 if (C == 1)
513 return X;
514
515 // If c is power of 2, return (shl x, log2(c)).
516 if (isPowerOf2_64(C))
517 return DAG.getNode(ISD::SHL, DL, VT, X,
518 DAG.getConstant(Log2_64(C), ShiftTy));
519
520 unsigned Log2Ceil = Log2_64_Ceil(C);
521 uint64_t Floor = 1LL << Log2_64(C);
522 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
523
524 // If |c - floor_c| <= |c - ceil_c|,
525 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
526 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
527 if (C - Floor <= Ceil - C) {
528 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
529 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
530 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
531 }
532
533 // If |c - floor_c| > |c - ceil_c|,
534 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
535 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
536 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
537 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
538}
539
540static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
541 const TargetLowering::DAGCombinerInfo &DCI,
542 const MipsSETargetLowering *TL) {
543 EVT VT = N->getValueType(0);
544
545 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
546 if (!VT.isVector())
547 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
548 VT, TL->getScalarShiftAmountTy(VT), DAG);
549
550 return SDValue(N, 0);
551}
552
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000553static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
554 SelectionDAG &DAG,
555 const MipsSubtarget *Subtarget) {
556 // See if this is a vector splat immediate node.
557 APInt SplatValue, SplatUndef;
558 unsigned SplatBitSize;
559 bool HasAnyUndefs;
560 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
561 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
562
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000563 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000564 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000565 EltSize, !Subtarget->isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000566 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000567 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000568 return SDValue();
569
Andrew Trickef9de2a2013-05-25 02:42:55 +0000570 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000571 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
572}
573
574static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
575 TargetLowering::DAGCombinerInfo &DCI,
576 const MipsSubtarget *Subtarget) {
577 EVT Ty = N->getValueType(0);
578
579 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
580 return SDValue();
581
582 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
583}
584
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000585// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
586// constant splats into MipsISD::SHRA_DSP for DSPr2.
587//
588// Performs the following transformations:
589// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
590// sign/zero-extension is completely overwritten by the new one performed by
591// the ISD::SRA and ISD::SHL nodes.
592// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
593// sequence.
594//
595// See performDSPShiftCombine for more information about the transformation
596// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000597static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
598 TargetLowering::DAGCombinerInfo &DCI,
599 const MipsSubtarget *Subtarget) {
600 EVT Ty = N->getValueType(0);
601
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000602 if (Subtarget->hasMSA()) {
603 SDValue Op0 = N->getOperand(0);
604 SDValue Op1 = N->getOperand(1);
605
606 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
607 // where $d + sizeof($c) == 32
608 // or $d + sizeof($c) <= 32 and SExt
609 // -> (MipsVExtractSExt $a, $b, $c)
610 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
611 SDValue Op0Op0 = Op0->getOperand(0);
612 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
613
614 if (!ShAmount)
615 return SDValue();
616
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000617 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
618 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
619 return SDValue();
620
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000621 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
622 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
623
624 if (TotalBits == 32 ||
625 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
626 TotalBits <= 32)) {
627 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
628 Op0Op0->getOperand(2) };
629 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
630 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
631 return Op0Op0;
632 }
633 }
634 }
635
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000636 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
637 return SDValue();
638
639 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
640}
641
642
643static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
644 TargetLowering::DAGCombinerInfo &DCI,
645 const MipsSubtarget *Subtarget) {
646 EVT Ty = N->getValueType(0);
647
648 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
649 return SDValue();
650
651 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
652}
653
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000654static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
655 bool IsV216 = (Ty == MVT::v2i16);
656
657 switch (CC) {
658 case ISD::SETEQ:
659 case ISD::SETNE: return true;
660 case ISD::SETLT:
661 case ISD::SETLE:
662 case ISD::SETGT:
663 case ISD::SETGE: return IsV216;
664 case ISD::SETULT:
665 case ISD::SETULE:
666 case ISD::SETUGT:
667 case ISD::SETUGE: return !IsV216;
668 default: return false;
669 }
670}
671
672static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
673 EVT Ty = N->getValueType(0);
674
675 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
676 return SDValue();
677
678 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
679 return SDValue();
680
Andrew Trickef9de2a2013-05-25 02:42:55 +0000681 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000682 N->getOperand(1), N->getOperand(2));
683}
684
685static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
686 EVT Ty = N->getValueType(0);
687
Daniel Sanders3ce56622013-09-24 12:18:31 +0000688 if (Ty.is128BitVector() && Ty.isInteger()) {
689 // Try the following combines:
690 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
691 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
692 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
693 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
694 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
695 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
696 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
697 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
698 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
699 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
700 // legalizer.
701 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000702
Daniel Sanders3ce56622013-09-24 12:18:31 +0000703 if (Op0->getOpcode() != ISD::SETCC)
704 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000705
Daniel Sanders3ce56622013-09-24 12:18:31 +0000706 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
707 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000708
Daniel Sanders3ce56622013-09-24 12:18:31 +0000709 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
710 Signed = true;
711 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
712 Signed = false;
713 else
714 return SDValue();
715
716 SDValue Op1 = N->getOperand(1);
717 SDValue Op2 = N->getOperand(2);
718 SDValue Op0Op0 = Op0->getOperand(0);
719 SDValue Op0Op1 = Op0->getOperand(1);
720
721 if (Op1 == Op0Op0 && Op2 == Op0Op1)
722 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
723 Ty, Op1, Op2);
724 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
725 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
726 Ty, Op1, Op2);
727 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
728 SDValue SetCC = N->getOperand(0);
729
730 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
731 return SDValue();
732
733 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
734 SetCC.getOperand(0), SetCC.getOperand(1),
735 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
736 }
737
738 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000739}
740
Daniel Sandersf7456c72013-09-23 13:22:24 +0000741static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
742 const MipsSubtarget *Subtarget) {
743 EVT Ty = N->getValueType(0);
744
745 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
746 // Try the following combines:
747 // (xor (or $a, $b), (build_vector allones))
748 // (xor (or $a, $b), (bitcast (build_vector allones)))
749 SDValue Op0 = N->getOperand(0);
750 SDValue Op1 = N->getOperand(1);
751 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000752
753 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
754 NotOp = Op1;
755 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
756 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000757 else
758 return SDValue();
759
760 if (NotOp->getOpcode() == ISD::OR)
761 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
762 NotOp->getOperand(1));
763 }
764
765 return SDValue();
766}
767
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000768SDValue
769MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
770 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000771 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000772
773 switch (N->getOpcode()) {
774 case ISD::ADDE:
775 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000776 case ISD::AND:
777 Val = performANDCombine(N, DAG, DCI, Subtarget);
778 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000779 case ISD::SUBE:
780 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000781 case ISD::MUL:
782 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000783 case ISD::SHL:
784 return performSHLCombine(N, DAG, DCI, Subtarget);
785 case ISD::SRA:
786 return performSRACombine(N, DAG, DCI, Subtarget);
787 case ISD::SRL:
788 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000789 case ISD::VSELECT:
790 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000791 case ISD::XOR:
792 Val = performXORCombine(N, DAG, Subtarget);
793 break;
794 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000795 Val = performSETCCCombine(N, DAG);
796 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000797 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000798
799 if (Val.getNode())
800 return Val;
801
802 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000803}
804
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000805MachineBasicBlock *
806MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
807 MachineBasicBlock *BB) const {
808 switch (MI->getOpcode()) {
809 default:
810 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
811 case Mips::BPOSGE32_PSEUDO:
812 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +0000813 case Mips::SNZ_B_PSEUDO:
814 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
815 case Mips::SNZ_H_PSEUDO:
816 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
817 case Mips::SNZ_W_PSEUDO:
818 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
819 case Mips::SNZ_D_PSEUDO:
820 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
821 case Mips::SNZ_V_PSEUDO:
822 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
823 case Mips::SZ_B_PSEUDO:
824 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
825 case Mips::SZ_H_PSEUDO:
826 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
827 case Mips::SZ_W_PSEUDO:
828 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
829 case Mips::SZ_D_PSEUDO:
830 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
831 case Mips::SZ_V_PSEUDO:
832 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +0000833 case Mips::COPY_FW_PSEUDO:
834 return emitCOPY_FW(MI, BB);
835 case Mips::COPY_FD_PSEUDO:
836 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +0000837 case Mips::INSERT_FW_PSEUDO:
838 return emitINSERT_FW(MI, BB);
839 case Mips::INSERT_FD_PSEUDO:
840 return emitINSERT_FD(MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000841 }
842}
843
844bool MipsSETargetLowering::
845isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
846 unsigned NextStackOffset,
847 const MipsFunctionInfo& FI) const {
848 if (!EnableMipsTailCalls)
849 return false;
850
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000851 // Return false if either the callee or caller has a byval argument.
852 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
853 return false;
854
855 // Return true if the callee's argument area is no larger than the
856 // caller's.
857 return NextStackOffset <= FI.getIncomingArgSize();
858}
859
860void MipsSETargetLowering::
861getOpndList(SmallVectorImpl<SDValue> &Ops,
862 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
863 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
864 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
865 // T9 should contain the address of the callee function if
866 // -reloction-model=pic or it is an indirect call.
867 if (IsPICCall || !GlobalOrExternal) {
868 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
869 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
870 } else
871 Ops.push_back(Callee);
872
873 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
874 InternalLinkage, CLI, Callee, Chain);
875}
876
Akira Hatanaka63791212013-09-07 00:52:30 +0000877SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
878 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
879
880 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
881 return MipsTargetLowering::lowerLOAD(Op, DAG);
882
883 // Replace a double precision load with two i32 loads and a buildpair64.
884 SDLoc DL(Op);
885 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
886 EVT PtrVT = Ptr.getValueType();
887
888 // i32 load from lower address.
889 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
890 MachinePointerInfo(), Nd.isVolatile(),
891 Nd.isNonTemporal(), Nd.isInvariant(),
892 Nd.getAlignment());
893
894 // i32 load from higher address.
895 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
896 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
897 MachinePointerInfo(), Nd.isVolatile(),
898 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +0000899 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka63791212013-09-07 00:52:30 +0000900
901 if (!Subtarget->isLittle())
902 std::swap(Lo, Hi);
903
904 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
905 SDValue Ops[2] = {BP, Hi.getValue(1)};
906 return DAG.getMergeValues(Ops, 2, DL);
907}
908
909SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
910 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
911
912 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
913 return MipsTargetLowering::lowerSTORE(Op, DAG);
914
915 // Replace a double precision store with two extractelement64s and i32 stores.
916 SDLoc DL(Op);
917 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
918 EVT PtrVT = Ptr.getValueType();
919 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
920 Val, DAG.getConstant(0, MVT::i32));
921 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
922 Val, DAG.getConstant(1, MVT::i32));
923
924 if (!Subtarget->isLittle())
925 std::swap(Lo, Hi);
926
927 // i32 store to lower address.
928 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
929 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
930 Nd.getTBAAInfo());
931
932 // i32 store to higher address.
933 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
934 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +0000935 Nd.isVolatile(), Nd.isNonTemporal(),
936 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +0000937}
938
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000939SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
940 bool HasLo, bool HasHi,
941 SelectionDAG &DAG) const {
942 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000943 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000944 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
945 Op.getOperand(0), Op.getOperand(1));
946 SDValue Lo, Hi;
947
948 if (HasLo)
949 Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
950 DAG.getConstant(Mips::sub_lo, MVT::i32));
951 if (HasHi)
952 Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
953 DAG.getConstant(Mips::sub_hi, MVT::i32));
954
955 if (!HasLo || !HasHi)
956 return HasLo ? Lo : Hi;
957
958 SDValue Vals[] = { Lo, Hi };
959 return DAG.getMergeValues(Vals, 2, DL);
960}
961
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000962
Andrew Trickef9de2a2013-05-25 02:42:55 +0000963static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000964 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
965 DAG.getConstant(0, MVT::i32));
966 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
967 DAG.getConstant(1, MVT::i32));
968 return DAG.getNode(MipsISD::InsertLOHI, DL, MVT::Untyped, InLo, InHi);
969}
970
Andrew Trickef9de2a2013-05-25 02:42:55 +0000971static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000972 SDValue Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
973 DAG.getConstant(Mips::sub_lo, MVT::i32));
974 SDValue Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
975 DAG.getConstant(Mips::sub_hi, MVT::i32));
976 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
977}
978
979// This function expands mips intrinsic nodes which have 64-bit input operands
980// or output values.
981//
982// out64 = intrinsic-node in64
983// =>
984// lo = copy (extract-element (in64, 0))
985// hi = copy (extract-element (in64, 1))
986// mips-specific-node
987// v0 = copy lo
988// v1 = copy hi
989// out64 = merge-values (v0, v1)
990//
991static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000992 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000993 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
994 SmallVector<SDValue, 3> Ops;
995 unsigned OpNo = 0;
996
997 // See if Op has a chain input.
998 if (HasChainIn)
999 Ops.push_back(Op->getOperand(OpNo++));
1000
1001 // The next operand is the intrinsic opcode.
1002 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1003
1004 // See if the next operand has type i64.
1005 SDValue Opnd = Op->getOperand(++OpNo), In64;
1006
1007 if (Opnd.getValueType() == MVT::i64)
1008 In64 = initAccumulator(Opnd, DL, DAG);
1009 else
1010 Ops.push_back(Opnd);
1011
1012 // Push the remaining operands.
1013 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1014 Ops.push_back(Op->getOperand(OpNo));
1015
1016 // Add In64 to the end of the list.
1017 if (In64.getNode())
1018 Ops.push_back(In64);
1019
1020 // Scan output.
1021 SmallVector<EVT, 2> ResTys;
1022
1023 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1024 I != E; ++I)
1025 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1026
1027 // Create node.
1028 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1029 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1030
1031 if (!HasChainIn)
1032 return Out;
1033
1034 assert(Val->getValueType(1) == MVT::Other);
1035 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1036 return DAG.getMergeValues(Vals, 2, DL);
1037}
1038
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001039// Lower an MSA copy intrinsic into the specified SelectionDAG node
1040static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1041 SDLoc DL(Op);
1042 SDValue Vec = Op->getOperand(1);
1043 SDValue Idx = Op->getOperand(2);
1044 EVT ResTy = Op->getValueType(0);
1045 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1046
1047 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1048 DAG.getValueType(EltTy));
1049
1050 return Result;
1051}
1052
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001053static SDValue
1054lowerMSASplatImm(SDLoc DL, EVT ResTy, SDValue ImmOp, SelectionDAG &DAG) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001055 EVT ViaVecTy = ResTy;
1056 SmallVector<SDValue, 16> Ops;
1057 SDValue ImmHiOp;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001058
Daniel Sandersf49dd822013-09-24 13:33:07 +00001059 if (ViaVecTy == MVT::v2i64) {
1060 ImmHiOp = DAG.getNode(ISD::SRA, DL, MVT::i32, ImmOp,
1061 DAG.getConstant(31, MVT::i32));
1062 for (unsigned i = 0; i < ViaVecTy.getVectorNumElements(); ++i) {
1063 Ops.push_back(ImmHiOp);
1064 Ops.push_back(ImmOp);
1065 }
1066 ViaVecTy = MVT::v4i32;
1067 } else {
1068 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1069 Ops.push_back(ImmOp);
1070 }
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001071
Daniel Sandersf49dd822013-09-24 13:33:07 +00001072 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, &Ops[0],
1073 Ops.size());
1074
1075 if (ResTy != ViaVecTy)
1076 Result = DAG.getNode(ISD::BITCAST, DL, ResTy, Result);
1077
1078 return Result;
1079}
1080
1081static SDValue
1082lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001083 return lowerMSASplatImm(SDLoc(Op), Op->getValueType(0),
1084 Op->getOperand(ImmOp), DAG);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001085}
1086
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001087SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1088 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001089 SDLoc DL(Op);
1090
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001091 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1092 default:
1093 return SDValue();
1094 case Intrinsic::mips_shilo:
1095 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1096 case Intrinsic::mips_dpau_h_qbl:
1097 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1098 case Intrinsic::mips_dpau_h_qbr:
1099 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1100 case Intrinsic::mips_dpsu_h_qbl:
1101 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1102 case Intrinsic::mips_dpsu_h_qbr:
1103 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1104 case Intrinsic::mips_dpa_w_ph:
1105 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1106 case Intrinsic::mips_dps_w_ph:
1107 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1108 case Intrinsic::mips_dpax_w_ph:
1109 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1110 case Intrinsic::mips_dpsx_w_ph:
1111 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1112 case Intrinsic::mips_mulsa_w_ph:
1113 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1114 case Intrinsic::mips_mult:
1115 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1116 case Intrinsic::mips_multu:
1117 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1118 case Intrinsic::mips_madd:
1119 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1120 case Intrinsic::mips_maddu:
1121 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1122 case Intrinsic::mips_msub:
1123 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1124 case Intrinsic::mips_msubu:
1125 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001126 case Intrinsic::mips_addv_b:
1127 case Intrinsic::mips_addv_h:
1128 case Intrinsic::mips_addv_w:
1129 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001130 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1131 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001132 case Intrinsic::mips_addvi_b:
1133 case Intrinsic::mips_addvi_h:
1134 case Intrinsic::mips_addvi_w:
1135 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001136 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1137 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001138 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001139 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1140 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001141 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001142 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1143 lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersce09d072013-08-28 12:14:50 +00001144 case Intrinsic::mips_bnz_b:
1145 case Intrinsic::mips_bnz_h:
1146 case Intrinsic::mips_bnz_w:
1147 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001148 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1149 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001150 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001151 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1152 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001153 case Intrinsic::mips_bsel_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001154 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sanderse1d24352013-09-24 12:04:44 +00001155 Op->getOperand(1), Op->getOperand(2),
1156 Op->getOperand(3));
1157 case Intrinsic::mips_bseli_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001158 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sanderse1d24352013-09-24 12:04:44 +00001159 Op->getOperand(1), Op->getOperand(2),
1160 lowerMSASplatImm(Op, 3, DAG));
Daniel Sandersce09d072013-08-28 12:14:50 +00001161 case Intrinsic::mips_bz_b:
1162 case Intrinsic::mips_bz_h:
1163 case Intrinsic::mips_bz_w:
1164 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001165 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1166 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001167 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001168 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1169 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001170 case Intrinsic::mips_ceq_b:
1171 case Intrinsic::mips_ceq_h:
1172 case Intrinsic::mips_ceq_w:
1173 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001174 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001175 Op->getOperand(2), ISD::SETEQ);
1176 case Intrinsic::mips_ceqi_b:
1177 case Intrinsic::mips_ceqi_h:
1178 case Intrinsic::mips_ceqi_w:
1179 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001180 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001181 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1182 case Intrinsic::mips_cle_s_b:
1183 case Intrinsic::mips_cle_s_h:
1184 case Intrinsic::mips_cle_s_w:
1185 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001186 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001187 Op->getOperand(2), ISD::SETLE);
1188 case Intrinsic::mips_clei_s_b:
1189 case Intrinsic::mips_clei_s_h:
1190 case Intrinsic::mips_clei_s_w:
1191 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001192 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001193 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1194 case Intrinsic::mips_cle_u_b:
1195 case Intrinsic::mips_cle_u_h:
1196 case Intrinsic::mips_cle_u_w:
1197 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001198 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001199 Op->getOperand(2), ISD::SETULE);
1200 case Intrinsic::mips_clei_u_b:
1201 case Intrinsic::mips_clei_u_h:
1202 case Intrinsic::mips_clei_u_w:
1203 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001204 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001205 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1206 case Intrinsic::mips_clt_s_b:
1207 case Intrinsic::mips_clt_s_h:
1208 case Intrinsic::mips_clt_s_w:
1209 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001210 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001211 Op->getOperand(2), ISD::SETLT);
1212 case Intrinsic::mips_clti_s_b:
1213 case Intrinsic::mips_clti_s_h:
1214 case Intrinsic::mips_clti_s_w:
1215 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001216 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001217 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1218 case Intrinsic::mips_clt_u_b:
1219 case Intrinsic::mips_clt_u_h:
1220 case Intrinsic::mips_clt_u_w:
1221 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001222 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001223 Op->getOperand(2), ISD::SETULT);
1224 case Intrinsic::mips_clti_u_b:
1225 case Intrinsic::mips_clti_u_h:
1226 case Intrinsic::mips_clti_u_w:
1227 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001228 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001229 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001230 case Intrinsic::mips_copy_s_b:
1231 case Intrinsic::mips_copy_s_h:
1232 case Intrinsic::mips_copy_s_w:
1233 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001234 case Intrinsic::mips_copy_s_d:
1235 // Don't lower directly into VEXTRACT_SEXT_ELT since i64 might be illegal.
1236 // Instead lower to the generic EXTRACT_VECTOR_ELT node and let the type
1237 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1238 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1239 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001240 case Intrinsic::mips_copy_u_b:
1241 case Intrinsic::mips_copy_u_h:
1242 case Intrinsic::mips_copy_u_w:
1243 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001244 case Intrinsic::mips_copy_u_d:
1245 // Don't lower directly into VEXTRACT_ZEXT_ELT since i64 might be illegal.
1246 // Instead lower to the generic EXTRACT_VECTOR_ELT node and let the type
1247 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1248 //
1249 // Note: When i64 is illegal, this results in copy_s.w instructions instead
1250 // of copy_u.w instructions. This makes no difference to the behaviour
1251 // since i64 is only illegal when the register file is 32-bit.
1252 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1253 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001254 case Intrinsic::mips_div_s_b:
1255 case Intrinsic::mips_div_s_h:
1256 case Intrinsic::mips_div_s_w:
1257 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001258 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1259 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001260 case Intrinsic::mips_div_u_b:
1261 case Intrinsic::mips_div_u_h:
1262 case Intrinsic::mips_div_u_w:
1263 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001264 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1265 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001266 case Intrinsic::mips_fadd_w:
1267 case Intrinsic::mips_fadd_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001268 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1269 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001270 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1271 case Intrinsic::mips_fceq_w:
1272 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001273 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001274 Op->getOperand(2), ISD::SETOEQ);
1275 case Intrinsic::mips_fcle_w:
1276 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001277 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001278 Op->getOperand(2), ISD::SETOLE);
1279 case Intrinsic::mips_fclt_w:
1280 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001281 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001282 Op->getOperand(2), ISD::SETOLT);
1283 case Intrinsic::mips_fcne_w:
1284 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001285 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001286 Op->getOperand(2), ISD::SETONE);
1287 case Intrinsic::mips_fcor_w:
1288 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001289 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001290 Op->getOperand(2), ISD::SETO);
1291 case Intrinsic::mips_fcueq_w:
1292 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001293 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001294 Op->getOperand(2), ISD::SETUEQ);
1295 case Intrinsic::mips_fcule_w:
1296 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001297 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001298 Op->getOperand(2), ISD::SETULE);
1299 case Intrinsic::mips_fcult_w:
1300 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001301 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001302 Op->getOperand(2), ISD::SETULT);
1303 case Intrinsic::mips_fcun_w:
1304 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001305 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001306 Op->getOperand(2), ISD::SETUO);
1307 case Intrinsic::mips_fcune_w:
1308 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001309 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001310 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001311 case Intrinsic::mips_fdiv_w:
1312 case Intrinsic::mips_fdiv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001313 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1314 Op->getOperand(2));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001315 case Intrinsic::mips_fill_b:
1316 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001317 case Intrinsic::mips_fill_w:
1318 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001319 SmallVector<SDValue, 16> Ops;
1320 EVT ResTy = Op->getValueType(0);
1321
1322 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1323 Ops.push_back(Op->getOperand(1));
1324
Daniel Sandersc72593e2013-09-27 13:20:41 +00001325 // If ResTy is v2i64 then the type legalizer will break this node down into
1326 // an equivalent v4i32.
1327 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, &Ops[0], Ops.size());
Daniel Sandersf49dd822013-09-24 13:33:07 +00001328 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001329 case Intrinsic::mips_flog2_w:
1330 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001331 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001332 case Intrinsic::mips_fmul_w:
1333 case Intrinsic::mips_fmul_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001334 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1335 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001336 case Intrinsic::mips_frint_w:
1337 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001338 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001339 case Intrinsic::mips_fsqrt_w:
1340 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001341 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001342 case Intrinsic::mips_fsub_w:
1343 case Intrinsic::mips_fsub_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001344 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1345 Op->getOperand(2));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001346 case Intrinsic::mips_ilvev_b:
1347 case Intrinsic::mips_ilvev_h:
1348 case Intrinsic::mips_ilvev_w:
1349 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001350 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001351 Op->getOperand(1), Op->getOperand(2));
1352 case Intrinsic::mips_ilvl_b:
1353 case Intrinsic::mips_ilvl_h:
1354 case Intrinsic::mips_ilvl_w:
1355 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001356 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001357 Op->getOperand(1), Op->getOperand(2));
1358 case Intrinsic::mips_ilvod_b:
1359 case Intrinsic::mips_ilvod_h:
1360 case Intrinsic::mips_ilvod_w:
1361 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001362 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001363 Op->getOperand(1), Op->getOperand(2));
1364 case Intrinsic::mips_ilvr_b:
1365 case Intrinsic::mips_ilvr_h:
1366 case Intrinsic::mips_ilvr_w:
1367 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001368 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001369 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001370 case Intrinsic::mips_insert_b:
1371 case Intrinsic::mips_insert_h:
1372 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001373 case Intrinsic::mips_insert_d:
1374 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1375 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001376 case Intrinsic::mips_ldi_b:
1377 case Intrinsic::mips_ldi_h:
1378 case Intrinsic::mips_ldi_w:
1379 case Intrinsic::mips_ldi_d:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001380 return lowerMSASplatImm(Op, 1, DAG);
Daniel Sanders3ce56622013-09-24 12:18:31 +00001381 case Intrinsic::mips_max_s_b:
1382 case Intrinsic::mips_max_s_h:
1383 case Intrinsic::mips_max_s_w:
1384 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001385 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1386 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001387 case Intrinsic::mips_max_u_b:
1388 case Intrinsic::mips_max_u_h:
1389 case Intrinsic::mips_max_u_w:
1390 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001391 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1392 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001393 case Intrinsic::mips_maxi_s_b:
1394 case Intrinsic::mips_maxi_s_h:
1395 case Intrinsic::mips_maxi_s_w:
1396 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001397 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1398 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001399 case Intrinsic::mips_maxi_u_b:
1400 case Intrinsic::mips_maxi_u_h:
1401 case Intrinsic::mips_maxi_u_w:
1402 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001403 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1404 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001405 case Intrinsic::mips_min_s_b:
1406 case Intrinsic::mips_min_s_h:
1407 case Intrinsic::mips_min_s_w:
1408 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001409 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1410 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001411 case Intrinsic::mips_min_u_b:
1412 case Intrinsic::mips_min_u_h:
1413 case Intrinsic::mips_min_u_w:
1414 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001415 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1416 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001417 case Intrinsic::mips_mini_s_b:
1418 case Intrinsic::mips_mini_s_h:
1419 case Intrinsic::mips_mini_s_w:
1420 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001421 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1422 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001423 case Intrinsic::mips_mini_u_b:
1424 case Intrinsic::mips_mini_u_h:
1425 case Intrinsic::mips_mini_u_w:
1426 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001427 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1428 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001429 case Intrinsic::mips_mod_s_b:
1430 case Intrinsic::mips_mod_s_h:
1431 case Intrinsic::mips_mod_s_w:
1432 case Intrinsic::mips_mod_s_d:
1433 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1434 Op->getOperand(2));
1435 case Intrinsic::mips_mod_u_b:
1436 case Intrinsic::mips_mod_u_h:
1437 case Intrinsic::mips_mod_u_w:
1438 case Intrinsic::mips_mod_u_d:
1439 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1440 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001441 case Intrinsic::mips_mulv_b:
1442 case Intrinsic::mips_mulv_h:
1443 case Intrinsic::mips_mulv_w:
1444 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001445 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1446 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001447 case Intrinsic::mips_nlzc_b:
1448 case Intrinsic::mips_nlzc_h:
1449 case Intrinsic::mips_nlzc_w:
1450 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001451 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001452 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001453 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1454 Op->getOperand(1), Op->getOperand(2));
1455 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001456 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001457 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001458 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1459 Op->getOperand(1),
1460 lowerMSASplatImm(Op, 2, DAG));
1461 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001462 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001463 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001464 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
1465 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001466 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001467 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1468 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001469 case Intrinsic::mips_pckev_b:
1470 case Intrinsic::mips_pckev_h:
1471 case Intrinsic::mips_pckev_w:
1472 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001473 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001474 Op->getOperand(1), Op->getOperand(2));
1475 case Intrinsic::mips_pckod_b:
1476 case Intrinsic::mips_pckod_h:
1477 case Intrinsic::mips_pckod_w:
1478 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001479 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001480 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00001481 case Intrinsic::mips_pcnt_b:
1482 case Intrinsic::mips_pcnt_h:
1483 case Intrinsic::mips_pcnt_w:
1484 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001485 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sanders26307182013-09-24 14:20:00 +00001486 case Intrinsic::mips_shf_b:
1487 case Intrinsic::mips_shf_h:
1488 case Intrinsic::mips_shf_w:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001489 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00001490 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001491 case Intrinsic::mips_sll_b:
1492 case Intrinsic::mips_sll_h:
1493 case Intrinsic::mips_sll_w:
1494 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001495 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
1496 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001497 case Intrinsic::mips_slli_b:
1498 case Intrinsic::mips_slli_h:
1499 case Intrinsic::mips_slli_w:
1500 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001501 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
1502 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00001503 case Intrinsic::mips_splati_b:
1504 case Intrinsic::mips_splati_h:
1505 case Intrinsic::mips_splati_w:
1506 case Intrinsic::mips_splati_d:
1507 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1508 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1509 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001510 case Intrinsic::mips_sra_b:
1511 case Intrinsic::mips_sra_h:
1512 case Intrinsic::mips_sra_w:
1513 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001514 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
1515 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001516 case Intrinsic::mips_srai_b:
1517 case Intrinsic::mips_srai_h:
1518 case Intrinsic::mips_srai_w:
1519 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001520 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
1521 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001522 case Intrinsic::mips_srl_b:
1523 case Intrinsic::mips_srl_h:
1524 case Intrinsic::mips_srl_w:
1525 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001526 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
1527 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001528 case Intrinsic::mips_srli_b:
1529 case Intrinsic::mips_srli_h:
1530 case Intrinsic::mips_srli_w:
1531 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001532 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
1533 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001534 case Intrinsic::mips_subv_b:
1535 case Intrinsic::mips_subv_h:
1536 case Intrinsic::mips_subv_w:
1537 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001538 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
1539 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001540 case Intrinsic::mips_subvi_b:
1541 case Intrinsic::mips_subvi_h:
1542 case Intrinsic::mips_subvi_w:
1543 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001544 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
1545 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00001546 case Intrinsic::mips_vshf_b:
1547 case Intrinsic::mips_vshf_h:
1548 case Intrinsic::mips_vshf_w:
1549 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001550 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00001551 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001552 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001553 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
1554 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001555 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001556 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
1557 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001558 }
1559}
1560
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00001561static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1562 SDLoc DL(Op);
1563 SDValue ChainIn = Op->getOperand(0);
1564 SDValue Address = Op->getOperand(2);
1565 SDValue Offset = Op->getOperand(3);
1566 EVT ResTy = Op->getValueType(0);
1567 EVT PtrTy = Address->getValueType(0);
1568
1569 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1570
1571 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
1572 false, false, 16);
1573}
1574
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001575SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
1576 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00001577 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1578 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001579 default:
1580 return SDValue();
1581 case Intrinsic::mips_extp:
1582 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
1583 case Intrinsic::mips_extpdp:
1584 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
1585 case Intrinsic::mips_extr_w:
1586 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
1587 case Intrinsic::mips_extr_r_w:
1588 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
1589 case Intrinsic::mips_extr_rs_w:
1590 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
1591 case Intrinsic::mips_extr_s_h:
1592 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
1593 case Intrinsic::mips_mthlip:
1594 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
1595 case Intrinsic::mips_mulsaq_s_w_ph:
1596 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
1597 case Intrinsic::mips_maq_s_w_phl:
1598 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
1599 case Intrinsic::mips_maq_s_w_phr:
1600 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
1601 case Intrinsic::mips_maq_sa_w_phl:
1602 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
1603 case Intrinsic::mips_maq_sa_w_phr:
1604 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
1605 case Intrinsic::mips_dpaq_s_w_ph:
1606 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
1607 case Intrinsic::mips_dpsq_s_w_ph:
1608 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
1609 case Intrinsic::mips_dpaq_sa_l_w:
1610 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
1611 case Intrinsic::mips_dpsq_sa_l_w:
1612 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
1613 case Intrinsic::mips_dpaqx_s_w_ph:
1614 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
1615 case Intrinsic::mips_dpaqx_sa_w_ph:
1616 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
1617 case Intrinsic::mips_dpsqx_s_w_ph:
1618 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
1619 case Intrinsic::mips_dpsqx_sa_w_ph:
1620 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00001621 case Intrinsic::mips_ld_b:
1622 case Intrinsic::mips_ld_h:
1623 case Intrinsic::mips_ld_w:
1624 case Intrinsic::mips_ld_d:
1625 case Intrinsic::mips_ldx_b:
1626 case Intrinsic::mips_ldx_h:
1627 case Intrinsic::mips_ldx_w:
1628 case Intrinsic::mips_ldx_d:
1629 return lowerMSALoadIntr(Op, DAG, Intr);
1630 }
1631}
1632
1633static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1634 SDLoc DL(Op);
1635 SDValue ChainIn = Op->getOperand(0);
1636 SDValue Value = Op->getOperand(2);
1637 SDValue Address = Op->getOperand(3);
1638 SDValue Offset = Op->getOperand(4);
1639 EVT PtrTy = Address->getValueType(0);
1640
1641 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1642
1643 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
1644 false, 16);
1645}
1646
1647SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
1648 SelectionDAG &DAG) const {
1649 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1650 switch (Intr) {
1651 default:
1652 return SDValue();
1653 case Intrinsic::mips_st_b:
1654 case Intrinsic::mips_st_h:
1655 case Intrinsic::mips_st_w:
1656 case Intrinsic::mips_st_d:
1657 case Intrinsic::mips_stx_b:
1658 case Intrinsic::mips_stx_h:
1659 case Intrinsic::mips_stx_w:
1660 case Intrinsic::mips_stx_d:
Daniel Sandersce09d072013-08-28 12:14:50 +00001661 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001662 }
1663}
1664
Daniel Sanders7a289d02013-09-23 12:02:46 +00001665/// \brief Check if the given BuildVectorSDNode is a splat.
1666/// This method currently relies on DAG nodes being reused when equivalent,
1667/// so it's possible for this to return false even when isConstantSplat returns
1668/// true.
1669static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00001670 unsigned int nOps = N->getNumOperands();
1671 assert(nOps > 1 && "isSplat has 0 or 1 sized build vector");
1672
1673 SDValue Operand0 = N->getOperand(0);
1674
1675 for (unsigned int i = 1; i < nOps; ++i) {
1676 if (N->getOperand(i) != Operand0)
1677 return false;
1678 }
1679
1680 return true;
1681}
1682
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001683// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
1684//
1685// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
1686// choose to sign-extend but we could have equally chosen zero-extend. The
1687// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
1688// result into this node later (possibly changing it to a zero-extend in the
1689// process).
1690SDValue MipsSETargetLowering::
1691lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
1692 SDLoc DL(Op);
1693 EVT ResTy = Op->getValueType(0);
1694 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001695 EVT VecTy = Op0->getValueType(0);
1696
1697 if (!VecTy.is128BitVector())
1698 return SDValue();
1699
1700 if (ResTy.isInteger()) {
1701 SDValue Op1 = Op->getOperand(1);
1702 EVT EltTy = VecTy.getVectorElementType();
1703 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
1704 DAG.getValueType(EltTy));
1705 }
1706
1707 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001708}
1709
Daniel Sandersf49dd822013-09-24 13:33:07 +00001710static bool isConstantOrUndef(const SDValue Op) {
1711 if (Op->getOpcode() == ISD::UNDEF)
1712 return true;
1713 if (dyn_cast<ConstantSDNode>(Op))
1714 return true;
1715 if (dyn_cast<ConstantFPSDNode>(Op))
1716 return true;
1717 return false;
1718}
1719
1720static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
1721 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
1722 if (isConstantOrUndef(Op->getOperand(i)))
1723 return true;
1724 return false;
1725}
1726
Daniel Sanders7a289d02013-09-23 12:02:46 +00001727// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
1728// backend.
1729//
1730// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001731// - Constant splats are legal as-is as long as the SplatBitSize is a power of
1732// 2 less than or equal to 64 and the value fits into a signed 10-bit
1733// immediate
1734// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
1735// is a power of 2 less than or equal to 64 and the value does not fit into a
1736// signed 10-bit immediate
1737// - Non-constant splats are legal as-is.
1738// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
1739// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00001740SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
1741 SelectionDAG &DAG) const {
1742 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
1743 EVT ResTy = Op->getValueType(0);
1744 SDLoc DL(Op);
1745 APInt SplatValue, SplatUndef;
1746 unsigned SplatBitSize;
1747 bool HasAnyUndefs;
1748
1749 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
1750 return SDValue();
1751
1752 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1753 HasAnyUndefs, 8,
Daniel Sandersf49dd822013-09-24 13:33:07 +00001754 !Subtarget->isLittle()) && SplatBitSize <= 64) {
1755 // We can only cope with 8, 16, 32, or 64-bit elements
1756 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
1757 SplatBitSize != 64)
1758 return SDValue();
1759
1760 // If the value fits into a simm10 then we can use ldi.[bhwd]
1761 if (SplatValue.isSignedIntN(10))
1762 return Op;
1763
1764 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00001765
1766 switch (SplatBitSize) {
1767 default:
1768 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00001769 case 8:
1770 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00001771 break;
1772 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001773 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00001774 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00001775 case 32:
1776 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00001777 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00001778 case 64:
1779 // There's no fill.d to fall back on for 64-bit values
1780 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00001781 }
1782
Daniel Sandersf49dd822013-09-24 13:33:07 +00001783 SmallVector<SDValue, 16> Ops;
1784 SDValue Constant = DAG.getConstant(SplatValue.sextOrSelf(32), MVT::i32);
1785
1786 for (unsigned i = 0; i < ViaVecTy.getVectorNumElements(); ++i)
1787 Ops.push_back(Constant);
1788
1789 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Node), ViaVecTy,
1790 &Ops[0], Ops.size());
1791
1792 if (ViaVecTy != ResTy)
1793 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00001794
1795 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00001796 } else if (isSplatVector(Node))
1797 return Op;
1798 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00001799 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
1800 // The resulting code is the same length as the expansion, but it doesn't
1801 // use memory operations
1802 EVT ResTy = Node->getValueType(0);
1803
1804 assert(ResTy.isVector());
1805
1806 unsigned NumElts = ResTy.getVectorNumElements();
1807 SDValue Vector = DAG.getUNDEF(ResTy);
1808 for (unsigned i = 0; i < NumElts; ++i) {
1809 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
1810 Node->getOperand(i),
1811 DAG.getConstant(i, MVT::i32));
1812 }
1813 return Vector;
1814 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00001815
1816 return SDValue();
1817}
1818
Daniel Sanders26307182013-09-24 14:20:00 +00001819// Lower VECTOR_SHUFFLE into SHF (if possible).
1820//
1821// SHF splits the vector into blocks of four elements, then shuffles these
1822// elements according to a <4 x i2> constant (encoded as an integer immediate).
1823//
1824// It is therefore possible to lower into SHF when the mask takes the form:
1825// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
1826// When undef's appear they are treated as if they were whatever value is
1827// necessary in order to fit the above form.
1828//
1829// For example:
1830// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
1831// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
1832// i32 7, i32 6, i32 5, i32 4>
1833// is lowered to:
1834// (SHF_H $w0, $w1, 27)
1835// where the 27 comes from:
1836// 3 + (2 << 2) + (1 << 4) + (0 << 6)
1837static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
1838 SmallVector<int, 16> Indices,
1839 SelectionDAG &DAG) {
1840 int SHFIndices[4] = { -1, -1, -1, -1 };
1841
1842 if (Indices.size() < 4)
1843 return SDValue();
1844
1845 for (unsigned i = 0; i < 4; ++i) {
1846 for (unsigned j = i; j < Indices.size(); j += 4) {
1847 int Idx = Indices[j];
1848
1849 // Convert from vector index to 4-element subvector index
1850 // If an index refers to an element outside of the subvector then give up
1851 if (Idx != -1) {
1852 Idx -= 4 * (j / 4);
1853 if (Idx < 0 || Idx >= 4)
1854 return SDValue();
1855 }
1856
1857 // If the mask has an undef, replace it with the current index.
1858 // Note that it might still be undef if the current index is also undef
1859 if (SHFIndices[i] == -1)
1860 SHFIndices[i] = Idx;
1861
1862 // Check that non-undef values are the same as in the mask. If they
1863 // aren't then give up
1864 if (!(Idx == -1 || Idx == SHFIndices[i]))
1865 return SDValue();
1866 }
1867 }
1868
1869 // Calculate the immediate. Replace any remaining undefs with zero
1870 APInt Imm(32, 0);
1871 for (int i = 3; i >= 0; --i) {
1872 int Idx = SHFIndices[i];
1873
1874 if (Idx == -1)
1875 Idx = 0;
1876
1877 Imm <<= 2;
1878 Imm |= Idx & 0x3;
1879 }
1880
1881 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
1882 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
1883}
1884
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001885// Lower VECTOR_SHUFFLE into ILVEV (if possible).
1886//
1887// ILVEV interleaves the even elements from each vector.
1888//
1889// It is possible to lower into ILVEV when the mask takes the form:
1890// <0, n, 2, n+2, 4, n+4, ...>
1891// where n is the number of elements in the vector.
1892//
1893// When undef's appear in the mask they are treated as if they were whatever
1894// value is necessary in order to fit the above form.
1895static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
1896 SmallVector<int, 16> Indices,
1897 SelectionDAG &DAG) {
1898 assert ((Indices.size() % 2) == 0);
1899 int WsIdx = 0;
1900 int WtIdx = ResTy.getVectorNumElements();
1901
1902 for (unsigned i = 0; i < Indices.size(); i += 2) {
1903 if (Indices[i] != -1 && Indices[i] != WsIdx)
1904 return SDValue();
1905 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
1906 return SDValue();
1907 WsIdx += 2;
1908 WtIdx += 2;
1909 }
1910
1911 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
1912 Op->getOperand(1));
1913}
1914
1915// Lower VECTOR_SHUFFLE into ILVOD (if possible).
1916//
1917// ILVOD interleaves the odd elements from each vector.
1918//
1919// It is possible to lower into ILVOD when the mask takes the form:
1920// <1, n+1, 3, n+3, 5, n+5, ...>
1921// where n is the number of elements in the vector.
1922//
1923// When undef's appear in the mask they are treated as if they were whatever
1924// value is necessary in order to fit the above form.
1925static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
1926 SmallVector<int, 16> Indices,
1927 SelectionDAG &DAG) {
1928 assert ((Indices.size() % 2) == 0);
1929 int WsIdx = 1;
1930 int WtIdx = ResTy.getVectorNumElements() + 1;
1931
1932 for (unsigned i = 0; i < Indices.size(); i += 2) {
1933 if (Indices[i] != -1 && Indices[i] != WsIdx)
1934 return SDValue();
1935 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
1936 return SDValue();
1937 WsIdx += 2;
1938 WtIdx += 2;
1939 }
1940
1941 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
1942 Op->getOperand(1));
1943}
1944
1945// Lower VECTOR_SHUFFLE into ILVL (if possible).
1946//
1947// ILVL interleaves consecutive elements from the left half of each vector.
1948//
1949// It is possible to lower into ILVL when the mask takes the form:
1950// <0, n, 1, n+1, 2, n+2, ...>
1951// where n is the number of elements in the vector.
1952//
1953// When undef's appear in the mask they are treated as if they were whatever
1954// value is necessary in order to fit the above form.
1955static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
1956 SmallVector<int, 16> Indices,
1957 SelectionDAG &DAG) {
1958 assert ((Indices.size() % 2) == 0);
1959 int WsIdx = 0;
1960 int WtIdx = ResTy.getVectorNumElements();
1961
1962 for (unsigned i = 0; i < Indices.size(); i += 2) {
1963 if (Indices[i] != -1 && Indices[i] != WsIdx)
1964 return SDValue();
1965 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
1966 return SDValue();
1967 WsIdx ++;
1968 WtIdx ++;
1969 }
1970
1971 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
1972 Op->getOperand(1));
1973}
1974
1975// Lower VECTOR_SHUFFLE into ILVR (if possible).
1976//
1977// ILVR interleaves consecutive elements from the right half of each vector.
1978//
1979// It is possible to lower into ILVR when the mask takes the form:
1980// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
1981// where n is the number of elements in the vector and x is half n.
1982//
1983// When undef's appear in the mask they are treated as if they were whatever
1984// value is necessary in order to fit the above form.
1985static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
1986 SmallVector<int, 16> Indices,
1987 SelectionDAG &DAG) {
1988 assert ((Indices.size() % 2) == 0);
1989 unsigned NumElts = ResTy.getVectorNumElements();
1990 int WsIdx = NumElts / 2;
1991 int WtIdx = NumElts + NumElts / 2;
1992
1993 for (unsigned i = 0; i < Indices.size(); i += 2) {
1994 if (Indices[i] != -1 && Indices[i] != WsIdx)
1995 return SDValue();
1996 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
1997 return SDValue();
1998 WsIdx ++;
1999 WtIdx ++;
2000 }
2001
2002 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2003 Op->getOperand(1));
2004}
2005
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002006// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2007//
2008// PCKEV copies the even elements of each vector into the result vector.
2009//
2010// It is possible to lower into PCKEV when the mask takes the form:
2011// <0, 2, 4, ..., n, n+2, n+4, ...>
2012// where n is the number of elements in the vector.
2013//
2014// When undef's appear in the mask they are treated as if they were whatever
2015// value is necessary in order to fit the above form.
2016static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2017 SmallVector<int, 16> Indices,
2018 SelectionDAG &DAG) {
2019 assert ((Indices.size() % 2) == 0);
2020 int Idx = 0;
2021
2022 for (unsigned i = 0; i < Indices.size(); ++i) {
2023 if (Indices[i] != -1 && Indices[i] != Idx)
2024 return SDValue();
2025 Idx += 2;
2026 }
2027
2028 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2029 Op->getOperand(1));
2030}
2031
2032// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2033//
2034// PCKOD copies the odd elements of each vector into the result vector.
2035//
2036// It is possible to lower into PCKOD when the mask takes the form:
2037// <1, 3, 5, ..., n+1, n+3, n+5, ...>
2038// where n is the number of elements in the vector.
2039//
2040// When undef's appear in the mask they are treated as if they were whatever
2041// value is necessary in order to fit the above form.
2042static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2043 SmallVector<int, 16> Indices,
2044 SelectionDAG &DAG) {
2045 assert ((Indices.size() % 2) == 0);
2046 int Idx = 1;
2047
2048 for (unsigned i = 0; i < Indices.size(); ++i) {
2049 if (Indices[i] != -1 && Indices[i] != Idx)
2050 return SDValue();
2051 Idx += 2;
2052 }
2053
2054 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2055 Op->getOperand(1));
2056}
2057
Daniel Sanderse5087042013-09-24 14:02:15 +00002058// Lower VECTOR_SHUFFLE into VSHF.
2059//
2060// This mostly consists of converting the shuffle indices in Indices into a
2061// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2062// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2063// if the type is v8i16 and all the indices are less than 8 then the second
2064// operand is unused and can be replaced with anything. We choose to replace it
2065// with the used operand since this reduces the number of instructions overall.
2066static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2067 SmallVector<int, 16> Indices,
2068 SelectionDAG &DAG) {
2069 SmallVector<SDValue, 16> Ops;
2070 SDValue Op0;
2071 SDValue Op1;
2072 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2073 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2074 bool Using1stVec = false;
2075 bool Using2ndVec = false;
2076 SDLoc DL(Op);
2077 int ResTyNumElts = ResTy.getVectorNumElements();
2078
2079 for (int i = 0; i < ResTyNumElts; ++i) {
2080 // Idx == -1 means UNDEF
2081 int Idx = Indices[i];
2082
2083 if (0 <= Idx && Idx < ResTyNumElts)
2084 Using1stVec = true;
2085 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2086 Using2ndVec = true;
2087 }
2088
2089 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2090 ++I)
2091 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2092
2093 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, &Ops[0],
2094 Ops.size());
2095
2096 if (Using1stVec && Using2ndVec) {
2097 Op0 = Op->getOperand(0);
2098 Op1 = Op->getOperand(1);
2099 } else if (Using1stVec)
2100 Op0 = Op1 = Op->getOperand(0);
2101 else if (Using2ndVec)
2102 Op0 = Op1 = Op->getOperand(1);
2103 else
2104 llvm_unreachable("shuffle vector mask references neither vector operand?");
2105
2106 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op0, Op1);
2107}
2108
2109// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2110// indices in the shuffle.
2111SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2112 SelectionDAG &DAG) const {
2113 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2114 EVT ResTy = Op->getValueType(0);
2115
2116 if (!ResTy.is128BitVector())
2117 return SDValue();
2118
2119 int ResTyNumElts = ResTy.getVectorNumElements();
2120 SmallVector<int, 16> Indices;
2121
2122 for (int i = 0; i < ResTyNumElts; ++i)
2123 Indices.push_back(Node->getMaskElt(i));
2124
Daniel Sanders26307182013-09-24 14:20:00 +00002125 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2126 if (Result.getNode())
2127 return Result;
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002128 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2129 if (Result.getNode())
2130 return Result;
2131 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2132 if (Result.getNode())
2133 return Result;
2134 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2135 if (Result.getNode())
2136 return Result;
2137 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2138 if (Result.getNode())
2139 return Result;
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002140 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2141 if (Result.getNode())
2142 return Result;
2143 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2144 if (Result.getNode())
2145 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002146 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2147}
2148
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002149MachineBasicBlock * MipsSETargetLowering::
2150emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2151 // $bb:
2152 // bposge32_pseudo $vr0
2153 // =>
2154 // $bb:
2155 // bposge32 $tbb
2156 // $fbb:
2157 // li $vr2, 0
2158 // b $sink
2159 // $tbb:
2160 // li $vr1, 1
2161 // $sink:
2162 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2163
2164 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2165 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002166 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002167 DebugLoc DL = MI->getDebugLoc();
2168 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2169 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2170 MachineFunction *F = BB->getParent();
2171 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2172 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2173 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2174 F->insert(It, FBB);
2175 F->insert(It, TBB);
2176 F->insert(It, Sink);
2177
2178 // Transfer the remainder of BB and its successor edges to Sink.
2179 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2180 BB->end());
2181 Sink->transferSuccessorsAndUpdatePHIs(BB);
2182
2183 // Add successors.
2184 BB->addSuccessor(FBB);
2185 BB->addSuccessor(TBB);
2186 FBB->addSuccessor(Sink);
2187 TBB->addSuccessor(Sink);
2188
2189 // Insert the real bposge32 instruction to $BB.
2190 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2191
2192 // Fill $FBB.
2193 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2194 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2195 .addReg(Mips::ZERO).addImm(0);
2196 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2197
2198 // Fill $TBB.
2199 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2200 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2201 .addReg(Mips::ZERO).addImm(1);
2202
2203 // Insert phi function to $Sink.
2204 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2205 MI->getOperand(0).getReg())
2206 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2207
2208 MI->eraseFromParent(); // The pseudo instruction is gone now.
2209 return Sink;
2210}
Daniel Sandersce09d072013-08-28 12:14:50 +00002211
2212MachineBasicBlock * MipsSETargetLowering::
2213emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2214 unsigned BranchOp) const{
2215 // $bb:
2216 // vany_nonzero $rd, $ws
2217 // =>
2218 // $bb:
2219 // bnz.b $ws, $tbb
2220 // b $fbb
2221 // $fbb:
2222 // li $rd1, 0
2223 // b $sink
2224 // $tbb:
2225 // li $rd2, 1
2226 // $sink:
2227 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2228
2229 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2230 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2231 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2232 DebugLoc DL = MI->getDebugLoc();
2233 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2234 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2235 MachineFunction *F = BB->getParent();
2236 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2237 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2238 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2239 F->insert(It, FBB);
2240 F->insert(It, TBB);
2241 F->insert(It, Sink);
2242
2243 // Transfer the remainder of BB and its successor edges to Sink.
2244 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2245 BB->end());
2246 Sink->transferSuccessorsAndUpdatePHIs(BB);
2247
2248 // Add successors.
2249 BB->addSuccessor(FBB);
2250 BB->addSuccessor(TBB);
2251 FBB->addSuccessor(Sink);
2252 TBB->addSuccessor(Sink);
2253
2254 // Insert the real bnz.b instruction to $BB.
2255 BuildMI(BB, DL, TII->get(BranchOp))
2256 .addReg(MI->getOperand(1).getReg())
2257 .addMBB(TBB);
2258
2259 // Fill $FBB.
2260 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2261 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2262 .addReg(Mips::ZERO).addImm(0);
2263 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2264
2265 // Fill $TBB.
2266 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2267 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2268 .addReg(Mips::ZERO).addImm(1);
2269
2270 // Insert phi function to $Sink.
2271 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2272 MI->getOperand(0).getReg())
2273 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2274
2275 MI->eraseFromParent(); // The pseudo instruction is gone now.
2276 return Sink;
2277}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002278
2279// Emit the COPY_FW pseudo instruction.
2280//
2281// copy_fw_pseudo $fd, $ws, n
2282// =>
2283// copy_u_w $rt, $ws, $n
2284// mtc1 $rt, $fd
2285//
2286// When n is zero, the equivalent operation can be performed with (potentially)
2287// zero instructions due to register overlaps. This optimization is never valid
2288// for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2289MachineBasicBlock * MipsSETargetLowering::
2290emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2291 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2292 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2293 DebugLoc DL = MI->getDebugLoc();
2294 unsigned Fd = MI->getOperand(0).getReg();
2295 unsigned Ws = MI->getOperand(1).getReg();
2296 unsigned Lane = MI->getOperand(2).getImm();
2297
2298 if (Lane == 0)
2299 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2300 else {
2301 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2302
2303 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(1);
2304 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2305 }
2306
2307 MI->eraseFromParent(); // The pseudo instruction is gone now.
2308 return BB;
2309}
2310
2311// Emit the COPY_FD pseudo instruction.
2312//
2313// copy_fd_pseudo $fd, $ws, n
2314// =>
2315// splati.d $wt, $ws, $n
2316// copy $fd, $wt:sub_64
2317//
2318// When n is zero, the equivalent operation can be performed with (potentially)
2319// zero instructions due to register overlaps. This optimization is always
2320// valid because FR=1 mode which is the only supported mode in MSA.
2321MachineBasicBlock * MipsSETargetLowering::
2322emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
2323 assert(Subtarget->isFP64bit());
2324
2325 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2326 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2327 unsigned Fd = MI->getOperand(0).getReg();
2328 unsigned Ws = MI->getOperand(1).getReg();
2329 unsigned Lane = MI->getOperand(2).getImm() * 2;
2330 DebugLoc DL = MI->getDebugLoc();
2331
2332 if (Lane == 0)
2333 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2334 else {
2335 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2336
2337 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2338 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2339 }
2340
2341 MI->eraseFromParent(); // The pseudo instruction is gone now.
2342 return BB;
2343}
Daniel Sandersa5150702013-09-27 12:31:32 +00002344
2345// Emit the INSERT_FW pseudo instruction.
2346//
2347// insert_fw_pseudo $wd, $wd_in, $n, $fs
2348// =>
2349// subreg_to_reg $wt:sub_lo, $fs
2350// insve_w $wd[$n], $wd_in, $wt[0]
2351MachineBasicBlock * MipsSETargetLowering::
2352emitINSERT_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2353 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2354 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2355 DebugLoc DL = MI->getDebugLoc();
2356 unsigned Wd = MI->getOperand(0).getReg();
2357 unsigned Wd_in = MI->getOperand(1).getReg();
2358 unsigned Lane = MI->getOperand(2).getImm();
2359 unsigned Fs = MI->getOperand(3).getReg();
2360 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2361
2362 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
2363 .addImm(0).addReg(Fs).addImm(Mips::sub_lo);
2364 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
2365 .addReg(Wd_in).addImm(Lane).addReg(Wt);
2366
2367 MI->eraseFromParent(); // The pseudo instruction is gone now.
2368 return BB;
2369}
2370
2371// Emit the INSERT_FD pseudo instruction.
2372//
2373// insert_fd_pseudo $wd, $fs, n
2374// =>
2375// subreg_to_reg $wt:sub_64, $fs
2376// insve_d $wd[$n], $wd_in, $wt[0]
2377MachineBasicBlock * MipsSETargetLowering::
2378emitINSERT_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
2379 assert(Subtarget->isFP64bit());
2380
2381 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2382 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2383 DebugLoc DL = MI->getDebugLoc();
2384 unsigned Wd = MI->getOperand(0).getReg();
2385 unsigned Wd_in = MI->getOperand(1).getReg();
2386 unsigned Lane = MI->getOperand(2).getImm();
2387 unsigned Fs = MI->getOperand(3).getReg();
2388 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2389
2390 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
2391 .addImm(0).addReg(Fs).addImm(Mips::sub_64);
2392 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
2393 .addReg(Wd_in).addImm(Lane).addReg(Wt);
2394
2395 MI->eraseFromParent(); // The pseudo instruction is gone now.
2396 return BB;
2397}