blob: aa33fab720f88eb5d8289e91bc0477ce6c9d7220 [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
Matt Arsenault25793a32014-02-05 23:15:53 +0000247MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
248 unsigned,
249 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000250 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
251
252 switch (SVT) {
253 case MVT::i64:
254 case MVT::i32:
255 if (Fast)
256 *Fast = true;
257 return true;
258 default:
259 return false;
260 }
261}
262
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000263SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
264 SelectionDAG &DAG) const {
265 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000266 case ISD::LOAD: return lowerLOAD(Op, DAG);
267 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000268 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
269 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
270 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
271 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
272 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
273 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000274 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
275 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000276 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
277 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000278 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000279 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000280 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000281 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000282 }
283
284 return MipsTargetLowering::LowerOperation(Op, DAG);
285}
286
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000287// selectMADD -
288// Transforms a subgraph in CurDAG if the following pattern is found:
289// (addc multLo, Lo0), (adde multHi, Hi0),
290// where,
291// multHi/Lo: product of multiplication
292// Lo0: initial value of Lo register
293// Hi0: initial value of Hi register
294// Return true if pattern matching was successful.
295static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
296 // ADDENode's second operand must be a flag output of an ADDC node in order
297 // for the matching to be successful.
298 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
299
300 if (ADDCNode->getOpcode() != ISD::ADDC)
301 return false;
302
303 SDValue MultHi = ADDENode->getOperand(0);
304 SDValue MultLo = ADDCNode->getOperand(0);
305 SDNode *MultNode = MultHi.getNode();
306 unsigned MultOpc = MultHi.getOpcode();
307
308 // MultHi and MultLo must be generated by the same node,
309 if (MultLo.getNode() != MultNode)
310 return false;
311
312 // and it must be a multiplication.
313 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
314 return false;
315
316 // MultLo amd MultHi must be the first and second output of MultNode
317 // respectively.
318 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
319 return false;
320
321 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
322 // of the values of MultNode, in which case MultNode will be removed in later
323 // phases.
324 // If there exist users other than ADDENode or ADDCNode, this function returns
325 // here, which will result in MultNode being mapped to a single MULT
326 // instruction node rather than a pair of MULT and MADD instructions being
327 // produced.
328 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
329 return false;
330
Andrew Trickef9de2a2013-05-25 02:42:55 +0000331 SDLoc DL(ADDENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000332
333 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000334 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000335 ADDCNode->getOperand(1),
336 ADDENode->getOperand(1));
337
338 // create MipsMAdd(u) node
339 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
340
341 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
342 MultNode->getOperand(0),// Factor 0
343 MultNode->getOperand(1),// Factor 1
344 ACCIn);
345
346 // replace uses of adde and addc here
347 if (!SDValue(ADDCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000348 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000349 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
350 }
351 if (!SDValue(ADDENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000352 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000353 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
354 }
355
356 return true;
357}
358
359// selectMSUB -
360// Transforms a subgraph in CurDAG if the following pattern is found:
361// (addc Lo0, multLo), (sube Hi0, multHi),
362// where,
363// multHi/Lo: product of multiplication
364// Lo0: initial value of Lo register
365// Hi0: initial value of Hi register
366// Return true if pattern matching was successful.
367static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
368 // SUBENode's second operand must be a flag output of an SUBC node in order
369 // for the matching to be successful.
370 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
371
372 if (SUBCNode->getOpcode() != ISD::SUBC)
373 return false;
374
375 SDValue MultHi = SUBENode->getOperand(1);
376 SDValue MultLo = SUBCNode->getOperand(1);
377 SDNode *MultNode = MultHi.getNode();
378 unsigned MultOpc = MultHi.getOpcode();
379
380 // MultHi and MultLo must be generated by the same node,
381 if (MultLo.getNode() != MultNode)
382 return false;
383
384 // and it must be a multiplication.
385 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
386 return false;
387
388 // MultLo amd MultHi must be the first and second output of MultNode
389 // respectively.
390 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
391 return false;
392
393 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
394 // of the values of MultNode, in which case MultNode will be removed in later
395 // phases.
396 // If there exist users other than SUBENode or SUBCNode, this function returns
397 // here, which will result in MultNode being mapped to a single MULT
398 // instruction node rather than a pair of MULT and MSUB instructions being
399 // produced.
400 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
401 return false;
402
Andrew Trickef9de2a2013-05-25 02:42:55 +0000403 SDLoc DL(SUBENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000404
405 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000406 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000407 SUBCNode->getOperand(0),
408 SUBENode->getOperand(0));
409
410 // create MipsSub(u) node
411 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
412
413 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
414 MultNode->getOperand(0),// Factor 0
415 MultNode->getOperand(1),// Factor 1
416 ACCIn);
417
418 // replace uses of sube and subc here
419 if (!SDValue(SUBCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000420 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000421 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
422 }
423 if (!SDValue(SUBENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000424 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000425 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
426 }
427
428 return true;
429}
430
431static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
432 TargetLowering::DAGCombinerInfo &DCI,
433 const MipsSubtarget *Subtarget) {
434 if (DCI.isBeforeLegalize())
435 return SDValue();
436
437 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
438 selectMADD(N, &DAG))
439 return SDValue(N, 0);
440
441 return SDValue();
442}
443
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000444// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
445//
446// Performs the following transformations:
447// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
448// sign/zero-extension is completely overwritten by the new one performed by
449// the ISD::AND.
450// - Removes redundant zero extensions performed by an ISD::AND.
451static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
452 TargetLowering::DAGCombinerInfo &DCI,
453 const MipsSubtarget *Subtarget) {
454 if (!Subtarget->hasMSA())
455 return SDValue();
456
457 SDValue Op0 = N->getOperand(0);
458 SDValue Op1 = N->getOperand(1);
459 unsigned Op0Opcode = Op0->getOpcode();
460
461 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
462 // where $d + 1 == 2^n and n == 32
463 // or $d + 1 == 2^n and n <= 32 and ZExt
464 // -> (MipsVExtractZExt $a, $b, $c)
465 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
466 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
467 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
468
469 if (!Mask)
470 return SDValue();
471
472 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
473
474 if (Log2IfPositive <= 0)
475 return SDValue(); // Mask+1 is not a power of 2
476
477 SDValue Op0Op2 = Op0->getOperand(2);
478 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
479 unsigned ExtendTySize = ExtendTy.getSizeInBits();
480 unsigned Log2 = Log2IfPositive;
481
482 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
483 Log2 == ExtendTySize) {
484 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
485 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
486 Op0->getVTList(), Ops, Op0->getNumOperands());
487 return Op0;
488 }
489 }
490
491 return SDValue();
492}
493
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000494// Determine if the specified node is a constant vector splat.
495//
496// Returns true and sets Imm if:
497// * N is a ISD::BUILD_VECTOR representing a constant splat
498//
499// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
500// differences are that it assumes the MSA has already been checked and the
501// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
502// must not be in order for binsri.d to be selectable).
503static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
504 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
505
506 if (Node == NULL)
507 return false;
508
509 APInt SplatValue, SplatUndef;
510 unsigned SplatBitSize;
511 bool HasAnyUndefs;
512
513 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
514 8, !IsLittleEndian))
515 return false;
516
517 Imm = SplatValue;
518
519 return true;
520}
521
Daniel Sandersab94b532013-10-30 15:20:38 +0000522// Test whether the given node is an all-ones build_vector.
523static bool isVectorAllOnes(SDValue N) {
524 // Look through bitcasts. Endianness doesn't matter because we are looking
525 // for an all-ones value.
526 if (N->getOpcode() == ISD::BITCAST)
527 N = N->getOperand(0);
528
529 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
530
531 if (!BVN)
532 return false;
533
534 APInt SplatValue, SplatUndef;
535 unsigned SplatBitSize;
536 bool HasAnyUndefs;
537
538 // Endianness doesn't matter in this context because we are looking for
539 // an all-ones value.
540 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
541 return SplatValue.isAllOnesValue();
542
543 return false;
544}
545
546// Test whether N is the bitwise inverse of OfNode.
547static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
548 if (N->getOpcode() != ISD::XOR)
549 return false;
550
551 if (isVectorAllOnes(N->getOperand(0)))
552 return N->getOperand(1) == OfNode;
553
554 if (isVectorAllOnes(N->getOperand(1)))
555 return N->getOperand(0) == OfNode;
556
557 return false;
558}
559
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000560// Perform combines where ISD::OR is the root node.
561//
562// Performs the following transformations:
563// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
564// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
565// vector type.
566static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
567 TargetLowering::DAGCombinerInfo &DCI,
568 const MipsSubtarget *Subtarget) {
569 if (!Subtarget->hasMSA())
570 return SDValue();
571
572 EVT Ty = N->getValueType(0);
573
574 if (!Ty.is128BitVector())
575 return SDValue();
576
577 SDValue Op0 = N->getOperand(0);
578 SDValue Op1 = N->getOperand(1);
579
580 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
581 SDValue Op0Op0 = Op0->getOperand(0);
582 SDValue Op0Op1 = Op0->getOperand(1);
583 SDValue Op1Op0 = Op1->getOperand(0);
584 SDValue Op1Op1 = Op1->getOperand(1);
585 bool IsLittleEndian = !Subtarget->isLittle();
586
587 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000588 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000589 APInt Mask, InvMask;
590
591 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
592 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
593 // looking.
594 // IfClr will be set if we find a valid match.
595 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
596 Cond = Op0Op0;
597 IfSet = Op0Op1;
598
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000599 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
600 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000601 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000602 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
603 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000604 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000605
606 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000607 }
608
609 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
610 // thing again using this mask.
611 // IfClr will be set if we find a valid match.
612 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
613 Cond = Op0Op1;
614 IfSet = Op0Op0;
615
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000616 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
617 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000618 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000619 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
620 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000621 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000622
623 IsConstantMask = true;
624 }
625
626 // If IfClr is not yet set, try looking for a non-constant match.
627 // IfClr will be set if we find a valid match amongst the eight
628 // possibilities.
629 if (!IfClr.getNode()) {
630 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
631 Cond = Op1Op0;
632 IfSet = Op1Op1;
633 IfClr = Op0Op1;
634 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
635 Cond = Op1Op0;
636 IfSet = Op1Op1;
637 IfClr = Op0Op0;
638 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
639 Cond = Op1Op1;
640 IfSet = Op1Op0;
641 IfClr = Op0Op1;
642 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
643 Cond = Op1Op1;
644 IfSet = Op1Op0;
645 IfClr = Op0Op0;
646 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
647 Cond = Op0Op0;
648 IfSet = Op0Op1;
649 IfClr = Op1Op1;
650 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
651 Cond = Op0Op0;
652 IfSet = Op0Op1;
653 IfClr = Op1Op0;
654 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
655 Cond = Op0Op1;
656 IfSet = Op0Op0;
657 IfClr = Op1Op1;
658 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
659 Cond = Op0Op1;
660 IfSet = Op0Op0;
661 IfClr = Op1Op0;
662 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000663 }
664
665 // At this point, IfClr will be set if we have a valid match.
666 if (!IfClr.getNode())
667 return SDValue();
668
669 assert(Cond.getNode() && IfSet.getNode());
670
671 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000672 if (IsConstantMask) {
673 if (Mask.isAllOnesValue())
674 return IfSet;
675 else if (Mask == 0)
676 return IfClr;
677 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000678
679 // Transform the DAG into an equivalent VSELECT.
680 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfClr, IfSet);
681 }
682
683 return SDValue();
684}
685
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000686static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
687 TargetLowering::DAGCombinerInfo &DCI,
688 const MipsSubtarget *Subtarget) {
689 if (DCI.isBeforeLegalize())
690 return SDValue();
691
692 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
693 selectMSUB(N, &DAG))
694 return SDValue(N, 0);
695
696 return SDValue();
697}
698
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000699static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
700 EVT ShiftTy, SelectionDAG &DAG) {
701 // Clear the upper (64 - VT.sizeInBits) bits.
702 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
703
704 // Return 0.
705 if (C == 0)
706 return DAG.getConstant(0, VT);
707
708 // Return x.
709 if (C == 1)
710 return X;
711
712 // If c is power of 2, return (shl x, log2(c)).
713 if (isPowerOf2_64(C))
714 return DAG.getNode(ISD::SHL, DL, VT, X,
715 DAG.getConstant(Log2_64(C), ShiftTy));
716
717 unsigned Log2Ceil = Log2_64_Ceil(C);
718 uint64_t Floor = 1LL << Log2_64(C);
719 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
720
721 // If |c - floor_c| <= |c - ceil_c|,
722 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
723 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
724 if (C - Floor <= Ceil - C) {
725 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
726 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
727 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
728 }
729
730 // If |c - floor_c| > |c - ceil_c|,
731 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
732 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
733 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
734 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
735}
736
737static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
738 const TargetLowering::DAGCombinerInfo &DCI,
739 const MipsSETargetLowering *TL) {
740 EVT VT = N->getValueType(0);
741
742 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
743 if (!VT.isVector())
744 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
745 VT, TL->getScalarShiftAmountTy(VT), DAG);
746
747 return SDValue(N, 0);
748}
749
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000750static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
751 SelectionDAG &DAG,
752 const MipsSubtarget *Subtarget) {
753 // See if this is a vector splat immediate node.
754 APInt SplatValue, SplatUndef;
755 unsigned SplatBitSize;
756 bool HasAnyUndefs;
757 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
758 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
759
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000760 if (!Subtarget->hasDSP())
761 return SDValue();
762
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000763 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000764 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000765 EltSize, !Subtarget->isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000766 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000767 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000768 return SDValue();
769
Andrew Trickef9de2a2013-05-25 02:42:55 +0000770 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000771 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
772}
773
774static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
775 TargetLowering::DAGCombinerInfo &DCI,
776 const MipsSubtarget *Subtarget) {
777 EVT Ty = N->getValueType(0);
778
779 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
780 return SDValue();
781
782 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
783}
784
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000785// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
786// constant splats into MipsISD::SHRA_DSP for DSPr2.
787//
788// Performs the following transformations:
789// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
790// sign/zero-extension is completely overwritten by the new one performed by
791// the ISD::SRA and ISD::SHL nodes.
792// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
793// sequence.
794//
795// See performDSPShiftCombine for more information about the transformation
796// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000797static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
798 TargetLowering::DAGCombinerInfo &DCI,
799 const MipsSubtarget *Subtarget) {
800 EVT Ty = N->getValueType(0);
801
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000802 if (Subtarget->hasMSA()) {
803 SDValue Op0 = N->getOperand(0);
804 SDValue Op1 = N->getOperand(1);
805
806 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
807 // where $d + sizeof($c) == 32
808 // or $d + sizeof($c) <= 32 and SExt
809 // -> (MipsVExtractSExt $a, $b, $c)
810 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
811 SDValue Op0Op0 = Op0->getOperand(0);
812 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
813
814 if (!ShAmount)
815 return SDValue();
816
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000817 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
818 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
819 return SDValue();
820
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000821 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
822 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
823
824 if (TotalBits == 32 ||
825 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
826 TotalBits <= 32)) {
827 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
828 Op0Op0->getOperand(2) };
829 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
830 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
831 return Op0Op0;
832 }
833 }
834 }
835
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000836 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
837 return SDValue();
838
839 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
840}
841
842
843static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
844 TargetLowering::DAGCombinerInfo &DCI,
845 const MipsSubtarget *Subtarget) {
846 EVT Ty = N->getValueType(0);
847
848 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
849 return SDValue();
850
851 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
852}
853
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000854static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
855 bool IsV216 = (Ty == MVT::v2i16);
856
857 switch (CC) {
858 case ISD::SETEQ:
859 case ISD::SETNE: return true;
860 case ISD::SETLT:
861 case ISD::SETLE:
862 case ISD::SETGT:
863 case ISD::SETGE: return IsV216;
864 case ISD::SETULT:
865 case ISD::SETULE:
866 case ISD::SETUGT:
867 case ISD::SETUGE: return !IsV216;
868 default: return false;
869 }
870}
871
872static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
873 EVT Ty = N->getValueType(0);
874
875 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
876 return SDValue();
877
878 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
879 return SDValue();
880
Andrew Trickef9de2a2013-05-25 02:42:55 +0000881 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000882 N->getOperand(1), N->getOperand(2));
883}
884
885static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
886 EVT Ty = N->getValueType(0);
887
Daniel Sanders3ce56622013-09-24 12:18:31 +0000888 if (Ty.is128BitVector() && Ty.isInteger()) {
889 // Try the following combines:
890 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
891 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
892 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
893 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
894 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
895 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
896 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
897 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
898 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
899 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
900 // legalizer.
901 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000902
Daniel Sanders3ce56622013-09-24 12:18:31 +0000903 if (Op0->getOpcode() != ISD::SETCC)
904 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000905
Daniel Sanders3ce56622013-09-24 12:18:31 +0000906 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
907 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000908
Daniel Sanders3ce56622013-09-24 12:18:31 +0000909 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
910 Signed = true;
911 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
912 Signed = false;
913 else
914 return SDValue();
915
916 SDValue Op1 = N->getOperand(1);
917 SDValue Op2 = N->getOperand(2);
918 SDValue Op0Op0 = Op0->getOperand(0);
919 SDValue Op0Op1 = Op0->getOperand(1);
920
921 if (Op1 == Op0Op0 && Op2 == Op0Op1)
922 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
923 Ty, Op1, Op2);
924 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
925 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
926 Ty, Op1, Op2);
927 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
928 SDValue SetCC = N->getOperand(0);
929
930 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
931 return SDValue();
932
933 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
934 SetCC.getOperand(0), SetCC.getOperand(1),
935 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
936 }
937
938 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000939}
940
Daniel Sandersf7456c72013-09-23 13:22:24 +0000941static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
942 const MipsSubtarget *Subtarget) {
943 EVT Ty = N->getValueType(0);
944
945 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
946 // Try the following combines:
947 // (xor (or $a, $b), (build_vector allones))
948 // (xor (or $a, $b), (bitcast (build_vector allones)))
949 SDValue Op0 = N->getOperand(0);
950 SDValue Op1 = N->getOperand(1);
951 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000952
953 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
954 NotOp = Op1;
955 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
956 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000957 else
958 return SDValue();
959
960 if (NotOp->getOpcode() == ISD::OR)
961 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
962 NotOp->getOperand(1));
963 }
964
965 return SDValue();
966}
967
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000968SDValue
969MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
970 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000971 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000972
973 switch (N->getOpcode()) {
974 case ISD::ADDE:
975 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000976 case ISD::AND:
977 Val = performANDCombine(N, DAG, DCI, Subtarget);
978 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000979 case ISD::OR:
980 Val = performORCombine(N, DAG, DCI, Subtarget);
981 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000982 case ISD::SUBE:
983 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000984 case ISD::MUL:
985 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000986 case ISD::SHL:
987 return performSHLCombine(N, DAG, DCI, Subtarget);
988 case ISD::SRA:
989 return performSRACombine(N, DAG, DCI, Subtarget);
990 case ISD::SRL:
991 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000992 case ISD::VSELECT:
993 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000994 case ISD::XOR:
995 Val = performXORCombine(N, DAG, Subtarget);
996 break;
997 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000998 Val = performSETCCCombine(N, DAG);
999 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001000 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001001
Daniel Sanders62aeab82013-10-30 13:31:27 +00001002 if (Val.getNode()) {
1003 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1004 N->printrWithDepth(dbgs(), &DAG);
1005 dbgs() << "\n=> \n";
1006 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1007 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001008 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001009 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001010
1011 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001012}
1013
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001014MachineBasicBlock *
1015MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1016 MachineBasicBlock *BB) const {
1017 switch (MI->getOpcode()) {
1018 default:
1019 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1020 case Mips::BPOSGE32_PSEUDO:
1021 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001022 case Mips::SNZ_B_PSEUDO:
1023 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1024 case Mips::SNZ_H_PSEUDO:
1025 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1026 case Mips::SNZ_W_PSEUDO:
1027 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1028 case Mips::SNZ_D_PSEUDO:
1029 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1030 case Mips::SNZ_V_PSEUDO:
1031 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1032 case Mips::SZ_B_PSEUDO:
1033 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1034 case Mips::SZ_H_PSEUDO:
1035 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1036 case Mips::SZ_W_PSEUDO:
1037 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1038 case Mips::SZ_D_PSEUDO:
1039 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1040 case Mips::SZ_V_PSEUDO:
1041 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001042 case Mips::COPY_FW_PSEUDO:
1043 return emitCOPY_FW(MI, BB);
1044 case Mips::COPY_FD_PSEUDO:
1045 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001046 case Mips::INSERT_FW_PSEUDO:
1047 return emitINSERT_FW(MI, BB);
1048 case Mips::INSERT_FD_PSEUDO:
1049 return emitINSERT_FD(MI, BB);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001050 case Mips::FILL_FW_PSEUDO:
1051 return emitFILL_FW(MI, BB);
1052 case Mips::FILL_FD_PSEUDO:
1053 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001054 case Mips::FEXP2_W_1_PSEUDO:
1055 return emitFEXP2_W_1(MI, BB);
1056 case Mips::FEXP2_D_1_PSEUDO:
1057 return emitFEXP2_D_1(MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001058 }
1059}
1060
1061bool MipsSETargetLowering::
1062isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
1063 unsigned NextStackOffset,
1064 const MipsFunctionInfo& FI) const {
1065 if (!EnableMipsTailCalls)
1066 return false;
1067
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001068 // Return false if either the callee or caller has a byval argument.
1069 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
1070 return false;
1071
1072 // Return true if the callee's argument area is no larger than the
1073 // caller's.
1074 return NextStackOffset <= FI.getIncomingArgSize();
1075}
1076
1077void MipsSETargetLowering::
1078getOpndList(SmallVectorImpl<SDValue> &Ops,
1079 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
1080 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
1081 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001082 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001083 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
1084 InternalLinkage, CLI, Callee, Chain);
1085}
1086
Akira Hatanaka63791212013-09-07 00:52:30 +00001087SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1088 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1089
1090 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1091 return MipsTargetLowering::lowerLOAD(Op, DAG);
1092
1093 // Replace a double precision load with two i32 loads and a buildpair64.
1094 SDLoc DL(Op);
1095 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1096 EVT PtrVT = Ptr.getValueType();
1097
1098 // i32 load from lower address.
1099 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1100 MachinePointerInfo(), Nd.isVolatile(),
1101 Nd.isNonTemporal(), Nd.isInvariant(),
1102 Nd.getAlignment());
1103
1104 // i32 load from higher address.
1105 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1106 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1107 MachinePointerInfo(), Nd.isVolatile(),
1108 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001109 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka63791212013-09-07 00:52:30 +00001110
1111 if (!Subtarget->isLittle())
1112 std::swap(Lo, Hi);
1113
1114 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1115 SDValue Ops[2] = {BP, Hi.getValue(1)};
1116 return DAG.getMergeValues(Ops, 2, DL);
1117}
1118
1119SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1120 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1121
1122 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1123 return MipsTargetLowering::lowerSTORE(Op, DAG);
1124
1125 // Replace a double precision store with two extractelement64s and i32 stores.
1126 SDLoc DL(Op);
1127 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1128 EVT PtrVT = Ptr.getValueType();
1129 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1130 Val, DAG.getConstant(0, MVT::i32));
1131 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1132 Val, DAG.getConstant(1, MVT::i32));
1133
1134 if (!Subtarget->isLittle())
1135 std::swap(Lo, Hi);
1136
1137 // i32 store to lower address.
1138 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1139 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
1140 Nd.getTBAAInfo());
1141
1142 // i32 store to higher address.
1143 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1144 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001145 Nd.isVolatile(), Nd.isNonTemporal(),
1146 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001147}
1148
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001149SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1150 bool HasLo, bool HasHi,
1151 SelectionDAG &DAG) const {
1152 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001153 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001154 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1155 Op.getOperand(0), Op.getOperand(1));
1156 SDValue Lo, Hi;
1157
1158 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001159 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001160 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001161 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001162
1163 if (!HasLo || !HasHi)
1164 return HasLo ? Lo : Hi;
1165
1166 SDValue Vals[] = { Lo, Hi };
1167 return DAG.getMergeValues(Vals, 2, DL);
1168}
1169
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001170
Andrew Trickef9de2a2013-05-25 02:42:55 +00001171static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001172 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1173 DAG.getConstant(0, MVT::i32));
1174 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1175 DAG.getConstant(1, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001176 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001177}
1178
Andrew Trickef9de2a2013-05-25 02:42:55 +00001179static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001180 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1181 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001182 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1183}
1184
1185// This function expands mips intrinsic nodes which have 64-bit input operands
1186// or output values.
1187//
1188// out64 = intrinsic-node in64
1189// =>
1190// lo = copy (extract-element (in64, 0))
1191// hi = copy (extract-element (in64, 1))
1192// mips-specific-node
1193// v0 = copy lo
1194// v1 = copy hi
1195// out64 = merge-values (v0, v1)
1196//
1197static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001198 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001199 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1200 SmallVector<SDValue, 3> Ops;
1201 unsigned OpNo = 0;
1202
1203 // See if Op has a chain input.
1204 if (HasChainIn)
1205 Ops.push_back(Op->getOperand(OpNo++));
1206
1207 // The next operand is the intrinsic opcode.
1208 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1209
1210 // See if the next operand has type i64.
1211 SDValue Opnd = Op->getOperand(++OpNo), In64;
1212
1213 if (Opnd.getValueType() == MVT::i64)
1214 In64 = initAccumulator(Opnd, DL, DAG);
1215 else
1216 Ops.push_back(Opnd);
1217
1218 // Push the remaining operands.
1219 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1220 Ops.push_back(Op->getOperand(OpNo));
1221
1222 // Add In64 to the end of the list.
1223 if (In64.getNode())
1224 Ops.push_back(In64);
1225
1226 // Scan output.
1227 SmallVector<EVT, 2> ResTys;
1228
1229 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1230 I != E; ++I)
1231 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1232
1233 // Create node.
1234 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1235 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1236
1237 if (!HasChainIn)
1238 return Out;
1239
1240 assert(Val->getValueType(1) == MVT::Other);
1241 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1242 return DAG.getMergeValues(Vals, 2, DL);
1243}
1244
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001245// Lower an MSA copy intrinsic into the specified SelectionDAG node
1246static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1247 SDLoc DL(Op);
1248 SDValue Vec = Op->getOperand(1);
1249 SDValue Idx = Op->getOperand(2);
1250 EVT ResTy = Op->getValueType(0);
1251 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1252
1253 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1254 DAG.getValueType(EltTy));
1255
1256 return Result;
1257}
1258
Daniel Sanders50b80412013-11-15 12:56:49 +00001259static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1260 EVT ResVecTy = Op->getValueType(0);
1261 EVT ViaVecTy = ResVecTy;
1262 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001263
Daniel Sanders50b80412013-11-15 12:56:49 +00001264 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1265 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1266 // lanes.
1267 SDValue LaneA;
1268 SDValue LaneB = Op->getOperand(2);
1269
1270 if (ResVecTy == MVT::v2i64) {
1271 LaneA = DAG.getConstant(0, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001272 ViaVecTy = MVT::v4i32;
Daniel Sanders50b80412013-11-15 12:56:49 +00001273 } else
1274 LaneA = LaneB;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001275
Daniel Sanders50b80412013-11-15 12:56:49 +00001276 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1277 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001278
Daniel Sanders50b80412013-11-15 12:56:49 +00001279 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, Ops,
1280 ViaVecTy.getVectorNumElements());
1281
1282 if (ViaVecTy != ResVecTy)
1283 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001284
1285 return Result;
1286}
1287
Daniel Sanders50b80412013-11-15 12:56:49 +00001288static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1289 return DAG.getConstant(Op->getConstantOperandVal(ImmOp), Op->getValueType(0));
1290}
1291
1292static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1293 bool BigEndian, SelectionDAG &DAG) {
1294 EVT ViaVecTy = VecTy;
1295 SDValue SplatValueA = SplatValue;
1296 SDValue SplatValueB = SplatValue;
1297 SDLoc DL(SplatValue);
1298
1299 if (VecTy == MVT::v2i64) {
1300 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1301 ViaVecTy = MVT::v4i32;
1302
1303 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1304 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
1305 DAG.getConstant(32, MVT::i32));
1306 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1307 }
1308
1309 // We currently hold the parts in little endian order. Swap them if
1310 // necessary.
1311 if (BigEndian)
1312 std::swap(SplatValueA, SplatValueB);
1313
1314 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1315 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1316 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1317 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1318
1319 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, Ops,
1320 ViaVecTy.getVectorNumElements());
1321
1322 if (VecTy != ViaVecTy)
1323 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1324
1325 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001326}
1327
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001328static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1329 unsigned Opc, SDValue Imm,
1330 bool BigEndian) {
1331 EVT VecTy = Op->getValueType(0);
1332 SDValue Exp2Imm;
1333 SDLoc DL(Op);
1334
Daniel Sanders50b80412013-11-15 12:56:49 +00001335 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1336 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001337 if (VecTy == MVT::v2i64) {
1338 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1339 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1340
1341 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001342 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), MVT::i32);
1343
1344 if (BigEndian)
1345 std::swap(BitImmLoOp, BitImmHiOp);
1346
1347 Exp2Imm =
1348 DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
1349 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, BitImmLoOp,
1350 BitImmHiOp, BitImmLoOp, BitImmHiOp));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001351 }
1352 }
1353
1354 if (Exp2Imm.getNode() == NULL) {
1355 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001356
1357 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1358 // only values 0-63 are valid.
1359 if (VecTy == MVT::v2i64)
1360 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1361
1362 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1363
1364 Exp2Imm =
1365 DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, VecTy), Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001366 }
1367
1368 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1369}
1370
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001371static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1372 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001373 SDLoc DL(Op);
Daniel Sanders50b80412013-11-15 12:56:49 +00001374 SDValue One = DAG.getConstant(1, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001375 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
1376
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001377 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1378 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001379}
1380
1381static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1382 SDLoc DL(Op);
1383 EVT ResTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001384 APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
1385 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
1386 SDValue BitMask = DAG.getConstant(~BitImm, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001387
1388 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1389}
1390
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001391SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1392 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001393 SDLoc DL(Op);
1394
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001395 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1396 default:
1397 return SDValue();
1398 case Intrinsic::mips_shilo:
1399 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1400 case Intrinsic::mips_dpau_h_qbl:
1401 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1402 case Intrinsic::mips_dpau_h_qbr:
1403 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1404 case Intrinsic::mips_dpsu_h_qbl:
1405 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1406 case Intrinsic::mips_dpsu_h_qbr:
1407 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1408 case Intrinsic::mips_dpa_w_ph:
1409 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1410 case Intrinsic::mips_dps_w_ph:
1411 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1412 case Intrinsic::mips_dpax_w_ph:
1413 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1414 case Intrinsic::mips_dpsx_w_ph:
1415 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1416 case Intrinsic::mips_mulsa_w_ph:
1417 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1418 case Intrinsic::mips_mult:
1419 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1420 case Intrinsic::mips_multu:
1421 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1422 case Intrinsic::mips_madd:
1423 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1424 case Intrinsic::mips_maddu:
1425 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1426 case Intrinsic::mips_msub:
1427 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1428 case Intrinsic::mips_msubu:
1429 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001430 case Intrinsic::mips_addv_b:
1431 case Intrinsic::mips_addv_h:
1432 case Intrinsic::mips_addv_w:
1433 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001434 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1435 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001436 case Intrinsic::mips_addvi_b:
1437 case Intrinsic::mips_addvi_h:
1438 case Intrinsic::mips_addvi_w:
1439 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001440 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1441 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001442 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001443 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1444 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001445 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001446 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1447 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001448 case Intrinsic::mips_bclr_b:
1449 case Intrinsic::mips_bclr_h:
1450 case Intrinsic::mips_bclr_w:
1451 case Intrinsic::mips_bclr_d:
1452 return lowerMSABitClear(Op, DAG);
1453 case Intrinsic::mips_bclri_b:
1454 case Intrinsic::mips_bclri_h:
1455 case Intrinsic::mips_bclri_w:
1456 case Intrinsic::mips_bclri_d:
1457 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001458 case Intrinsic::mips_binsli_b:
1459 case Intrinsic::mips_binsli_h:
1460 case Intrinsic::mips_binsli_w:
1461 case Intrinsic::mips_binsli_d: {
1462 EVT VecTy = Op->getValueType(0);
1463 EVT EltTy = VecTy.getVectorElementType();
1464 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
1465 Op->getConstantOperandVal(3));
1466 return DAG.getNode(ISD::VSELECT, DL, VecTy,
1467 DAG.getConstant(Mask, VecTy, true), Op->getOperand(1),
1468 Op->getOperand(2));
1469 }
1470 case Intrinsic::mips_binsri_b:
1471 case Intrinsic::mips_binsri_h:
1472 case Intrinsic::mips_binsri_w:
1473 case Intrinsic::mips_binsri_d: {
1474 EVT VecTy = Op->getValueType(0);
1475 EVT EltTy = VecTy.getVectorElementType();
1476 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
1477 Op->getConstantOperandVal(3));
1478 return DAG.getNode(ISD::VSELECT, DL, VecTy,
1479 DAG.getConstant(Mask, VecTy, true), Op->getOperand(1),
1480 Op->getOperand(2));
1481 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001482 case Intrinsic::mips_bmnz_v:
1483 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1484 Op->getOperand(2), Op->getOperand(1));
1485 case Intrinsic::mips_bmnzi_b:
1486 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1487 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1488 Op->getOperand(1));
1489 case Intrinsic::mips_bmz_v:
1490 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1491 Op->getOperand(1), Op->getOperand(2));
1492 case Intrinsic::mips_bmzi_b:
1493 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1494 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1495 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001496 case Intrinsic::mips_bneg_b:
1497 case Intrinsic::mips_bneg_h:
1498 case Intrinsic::mips_bneg_w:
1499 case Intrinsic::mips_bneg_d: {
1500 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001501 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001502
1503 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1504 DAG.getNode(ISD::SHL, DL, VecTy, One,
1505 Op->getOperand(2)));
1506 }
1507 case Intrinsic::mips_bnegi_b:
1508 case Intrinsic::mips_bnegi_h:
1509 case Intrinsic::mips_bnegi_w:
1510 case Intrinsic::mips_bnegi_d:
1511 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
1512 !Subtarget->isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001513 case Intrinsic::mips_bnz_b:
1514 case Intrinsic::mips_bnz_h:
1515 case Intrinsic::mips_bnz_w:
1516 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001517 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1518 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001519 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001520 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1521 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001522 case Intrinsic::mips_bsel_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001523 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sanderse1d24352013-09-24 12:04:44 +00001524 Op->getOperand(1), Op->getOperand(2),
1525 Op->getOperand(3));
1526 case Intrinsic::mips_bseli_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001527 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sanderse1d24352013-09-24 12:04:44 +00001528 Op->getOperand(1), Op->getOperand(2),
1529 lowerMSASplatImm(Op, 3, DAG));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001530 case Intrinsic::mips_bset_b:
1531 case Intrinsic::mips_bset_h:
1532 case Intrinsic::mips_bset_w:
1533 case Intrinsic::mips_bset_d: {
1534 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001535 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001536
1537 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1538 DAG.getNode(ISD::SHL, DL, VecTy, One,
1539 Op->getOperand(2)));
1540 }
1541 case Intrinsic::mips_bseti_b:
1542 case Intrinsic::mips_bseti_h:
1543 case Intrinsic::mips_bseti_w:
1544 case Intrinsic::mips_bseti_d:
1545 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
1546 !Subtarget->isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001547 case Intrinsic::mips_bz_b:
1548 case Intrinsic::mips_bz_h:
1549 case Intrinsic::mips_bz_w:
1550 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001551 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1552 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001553 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001554 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1555 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001556 case Intrinsic::mips_ceq_b:
1557 case Intrinsic::mips_ceq_h:
1558 case Intrinsic::mips_ceq_w:
1559 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001560 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001561 Op->getOperand(2), ISD::SETEQ);
1562 case Intrinsic::mips_ceqi_b:
1563 case Intrinsic::mips_ceqi_h:
1564 case Intrinsic::mips_ceqi_w:
1565 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001566 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001567 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1568 case Intrinsic::mips_cle_s_b:
1569 case Intrinsic::mips_cle_s_h:
1570 case Intrinsic::mips_cle_s_w:
1571 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001572 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001573 Op->getOperand(2), ISD::SETLE);
1574 case Intrinsic::mips_clei_s_b:
1575 case Intrinsic::mips_clei_s_h:
1576 case Intrinsic::mips_clei_s_w:
1577 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001578 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001579 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1580 case Intrinsic::mips_cle_u_b:
1581 case Intrinsic::mips_cle_u_h:
1582 case Intrinsic::mips_cle_u_w:
1583 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001584 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001585 Op->getOperand(2), ISD::SETULE);
1586 case Intrinsic::mips_clei_u_b:
1587 case Intrinsic::mips_clei_u_h:
1588 case Intrinsic::mips_clei_u_w:
1589 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001590 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001591 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1592 case Intrinsic::mips_clt_s_b:
1593 case Intrinsic::mips_clt_s_h:
1594 case Intrinsic::mips_clt_s_w:
1595 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001596 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001597 Op->getOperand(2), ISD::SETLT);
1598 case Intrinsic::mips_clti_s_b:
1599 case Intrinsic::mips_clti_s_h:
1600 case Intrinsic::mips_clti_s_w:
1601 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001602 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001603 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1604 case Intrinsic::mips_clt_u_b:
1605 case Intrinsic::mips_clt_u_h:
1606 case Intrinsic::mips_clt_u_w:
1607 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001608 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001609 Op->getOperand(2), ISD::SETULT);
1610 case Intrinsic::mips_clti_u_b:
1611 case Intrinsic::mips_clti_u_h:
1612 case Intrinsic::mips_clti_u_w:
1613 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001614 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001615 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001616 case Intrinsic::mips_copy_s_b:
1617 case Intrinsic::mips_copy_s_h:
1618 case Intrinsic::mips_copy_s_w:
1619 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001620 case Intrinsic::mips_copy_s_d:
Matheus Almeida74070322014-01-29 14:05:28 +00001621 if (HasMips64)
1622 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1623 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1624 else {
1625 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1626 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1627 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1628 Op->getValueType(0), Op->getOperand(1),
1629 Op->getOperand(2));
1630 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001631 case Intrinsic::mips_copy_u_b:
1632 case Intrinsic::mips_copy_u_h:
1633 case Intrinsic::mips_copy_u_w:
1634 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001635 case Intrinsic::mips_copy_u_d:
Matheus Almeida74070322014-01-29 14:05:28 +00001636 if (HasMips64)
1637 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1638 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1639 else {
1640 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1641 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1642 // Note: When i64 is illegal, this results in copy_s.w instructions
1643 // instead of copy_u.w instructions. This makes no difference to the
1644 // behaviour since i64 is only illegal when the register file is 32-bit.
1645 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1646 Op->getValueType(0), Op->getOperand(1),
1647 Op->getOperand(2));
1648 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001649 case Intrinsic::mips_div_s_b:
1650 case Intrinsic::mips_div_s_h:
1651 case Intrinsic::mips_div_s_w:
1652 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001653 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1654 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001655 case Intrinsic::mips_div_u_b:
1656 case Intrinsic::mips_div_u_h:
1657 case Intrinsic::mips_div_u_w:
1658 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001659 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1660 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001661 case Intrinsic::mips_fadd_w:
1662 case Intrinsic::mips_fadd_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001663 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1664 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001665 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1666 case Intrinsic::mips_fceq_w:
1667 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001668 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001669 Op->getOperand(2), ISD::SETOEQ);
1670 case Intrinsic::mips_fcle_w:
1671 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001672 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001673 Op->getOperand(2), ISD::SETOLE);
1674 case Intrinsic::mips_fclt_w:
1675 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001676 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001677 Op->getOperand(2), ISD::SETOLT);
1678 case Intrinsic::mips_fcne_w:
1679 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001680 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001681 Op->getOperand(2), ISD::SETONE);
1682 case Intrinsic::mips_fcor_w:
1683 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001684 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001685 Op->getOperand(2), ISD::SETO);
1686 case Intrinsic::mips_fcueq_w:
1687 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001688 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001689 Op->getOperand(2), ISD::SETUEQ);
1690 case Intrinsic::mips_fcule_w:
1691 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001692 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001693 Op->getOperand(2), ISD::SETULE);
1694 case Intrinsic::mips_fcult_w:
1695 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001696 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001697 Op->getOperand(2), ISD::SETULT);
1698 case Intrinsic::mips_fcun_w:
1699 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001700 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001701 Op->getOperand(2), ISD::SETUO);
1702 case Intrinsic::mips_fcune_w:
1703 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001704 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001705 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001706 case Intrinsic::mips_fdiv_w:
1707 case Intrinsic::mips_fdiv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001708 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1709 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001710 case Intrinsic::mips_ffint_u_w:
1711 case Intrinsic::mips_ffint_u_d:
1712 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1713 Op->getOperand(1));
1714 case Intrinsic::mips_ffint_s_w:
1715 case Intrinsic::mips_ffint_s_d:
1716 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1717 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001718 case Intrinsic::mips_fill_b:
1719 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001720 case Intrinsic::mips_fill_w:
1721 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001722 SmallVector<SDValue, 16> Ops;
1723 EVT ResTy = Op->getValueType(0);
1724
1725 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1726 Ops.push_back(Op->getOperand(1));
1727
Daniel Sandersc72593e2013-09-27 13:20:41 +00001728 // If ResTy is v2i64 then the type legalizer will break this node down into
1729 // an equivalent v4i32.
1730 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, &Ops[0], Ops.size());
Daniel Sandersf49dd822013-09-24 13:33:07 +00001731 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001732 case Intrinsic::mips_fexp2_w:
1733 case Intrinsic::mips_fexp2_d: {
1734 EVT ResTy = Op->getValueType(0);
1735 return DAG.getNode(
1736 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1737 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1738 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001739 case Intrinsic::mips_flog2_w:
1740 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001741 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001742 case Intrinsic::mips_fmadd_w:
1743 case Intrinsic::mips_fmadd_d:
1744 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1745 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001746 case Intrinsic::mips_fmul_w:
1747 case Intrinsic::mips_fmul_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001748 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1749 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001750 case Intrinsic::mips_fmsub_w:
1751 case Intrinsic::mips_fmsub_d: {
1752 EVT ResTy = Op->getValueType(0);
1753 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1754 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1755 Op->getOperand(2), Op->getOperand(3)));
1756 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001757 case Intrinsic::mips_frint_w:
1758 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001759 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001760 case Intrinsic::mips_fsqrt_w:
1761 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001762 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001763 case Intrinsic::mips_fsub_w:
1764 case Intrinsic::mips_fsub_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001765 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1766 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001767 case Intrinsic::mips_ftrunc_u_w:
1768 case Intrinsic::mips_ftrunc_u_d:
1769 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1770 Op->getOperand(1));
1771 case Intrinsic::mips_ftrunc_s_w:
1772 case Intrinsic::mips_ftrunc_s_d:
1773 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1774 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001775 case Intrinsic::mips_ilvev_b:
1776 case Intrinsic::mips_ilvev_h:
1777 case Intrinsic::mips_ilvev_w:
1778 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001779 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001780 Op->getOperand(1), Op->getOperand(2));
1781 case Intrinsic::mips_ilvl_b:
1782 case Intrinsic::mips_ilvl_h:
1783 case Intrinsic::mips_ilvl_w:
1784 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001785 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001786 Op->getOperand(1), Op->getOperand(2));
1787 case Intrinsic::mips_ilvod_b:
1788 case Intrinsic::mips_ilvod_h:
1789 case Intrinsic::mips_ilvod_w:
1790 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001791 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001792 Op->getOperand(1), Op->getOperand(2));
1793 case Intrinsic::mips_ilvr_b:
1794 case Intrinsic::mips_ilvr_h:
1795 case Intrinsic::mips_ilvr_w:
1796 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001797 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001798 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001799 case Intrinsic::mips_insert_b:
1800 case Intrinsic::mips_insert_h:
1801 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001802 case Intrinsic::mips_insert_d:
1803 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1804 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001805 case Intrinsic::mips_ldi_b:
1806 case Intrinsic::mips_ldi_h:
1807 case Intrinsic::mips_ldi_w:
1808 case Intrinsic::mips_ldi_d:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001809 return lowerMSASplatImm(Op, 1, DAG);
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001810 case Intrinsic::mips_lsa: {
1811 EVT ResTy = Op->getValueType(0);
1812 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1813 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1814 Op->getOperand(2), Op->getOperand(3)));
1815 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001816 case Intrinsic::mips_maddv_b:
1817 case Intrinsic::mips_maddv_h:
1818 case Intrinsic::mips_maddv_w:
1819 case Intrinsic::mips_maddv_d: {
1820 EVT ResTy = Op->getValueType(0);
1821 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1822 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1823 Op->getOperand(2), Op->getOperand(3)));
1824 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001825 case Intrinsic::mips_max_s_b:
1826 case Intrinsic::mips_max_s_h:
1827 case Intrinsic::mips_max_s_w:
1828 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001829 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1830 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001831 case Intrinsic::mips_max_u_b:
1832 case Intrinsic::mips_max_u_h:
1833 case Intrinsic::mips_max_u_w:
1834 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001835 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1836 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001837 case Intrinsic::mips_maxi_s_b:
1838 case Intrinsic::mips_maxi_s_h:
1839 case Intrinsic::mips_maxi_s_w:
1840 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001841 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1842 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001843 case Intrinsic::mips_maxi_u_b:
1844 case Intrinsic::mips_maxi_u_h:
1845 case Intrinsic::mips_maxi_u_w:
1846 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001847 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1848 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001849 case Intrinsic::mips_min_s_b:
1850 case Intrinsic::mips_min_s_h:
1851 case Intrinsic::mips_min_s_w:
1852 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001853 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1854 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001855 case Intrinsic::mips_min_u_b:
1856 case Intrinsic::mips_min_u_h:
1857 case Intrinsic::mips_min_u_w:
1858 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001859 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1860 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001861 case Intrinsic::mips_mini_s_b:
1862 case Intrinsic::mips_mini_s_h:
1863 case Intrinsic::mips_mini_s_w:
1864 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001865 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1866 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001867 case Intrinsic::mips_mini_u_b:
1868 case Intrinsic::mips_mini_u_h:
1869 case Intrinsic::mips_mini_u_w:
1870 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001871 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1872 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001873 case Intrinsic::mips_mod_s_b:
1874 case Intrinsic::mips_mod_s_h:
1875 case Intrinsic::mips_mod_s_w:
1876 case Intrinsic::mips_mod_s_d:
1877 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1878 Op->getOperand(2));
1879 case Intrinsic::mips_mod_u_b:
1880 case Intrinsic::mips_mod_u_h:
1881 case Intrinsic::mips_mod_u_w:
1882 case Intrinsic::mips_mod_u_d:
1883 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1884 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001885 case Intrinsic::mips_mulv_b:
1886 case Intrinsic::mips_mulv_h:
1887 case Intrinsic::mips_mulv_w:
1888 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001889 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1890 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001891 case Intrinsic::mips_msubv_b:
1892 case Intrinsic::mips_msubv_h:
1893 case Intrinsic::mips_msubv_w:
1894 case Intrinsic::mips_msubv_d: {
1895 EVT ResTy = Op->getValueType(0);
1896 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
1897 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1898 Op->getOperand(2), Op->getOperand(3)));
1899 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001900 case Intrinsic::mips_nlzc_b:
1901 case Intrinsic::mips_nlzc_h:
1902 case Intrinsic::mips_nlzc_w:
1903 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001904 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001905 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001906 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1907 Op->getOperand(1), Op->getOperand(2));
1908 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001909 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001910 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001911 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1912 Op->getOperand(1),
1913 lowerMSASplatImm(Op, 2, DAG));
1914 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001915 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001916 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001917 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
1918 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001919 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001920 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1921 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001922 case Intrinsic::mips_pckev_b:
1923 case Intrinsic::mips_pckev_h:
1924 case Intrinsic::mips_pckev_w:
1925 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001926 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001927 Op->getOperand(1), Op->getOperand(2));
1928 case Intrinsic::mips_pckod_b:
1929 case Intrinsic::mips_pckod_h:
1930 case Intrinsic::mips_pckod_w:
1931 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001932 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001933 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00001934 case Intrinsic::mips_pcnt_b:
1935 case Intrinsic::mips_pcnt_h:
1936 case Intrinsic::mips_pcnt_w:
1937 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001938 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sanders26307182013-09-24 14:20:00 +00001939 case Intrinsic::mips_shf_b:
1940 case Intrinsic::mips_shf_h:
1941 case Intrinsic::mips_shf_w:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001942 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00001943 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001944 case Intrinsic::mips_sll_b:
1945 case Intrinsic::mips_sll_h:
1946 case Intrinsic::mips_sll_w:
1947 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001948 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
1949 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001950 case Intrinsic::mips_slli_b:
1951 case Intrinsic::mips_slli_h:
1952 case Intrinsic::mips_slli_w:
1953 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001954 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
1955 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00001956 case Intrinsic::mips_splat_b:
1957 case Intrinsic::mips_splat_h:
1958 case Intrinsic::mips_splat_w:
1959 case Intrinsic::mips_splat_d:
1960 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
1961 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
1962 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
1963 // Instead we lower to MipsISD::VSHF and match from there.
1964 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00001965 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00001966 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00001967 case Intrinsic::mips_splati_b:
1968 case Intrinsic::mips_splati_h:
1969 case Intrinsic::mips_splati_w:
1970 case Intrinsic::mips_splati_d:
1971 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1972 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1973 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001974 case Intrinsic::mips_sra_b:
1975 case Intrinsic::mips_sra_h:
1976 case Intrinsic::mips_sra_w:
1977 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001978 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
1979 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001980 case Intrinsic::mips_srai_b:
1981 case Intrinsic::mips_srai_h:
1982 case Intrinsic::mips_srai_w:
1983 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001984 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
1985 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001986 case Intrinsic::mips_srl_b:
1987 case Intrinsic::mips_srl_h:
1988 case Intrinsic::mips_srl_w:
1989 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001990 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
1991 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001992 case Intrinsic::mips_srli_b:
1993 case Intrinsic::mips_srli_h:
1994 case Intrinsic::mips_srli_w:
1995 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001996 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
1997 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001998 case Intrinsic::mips_subv_b:
1999 case Intrinsic::mips_subv_h:
2000 case Intrinsic::mips_subv_w:
2001 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002002 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2003 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002004 case Intrinsic::mips_subvi_b:
2005 case Intrinsic::mips_subvi_h:
2006 case Intrinsic::mips_subvi_w:
2007 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002008 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2009 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002010 case Intrinsic::mips_vshf_b:
2011 case Intrinsic::mips_vshf_h:
2012 case Intrinsic::mips_vshf_w:
2013 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002014 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002015 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002016 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002017 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2018 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002019 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002020 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2021 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002022 }
2023}
2024
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002025static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2026 SDLoc DL(Op);
2027 SDValue ChainIn = Op->getOperand(0);
2028 SDValue Address = Op->getOperand(2);
2029 SDValue Offset = Op->getOperand(3);
2030 EVT ResTy = Op->getValueType(0);
2031 EVT PtrTy = Address->getValueType(0);
2032
2033 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2034
2035 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
2036 false, false, 16);
2037}
2038
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002039SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2040 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002041 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2042 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002043 default:
2044 return SDValue();
2045 case Intrinsic::mips_extp:
2046 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2047 case Intrinsic::mips_extpdp:
2048 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2049 case Intrinsic::mips_extr_w:
2050 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2051 case Intrinsic::mips_extr_r_w:
2052 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2053 case Intrinsic::mips_extr_rs_w:
2054 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2055 case Intrinsic::mips_extr_s_h:
2056 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2057 case Intrinsic::mips_mthlip:
2058 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2059 case Intrinsic::mips_mulsaq_s_w_ph:
2060 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2061 case Intrinsic::mips_maq_s_w_phl:
2062 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2063 case Intrinsic::mips_maq_s_w_phr:
2064 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2065 case Intrinsic::mips_maq_sa_w_phl:
2066 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2067 case Intrinsic::mips_maq_sa_w_phr:
2068 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2069 case Intrinsic::mips_dpaq_s_w_ph:
2070 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2071 case Intrinsic::mips_dpsq_s_w_ph:
2072 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2073 case Intrinsic::mips_dpaq_sa_l_w:
2074 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2075 case Intrinsic::mips_dpsq_sa_l_w:
2076 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2077 case Intrinsic::mips_dpaqx_s_w_ph:
2078 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2079 case Intrinsic::mips_dpaqx_sa_w_ph:
2080 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2081 case Intrinsic::mips_dpsqx_s_w_ph:
2082 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2083 case Intrinsic::mips_dpsqx_sa_w_ph:
2084 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002085 case Intrinsic::mips_ld_b:
2086 case Intrinsic::mips_ld_h:
2087 case Intrinsic::mips_ld_w:
2088 case Intrinsic::mips_ld_d:
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002089 return lowerMSALoadIntr(Op, DAG, Intr);
2090 }
2091}
2092
2093static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2094 SDLoc DL(Op);
2095 SDValue ChainIn = Op->getOperand(0);
2096 SDValue Value = Op->getOperand(2);
2097 SDValue Address = Op->getOperand(3);
2098 SDValue Offset = Op->getOperand(4);
2099 EVT PtrTy = Address->getValueType(0);
2100
2101 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2102
2103 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
2104 false, 16);
2105}
2106
2107SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2108 SelectionDAG &DAG) const {
2109 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2110 switch (Intr) {
2111 default:
2112 return SDValue();
2113 case Intrinsic::mips_st_b:
2114 case Intrinsic::mips_st_h:
2115 case Intrinsic::mips_st_w:
2116 case Intrinsic::mips_st_d:
Daniel Sandersce09d072013-08-28 12:14:50 +00002117 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002118 }
2119}
2120
Daniel Sanders7a289d02013-09-23 12:02:46 +00002121/// \brief Check if the given BuildVectorSDNode is a splat.
2122/// This method currently relies on DAG nodes being reused when equivalent,
2123/// so it's possible for this to return false even when isConstantSplat returns
2124/// true.
2125static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002126 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002127 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002128
2129 SDValue Operand0 = N->getOperand(0);
2130
2131 for (unsigned int i = 1; i < nOps; ++i) {
2132 if (N->getOperand(i) != Operand0)
2133 return false;
2134 }
2135
2136 return true;
2137}
2138
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002139// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2140//
2141// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2142// choose to sign-extend but we could have equally chosen zero-extend. The
2143// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2144// result into this node later (possibly changing it to a zero-extend in the
2145// process).
2146SDValue MipsSETargetLowering::
2147lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2148 SDLoc DL(Op);
2149 EVT ResTy = Op->getValueType(0);
2150 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002151 EVT VecTy = Op0->getValueType(0);
2152
2153 if (!VecTy.is128BitVector())
2154 return SDValue();
2155
2156 if (ResTy.isInteger()) {
2157 SDValue Op1 = Op->getOperand(1);
2158 EVT EltTy = VecTy.getVectorElementType();
2159 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2160 DAG.getValueType(EltTy));
2161 }
2162
2163 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002164}
2165
Daniel Sandersf49dd822013-09-24 13:33:07 +00002166static bool isConstantOrUndef(const SDValue Op) {
2167 if (Op->getOpcode() == ISD::UNDEF)
2168 return true;
2169 if (dyn_cast<ConstantSDNode>(Op))
2170 return true;
2171 if (dyn_cast<ConstantFPSDNode>(Op))
2172 return true;
2173 return false;
2174}
2175
2176static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2177 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2178 if (isConstantOrUndef(Op->getOperand(i)))
2179 return true;
2180 return false;
2181}
2182
Daniel Sanders7a289d02013-09-23 12:02:46 +00002183// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2184// backend.
2185//
2186// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002187// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2188// 2 less than or equal to 64 and the value fits into a signed 10-bit
2189// immediate
2190// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2191// is a power of 2 less than or equal to 64 and the value does not fit into a
2192// signed 10-bit immediate
2193// - Non-constant splats are legal as-is.
2194// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2195// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002196SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2197 SelectionDAG &DAG) const {
2198 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2199 EVT ResTy = Op->getValueType(0);
2200 SDLoc DL(Op);
2201 APInt SplatValue, SplatUndef;
2202 unsigned SplatBitSize;
2203 bool HasAnyUndefs;
2204
2205 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
2206 return SDValue();
2207
2208 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2209 HasAnyUndefs, 8,
Daniel Sandersf49dd822013-09-24 13:33:07 +00002210 !Subtarget->isLittle()) && SplatBitSize <= 64) {
2211 // We can only cope with 8, 16, 32, or 64-bit elements
2212 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2213 SplatBitSize != 64)
2214 return SDValue();
2215
2216 // If the value fits into a simm10 then we can use ldi.[bhwd]
Daniel Sandersfd8e4162013-11-22 11:24:50 +00002217 // However, if it isn't an integer type we will have to bitcast from an
Daniel Sandersd40aea82013-11-22 13:22:52 +00002218 // integer type first. Also, if there are any undefs, we must lower them
Daniel Sanders630dbe02013-11-22 13:14:06 +00002219 // to defined values first.
2220 if (ResTy.isInteger() && !HasAnyUndefs && SplatValue.isSignedIntN(10))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002221 return Op;
2222
2223 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002224
2225 switch (SplatBitSize) {
2226 default:
2227 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002228 case 8:
2229 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002230 break;
2231 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002232 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002233 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002234 case 32:
2235 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002236 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002237 case 64:
2238 // There's no fill.d to fall back on for 64-bit values
2239 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002240 }
2241
Daniel Sanders50b80412013-11-15 12:56:49 +00002242 // SelectionDAG::getConstant will promote SplatValue appropriately.
2243 SDValue Result = DAG.getConstant(SplatValue, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002244
Daniel Sanders50b80412013-11-15 12:56:49 +00002245 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002246 if (ViaVecTy != ResTy)
2247 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002248
2249 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002250 } else if (isSplatVector(Node))
2251 return Op;
2252 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002253 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2254 // The resulting code is the same length as the expansion, but it doesn't
2255 // use memory operations
2256 EVT ResTy = Node->getValueType(0);
2257
2258 assert(ResTy.isVector());
2259
2260 unsigned NumElts = ResTy.getVectorNumElements();
2261 SDValue Vector = DAG.getUNDEF(ResTy);
2262 for (unsigned i = 0; i < NumElts; ++i) {
2263 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2264 Node->getOperand(i),
2265 DAG.getConstant(i, MVT::i32));
2266 }
2267 return Vector;
2268 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002269
2270 return SDValue();
2271}
2272
Daniel Sanders26307182013-09-24 14:20:00 +00002273// Lower VECTOR_SHUFFLE into SHF (if possible).
2274//
2275// SHF splits the vector into blocks of four elements, then shuffles these
2276// elements according to a <4 x i2> constant (encoded as an integer immediate).
2277//
2278// It is therefore possible to lower into SHF when the mask takes the form:
2279// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2280// When undef's appear they are treated as if they were whatever value is
2281// necessary in order to fit the above form.
2282//
2283// For example:
2284// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2285// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2286// i32 7, i32 6, i32 5, i32 4>
2287// is lowered to:
2288// (SHF_H $w0, $w1, 27)
2289// where the 27 comes from:
2290// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2291static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2292 SmallVector<int, 16> Indices,
2293 SelectionDAG &DAG) {
2294 int SHFIndices[4] = { -1, -1, -1, -1 };
2295
2296 if (Indices.size() < 4)
2297 return SDValue();
2298
2299 for (unsigned i = 0; i < 4; ++i) {
2300 for (unsigned j = i; j < Indices.size(); j += 4) {
2301 int Idx = Indices[j];
2302
2303 // Convert from vector index to 4-element subvector index
2304 // If an index refers to an element outside of the subvector then give up
2305 if (Idx != -1) {
2306 Idx -= 4 * (j / 4);
2307 if (Idx < 0 || Idx >= 4)
2308 return SDValue();
2309 }
2310
2311 // If the mask has an undef, replace it with the current index.
2312 // Note that it might still be undef if the current index is also undef
2313 if (SHFIndices[i] == -1)
2314 SHFIndices[i] = Idx;
2315
2316 // Check that non-undef values are the same as in the mask. If they
2317 // aren't then give up
2318 if (!(Idx == -1 || Idx == SHFIndices[i]))
2319 return SDValue();
2320 }
2321 }
2322
2323 // Calculate the immediate. Replace any remaining undefs with zero
2324 APInt Imm(32, 0);
2325 for (int i = 3; i >= 0; --i) {
2326 int Idx = SHFIndices[i];
2327
2328 if (Idx == -1)
2329 Idx = 0;
2330
2331 Imm <<= 2;
2332 Imm |= Idx & 0x3;
2333 }
2334
2335 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
2336 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
2337}
2338
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002339// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2340//
2341// ILVEV interleaves the even elements from each vector.
2342//
2343// It is possible to lower into ILVEV when the mask takes the form:
2344// <0, n, 2, n+2, 4, n+4, ...>
2345// where n is the number of elements in the vector.
2346//
2347// When undef's appear in the mask they are treated as if they were whatever
2348// value is necessary in order to fit the above form.
2349static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2350 SmallVector<int, 16> Indices,
2351 SelectionDAG &DAG) {
2352 assert ((Indices.size() % 2) == 0);
2353 int WsIdx = 0;
2354 int WtIdx = ResTy.getVectorNumElements();
2355
2356 for (unsigned i = 0; i < Indices.size(); i += 2) {
2357 if (Indices[i] != -1 && Indices[i] != WsIdx)
2358 return SDValue();
2359 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2360 return SDValue();
2361 WsIdx += 2;
2362 WtIdx += 2;
2363 }
2364
2365 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
2366 Op->getOperand(1));
2367}
2368
2369// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2370//
2371// ILVOD interleaves the odd elements from each vector.
2372//
2373// It is possible to lower into ILVOD when the mask takes the form:
2374// <1, n+1, 3, n+3, 5, n+5, ...>
2375// where n is the number of elements in the vector.
2376//
2377// When undef's appear in the mask they are treated as if they were whatever
2378// value is necessary in order to fit the above form.
2379static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2380 SmallVector<int, 16> Indices,
2381 SelectionDAG &DAG) {
2382 assert ((Indices.size() % 2) == 0);
2383 int WsIdx = 1;
2384 int WtIdx = ResTy.getVectorNumElements() + 1;
2385
2386 for (unsigned i = 0; i < Indices.size(); i += 2) {
2387 if (Indices[i] != -1 && Indices[i] != WsIdx)
2388 return SDValue();
2389 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2390 return SDValue();
2391 WsIdx += 2;
2392 WtIdx += 2;
2393 }
2394
2395 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
2396 Op->getOperand(1));
2397}
2398
2399// Lower VECTOR_SHUFFLE into ILVL (if possible).
2400//
2401// ILVL interleaves consecutive elements from the left half of each vector.
2402//
2403// It is possible to lower into ILVL when the mask takes the form:
2404// <0, n, 1, n+1, 2, n+2, ...>
2405// where n is the number of elements in the vector.
2406//
2407// When undef's appear in the mask they are treated as if they were whatever
2408// value is necessary in order to fit the above form.
2409static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2410 SmallVector<int, 16> Indices,
2411 SelectionDAG &DAG) {
2412 assert ((Indices.size() % 2) == 0);
2413 int WsIdx = 0;
2414 int WtIdx = ResTy.getVectorNumElements();
2415
2416 for (unsigned i = 0; i < Indices.size(); i += 2) {
2417 if (Indices[i] != -1 && Indices[i] != WsIdx)
2418 return SDValue();
2419 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2420 return SDValue();
2421 WsIdx ++;
2422 WtIdx ++;
2423 }
2424
2425 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2426 Op->getOperand(1));
2427}
2428
2429// Lower VECTOR_SHUFFLE into ILVR (if possible).
2430//
2431// ILVR interleaves consecutive elements from the right half of each vector.
2432//
2433// It is possible to lower into ILVR when the mask takes the form:
2434// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2435// where n is the number of elements in the vector and x is half n.
2436//
2437// When undef's appear in the mask they are treated as if they were whatever
2438// value is necessary in order to fit the above form.
2439static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2440 SmallVector<int, 16> Indices,
2441 SelectionDAG &DAG) {
2442 assert ((Indices.size() % 2) == 0);
2443 unsigned NumElts = ResTy.getVectorNumElements();
2444 int WsIdx = NumElts / 2;
2445 int WtIdx = NumElts + NumElts / 2;
2446
2447 for (unsigned i = 0; i < Indices.size(); i += 2) {
2448 if (Indices[i] != -1 && Indices[i] != WsIdx)
2449 return SDValue();
2450 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2451 return SDValue();
2452 WsIdx ++;
2453 WtIdx ++;
2454 }
2455
2456 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2457 Op->getOperand(1));
2458}
2459
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002460// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2461//
2462// PCKEV copies the even elements of each vector into the result vector.
2463//
2464// It is possible to lower into PCKEV when the mask takes the form:
2465// <0, 2, 4, ..., n, n+2, n+4, ...>
2466// where n is the number of elements in the vector.
2467//
2468// When undef's appear in the mask they are treated as if they were whatever
2469// value is necessary in order to fit the above form.
2470static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2471 SmallVector<int, 16> Indices,
2472 SelectionDAG &DAG) {
2473 assert ((Indices.size() % 2) == 0);
2474 int Idx = 0;
2475
2476 for (unsigned i = 0; i < Indices.size(); ++i) {
2477 if (Indices[i] != -1 && Indices[i] != Idx)
2478 return SDValue();
2479 Idx += 2;
2480 }
2481
2482 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2483 Op->getOperand(1));
2484}
2485
2486// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2487//
2488// PCKOD copies the odd elements of each vector into the result vector.
2489//
2490// It is possible to lower into PCKOD when the mask takes the form:
2491// <1, 3, 5, ..., n+1, n+3, n+5, ...>
2492// where n is the number of elements in the vector.
2493//
2494// When undef's appear in the mask they are treated as if they were whatever
2495// value is necessary in order to fit the above form.
2496static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2497 SmallVector<int, 16> Indices,
2498 SelectionDAG &DAG) {
2499 assert ((Indices.size() % 2) == 0);
2500 int Idx = 1;
2501
2502 for (unsigned i = 0; i < Indices.size(); ++i) {
2503 if (Indices[i] != -1 && Indices[i] != Idx)
2504 return SDValue();
2505 Idx += 2;
2506 }
2507
2508 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2509 Op->getOperand(1));
2510}
2511
Daniel Sanderse5087042013-09-24 14:02:15 +00002512// Lower VECTOR_SHUFFLE into VSHF.
2513//
2514// This mostly consists of converting the shuffle indices in Indices into a
2515// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2516// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2517// if the type is v8i16 and all the indices are less than 8 then the second
2518// operand is unused and can be replaced with anything. We choose to replace it
2519// with the used operand since this reduces the number of instructions overall.
2520static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2521 SmallVector<int, 16> Indices,
2522 SelectionDAG &DAG) {
2523 SmallVector<SDValue, 16> Ops;
2524 SDValue Op0;
2525 SDValue Op1;
2526 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2527 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2528 bool Using1stVec = false;
2529 bool Using2ndVec = false;
2530 SDLoc DL(Op);
2531 int ResTyNumElts = ResTy.getVectorNumElements();
2532
2533 for (int i = 0; i < ResTyNumElts; ++i) {
2534 // Idx == -1 means UNDEF
2535 int Idx = Indices[i];
2536
2537 if (0 <= Idx && Idx < ResTyNumElts)
2538 Using1stVec = true;
2539 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2540 Using2ndVec = true;
2541 }
2542
2543 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2544 ++I)
2545 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2546
2547 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, &Ops[0],
2548 Ops.size());
2549
2550 if (Using1stVec && Using2ndVec) {
2551 Op0 = Op->getOperand(0);
2552 Op1 = Op->getOperand(1);
2553 } else if (Using1stVec)
2554 Op0 = Op1 = Op->getOperand(0);
2555 else if (Using2ndVec)
2556 Op0 = Op1 = Op->getOperand(1);
2557 else
2558 llvm_unreachable("shuffle vector mask references neither vector operand?");
2559
2560 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op0, Op1);
2561}
2562
2563// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2564// indices in the shuffle.
2565SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2566 SelectionDAG &DAG) const {
2567 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2568 EVT ResTy = Op->getValueType(0);
2569
2570 if (!ResTy.is128BitVector())
2571 return SDValue();
2572
2573 int ResTyNumElts = ResTy.getVectorNumElements();
2574 SmallVector<int, 16> Indices;
2575
2576 for (int i = 0; i < ResTyNumElts; ++i)
2577 Indices.push_back(Node->getMaskElt(i));
2578
Daniel Sanders26307182013-09-24 14:20:00 +00002579 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2580 if (Result.getNode())
2581 return Result;
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002582 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2583 if (Result.getNode())
2584 return Result;
2585 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2586 if (Result.getNode())
2587 return Result;
2588 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2589 if (Result.getNode())
2590 return Result;
2591 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2592 if (Result.getNode())
2593 return Result;
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002594 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2595 if (Result.getNode())
2596 return Result;
2597 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2598 if (Result.getNode())
2599 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002600 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2601}
2602
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002603MachineBasicBlock * MipsSETargetLowering::
2604emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2605 // $bb:
2606 // bposge32_pseudo $vr0
2607 // =>
2608 // $bb:
2609 // bposge32 $tbb
2610 // $fbb:
2611 // li $vr2, 0
2612 // b $sink
2613 // $tbb:
2614 // li $vr1, 1
2615 // $sink:
2616 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2617
2618 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2619 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002620 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002621 DebugLoc DL = MI->getDebugLoc();
2622 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2623 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2624 MachineFunction *F = BB->getParent();
2625 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2626 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2627 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2628 F->insert(It, FBB);
2629 F->insert(It, TBB);
2630 F->insert(It, Sink);
2631
2632 // Transfer the remainder of BB and its successor edges to Sink.
2633 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2634 BB->end());
2635 Sink->transferSuccessorsAndUpdatePHIs(BB);
2636
2637 // Add successors.
2638 BB->addSuccessor(FBB);
2639 BB->addSuccessor(TBB);
2640 FBB->addSuccessor(Sink);
2641 TBB->addSuccessor(Sink);
2642
2643 // Insert the real bposge32 instruction to $BB.
2644 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2645
2646 // Fill $FBB.
2647 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2648 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2649 .addReg(Mips::ZERO).addImm(0);
2650 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2651
2652 // Fill $TBB.
2653 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2654 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2655 .addReg(Mips::ZERO).addImm(1);
2656
2657 // Insert phi function to $Sink.
2658 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2659 MI->getOperand(0).getReg())
2660 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2661
2662 MI->eraseFromParent(); // The pseudo instruction is gone now.
2663 return Sink;
2664}
Daniel Sandersce09d072013-08-28 12:14:50 +00002665
2666MachineBasicBlock * MipsSETargetLowering::
2667emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2668 unsigned BranchOp) const{
2669 // $bb:
2670 // vany_nonzero $rd, $ws
2671 // =>
2672 // $bb:
2673 // bnz.b $ws, $tbb
2674 // b $fbb
2675 // $fbb:
2676 // li $rd1, 0
2677 // b $sink
2678 // $tbb:
2679 // li $rd2, 1
2680 // $sink:
2681 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2682
2683 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2684 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2685 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2686 DebugLoc DL = MI->getDebugLoc();
2687 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2688 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2689 MachineFunction *F = BB->getParent();
2690 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2691 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2692 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2693 F->insert(It, FBB);
2694 F->insert(It, TBB);
2695 F->insert(It, Sink);
2696
2697 // Transfer the remainder of BB and its successor edges to Sink.
2698 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2699 BB->end());
2700 Sink->transferSuccessorsAndUpdatePHIs(BB);
2701
2702 // Add successors.
2703 BB->addSuccessor(FBB);
2704 BB->addSuccessor(TBB);
2705 FBB->addSuccessor(Sink);
2706 TBB->addSuccessor(Sink);
2707
2708 // Insert the real bnz.b instruction to $BB.
2709 BuildMI(BB, DL, TII->get(BranchOp))
2710 .addReg(MI->getOperand(1).getReg())
2711 .addMBB(TBB);
2712
2713 // Fill $FBB.
2714 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2715 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2716 .addReg(Mips::ZERO).addImm(0);
2717 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2718
2719 // Fill $TBB.
2720 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2721 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2722 .addReg(Mips::ZERO).addImm(1);
2723
2724 // Insert phi function to $Sink.
2725 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2726 MI->getOperand(0).getReg())
2727 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2728
2729 MI->eraseFromParent(); // The pseudo instruction is gone now.
2730 return Sink;
2731}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002732
2733// Emit the COPY_FW pseudo instruction.
2734//
2735// copy_fw_pseudo $fd, $ws, n
2736// =>
2737// copy_u_w $rt, $ws, $n
2738// mtc1 $rt, $fd
2739//
2740// When n is zero, the equivalent operation can be performed with (potentially)
2741// zero instructions due to register overlaps. This optimization is never valid
2742// for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2743MachineBasicBlock * MipsSETargetLowering::
2744emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2745 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2746 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2747 DebugLoc DL = MI->getDebugLoc();
2748 unsigned Fd = MI->getOperand(0).getReg();
2749 unsigned Ws = MI->getOperand(1).getReg();
2750 unsigned Lane = MI->getOperand(2).getImm();
2751
2752 if (Lane == 0)
2753 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2754 else {
2755 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2756
2757 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(1);
2758 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2759 }
2760
2761 MI->eraseFromParent(); // The pseudo instruction is gone now.
2762 return BB;
2763}
2764
2765// Emit the COPY_FD pseudo instruction.
2766//
2767// copy_fd_pseudo $fd, $ws, n
2768// =>
2769// splati.d $wt, $ws, $n
2770// copy $fd, $wt:sub_64
2771//
2772// When n is zero, the equivalent operation can be performed with (potentially)
2773// zero instructions due to register overlaps. This optimization is always
2774// valid because FR=1 mode which is the only supported mode in MSA.
2775MachineBasicBlock * MipsSETargetLowering::
2776emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
2777 assert(Subtarget->isFP64bit());
2778
2779 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2780 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2781 unsigned Fd = MI->getOperand(0).getReg();
2782 unsigned Ws = MI->getOperand(1).getReg();
2783 unsigned Lane = MI->getOperand(2).getImm() * 2;
2784 DebugLoc DL = MI->getDebugLoc();
2785
2786 if (Lane == 0)
2787 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2788 else {
2789 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2790
2791 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2792 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2793 }
2794
2795 MI->eraseFromParent(); // The pseudo instruction is gone now.
2796 return BB;
2797}
Daniel Sandersa5150702013-09-27 12:31:32 +00002798
2799// Emit the INSERT_FW pseudo instruction.
2800//
2801// insert_fw_pseudo $wd, $wd_in, $n, $fs
2802// =>
2803// subreg_to_reg $wt:sub_lo, $fs
2804// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002805MachineBasicBlock *
2806MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2807 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002808 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2809 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2810 DebugLoc DL = MI->getDebugLoc();
2811 unsigned Wd = MI->getOperand(0).getReg();
2812 unsigned Wd_in = MI->getOperand(1).getReg();
2813 unsigned Lane = MI->getOperand(2).getImm();
2814 unsigned Fs = MI->getOperand(3).getReg();
2815 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2816
2817 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002818 .addImm(0)
2819 .addReg(Fs)
2820 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00002821 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002822 .addReg(Wd_in)
2823 .addImm(Lane)
2824 .addReg(Wt);
Daniel Sandersa5150702013-09-27 12:31:32 +00002825
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002826 MI->eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00002827 return BB;
2828}
2829
2830// Emit the INSERT_FD pseudo instruction.
2831//
2832// insert_fd_pseudo $wd, $fs, n
2833// =>
2834// subreg_to_reg $wt:sub_64, $fs
2835// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002836MachineBasicBlock *
2837MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2838 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002839 assert(Subtarget->isFP64bit());
2840
2841 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2842 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2843 DebugLoc DL = MI->getDebugLoc();
2844 unsigned Wd = MI->getOperand(0).getReg();
2845 unsigned Wd_in = MI->getOperand(1).getReg();
2846 unsigned Lane = MI->getOperand(2).getImm();
2847 unsigned Fs = MI->getOperand(3).getReg();
2848 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2849
2850 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002851 .addImm(0)
2852 .addReg(Fs)
2853 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00002854 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002855 .addReg(Wd_in)
2856 .addImm(Lane)
2857 .addReg(Wt);
2858
2859 MI->eraseFromParent(); // The pseudo instruction is gone now.
2860 return BB;
2861}
2862
2863// Emit the FILL_FW pseudo instruction.
2864//
2865// fill_fw_pseudo $wd, $fs
2866// =>
2867// implicit_def $wt1
2868// insert_subreg $wt2:subreg_lo, $wt1, $fs
2869// splati.w $wd, $wt2[0]
2870MachineBasicBlock *
2871MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
2872 MachineBasicBlock *BB) const {
2873 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2874 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2875 DebugLoc DL = MI->getDebugLoc();
2876 unsigned Wd = MI->getOperand(0).getReg();
2877 unsigned Fs = MI->getOperand(1).getReg();
2878 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2879 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2880
2881 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2882 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2883 .addReg(Wt1)
2884 .addReg(Fs)
2885 .addImm(Mips::sub_lo);
2886 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
2887
2888 MI->eraseFromParent(); // The pseudo instruction is gone now.
2889 return BB;
2890}
2891
2892// Emit the FILL_FD pseudo instruction.
2893//
2894// fill_fd_pseudo $wd, $fs
2895// =>
2896// implicit_def $wt1
2897// insert_subreg $wt2:subreg_64, $wt1, $fs
2898// splati.d $wd, $wt2[0]
2899MachineBasicBlock *
2900MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
2901 MachineBasicBlock *BB) const {
2902 assert(Subtarget->isFP64bit());
2903
2904 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2905 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2906 DebugLoc DL = MI->getDebugLoc();
2907 unsigned Wd = MI->getOperand(0).getReg();
2908 unsigned Fs = MI->getOperand(1).getReg();
2909 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2910 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2911
2912 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2913 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2914 .addReg(Wt1)
2915 .addReg(Fs)
2916 .addImm(Mips::sub_64);
2917 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00002918
2919 MI->eraseFromParent(); // The pseudo instruction is gone now.
2920 return BB;
2921}
Daniel Sandersa9521602013-10-23 10:36:52 +00002922
2923// Emit the FEXP2_W_1 pseudo instructions.
2924//
2925// fexp2_w_1_pseudo $wd, $wt
2926// =>
2927// ldi.w $ws, 1
2928// fexp2.w $wd, $ws, $wt
2929MachineBasicBlock *
2930MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
2931 MachineBasicBlock *BB) const {
2932 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2933 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2934 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
2935 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2936 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2937 DebugLoc DL = MI->getDebugLoc();
2938
2939 // Splat 1.0 into a vector
2940 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
2941 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
2942
2943 // Emit 1.0 * fexp2(Wt)
2944 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
2945 .addReg(Ws2)
2946 .addReg(MI->getOperand(1).getReg());
2947
2948 MI->eraseFromParent(); // The pseudo instruction is gone now.
2949 return BB;
2950}
2951
2952// Emit the FEXP2_D_1 pseudo instructions.
2953//
2954// fexp2_d_1_pseudo $wd, $wt
2955// =>
2956// ldi.d $ws, 1
2957// fexp2.d $wd, $ws, $wt
2958MachineBasicBlock *
2959MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
2960 MachineBasicBlock *BB) const {
2961 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2962 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2963 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
2964 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2965 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2966 DebugLoc DL = MI->getDebugLoc();
2967
2968 // Splat 1.0 into a vector
2969 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
2970 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
2971
2972 // Emit 1.0 * fexp2(Wt)
2973 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
2974 .addReg(Ws2)
2975 .addReg(MI->getOperand(1).getReg());
2976
2977 MI->eraseFromParent(); // The pseudo instruction is gone now.
2978 return BB;
2979}