blob: eb9656acaefc7f87162890e21bbc9ff772a81d0c [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//===----------------------------------------------------------------------===//
Daniel Sanders62aeab82013-10-30 13:31:27 +000013#define DEBUG_TYPE "mips-isel"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000014#include "MipsSEISelLowering.h"
15#include "MipsRegisterInfo.h"
16#include "MipsTargetMachine.h"
17#include "llvm/CodeGen/MachineInstrBuilder.h"
18#include "llvm/CodeGen/MachineRegisterInfo.h"
Akira Hatanakaa6bbde52013-04-13 02:13:30 +000019#include "llvm/IR/Intrinsics.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000020#include "llvm/Support/CommandLine.h"
Daniel Sanders62aeab82013-10-30 13:31:27 +000021#include "llvm/Support/Debug.h"
Hans Wennborg3e9b1c12013-10-30 16:10:10 +000022#include "llvm/Support/raw_ostream.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000023#include "llvm/Target/TargetInstrInfo.h"
24
25using namespace llvm;
26
27static cl::opt<bool>
28EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
29 cl::desc("MIPS: Enable tail calls."), cl::init(false));
30
Akira Hatanaka63791212013-09-07 00:52:30 +000031static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
32 cl::desc("Expand double precision loads and "
33 "stores to their single precision "
34 "counterparts"));
35
Akira Hatanaka96ca1822013-03-13 00:54:29 +000036MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
37 : MipsTargetLowering(TM) {
38 // Set up the register classes
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);
Daniel Sanders53fe6c42013-10-30 13:51:01 +000097 setTargetDAGCombine(ISD::OR);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +000098 setTargetDAGCombine(ISD::SRA);
Daniel Sanderse1d24352013-09-24 12:04:44 +000099 setTargetDAGCombine(ISD::VSELECT);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000100 setTargetDAGCombine(ISD::XOR);
Jack Carter3a2c2d42013-08-13 20:54:07 +0000101 }
102
Reed Kotlerc03807a2013-08-30 19:40:56 +0000103 if (!Subtarget->mipsSEUsesSoftFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000104 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
105
106 // When dealing with single precision only, use libcalls
107 if (!Subtarget->isSingleFloat()) {
Akira Hatanakabfb66242013-08-20 23:38:40 +0000108 if (Subtarget->isFP64bit())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000109 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
110 else
111 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
112 }
113 }
114
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000115 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
116 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
117 setOperationAction(ISD::MULHS, MVT::i32, Custom);
118 setOperationAction(ISD::MULHU, MVT::i32, Custom);
119
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000120 if (HasMips64) {
121 setOperationAction(ISD::MULHS, MVT::i64, Custom);
122 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000123 setOperationAction(ISD::MUL, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000124 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000125
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
127 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
128
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000129 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
130 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
131 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
132 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000133 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
134 setOperationAction(ISD::LOAD, MVT::i32, Custom);
135 setOperationAction(ISD::STORE, MVT::i32, Custom);
136
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000137 setTargetDAGCombine(ISD::ADDE);
138 setTargetDAGCombine(ISD::SUBE);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000139 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000140
Daniel Sandersce09d072013-08-28 12:14:50 +0000141 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000142 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
143 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
144
Akira Hatanaka63791212013-09-07 00:52:30 +0000145 if (NoDPLoadStore) {
146 setOperationAction(ISD::LOAD, MVT::f64, Custom);
147 setOperationAction(ISD::STORE, MVT::f64, Custom);
148 }
149
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000150 computeRegisterProperties();
151}
152
153const MipsTargetLowering *
154llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
155 return new MipsSETargetLowering(TM);
156}
157
Daniel Sanders7a289d02013-09-23 12:02:46 +0000158// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000159void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000160addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
161 addRegisterClass(Ty, RC);
162
163 // Expand all builtin opcodes.
164 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
165 setOperationAction(Opc, Ty, Expand);
166
167 setOperationAction(ISD::BITCAST, Ty, Legal);
168 setOperationAction(ISD::LOAD, Ty, Legal);
169 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000170 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
171 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000172 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000173
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000174 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000175 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000176 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000177 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000178 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000179 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000180 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000181 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000182 setOperationAction(ISD::SHL, Ty, Legal);
183 setOperationAction(ISD::SRA, Ty, Legal);
184 setOperationAction(ISD::SRL, Ty, Legal);
185 setOperationAction(ISD::SUB, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000186 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000187 setOperationAction(ISD::UREM, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000188 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000189 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000190 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000191
Daniel Sanders015972b2013-10-11 10:00:06 +0000192 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
193 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
194 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
195 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
196 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
197 }
198
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000199 setOperationAction(ISD::SETCC, Ty, Legal);
200 setCondCodeAction(ISD::SETNE, Ty, Expand);
201 setCondCodeAction(ISD::SETGE, Ty, Expand);
202 setCondCodeAction(ISD::SETGT, Ty, Expand);
203 setCondCodeAction(ISD::SETUGE, Ty, Expand);
204 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000205}
206
Daniel Sanders7a289d02013-09-23 12:02:46 +0000207// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000208void MipsSETargetLowering::
209addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000210 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000211
212 // Expand all builtin opcodes.
213 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
214 setOperationAction(Opc, Ty, Expand);
215
216 setOperationAction(ISD::LOAD, Ty, Legal);
217 setOperationAction(ISD::STORE, Ty, Legal);
218 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000219 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000220 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000221 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000222
223 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000224 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000225 setOperationAction(ISD::FADD, Ty, Legal);
226 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000227 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000228 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000229 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000230 setOperationAction(ISD::FMUL, Ty, Legal);
231 setOperationAction(ISD::FRINT, Ty, Legal);
232 setOperationAction(ISD::FSQRT, Ty, Legal);
233 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000234 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000235
236 setOperationAction(ISD::SETCC, Ty, Legal);
237 setCondCodeAction(ISD::SETOGE, Ty, Expand);
238 setCondCodeAction(ISD::SETOGT, Ty, Expand);
239 setCondCodeAction(ISD::SETUGE, Ty, Expand);
240 setCondCodeAction(ISD::SETUGT, Ty, Expand);
241 setCondCodeAction(ISD::SETGE, Ty, Expand);
242 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000243 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000244}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000245
246bool
247MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
248 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
249
250 switch (SVT) {
251 case MVT::i64:
252 case MVT::i32:
253 if (Fast)
254 *Fast = true;
255 return true;
256 default:
257 return false;
258 }
259}
260
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000261SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
262 SelectionDAG &DAG) const {
263 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000264 case ISD::LOAD: return lowerLOAD(Op, DAG);
265 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000266 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
267 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
268 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
269 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
270 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
271 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000272 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
273 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000274 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
275 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000276 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000277 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000278 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000279 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000280 }
281
282 return MipsTargetLowering::LowerOperation(Op, DAG);
283}
284
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000285// selectMADD -
286// Transforms a subgraph in CurDAG if the following pattern is found:
287// (addc multLo, Lo0), (adde multHi, Hi0),
288// where,
289// multHi/Lo: product of multiplication
290// Lo0: initial value of Lo register
291// Hi0: initial value of Hi register
292// Return true if pattern matching was successful.
293static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
294 // ADDENode's second operand must be a flag output of an ADDC node in order
295 // for the matching to be successful.
296 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
297
298 if (ADDCNode->getOpcode() != ISD::ADDC)
299 return false;
300
301 SDValue MultHi = ADDENode->getOperand(0);
302 SDValue MultLo = ADDCNode->getOperand(0);
303 SDNode *MultNode = MultHi.getNode();
304 unsigned MultOpc = MultHi.getOpcode();
305
306 // MultHi and MultLo must be generated by the same node,
307 if (MultLo.getNode() != MultNode)
308 return false;
309
310 // and it must be a multiplication.
311 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
312 return false;
313
314 // MultLo amd MultHi must be the first and second output of MultNode
315 // respectively.
316 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
317 return false;
318
319 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
320 // of the values of MultNode, in which case MultNode will be removed in later
321 // phases.
322 // If there exist users other than ADDENode or ADDCNode, this function returns
323 // here, which will result in MultNode being mapped to a single MULT
324 // instruction node rather than a pair of MULT and MADD instructions being
325 // produced.
326 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
327 return false;
328
Andrew Trickef9de2a2013-05-25 02:42:55 +0000329 SDLoc DL(ADDENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000330
331 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000332 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000333 ADDCNode->getOperand(1),
334 ADDENode->getOperand(1));
335
336 // create MipsMAdd(u) node
337 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
338
339 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
340 MultNode->getOperand(0),// Factor 0
341 MultNode->getOperand(1),// Factor 1
342 ACCIn);
343
344 // replace uses of adde and addc here
345 if (!SDValue(ADDCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000346 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000347 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
348 }
349 if (!SDValue(ADDENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000350 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000351 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
352 }
353
354 return true;
355}
356
357// selectMSUB -
358// Transforms a subgraph in CurDAG if the following pattern is found:
359// (addc Lo0, multLo), (sube Hi0, multHi),
360// where,
361// multHi/Lo: product of multiplication
362// Lo0: initial value of Lo register
363// Hi0: initial value of Hi register
364// Return true if pattern matching was successful.
365static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
366 // SUBENode's second operand must be a flag output of an SUBC node in order
367 // for the matching to be successful.
368 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
369
370 if (SUBCNode->getOpcode() != ISD::SUBC)
371 return false;
372
373 SDValue MultHi = SUBENode->getOperand(1);
374 SDValue MultLo = SUBCNode->getOperand(1);
375 SDNode *MultNode = MultHi.getNode();
376 unsigned MultOpc = MultHi.getOpcode();
377
378 // MultHi and MultLo must be generated by the same node,
379 if (MultLo.getNode() != MultNode)
380 return false;
381
382 // and it must be a multiplication.
383 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
384 return false;
385
386 // MultLo amd MultHi must be the first and second output of MultNode
387 // respectively.
388 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
389 return false;
390
391 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
392 // of the values of MultNode, in which case MultNode will be removed in later
393 // phases.
394 // If there exist users other than SUBENode or SUBCNode, this function returns
395 // here, which will result in MultNode being mapped to a single MULT
396 // instruction node rather than a pair of MULT and MSUB instructions being
397 // produced.
398 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
399 return false;
400
Andrew Trickef9de2a2013-05-25 02:42:55 +0000401 SDLoc DL(SUBENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000402
403 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000404 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000405 SUBCNode->getOperand(0),
406 SUBENode->getOperand(0));
407
408 // create MipsSub(u) node
409 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
410
411 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
412 MultNode->getOperand(0),// Factor 0
413 MultNode->getOperand(1),// Factor 1
414 ACCIn);
415
416 // replace uses of sube and subc here
417 if (!SDValue(SUBCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000418 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000419 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
420 }
421 if (!SDValue(SUBENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000422 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000423 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
424 }
425
426 return true;
427}
428
429static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
430 TargetLowering::DAGCombinerInfo &DCI,
431 const MipsSubtarget *Subtarget) {
432 if (DCI.isBeforeLegalize())
433 return SDValue();
434
435 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
436 selectMADD(N, &DAG))
437 return SDValue(N, 0);
438
439 return SDValue();
440}
441
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000442// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
443//
444// Performs the following transformations:
445// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
446// sign/zero-extension is completely overwritten by the new one performed by
447// the ISD::AND.
448// - Removes redundant zero extensions performed by an ISD::AND.
449static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
450 TargetLowering::DAGCombinerInfo &DCI,
451 const MipsSubtarget *Subtarget) {
452 if (!Subtarget->hasMSA())
453 return SDValue();
454
455 SDValue Op0 = N->getOperand(0);
456 SDValue Op1 = N->getOperand(1);
457 unsigned Op0Opcode = Op0->getOpcode();
458
459 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
460 // where $d + 1 == 2^n and n == 32
461 // or $d + 1 == 2^n and n <= 32 and ZExt
462 // -> (MipsVExtractZExt $a, $b, $c)
463 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
464 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
465 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
466
467 if (!Mask)
468 return SDValue();
469
470 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
471
472 if (Log2IfPositive <= 0)
473 return SDValue(); // Mask+1 is not a power of 2
474
475 SDValue Op0Op2 = Op0->getOperand(2);
476 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
477 unsigned ExtendTySize = ExtendTy.getSizeInBits();
478 unsigned Log2 = Log2IfPositive;
479
480 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
481 Log2 == ExtendTySize) {
482 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
483 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
484 Op0->getVTList(), Ops, Op0->getNumOperands());
485 return Op0;
486 }
487 }
488
489 return SDValue();
490}
491
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000492// Determine if the specified node is a constant vector splat.
493//
494// Returns true and sets Imm if:
495// * N is a ISD::BUILD_VECTOR representing a constant splat
496//
497// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
498// differences are that it assumes the MSA has already been checked and the
499// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
500// must not be in order for binsri.d to be selectable).
501static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
502 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
503
504 if (Node == NULL)
505 return false;
506
507 APInt SplatValue, SplatUndef;
508 unsigned SplatBitSize;
509 bool HasAnyUndefs;
510
511 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
512 8, !IsLittleEndian))
513 return false;
514
515 Imm = SplatValue;
516
517 return true;
518}
519
Daniel Sandersab94b532013-10-30 15:20:38 +0000520// Test whether the given node is an all-ones build_vector.
521static bool isVectorAllOnes(SDValue N) {
522 // Look through bitcasts. Endianness doesn't matter because we are looking
523 // for an all-ones value.
524 if (N->getOpcode() == ISD::BITCAST)
525 N = N->getOperand(0);
526
527 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
528
529 if (!BVN)
530 return false;
531
532 APInt SplatValue, SplatUndef;
533 unsigned SplatBitSize;
534 bool HasAnyUndefs;
535
536 // Endianness doesn't matter in this context because we are looking for
537 // an all-ones value.
538 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
539 return SplatValue.isAllOnesValue();
540
541 return false;
542}
543
544// Test whether N is the bitwise inverse of OfNode.
545static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
546 if (N->getOpcode() != ISD::XOR)
547 return false;
548
549 if (isVectorAllOnes(N->getOperand(0)))
550 return N->getOperand(1) == OfNode;
551
552 if (isVectorAllOnes(N->getOperand(1)))
553 return N->getOperand(0) == OfNode;
554
555 return false;
556}
557
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000558// Perform combines where ISD::OR is the root node.
559//
560// Performs the following transformations:
561// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
562// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
563// vector type.
564static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
565 TargetLowering::DAGCombinerInfo &DCI,
566 const MipsSubtarget *Subtarget) {
567 if (!Subtarget->hasMSA())
568 return SDValue();
569
570 EVT Ty = N->getValueType(0);
571
572 if (!Ty.is128BitVector())
573 return SDValue();
574
575 SDValue Op0 = N->getOperand(0);
576 SDValue Op1 = N->getOperand(1);
577
578 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
579 SDValue Op0Op0 = Op0->getOperand(0);
580 SDValue Op0Op1 = Op0->getOperand(1);
581 SDValue Op1Op0 = Op1->getOperand(0);
582 SDValue Op1Op1 = Op1->getOperand(1);
583 bool IsLittleEndian = !Subtarget->isLittle();
584
585 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000586 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000587 APInt Mask, InvMask;
588
589 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
590 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
591 // looking.
592 // IfClr will be set if we find a valid match.
593 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
594 Cond = Op0Op0;
595 IfSet = Op0Op1;
596
597 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) && Mask == ~InvMask)
598 IfClr = Op1Op1;
599 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) && Mask == ~InvMask)
600 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000601
602 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000603 }
604
605 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
606 // thing again using this mask.
607 // IfClr will be set if we find a valid match.
608 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
609 Cond = Op0Op1;
610 IfSet = Op0Op0;
611
612 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) && Mask == ~InvMask)
613 IfClr = Op1Op1;
614 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) && Mask == ~InvMask)
615 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000616
617 IsConstantMask = true;
618 }
619
620 // If IfClr is not yet set, try looking for a non-constant match.
621 // IfClr will be set if we find a valid match amongst the eight
622 // possibilities.
623 if (!IfClr.getNode()) {
624 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
625 Cond = Op1Op0;
626 IfSet = Op1Op1;
627 IfClr = Op0Op1;
628 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
629 Cond = Op1Op0;
630 IfSet = Op1Op1;
631 IfClr = Op0Op0;
632 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
633 Cond = Op1Op1;
634 IfSet = Op1Op0;
635 IfClr = Op0Op1;
636 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
637 Cond = Op1Op1;
638 IfSet = Op1Op0;
639 IfClr = Op0Op0;
640 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
641 Cond = Op0Op0;
642 IfSet = Op0Op1;
643 IfClr = Op1Op1;
644 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
645 Cond = Op0Op0;
646 IfSet = Op0Op1;
647 IfClr = Op1Op0;
648 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
649 Cond = Op0Op1;
650 IfSet = Op0Op0;
651 IfClr = Op1Op1;
652 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
653 Cond = Op0Op1;
654 IfSet = Op0Op0;
655 IfClr = Op1Op0;
656 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000657 }
658
659 // At this point, IfClr will be set if we have a valid match.
660 if (!IfClr.getNode())
661 return SDValue();
662
663 assert(Cond.getNode() && IfSet.getNode());
664
665 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000666 if (IsConstantMask) {
667 if (Mask.isAllOnesValue())
668 return IfSet;
669 else if (Mask == 0)
670 return IfClr;
671 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000672
673 // Transform the DAG into an equivalent VSELECT.
674 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfClr, IfSet);
675 }
676
677 return SDValue();
678}
679
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000680static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
681 TargetLowering::DAGCombinerInfo &DCI,
682 const MipsSubtarget *Subtarget) {
683 if (DCI.isBeforeLegalize())
684 return SDValue();
685
686 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
687 selectMSUB(N, &DAG))
688 return SDValue(N, 0);
689
690 return SDValue();
691}
692
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000693static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
694 EVT ShiftTy, SelectionDAG &DAG) {
695 // Clear the upper (64 - VT.sizeInBits) bits.
696 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
697
698 // Return 0.
699 if (C == 0)
700 return DAG.getConstant(0, VT);
701
702 // Return x.
703 if (C == 1)
704 return X;
705
706 // If c is power of 2, return (shl x, log2(c)).
707 if (isPowerOf2_64(C))
708 return DAG.getNode(ISD::SHL, DL, VT, X,
709 DAG.getConstant(Log2_64(C), ShiftTy));
710
711 unsigned Log2Ceil = Log2_64_Ceil(C);
712 uint64_t Floor = 1LL << Log2_64(C);
713 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
714
715 // If |c - floor_c| <= |c - ceil_c|,
716 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
717 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
718 if (C - Floor <= Ceil - C) {
719 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
720 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
721 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
722 }
723
724 // If |c - floor_c| > |c - ceil_c|,
725 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
726 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
727 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
728 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
729}
730
731static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
732 const TargetLowering::DAGCombinerInfo &DCI,
733 const MipsSETargetLowering *TL) {
734 EVT VT = N->getValueType(0);
735
736 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
737 if (!VT.isVector())
738 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
739 VT, TL->getScalarShiftAmountTy(VT), DAG);
740
741 return SDValue(N, 0);
742}
743
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000744static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
745 SelectionDAG &DAG,
746 const MipsSubtarget *Subtarget) {
747 // See if this is a vector splat immediate node.
748 APInt SplatValue, SplatUndef;
749 unsigned SplatBitSize;
750 bool HasAnyUndefs;
751 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
752 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
753
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000754 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000755 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000756 EltSize, !Subtarget->isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000757 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000758 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000759 return SDValue();
760
Andrew Trickef9de2a2013-05-25 02:42:55 +0000761 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000762 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
763}
764
765static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
766 TargetLowering::DAGCombinerInfo &DCI,
767 const MipsSubtarget *Subtarget) {
768 EVT Ty = N->getValueType(0);
769
770 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
771 return SDValue();
772
773 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
774}
775
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000776// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
777// constant splats into MipsISD::SHRA_DSP for DSPr2.
778//
779// Performs the following transformations:
780// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
781// sign/zero-extension is completely overwritten by the new one performed by
782// the ISD::SRA and ISD::SHL nodes.
783// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
784// sequence.
785//
786// See performDSPShiftCombine for more information about the transformation
787// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000788static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
789 TargetLowering::DAGCombinerInfo &DCI,
790 const MipsSubtarget *Subtarget) {
791 EVT Ty = N->getValueType(0);
792
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000793 if (Subtarget->hasMSA()) {
794 SDValue Op0 = N->getOperand(0);
795 SDValue Op1 = N->getOperand(1);
796
797 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
798 // where $d + sizeof($c) == 32
799 // or $d + sizeof($c) <= 32 and SExt
800 // -> (MipsVExtractSExt $a, $b, $c)
801 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
802 SDValue Op0Op0 = Op0->getOperand(0);
803 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
804
805 if (!ShAmount)
806 return SDValue();
807
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000808 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
809 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
810 return SDValue();
811
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000812 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
813 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
814
815 if (TotalBits == 32 ||
816 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
817 TotalBits <= 32)) {
818 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
819 Op0Op0->getOperand(2) };
820 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
821 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
822 return Op0Op0;
823 }
824 }
825 }
826
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000827 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
828 return SDValue();
829
830 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
831}
832
833
834static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
835 TargetLowering::DAGCombinerInfo &DCI,
836 const MipsSubtarget *Subtarget) {
837 EVT Ty = N->getValueType(0);
838
839 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
840 return SDValue();
841
842 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
843}
844
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000845static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
846 bool IsV216 = (Ty == MVT::v2i16);
847
848 switch (CC) {
849 case ISD::SETEQ:
850 case ISD::SETNE: return true;
851 case ISD::SETLT:
852 case ISD::SETLE:
853 case ISD::SETGT:
854 case ISD::SETGE: return IsV216;
855 case ISD::SETULT:
856 case ISD::SETULE:
857 case ISD::SETUGT:
858 case ISD::SETUGE: return !IsV216;
859 default: return false;
860 }
861}
862
863static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
864 EVT Ty = N->getValueType(0);
865
866 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
867 return SDValue();
868
869 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
870 return SDValue();
871
Andrew Trickef9de2a2013-05-25 02:42:55 +0000872 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000873 N->getOperand(1), N->getOperand(2));
874}
875
876static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
877 EVT Ty = N->getValueType(0);
878
Daniel Sanders3ce56622013-09-24 12:18:31 +0000879 if (Ty.is128BitVector() && Ty.isInteger()) {
880 // Try the following combines:
881 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
882 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
883 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
884 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
885 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
886 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
887 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
888 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
889 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
890 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
891 // legalizer.
892 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000893
Daniel Sanders3ce56622013-09-24 12:18:31 +0000894 if (Op0->getOpcode() != ISD::SETCC)
895 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000896
Daniel Sanders3ce56622013-09-24 12:18:31 +0000897 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
898 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000899
Daniel Sanders3ce56622013-09-24 12:18:31 +0000900 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
901 Signed = true;
902 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
903 Signed = false;
904 else
905 return SDValue();
906
907 SDValue Op1 = N->getOperand(1);
908 SDValue Op2 = N->getOperand(2);
909 SDValue Op0Op0 = Op0->getOperand(0);
910 SDValue Op0Op1 = Op0->getOperand(1);
911
912 if (Op1 == Op0Op0 && Op2 == Op0Op1)
913 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
914 Ty, Op1, Op2);
915 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
916 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
917 Ty, Op1, Op2);
918 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
919 SDValue SetCC = N->getOperand(0);
920
921 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
922 return SDValue();
923
924 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
925 SetCC.getOperand(0), SetCC.getOperand(1),
926 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
927 }
928
929 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000930}
931
Daniel Sandersf7456c72013-09-23 13:22:24 +0000932static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
933 const MipsSubtarget *Subtarget) {
934 EVT Ty = N->getValueType(0);
935
936 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
937 // Try the following combines:
938 // (xor (or $a, $b), (build_vector allones))
939 // (xor (or $a, $b), (bitcast (build_vector allones)))
940 SDValue Op0 = N->getOperand(0);
941 SDValue Op1 = N->getOperand(1);
942 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000943
944 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
945 NotOp = Op1;
946 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
947 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000948 else
949 return SDValue();
950
951 if (NotOp->getOpcode() == ISD::OR)
952 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
953 NotOp->getOperand(1));
954 }
955
956 return SDValue();
957}
958
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000959SDValue
960MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
961 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000962 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000963
964 switch (N->getOpcode()) {
965 case ISD::ADDE:
966 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000967 case ISD::AND:
968 Val = performANDCombine(N, DAG, DCI, Subtarget);
969 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000970 case ISD::OR:
971 Val = performORCombine(N, DAG, DCI, Subtarget);
972 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000973 case ISD::SUBE:
974 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000975 case ISD::MUL:
976 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000977 case ISD::SHL:
978 return performSHLCombine(N, DAG, DCI, Subtarget);
979 case ISD::SRA:
980 return performSRACombine(N, DAG, DCI, Subtarget);
981 case ISD::SRL:
982 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000983 case ISD::VSELECT:
984 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000985 case ISD::XOR:
986 Val = performXORCombine(N, DAG, Subtarget);
987 break;
988 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000989 Val = performSETCCCombine(N, DAG);
990 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000991 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000992
Daniel Sanders62aeab82013-10-30 13:31:27 +0000993 if (Val.getNode()) {
994 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
995 N->printrWithDepth(dbgs(), &DAG);
996 dbgs() << "\n=> \n";
997 Val.getNode()->printrWithDepth(dbgs(), &DAG);
998 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000999 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001000 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001001
1002 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001003}
1004
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001005MachineBasicBlock *
1006MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1007 MachineBasicBlock *BB) const {
1008 switch (MI->getOpcode()) {
1009 default:
1010 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1011 case Mips::BPOSGE32_PSEUDO:
1012 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001013 case Mips::SNZ_B_PSEUDO:
1014 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1015 case Mips::SNZ_H_PSEUDO:
1016 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1017 case Mips::SNZ_W_PSEUDO:
1018 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1019 case Mips::SNZ_D_PSEUDO:
1020 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1021 case Mips::SNZ_V_PSEUDO:
1022 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1023 case Mips::SZ_B_PSEUDO:
1024 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1025 case Mips::SZ_H_PSEUDO:
1026 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1027 case Mips::SZ_W_PSEUDO:
1028 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1029 case Mips::SZ_D_PSEUDO:
1030 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1031 case Mips::SZ_V_PSEUDO:
1032 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001033 case Mips::COPY_FW_PSEUDO:
1034 return emitCOPY_FW(MI, BB);
1035 case Mips::COPY_FD_PSEUDO:
1036 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001037 case Mips::INSERT_FW_PSEUDO:
1038 return emitINSERT_FW(MI, BB);
1039 case Mips::INSERT_FD_PSEUDO:
1040 return emitINSERT_FD(MI, BB);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001041 case Mips::FILL_FW_PSEUDO:
1042 return emitFILL_FW(MI, BB);
1043 case Mips::FILL_FD_PSEUDO:
1044 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001045 case Mips::FEXP2_W_1_PSEUDO:
1046 return emitFEXP2_W_1(MI, BB);
1047 case Mips::FEXP2_D_1_PSEUDO:
1048 return emitFEXP2_D_1(MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001049 }
1050}
1051
1052bool MipsSETargetLowering::
1053isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
1054 unsigned NextStackOffset,
1055 const MipsFunctionInfo& FI) const {
1056 if (!EnableMipsTailCalls)
1057 return false;
1058
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001059 // Return false if either the callee or caller has a byval argument.
1060 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
1061 return false;
1062
1063 // Return true if the callee's argument area is no larger than the
1064 // caller's.
1065 return NextStackOffset <= FI.getIncomingArgSize();
1066}
1067
1068void MipsSETargetLowering::
1069getOpndList(SmallVectorImpl<SDValue> &Ops,
1070 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
1071 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
1072 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
1073 // T9 should contain the address of the callee function if
1074 // -reloction-model=pic or it is an indirect call.
1075 if (IsPICCall || !GlobalOrExternal) {
1076 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
1077 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
1078 } else
1079 Ops.push_back(Callee);
1080
1081 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
1082 InternalLinkage, CLI, Callee, Chain);
1083}
1084
Akira Hatanaka63791212013-09-07 00:52:30 +00001085SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1086 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1087
1088 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1089 return MipsTargetLowering::lowerLOAD(Op, DAG);
1090
1091 // Replace a double precision load with two i32 loads and a buildpair64.
1092 SDLoc DL(Op);
1093 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1094 EVT PtrVT = Ptr.getValueType();
1095
1096 // i32 load from lower address.
1097 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1098 MachinePointerInfo(), Nd.isVolatile(),
1099 Nd.isNonTemporal(), Nd.isInvariant(),
1100 Nd.getAlignment());
1101
1102 // i32 load from higher address.
1103 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1104 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1105 MachinePointerInfo(), Nd.isVolatile(),
1106 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001107 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka63791212013-09-07 00:52:30 +00001108
1109 if (!Subtarget->isLittle())
1110 std::swap(Lo, Hi);
1111
1112 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1113 SDValue Ops[2] = {BP, Hi.getValue(1)};
1114 return DAG.getMergeValues(Ops, 2, DL);
1115}
1116
1117SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1118 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1119
1120 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1121 return MipsTargetLowering::lowerSTORE(Op, DAG);
1122
1123 // Replace a double precision store with two extractelement64s and i32 stores.
1124 SDLoc DL(Op);
1125 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1126 EVT PtrVT = Ptr.getValueType();
1127 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1128 Val, DAG.getConstant(0, MVT::i32));
1129 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1130 Val, DAG.getConstant(1, MVT::i32));
1131
1132 if (!Subtarget->isLittle())
1133 std::swap(Lo, Hi);
1134
1135 // i32 store to lower address.
1136 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1137 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
1138 Nd.getTBAAInfo());
1139
1140 // i32 store to higher address.
1141 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1142 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001143 Nd.isVolatile(), Nd.isNonTemporal(),
1144 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001145}
1146
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001147SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1148 bool HasLo, bool HasHi,
1149 SelectionDAG &DAG) const {
1150 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001151 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001152 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1153 Op.getOperand(0), Op.getOperand(1));
1154 SDValue Lo, Hi;
1155
1156 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001157 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001158 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001159 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001160
1161 if (!HasLo || !HasHi)
1162 return HasLo ? Lo : Hi;
1163
1164 SDValue Vals[] = { Lo, Hi };
1165 return DAG.getMergeValues(Vals, 2, DL);
1166}
1167
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001168
Andrew Trickef9de2a2013-05-25 02:42:55 +00001169static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001170 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1171 DAG.getConstant(0, MVT::i32));
1172 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1173 DAG.getConstant(1, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001174 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001175}
1176
Andrew Trickef9de2a2013-05-25 02:42:55 +00001177static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001178 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1179 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001180 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1181}
1182
1183// This function expands mips intrinsic nodes which have 64-bit input operands
1184// or output values.
1185//
1186// out64 = intrinsic-node in64
1187// =>
1188// lo = copy (extract-element (in64, 0))
1189// hi = copy (extract-element (in64, 1))
1190// mips-specific-node
1191// v0 = copy lo
1192// v1 = copy hi
1193// out64 = merge-values (v0, v1)
1194//
1195static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001196 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001197 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1198 SmallVector<SDValue, 3> Ops;
1199 unsigned OpNo = 0;
1200
1201 // See if Op has a chain input.
1202 if (HasChainIn)
1203 Ops.push_back(Op->getOperand(OpNo++));
1204
1205 // The next operand is the intrinsic opcode.
1206 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1207
1208 // See if the next operand has type i64.
1209 SDValue Opnd = Op->getOperand(++OpNo), In64;
1210
1211 if (Opnd.getValueType() == MVT::i64)
1212 In64 = initAccumulator(Opnd, DL, DAG);
1213 else
1214 Ops.push_back(Opnd);
1215
1216 // Push the remaining operands.
1217 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1218 Ops.push_back(Op->getOperand(OpNo));
1219
1220 // Add In64 to the end of the list.
1221 if (In64.getNode())
1222 Ops.push_back(In64);
1223
1224 // Scan output.
1225 SmallVector<EVT, 2> ResTys;
1226
1227 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1228 I != E; ++I)
1229 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1230
1231 // Create node.
1232 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1233 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1234
1235 if (!HasChainIn)
1236 return Out;
1237
1238 assert(Val->getValueType(1) == MVT::Other);
1239 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1240 return DAG.getMergeValues(Vals, 2, DL);
1241}
1242
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001243// Lower an MSA copy intrinsic into the specified SelectionDAG node
1244static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1245 SDLoc DL(Op);
1246 SDValue Vec = Op->getOperand(1);
1247 SDValue Idx = Op->getOperand(2);
1248 EVT ResTy = Op->getValueType(0);
1249 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1250
1251 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1252 DAG.getValueType(EltTy));
1253
1254 return Result;
1255}
1256
Daniel Sanders50b80412013-11-15 12:56:49 +00001257static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1258 EVT ResVecTy = Op->getValueType(0);
1259 EVT ViaVecTy = ResVecTy;
1260 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001261
Daniel Sanders50b80412013-11-15 12:56:49 +00001262 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1263 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1264 // lanes.
1265 SDValue LaneA;
1266 SDValue LaneB = Op->getOperand(2);
1267
1268 if (ResVecTy == MVT::v2i64) {
1269 LaneA = DAG.getConstant(0, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001270 ViaVecTy = MVT::v4i32;
Daniel Sanders50b80412013-11-15 12:56:49 +00001271 } else
1272 LaneA = LaneB;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001273
Daniel Sanders50b80412013-11-15 12:56:49 +00001274 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1275 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001276
Daniel Sanders50b80412013-11-15 12:56:49 +00001277 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, Ops,
1278 ViaVecTy.getVectorNumElements());
1279
1280 if (ViaVecTy != ResVecTy)
1281 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001282
1283 return Result;
1284}
1285
Daniel Sanders50b80412013-11-15 12:56:49 +00001286static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1287 return DAG.getConstant(Op->getConstantOperandVal(ImmOp), Op->getValueType(0));
1288}
1289
1290static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1291 bool BigEndian, SelectionDAG &DAG) {
1292 EVT ViaVecTy = VecTy;
1293 SDValue SplatValueA = SplatValue;
1294 SDValue SplatValueB = SplatValue;
1295 SDLoc DL(SplatValue);
1296
1297 if (VecTy == MVT::v2i64) {
1298 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1299 ViaVecTy = MVT::v4i32;
1300
1301 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1302 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
1303 DAG.getConstant(32, MVT::i32));
1304 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1305 }
1306
1307 // We currently hold the parts in little endian order. Swap them if
1308 // necessary.
1309 if (BigEndian)
1310 std::swap(SplatValueA, SplatValueB);
1311
1312 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1313 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1314 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1315 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1316
1317 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, Ops,
1318 ViaVecTy.getVectorNumElements());
1319
1320 if (VecTy != ViaVecTy)
1321 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1322
1323 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001324}
1325
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001326static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1327 unsigned Opc, SDValue Imm,
1328 bool BigEndian) {
1329 EVT VecTy = Op->getValueType(0);
1330 SDValue Exp2Imm;
1331 SDLoc DL(Op);
1332
Daniel Sanders50b80412013-11-15 12:56:49 +00001333 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1334 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001335 if (VecTy == MVT::v2i64) {
1336 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1337 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1338
1339 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001340 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), MVT::i32);
1341
1342 if (BigEndian)
1343 std::swap(BitImmLoOp, BitImmHiOp);
1344
1345 Exp2Imm =
1346 DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
1347 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, BitImmLoOp,
1348 BitImmHiOp, BitImmLoOp, BitImmHiOp));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001349 }
1350 }
1351
1352 if (Exp2Imm.getNode() == NULL) {
1353 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001354
1355 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1356 // only values 0-63 are valid.
1357 if (VecTy == MVT::v2i64)
1358 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1359
1360 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1361
1362 Exp2Imm =
1363 DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, VecTy), Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001364 }
1365
1366 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1367}
1368
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001369static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1370 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001371 SDLoc DL(Op);
Daniel Sanders50b80412013-11-15 12:56:49 +00001372 SDValue One = DAG.getConstant(1, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001373 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
1374
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001375 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1376 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001377}
1378
1379static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1380 SDLoc DL(Op);
1381 EVT ResTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001382 APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
1383 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
1384 SDValue BitMask = DAG.getConstant(~BitImm, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001385
1386 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1387}
1388
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001389SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1390 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001391 SDLoc DL(Op);
1392
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001393 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1394 default:
1395 return SDValue();
1396 case Intrinsic::mips_shilo:
1397 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1398 case Intrinsic::mips_dpau_h_qbl:
1399 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1400 case Intrinsic::mips_dpau_h_qbr:
1401 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1402 case Intrinsic::mips_dpsu_h_qbl:
1403 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1404 case Intrinsic::mips_dpsu_h_qbr:
1405 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1406 case Intrinsic::mips_dpa_w_ph:
1407 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1408 case Intrinsic::mips_dps_w_ph:
1409 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1410 case Intrinsic::mips_dpax_w_ph:
1411 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1412 case Intrinsic::mips_dpsx_w_ph:
1413 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1414 case Intrinsic::mips_mulsa_w_ph:
1415 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1416 case Intrinsic::mips_mult:
1417 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1418 case Intrinsic::mips_multu:
1419 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1420 case Intrinsic::mips_madd:
1421 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1422 case Intrinsic::mips_maddu:
1423 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1424 case Intrinsic::mips_msub:
1425 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1426 case Intrinsic::mips_msubu:
1427 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001428 case Intrinsic::mips_addv_b:
1429 case Intrinsic::mips_addv_h:
1430 case Intrinsic::mips_addv_w:
1431 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001432 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1433 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001434 case Intrinsic::mips_addvi_b:
1435 case Intrinsic::mips_addvi_h:
1436 case Intrinsic::mips_addvi_w:
1437 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001438 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1439 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001440 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001441 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1442 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001443 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001444 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1445 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001446 case Intrinsic::mips_bclr_b:
1447 case Intrinsic::mips_bclr_h:
1448 case Intrinsic::mips_bclr_w:
1449 case Intrinsic::mips_bclr_d:
1450 return lowerMSABitClear(Op, DAG);
1451 case Intrinsic::mips_bclri_b:
1452 case Intrinsic::mips_bclri_h:
1453 case Intrinsic::mips_bclri_w:
1454 case Intrinsic::mips_bclri_d:
1455 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001456 case Intrinsic::mips_binsli_b:
1457 case Intrinsic::mips_binsli_h:
1458 case Intrinsic::mips_binsli_w:
1459 case Intrinsic::mips_binsli_d: {
1460 EVT VecTy = Op->getValueType(0);
1461 EVT EltTy = VecTy.getVectorElementType();
1462 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
1463 Op->getConstantOperandVal(3));
1464 return DAG.getNode(ISD::VSELECT, DL, VecTy,
1465 DAG.getConstant(Mask, VecTy, true), Op->getOperand(1),
1466 Op->getOperand(2));
1467 }
1468 case Intrinsic::mips_binsri_b:
1469 case Intrinsic::mips_binsri_h:
1470 case Intrinsic::mips_binsri_w:
1471 case Intrinsic::mips_binsri_d: {
1472 EVT VecTy = Op->getValueType(0);
1473 EVT EltTy = VecTy.getVectorElementType();
1474 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
1475 Op->getConstantOperandVal(3));
1476 return DAG.getNode(ISD::VSELECT, DL, VecTy,
1477 DAG.getConstant(Mask, VecTy, true), Op->getOperand(1),
1478 Op->getOperand(2));
1479 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001480 case Intrinsic::mips_bmnz_v:
1481 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1482 Op->getOperand(2), Op->getOperand(1));
1483 case Intrinsic::mips_bmnzi_b:
1484 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1485 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1486 Op->getOperand(1));
1487 case Intrinsic::mips_bmz_v:
1488 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1489 Op->getOperand(1), Op->getOperand(2));
1490 case Intrinsic::mips_bmzi_b:
1491 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1492 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1493 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001494 case Intrinsic::mips_bneg_b:
1495 case Intrinsic::mips_bneg_h:
1496 case Intrinsic::mips_bneg_w:
1497 case Intrinsic::mips_bneg_d: {
1498 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001499 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001500
1501 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1502 DAG.getNode(ISD::SHL, DL, VecTy, One,
1503 Op->getOperand(2)));
1504 }
1505 case Intrinsic::mips_bnegi_b:
1506 case Intrinsic::mips_bnegi_h:
1507 case Intrinsic::mips_bnegi_w:
1508 case Intrinsic::mips_bnegi_d:
1509 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
1510 !Subtarget->isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001511 case Intrinsic::mips_bnz_b:
1512 case Intrinsic::mips_bnz_h:
1513 case Intrinsic::mips_bnz_w:
1514 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001515 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1516 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001517 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001518 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1519 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001520 case Intrinsic::mips_bsel_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001521 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sanderse1d24352013-09-24 12:04:44 +00001522 Op->getOperand(1), Op->getOperand(2),
1523 Op->getOperand(3));
1524 case Intrinsic::mips_bseli_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001525 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sanderse1d24352013-09-24 12:04:44 +00001526 Op->getOperand(1), Op->getOperand(2),
1527 lowerMSASplatImm(Op, 3, DAG));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001528 case Intrinsic::mips_bset_b:
1529 case Intrinsic::mips_bset_h:
1530 case Intrinsic::mips_bset_w:
1531 case Intrinsic::mips_bset_d: {
1532 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001533 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001534
1535 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1536 DAG.getNode(ISD::SHL, DL, VecTy, One,
1537 Op->getOperand(2)));
1538 }
1539 case Intrinsic::mips_bseti_b:
1540 case Intrinsic::mips_bseti_h:
1541 case Intrinsic::mips_bseti_w:
1542 case Intrinsic::mips_bseti_d:
1543 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
1544 !Subtarget->isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001545 case Intrinsic::mips_bz_b:
1546 case Intrinsic::mips_bz_h:
1547 case Intrinsic::mips_bz_w:
1548 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001549 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1550 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001551 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001552 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1553 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001554 case Intrinsic::mips_ceq_b:
1555 case Intrinsic::mips_ceq_h:
1556 case Intrinsic::mips_ceq_w:
1557 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001558 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001559 Op->getOperand(2), ISD::SETEQ);
1560 case Intrinsic::mips_ceqi_b:
1561 case Intrinsic::mips_ceqi_h:
1562 case Intrinsic::mips_ceqi_w:
1563 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001564 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001565 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1566 case Intrinsic::mips_cle_s_b:
1567 case Intrinsic::mips_cle_s_h:
1568 case Intrinsic::mips_cle_s_w:
1569 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001570 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001571 Op->getOperand(2), ISD::SETLE);
1572 case Intrinsic::mips_clei_s_b:
1573 case Intrinsic::mips_clei_s_h:
1574 case Intrinsic::mips_clei_s_w:
1575 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001576 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001577 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1578 case Intrinsic::mips_cle_u_b:
1579 case Intrinsic::mips_cle_u_h:
1580 case Intrinsic::mips_cle_u_w:
1581 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001582 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001583 Op->getOperand(2), ISD::SETULE);
1584 case Intrinsic::mips_clei_u_b:
1585 case Intrinsic::mips_clei_u_h:
1586 case Intrinsic::mips_clei_u_w:
1587 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001588 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001589 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1590 case Intrinsic::mips_clt_s_b:
1591 case Intrinsic::mips_clt_s_h:
1592 case Intrinsic::mips_clt_s_w:
1593 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001594 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001595 Op->getOperand(2), ISD::SETLT);
1596 case Intrinsic::mips_clti_s_b:
1597 case Intrinsic::mips_clti_s_h:
1598 case Intrinsic::mips_clti_s_w:
1599 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001600 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001601 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1602 case Intrinsic::mips_clt_u_b:
1603 case Intrinsic::mips_clt_u_h:
1604 case Intrinsic::mips_clt_u_w:
1605 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001606 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001607 Op->getOperand(2), ISD::SETULT);
1608 case Intrinsic::mips_clti_u_b:
1609 case Intrinsic::mips_clti_u_h:
1610 case Intrinsic::mips_clti_u_w:
1611 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001612 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001613 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001614 case Intrinsic::mips_copy_s_b:
1615 case Intrinsic::mips_copy_s_h:
1616 case Intrinsic::mips_copy_s_w:
1617 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001618 case Intrinsic::mips_copy_s_d:
1619 // Don't lower directly into VEXTRACT_SEXT_ELT since i64 might be illegal.
1620 // Instead lower to the generic EXTRACT_VECTOR_ELT node and let the type
1621 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1622 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1623 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001624 case Intrinsic::mips_copy_u_b:
1625 case Intrinsic::mips_copy_u_h:
1626 case Intrinsic::mips_copy_u_w:
1627 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001628 case Intrinsic::mips_copy_u_d:
1629 // Don't lower directly into VEXTRACT_ZEXT_ELT since i64 might be illegal.
1630 // Instead lower to the generic EXTRACT_VECTOR_ELT node and let the type
1631 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1632 //
1633 // Note: When i64 is illegal, this results in copy_s.w instructions instead
1634 // of copy_u.w instructions. This makes no difference to the behaviour
1635 // since i64 is only illegal when the register file is 32-bit.
1636 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1637 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001638 case Intrinsic::mips_div_s_b:
1639 case Intrinsic::mips_div_s_h:
1640 case Intrinsic::mips_div_s_w:
1641 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001642 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1643 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001644 case Intrinsic::mips_div_u_b:
1645 case Intrinsic::mips_div_u_h:
1646 case Intrinsic::mips_div_u_w:
1647 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001648 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1649 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001650 case Intrinsic::mips_fadd_w:
1651 case Intrinsic::mips_fadd_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001652 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1653 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001654 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1655 case Intrinsic::mips_fceq_w:
1656 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001657 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001658 Op->getOperand(2), ISD::SETOEQ);
1659 case Intrinsic::mips_fcle_w:
1660 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001661 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001662 Op->getOperand(2), ISD::SETOLE);
1663 case Intrinsic::mips_fclt_w:
1664 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001665 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001666 Op->getOperand(2), ISD::SETOLT);
1667 case Intrinsic::mips_fcne_w:
1668 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001669 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001670 Op->getOperand(2), ISD::SETONE);
1671 case Intrinsic::mips_fcor_w:
1672 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001673 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001674 Op->getOperand(2), ISD::SETO);
1675 case Intrinsic::mips_fcueq_w:
1676 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001677 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001678 Op->getOperand(2), ISD::SETUEQ);
1679 case Intrinsic::mips_fcule_w:
1680 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001681 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001682 Op->getOperand(2), ISD::SETULE);
1683 case Intrinsic::mips_fcult_w:
1684 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001685 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001686 Op->getOperand(2), ISD::SETULT);
1687 case Intrinsic::mips_fcun_w:
1688 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001689 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001690 Op->getOperand(2), ISD::SETUO);
1691 case Intrinsic::mips_fcune_w:
1692 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001693 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001694 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001695 case Intrinsic::mips_fdiv_w:
1696 case Intrinsic::mips_fdiv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001697 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1698 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001699 case Intrinsic::mips_ffint_u_w:
1700 case Intrinsic::mips_ffint_u_d:
1701 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1702 Op->getOperand(1));
1703 case Intrinsic::mips_ffint_s_w:
1704 case Intrinsic::mips_ffint_s_d:
1705 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1706 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001707 case Intrinsic::mips_fill_b:
1708 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001709 case Intrinsic::mips_fill_w:
1710 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001711 SmallVector<SDValue, 16> Ops;
1712 EVT ResTy = Op->getValueType(0);
1713
1714 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1715 Ops.push_back(Op->getOperand(1));
1716
Daniel Sandersc72593e2013-09-27 13:20:41 +00001717 // If ResTy is v2i64 then the type legalizer will break this node down into
1718 // an equivalent v4i32.
1719 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, &Ops[0], Ops.size());
Daniel Sandersf49dd822013-09-24 13:33:07 +00001720 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001721 case Intrinsic::mips_fexp2_w:
1722 case Intrinsic::mips_fexp2_d: {
1723 EVT ResTy = Op->getValueType(0);
1724 return DAG.getNode(
1725 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1726 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1727 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001728 case Intrinsic::mips_flog2_w:
1729 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001730 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001731 case Intrinsic::mips_fmadd_w:
1732 case Intrinsic::mips_fmadd_d:
1733 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1734 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001735 case Intrinsic::mips_fmul_w:
1736 case Intrinsic::mips_fmul_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001737 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1738 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001739 case Intrinsic::mips_fmsub_w:
1740 case Intrinsic::mips_fmsub_d: {
1741 EVT ResTy = Op->getValueType(0);
1742 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1743 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1744 Op->getOperand(2), Op->getOperand(3)));
1745 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001746 case Intrinsic::mips_frint_w:
1747 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001748 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001749 case Intrinsic::mips_fsqrt_w:
1750 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001751 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001752 case Intrinsic::mips_fsub_w:
1753 case Intrinsic::mips_fsub_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001754 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1755 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001756 case Intrinsic::mips_ftrunc_u_w:
1757 case Intrinsic::mips_ftrunc_u_d:
1758 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1759 Op->getOperand(1));
1760 case Intrinsic::mips_ftrunc_s_w:
1761 case Intrinsic::mips_ftrunc_s_d:
1762 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1763 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001764 case Intrinsic::mips_ilvev_b:
1765 case Intrinsic::mips_ilvev_h:
1766 case Intrinsic::mips_ilvev_w:
1767 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001768 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001769 Op->getOperand(1), Op->getOperand(2));
1770 case Intrinsic::mips_ilvl_b:
1771 case Intrinsic::mips_ilvl_h:
1772 case Intrinsic::mips_ilvl_w:
1773 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001774 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001775 Op->getOperand(1), Op->getOperand(2));
1776 case Intrinsic::mips_ilvod_b:
1777 case Intrinsic::mips_ilvod_h:
1778 case Intrinsic::mips_ilvod_w:
1779 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001780 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001781 Op->getOperand(1), Op->getOperand(2));
1782 case Intrinsic::mips_ilvr_b:
1783 case Intrinsic::mips_ilvr_h:
1784 case Intrinsic::mips_ilvr_w:
1785 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001786 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001787 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001788 case Intrinsic::mips_insert_b:
1789 case Intrinsic::mips_insert_h:
1790 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001791 case Intrinsic::mips_insert_d:
1792 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1793 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001794 case Intrinsic::mips_ldi_b:
1795 case Intrinsic::mips_ldi_h:
1796 case Intrinsic::mips_ldi_w:
1797 case Intrinsic::mips_ldi_d:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001798 return lowerMSASplatImm(Op, 1, DAG);
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001799 case Intrinsic::mips_lsa: {
1800 EVT ResTy = Op->getValueType(0);
1801 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1802 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1803 Op->getOperand(2), Op->getOperand(3)));
1804 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001805 case Intrinsic::mips_maddv_b:
1806 case Intrinsic::mips_maddv_h:
1807 case Intrinsic::mips_maddv_w:
1808 case Intrinsic::mips_maddv_d: {
1809 EVT ResTy = Op->getValueType(0);
1810 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1811 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1812 Op->getOperand(2), Op->getOperand(3)));
1813 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001814 case Intrinsic::mips_max_s_b:
1815 case Intrinsic::mips_max_s_h:
1816 case Intrinsic::mips_max_s_w:
1817 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001818 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1819 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001820 case Intrinsic::mips_max_u_b:
1821 case Intrinsic::mips_max_u_h:
1822 case Intrinsic::mips_max_u_w:
1823 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001824 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1825 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001826 case Intrinsic::mips_maxi_s_b:
1827 case Intrinsic::mips_maxi_s_h:
1828 case Intrinsic::mips_maxi_s_w:
1829 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001830 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1831 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001832 case Intrinsic::mips_maxi_u_b:
1833 case Intrinsic::mips_maxi_u_h:
1834 case Intrinsic::mips_maxi_u_w:
1835 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001836 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1837 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001838 case Intrinsic::mips_min_s_b:
1839 case Intrinsic::mips_min_s_h:
1840 case Intrinsic::mips_min_s_w:
1841 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001842 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1843 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001844 case Intrinsic::mips_min_u_b:
1845 case Intrinsic::mips_min_u_h:
1846 case Intrinsic::mips_min_u_w:
1847 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001848 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1849 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001850 case Intrinsic::mips_mini_s_b:
1851 case Intrinsic::mips_mini_s_h:
1852 case Intrinsic::mips_mini_s_w:
1853 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001854 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1855 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001856 case Intrinsic::mips_mini_u_b:
1857 case Intrinsic::mips_mini_u_h:
1858 case Intrinsic::mips_mini_u_w:
1859 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001860 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1861 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001862 case Intrinsic::mips_mod_s_b:
1863 case Intrinsic::mips_mod_s_h:
1864 case Intrinsic::mips_mod_s_w:
1865 case Intrinsic::mips_mod_s_d:
1866 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1867 Op->getOperand(2));
1868 case Intrinsic::mips_mod_u_b:
1869 case Intrinsic::mips_mod_u_h:
1870 case Intrinsic::mips_mod_u_w:
1871 case Intrinsic::mips_mod_u_d:
1872 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1873 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001874 case Intrinsic::mips_mulv_b:
1875 case Intrinsic::mips_mulv_h:
1876 case Intrinsic::mips_mulv_w:
1877 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001878 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1879 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001880 case Intrinsic::mips_msubv_b:
1881 case Intrinsic::mips_msubv_h:
1882 case Intrinsic::mips_msubv_w:
1883 case Intrinsic::mips_msubv_d: {
1884 EVT ResTy = Op->getValueType(0);
1885 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
1886 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1887 Op->getOperand(2), Op->getOperand(3)));
1888 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001889 case Intrinsic::mips_nlzc_b:
1890 case Intrinsic::mips_nlzc_h:
1891 case Intrinsic::mips_nlzc_w:
1892 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001893 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001894 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001895 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1896 Op->getOperand(1), Op->getOperand(2));
1897 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001898 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001899 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001900 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1901 Op->getOperand(1),
1902 lowerMSASplatImm(Op, 2, DAG));
1903 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001904 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001905 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001906 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
1907 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001908 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001909 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1910 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001911 case Intrinsic::mips_pckev_b:
1912 case Intrinsic::mips_pckev_h:
1913 case Intrinsic::mips_pckev_w:
1914 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001915 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001916 Op->getOperand(1), Op->getOperand(2));
1917 case Intrinsic::mips_pckod_b:
1918 case Intrinsic::mips_pckod_h:
1919 case Intrinsic::mips_pckod_w:
1920 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001921 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001922 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00001923 case Intrinsic::mips_pcnt_b:
1924 case Intrinsic::mips_pcnt_h:
1925 case Intrinsic::mips_pcnt_w:
1926 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001927 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sanders26307182013-09-24 14:20:00 +00001928 case Intrinsic::mips_shf_b:
1929 case Intrinsic::mips_shf_h:
1930 case Intrinsic::mips_shf_w:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001931 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00001932 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001933 case Intrinsic::mips_sll_b:
1934 case Intrinsic::mips_sll_h:
1935 case Intrinsic::mips_sll_w:
1936 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001937 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
1938 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001939 case Intrinsic::mips_slli_b:
1940 case Intrinsic::mips_slli_h:
1941 case Intrinsic::mips_slli_w:
1942 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001943 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
1944 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00001945 case Intrinsic::mips_splat_b:
1946 case Intrinsic::mips_splat_h:
1947 case Intrinsic::mips_splat_w:
1948 case Intrinsic::mips_splat_d:
1949 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
1950 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
1951 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
1952 // Instead we lower to MipsISD::VSHF and match from there.
1953 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00001954 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00001955 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00001956 case Intrinsic::mips_splati_b:
1957 case Intrinsic::mips_splati_h:
1958 case Intrinsic::mips_splati_w:
1959 case Intrinsic::mips_splati_d:
1960 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1961 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1962 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001963 case Intrinsic::mips_sra_b:
1964 case Intrinsic::mips_sra_h:
1965 case Intrinsic::mips_sra_w:
1966 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001967 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
1968 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001969 case Intrinsic::mips_srai_b:
1970 case Intrinsic::mips_srai_h:
1971 case Intrinsic::mips_srai_w:
1972 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001973 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
1974 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001975 case Intrinsic::mips_srl_b:
1976 case Intrinsic::mips_srl_h:
1977 case Intrinsic::mips_srl_w:
1978 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001979 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
1980 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001981 case Intrinsic::mips_srli_b:
1982 case Intrinsic::mips_srli_h:
1983 case Intrinsic::mips_srli_w:
1984 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001985 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
1986 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001987 case Intrinsic::mips_subv_b:
1988 case Intrinsic::mips_subv_h:
1989 case Intrinsic::mips_subv_w:
1990 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001991 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
1992 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001993 case Intrinsic::mips_subvi_b:
1994 case Intrinsic::mips_subvi_h:
1995 case Intrinsic::mips_subvi_w:
1996 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001997 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
1998 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00001999 case Intrinsic::mips_vshf_b:
2000 case Intrinsic::mips_vshf_h:
2001 case Intrinsic::mips_vshf_w:
2002 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002003 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002004 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002005 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002006 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2007 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002008 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002009 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2010 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002011 }
2012}
2013
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002014static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2015 SDLoc DL(Op);
2016 SDValue ChainIn = Op->getOperand(0);
2017 SDValue Address = Op->getOperand(2);
2018 SDValue Offset = Op->getOperand(3);
2019 EVT ResTy = Op->getValueType(0);
2020 EVT PtrTy = Address->getValueType(0);
2021
2022 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2023
2024 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
2025 false, false, 16);
2026}
2027
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002028SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2029 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002030 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2031 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002032 default:
2033 return SDValue();
2034 case Intrinsic::mips_extp:
2035 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2036 case Intrinsic::mips_extpdp:
2037 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2038 case Intrinsic::mips_extr_w:
2039 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2040 case Intrinsic::mips_extr_r_w:
2041 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2042 case Intrinsic::mips_extr_rs_w:
2043 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2044 case Intrinsic::mips_extr_s_h:
2045 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2046 case Intrinsic::mips_mthlip:
2047 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2048 case Intrinsic::mips_mulsaq_s_w_ph:
2049 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2050 case Intrinsic::mips_maq_s_w_phl:
2051 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2052 case Intrinsic::mips_maq_s_w_phr:
2053 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2054 case Intrinsic::mips_maq_sa_w_phl:
2055 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2056 case Intrinsic::mips_maq_sa_w_phr:
2057 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2058 case Intrinsic::mips_dpaq_s_w_ph:
2059 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2060 case Intrinsic::mips_dpsq_s_w_ph:
2061 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2062 case Intrinsic::mips_dpaq_sa_l_w:
2063 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2064 case Intrinsic::mips_dpsq_sa_l_w:
2065 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2066 case Intrinsic::mips_dpaqx_s_w_ph:
2067 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2068 case Intrinsic::mips_dpaqx_sa_w_ph:
2069 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2070 case Intrinsic::mips_dpsqx_s_w_ph:
2071 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2072 case Intrinsic::mips_dpsqx_sa_w_ph:
2073 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002074 case Intrinsic::mips_ld_b:
2075 case Intrinsic::mips_ld_h:
2076 case Intrinsic::mips_ld_w:
2077 case Intrinsic::mips_ld_d:
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002078 return lowerMSALoadIntr(Op, DAG, Intr);
2079 }
2080}
2081
2082static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2083 SDLoc DL(Op);
2084 SDValue ChainIn = Op->getOperand(0);
2085 SDValue Value = Op->getOperand(2);
2086 SDValue Address = Op->getOperand(3);
2087 SDValue Offset = Op->getOperand(4);
2088 EVT PtrTy = Address->getValueType(0);
2089
2090 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2091
2092 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
2093 false, 16);
2094}
2095
2096SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2097 SelectionDAG &DAG) const {
2098 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2099 switch (Intr) {
2100 default:
2101 return SDValue();
2102 case Intrinsic::mips_st_b:
2103 case Intrinsic::mips_st_h:
2104 case Intrinsic::mips_st_w:
2105 case Intrinsic::mips_st_d:
Daniel Sandersce09d072013-08-28 12:14:50 +00002106 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002107 }
2108}
2109
Daniel Sanders7a289d02013-09-23 12:02:46 +00002110/// \brief Check if the given BuildVectorSDNode is a splat.
2111/// This method currently relies on DAG nodes being reused when equivalent,
2112/// so it's possible for this to return false even when isConstantSplat returns
2113/// true.
2114static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002115 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002116 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002117
2118 SDValue Operand0 = N->getOperand(0);
2119
2120 for (unsigned int i = 1; i < nOps; ++i) {
2121 if (N->getOperand(i) != Operand0)
2122 return false;
2123 }
2124
2125 return true;
2126}
2127
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002128// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2129//
2130// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2131// choose to sign-extend but we could have equally chosen zero-extend. The
2132// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2133// result into this node later (possibly changing it to a zero-extend in the
2134// process).
2135SDValue MipsSETargetLowering::
2136lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2137 SDLoc DL(Op);
2138 EVT ResTy = Op->getValueType(0);
2139 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002140 EVT VecTy = Op0->getValueType(0);
2141
2142 if (!VecTy.is128BitVector())
2143 return SDValue();
2144
2145 if (ResTy.isInteger()) {
2146 SDValue Op1 = Op->getOperand(1);
2147 EVT EltTy = VecTy.getVectorElementType();
2148 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2149 DAG.getValueType(EltTy));
2150 }
2151
2152 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002153}
2154
Daniel Sandersf49dd822013-09-24 13:33:07 +00002155static bool isConstantOrUndef(const SDValue Op) {
2156 if (Op->getOpcode() == ISD::UNDEF)
2157 return true;
2158 if (dyn_cast<ConstantSDNode>(Op))
2159 return true;
2160 if (dyn_cast<ConstantFPSDNode>(Op))
2161 return true;
2162 return false;
2163}
2164
2165static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2166 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2167 if (isConstantOrUndef(Op->getOperand(i)))
2168 return true;
2169 return false;
2170}
2171
Daniel Sanders7a289d02013-09-23 12:02:46 +00002172// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2173// backend.
2174//
2175// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002176// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2177// 2 less than or equal to 64 and the value fits into a signed 10-bit
2178// immediate
2179// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2180// is a power of 2 less than or equal to 64 and the value does not fit into a
2181// signed 10-bit immediate
2182// - Non-constant splats are legal as-is.
2183// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2184// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002185SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2186 SelectionDAG &DAG) const {
2187 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2188 EVT ResTy = Op->getValueType(0);
2189 SDLoc DL(Op);
2190 APInt SplatValue, SplatUndef;
2191 unsigned SplatBitSize;
2192 bool HasAnyUndefs;
2193
2194 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
2195 return SDValue();
2196
2197 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2198 HasAnyUndefs, 8,
Daniel Sandersf49dd822013-09-24 13:33:07 +00002199 !Subtarget->isLittle()) && SplatBitSize <= 64) {
2200 // We can only cope with 8, 16, 32, or 64-bit elements
2201 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2202 SplatBitSize != 64)
2203 return SDValue();
2204
2205 // If the value fits into a simm10 then we can use ldi.[bhwd]
2206 if (SplatValue.isSignedIntN(10))
2207 return Op;
2208
2209 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002210
2211 switch (SplatBitSize) {
2212 default:
2213 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002214 case 8:
2215 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002216 break;
2217 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002218 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002219 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002220 case 32:
2221 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002222 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002223 case 64:
2224 // There's no fill.d to fall back on for 64-bit values
2225 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002226 }
2227
Daniel Sanders50b80412013-11-15 12:56:49 +00002228 // SelectionDAG::getConstant will promote SplatValue appropriately.
2229 SDValue Result = DAG.getConstant(SplatValue, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002230
Daniel Sanders50b80412013-11-15 12:56:49 +00002231 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002232 if (ViaVecTy != ResTy)
2233 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002234
2235 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002236 } else if (isSplatVector(Node))
2237 return Op;
2238 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002239 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2240 // The resulting code is the same length as the expansion, but it doesn't
2241 // use memory operations
2242 EVT ResTy = Node->getValueType(0);
2243
2244 assert(ResTy.isVector());
2245
2246 unsigned NumElts = ResTy.getVectorNumElements();
2247 SDValue Vector = DAG.getUNDEF(ResTy);
2248 for (unsigned i = 0; i < NumElts; ++i) {
2249 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2250 Node->getOperand(i),
2251 DAG.getConstant(i, MVT::i32));
2252 }
2253 return Vector;
2254 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002255
2256 return SDValue();
2257}
2258
Daniel Sanders26307182013-09-24 14:20:00 +00002259// Lower VECTOR_SHUFFLE into SHF (if possible).
2260//
2261// SHF splits the vector into blocks of four elements, then shuffles these
2262// elements according to a <4 x i2> constant (encoded as an integer immediate).
2263//
2264// It is therefore possible to lower into SHF when the mask takes the form:
2265// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2266// When undef's appear they are treated as if they were whatever value is
2267// necessary in order to fit the above form.
2268//
2269// For example:
2270// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2271// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2272// i32 7, i32 6, i32 5, i32 4>
2273// is lowered to:
2274// (SHF_H $w0, $w1, 27)
2275// where the 27 comes from:
2276// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2277static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2278 SmallVector<int, 16> Indices,
2279 SelectionDAG &DAG) {
2280 int SHFIndices[4] = { -1, -1, -1, -1 };
2281
2282 if (Indices.size() < 4)
2283 return SDValue();
2284
2285 for (unsigned i = 0; i < 4; ++i) {
2286 for (unsigned j = i; j < Indices.size(); j += 4) {
2287 int Idx = Indices[j];
2288
2289 // Convert from vector index to 4-element subvector index
2290 // If an index refers to an element outside of the subvector then give up
2291 if (Idx != -1) {
2292 Idx -= 4 * (j / 4);
2293 if (Idx < 0 || Idx >= 4)
2294 return SDValue();
2295 }
2296
2297 // If the mask has an undef, replace it with the current index.
2298 // Note that it might still be undef if the current index is also undef
2299 if (SHFIndices[i] == -1)
2300 SHFIndices[i] = Idx;
2301
2302 // Check that non-undef values are the same as in the mask. If they
2303 // aren't then give up
2304 if (!(Idx == -1 || Idx == SHFIndices[i]))
2305 return SDValue();
2306 }
2307 }
2308
2309 // Calculate the immediate. Replace any remaining undefs with zero
2310 APInt Imm(32, 0);
2311 for (int i = 3; i >= 0; --i) {
2312 int Idx = SHFIndices[i];
2313
2314 if (Idx == -1)
2315 Idx = 0;
2316
2317 Imm <<= 2;
2318 Imm |= Idx & 0x3;
2319 }
2320
2321 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
2322 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
2323}
2324
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002325// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2326//
2327// ILVEV interleaves the even elements from each vector.
2328//
2329// It is possible to lower into ILVEV when the mask takes the form:
2330// <0, n, 2, n+2, 4, n+4, ...>
2331// where n is the number of elements in the vector.
2332//
2333// When undef's appear in the mask they are treated as if they were whatever
2334// value is necessary in order to fit the above form.
2335static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2336 SmallVector<int, 16> Indices,
2337 SelectionDAG &DAG) {
2338 assert ((Indices.size() % 2) == 0);
2339 int WsIdx = 0;
2340 int WtIdx = ResTy.getVectorNumElements();
2341
2342 for (unsigned i = 0; i < Indices.size(); i += 2) {
2343 if (Indices[i] != -1 && Indices[i] != WsIdx)
2344 return SDValue();
2345 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2346 return SDValue();
2347 WsIdx += 2;
2348 WtIdx += 2;
2349 }
2350
2351 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
2352 Op->getOperand(1));
2353}
2354
2355// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2356//
2357// ILVOD interleaves the odd elements from each vector.
2358//
2359// It is possible to lower into ILVOD when the mask takes the form:
2360// <1, n+1, 3, n+3, 5, n+5, ...>
2361// where n is the number of elements in the vector.
2362//
2363// When undef's appear in the mask they are treated as if they were whatever
2364// value is necessary in order to fit the above form.
2365static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2366 SmallVector<int, 16> Indices,
2367 SelectionDAG &DAG) {
2368 assert ((Indices.size() % 2) == 0);
2369 int WsIdx = 1;
2370 int WtIdx = ResTy.getVectorNumElements() + 1;
2371
2372 for (unsigned i = 0; i < Indices.size(); i += 2) {
2373 if (Indices[i] != -1 && Indices[i] != WsIdx)
2374 return SDValue();
2375 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2376 return SDValue();
2377 WsIdx += 2;
2378 WtIdx += 2;
2379 }
2380
2381 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
2382 Op->getOperand(1));
2383}
2384
2385// Lower VECTOR_SHUFFLE into ILVL (if possible).
2386//
2387// ILVL interleaves consecutive elements from the left half of each vector.
2388//
2389// It is possible to lower into ILVL when the mask takes the form:
2390// <0, n, 1, n+1, 2, n+2, ...>
2391// where n is the number of elements in the vector.
2392//
2393// When undef's appear in the mask they are treated as if they were whatever
2394// value is necessary in order to fit the above form.
2395static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2396 SmallVector<int, 16> Indices,
2397 SelectionDAG &DAG) {
2398 assert ((Indices.size() % 2) == 0);
2399 int WsIdx = 0;
2400 int WtIdx = ResTy.getVectorNumElements();
2401
2402 for (unsigned i = 0; i < Indices.size(); i += 2) {
2403 if (Indices[i] != -1 && Indices[i] != WsIdx)
2404 return SDValue();
2405 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2406 return SDValue();
2407 WsIdx ++;
2408 WtIdx ++;
2409 }
2410
2411 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2412 Op->getOperand(1));
2413}
2414
2415// Lower VECTOR_SHUFFLE into ILVR (if possible).
2416//
2417// ILVR interleaves consecutive elements from the right half of each vector.
2418//
2419// It is possible to lower into ILVR when the mask takes the form:
2420// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2421// where n is the number of elements in the vector and x is half n.
2422//
2423// When undef's appear in the mask they are treated as if they were whatever
2424// value is necessary in order to fit the above form.
2425static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2426 SmallVector<int, 16> Indices,
2427 SelectionDAG &DAG) {
2428 assert ((Indices.size() % 2) == 0);
2429 unsigned NumElts = ResTy.getVectorNumElements();
2430 int WsIdx = NumElts / 2;
2431 int WtIdx = NumElts + NumElts / 2;
2432
2433 for (unsigned i = 0; i < Indices.size(); i += 2) {
2434 if (Indices[i] != -1 && Indices[i] != WsIdx)
2435 return SDValue();
2436 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2437 return SDValue();
2438 WsIdx ++;
2439 WtIdx ++;
2440 }
2441
2442 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2443 Op->getOperand(1));
2444}
2445
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002446// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2447//
2448// PCKEV copies the even elements of each vector into the result vector.
2449//
2450// It is possible to lower into PCKEV when the mask takes the form:
2451// <0, 2, 4, ..., n, n+2, n+4, ...>
2452// where n is the number of elements in the vector.
2453//
2454// When undef's appear in the mask they are treated as if they were whatever
2455// value is necessary in order to fit the above form.
2456static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2457 SmallVector<int, 16> Indices,
2458 SelectionDAG &DAG) {
2459 assert ((Indices.size() % 2) == 0);
2460 int Idx = 0;
2461
2462 for (unsigned i = 0; i < Indices.size(); ++i) {
2463 if (Indices[i] != -1 && Indices[i] != Idx)
2464 return SDValue();
2465 Idx += 2;
2466 }
2467
2468 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2469 Op->getOperand(1));
2470}
2471
2472// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2473//
2474// PCKOD copies the odd elements of each vector into the result vector.
2475//
2476// It is possible to lower into PCKOD when the mask takes the form:
2477// <1, 3, 5, ..., n+1, n+3, n+5, ...>
2478// where n is the number of elements in the vector.
2479//
2480// When undef's appear in the mask they are treated as if they were whatever
2481// value is necessary in order to fit the above form.
2482static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2483 SmallVector<int, 16> Indices,
2484 SelectionDAG &DAG) {
2485 assert ((Indices.size() % 2) == 0);
2486 int Idx = 1;
2487
2488 for (unsigned i = 0; i < Indices.size(); ++i) {
2489 if (Indices[i] != -1 && Indices[i] != Idx)
2490 return SDValue();
2491 Idx += 2;
2492 }
2493
2494 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2495 Op->getOperand(1));
2496}
2497
Daniel Sanderse5087042013-09-24 14:02:15 +00002498// Lower VECTOR_SHUFFLE into VSHF.
2499//
2500// This mostly consists of converting the shuffle indices in Indices into a
2501// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2502// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2503// if the type is v8i16 and all the indices are less than 8 then the second
2504// operand is unused and can be replaced with anything. We choose to replace it
2505// with the used operand since this reduces the number of instructions overall.
2506static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2507 SmallVector<int, 16> Indices,
2508 SelectionDAG &DAG) {
2509 SmallVector<SDValue, 16> Ops;
2510 SDValue Op0;
2511 SDValue Op1;
2512 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2513 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2514 bool Using1stVec = false;
2515 bool Using2ndVec = false;
2516 SDLoc DL(Op);
2517 int ResTyNumElts = ResTy.getVectorNumElements();
2518
2519 for (int i = 0; i < ResTyNumElts; ++i) {
2520 // Idx == -1 means UNDEF
2521 int Idx = Indices[i];
2522
2523 if (0 <= Idx && Idx < ResTyNumElts)
2524 Using1stVec = true;
2525 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2526 Using2ndVec = true;
2527 }
2528
2529 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2530 ++I)
2531 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2532
2533 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, &Ops[0],
2534 Ops.size());
2535
2536 if (Using1stVec && Using2ndVec) {
2537 Op0 = Op->getOperand(0);
2538 Op1 = Op->getOperand(1);
2539 } else if (Using1stVec)
2540 Op0 = Op1 = Op->getOperand(0);
2541 else if (Using2ndVec)
2542 Op0 = Op1 = Op->getOperand(1);
2543 else
2544 llvm_unreachable("shuffle vector mask references neither vector operand?");
2545
2546 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op0, Op1);
2547}
2548
2549// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2550// indices in the shuffle.
2551SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2552 SelectionDAG &DAG) const {
2553 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2554 EVT ResTy = Op->getValueType(0);
2555
2556 if (!ResTy.is128BitVector())
2557 return SDValue();
2558
2559 int ResTyNumElts = ResTy.getVectorNumElements();
2560 SmallVector<int, 16> Indices;
2561
2562 for (int i = 0; i < ResTyNumElts; ++i)
2563 Indices.push_back(Node->getMaskElt(i));
2564
Daniel Sanders26307182013-09-24 14:20:00 +00002565 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2566 if (Result.getNode())
2567 return Result;
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002568 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2569 if (Result.getNode())
2570 return Result;
2571 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2572 if (Result.getNode())
2573 return Result;
2574 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2575 if (Result.getNode())
2576 return Result;
2577 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2578 if (Result.getNode())
2579 return Result;
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002580 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2581 if (Result.getNode())
2582 return Result;
2583 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2584 if (Result.getNode())
2585 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002586 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2587}
2588
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002589MachineBasicBlock * MipsSETargetLowering::
2590emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2591 // $bb:
2592 // bposge32_pseudo $vr0
2593 // =>
2594 // $bb:
2595 // bposge32 $tbb
2596 // $fbb:
2597 // li $vr2, 0
2598 // b $sink
2599 // $tbb:
2600 // li $vr1, 1
2601 // $sink:
2602 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2603
2604 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2605 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002606 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002607 DebugLoc DL = MI->getDebugLoc();
2608 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2609 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2610 MachineFunction *F = BB->getParent();
2611 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2612 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2613 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2614 F->insert(It, FBB);
2615 F->insert(It, TBB);
2616 F->insert(It, Sink);
2617
2618 // Transfer the remainder of BB and its successor edges to Sink.
2619 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2620 BB->end());
2621 Sink->transferSuccessorsAndUpdatePHIs(BB);
2622
2623 // Add successors.
2624 BB->addSuccessor(FBB);
2625 BB->addSuccessor(TBB);
2626 FBB->addSuccessor(Sink);
2627 TBB->addSuccessor(Sink);
2628
2629 // Insert the real bposge32 instruction to $BB.
2630 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2631
2632 // Fill $FBB.
2633 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2634 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2635 .addReg(Mips::ZERO).addImm(0);
2636 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2637
2638 // Fill $TBB.
2639 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2640 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2641 .addReg(Mips::ZERO).addImm(1);
2642
2643 // Insert phi function to $Sink.
2644 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2645 MI->getOperand(0).getReg())
2646 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2647
2648 MI->eraseFromParent(); // The pseudo instruction is gone now.
2649 return Sink;
2650}
Daniel Sandersce09d072013-08-28 12:14:50 +00002651
2652MachineBasicBlock * MipsSETargetLowering::
2653emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2654 unsigned BranchOp) const{
2655 // $bb:
2656 // vany_nonzero $rd, $ws
2657 // =>
2658 // $bb:
2659 // bnz.b $ws, $tbb
2660 // b $fbb
2661 // $fbb:
2662 // li $rd1, 0
2663 // b $sink
2664 // $tbb:
2665 // li $rd2, 1
2666 // $sink:
2667 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2668
2669 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2670 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2671 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2672 DebugLoc DL = MI->getDebugLoc();
2673 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2674 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2675 MachineFunction *F = BB->getParent();
2676 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2677 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2678 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2679 F->insert(It, FBB);
2680 F->insert(It, TBB);
2681 F->insert(It, Sink);
2682
2683 // Transfer the remainder of BB and its successor edges to Sink.
2684 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2685 BB->end());
2686 Sink->transferSuccessorsAndUpdatePHIs(BB);
2687
2688 // Add successors.
2689 BB->addSuccessor(FBB);
2690 BB->addSuccessor(TBB);
2691 FBB->addSuccessor(Sink);
2692 TBB->addSuccessor(Sink);
2693
2694 // Insert the real bnz.b instruction to $BB.
2695 BuildMI(BB, DL, TII->get(BranchOp))
2696 .addReg(MI->getOperand(1).getReg())
2697 .addMBB(TBB);
2698
2699 // Fill $FBB.
2700 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2701 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2702 .addReg(Mips::ZERO).addImm(0);
2703 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2704
2705 // Fill $TBB.
2706 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2707 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2708 .addReg(Mips::ZERO).addImm(1);
2709
2710 // Insert phi function to $Sink.
2711 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2712 MI->getOperand(0).getReg())
2713 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2714
2715 MI->eraseFromParent(); // The pseudo instruction is gone now.
2716 return Sink;
2717}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002718
2719// Emit the COPY_FW pseudo instruction.
2720//
2721// copy_fw_pseudo $fd, $ws, n
2722// =>
2723// copy_u_w $rt, $ws, $n
2724// mtc1 $rt, $fd
2725//
2726// When n is zero, the equivalent operation can be performed with (potentially)
2727// zero instructions due to register overlaps. This optimization is never valid
2728// for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2729MachineBasicBlock * MipsSETargetLowering::
2730emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2731 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2732 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2733 DebugLoc DL = MI->getDebugLoc();
2734 unsigned Fd = MI->getOperand(0).getReg();
2735 unsigned Ws = MI->getOperand(1).getReg();
2736 unsigned Lane = MI->getOperand(2).getImm();
2737
2738 if (Lane == 0)
2739 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2740 else {
2741 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2742
2743 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(1);
2744 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2745 }
2746
2747 MI->eraseFromParent(); // The pseudo instruction is gone now.
2748 return BB;
2749}
2750
2751// Emit the COPY_FD pseudo instruction.
2752//
2753// copy_fd_pseudo $fd, $ws, n
2754// =>
2755// splati.d $wt, $ws, $n
2756// copy $fd, $wt:sub_64
2757//
2758// When n is zero, the equivalent operation can be performed with (potentially)
2759// zero instructions due to register overlaps. This optimization is always
2760// valid because FR=1 mode which is the only supported mode in MSA.
2761MachineBasicBlock * MipsSETargetLowering::
2762emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
2763 assert(Subtarget->isFP64bit());
2764
2765 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2766 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2767 unsigned Fd = MI->getOperand(0).getReg();
2768 unsigned Ws = MI->getOperand(1).getReg();
2769 unsigned Lane = MI->getOperand(2).getImm() * 2;
2770 DebugLoc DL = MI->getDebugLoc();
2771
2772 if (Lane == 0)
2773 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2774 else {
2775 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2776
2777 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2778 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2779 }
2780
2781 MI->eraseFromParent(); // The pseudo instruction is gone now.
2782 return BB;
2783}
Daniel Sandersa5150702013-09-27 12:31:32 +00002784
2785// Emit the INSERT_FW pseudo instruction.
2786//
2787// insert_fw_pseudo $wd, $wd_in, $n, $fs
2788// =>
2789// subreg_to_reg $wt:sub_lo, $fs
2790// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002791MachineBasicBlock *
2792MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2793 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002794 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2795 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2796 DebugLoc DL = MI->getDebugLoc();
2797 unsigned Wd = MI->getOperand(0).getReg();
2798 unsigned Wd_in = MI->getOperand(1).getReg();
2799 unsigned Lane = MI->getOperand(2).getImm();
2800 unsigned Fs = MI->getOperand(3).getReg();
2801 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2802
2803 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002804 .addImm(0)
2805 .addReg(Fs)
2806 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00002807 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002808 .addReg(Wd_in)
2809 .addImm(Lane)
2810 .addReg(Wt);
Daniel Sandersa5150702013-09-27 12:31:32 +00002811
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002812 MI->eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00002813 return BB;
2814}
2815
2816// Emit the INSERT_FD pseudo instruction.
2817//
2818// insert_fd_pseudo $wd, $fs, n
2819// =>
2820// subreg_to_reg $wt:sub_64, $fs
2821// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002822MachineBasicBlock *
2823MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2824 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002825 assert(Subtarget->isFP64bit());
2826
2827 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2828 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2829 DebugLoc DL = MI->getDebugLoc();
2830 unsigned Wd = MI->getOperand(0).getReg();
2831 unsigned Wd_in = MI->getOperand(1).getReg();
2832 unsigned Lane = MI->getOperand(2).getImm();
2833 unsigned Fs = MI->getOperand(3).getReg();
2834 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2835
2836 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002837 .addImm(0)
2838 .addReg(Fs)
2839 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00002840 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002841 .addReg(Wd_in)
2842 .addImm(Lane)
2843 .addReg(Wt);
2844
2845 MI->eraseFromParent(); // The pseudo instruction is gone now.
2846 return BB;
2847}
2848
2849// Emit the FILL_FW pseudo instruction.
2850//
2851// fill_fw_pseudo $wd, $fs
2852// =>
2853// implicit_def $wt1
2854// insert_subreg $wt2:subreg_lo, $wt1, $fs
2855// splati.w $wd, $wt2[0]
2856MachineBasicBlock *
2857MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
2858 MachineBasicBlock *BB) const {
2859 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2860 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2861 DebugLoc DL = MI->getDebugLoc();
2862 unsigned Wd = MI->getOperand(0).getReg();
2863 unsigned Fs = MI->getOperand(1).getReg();
2864 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2865 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2866
2867 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2868 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2869 .addReg(Wt1)
2870 .addReg(Fs)
2871 .addImm(Mips::sub_lo);
2872 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
2873
2874 MI->eraseFromParent(); // The pseudo instruction is gone now.
2875 return BB;
2876}
2877
2878// Emit the FILL_FD pseudo instruction.
2879//
2880// fill_fd_pseudo $wd, $fs
2881// =>
2882// implicit_def $wt1
2883// insert_subreg $wt2:subreg_64, $wt1, $fs
2884// splati.d $wd, $wt2[0]
2885MachineBasicBlock *
2886MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
2887 MachineBasicBlock *BB) const {
2888 assert(Subtarget->isFP64bit());
2889
2890 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2891 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2892 DebugLoc DL = MI->getDebugLoc();
2893 unsigned Wd = MI->getOperand(0).getReg();
2894 unsigned Fs = MI->getOperand(1).getReg();
2895 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2896 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2897
2898 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2899 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2900 .addReg(Wt1)
2901 .addReg(Fs)
2902 .addImm(Mips::sub_64);
2903 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00002904
2905 MI->eraseFromParent(); // The pseudo instruction is gone now.
2906 return BB;
2907}
Daniel Sandersa9521602013-10-23 10:36:52 +00002908
2909// Emit the FEXP2_W_1 pseudo instructions.
2910//
2911// fexp2_w_1_pseudo $wd, $wt
2912// =>
2913// ldi.w $ws, 1
2914// fexp2.w $wd, $ws, $wt
2915MachineBasicBlock *
2916MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
2917 MachineBasicBlock *BB) const {
2918 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2919 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2920 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
2921 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2922 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2923 DebugLoc DL = MI->getDebugLoc();
2924
2925 // Splat 1.0 into a vector
2926 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
2927 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
2928
2929 // Emit 1.0 * fexp2(Wt)
2930 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
2931 .addReg(Ws2)
2932 .addReg(MI->getOperand(1).getReg());
2933
2934 MI->eraseFromParent(); // The pseudo instruction is gone now.
2935 return BB;
2936}
2937
2938// Emit the FEXP2_D_1 pseudo instructions.
2939//
2940// fexp2_d_1_pseudo $wd, $wt
2941// =>
2942// ldi.d $ws, 1
2943// fexp2.d $wd, $ws, $wt
2944MachineBasicBlock *
2945MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
2946 MachineBasicBlock *BB) const {
2947 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2948 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2949 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
2950 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2951 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2952 DebugLoc DL = MI->getDebugLoc();
2953
2954 // Splat 1.0 into a vector
2955 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
2956 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
2957
2958 // Emit 1.0 * fexp2(Wt)
2959 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
2960 .addReg(Ws2)
2961 .addReg(MI->getOperand(1).getReg());
2962
2963 MI->eraseFromParent(); // The pseudo instruction is gone now.
2964 return BB;
2965}