blob: c40426d0e2023f2058399b394ed8f343f40f43be [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
Kai Nacke93fe5e82014-03-20 11:51:58 +0000120 if (Subtarget->hasCnMips())
121 setOperationAction(ISD::MUL, MVT::i64, Legal);
122 else if (HasMips64)
123 setOperationAction(ISD::MUL, MVT::i64, Custom);
124
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000125 if (HasMips64) {
126 setOperationAction(ISD::MULHS, MVT::i64, Custom);
127 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000128 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000129
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000130 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
131 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
132
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000133 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
134 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
135 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
136 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000137 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
138 setOperationAction(ISD::LOAD, MVT::i32, Custom);
139 setOperationAction(ISD::STORE, MVT::i32, Custom);
140
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000141 setTargetDAGCombine(ISD::ADDE);
142 setTargetDAGCombine(ISD::SUBE);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000143 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000144
Daniel Sandersce09d072013-08-28 12:14:50 +0000145 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000146 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
147 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
148
Akira Hatanaka63791212013-09-07 00:52:30 +0000149 if (NoDPLoadStore) {
150 setOperationAction(ISD::LOAD, MVT::f64, Custom);
151 setOperationAction(ISD::STORE, MVT::f64, Custom);
152 }
153
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000154 computeRegisterProperties();
155}
156
157const MipsTargetLowering *
158llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
159 return new MipsSETargetLowering(TM);
160}
161
Daniel Sanders7a289d02013-09-23 12:02:46 +0000162// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000163void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000164addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
165 addRegisterClass(Ty, RC);
166
167 // Expand all builtin opcodes.
168 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
169 setOperationAction(Opc, Ty, Expand);
170
171 setOperationAction(ISD::BITCAST, Ty, Legal);
172 setOperationAction(ISD::LOAD, Ty, Legal);
173 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000174 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
175 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000176 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000177
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000178 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000179 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000180 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000181 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000182 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000183 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000184 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000185 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000186 setOperationAction(ISD::SHL, Ty, Legal);
187 setOperationAction(ISD::SRA, Ty, Legal);
188 setOperationAction(ISD::SRL, Ty, Legal);
189 setOperationAction(ISD::SUB, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000190 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000191 setOperationAction(ISD::UREM, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000192 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000193 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000194 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000195
Daniel Sanders015972b2013-10-11 10:00:06 +0000196 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
197 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
198 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
199 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
200 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
201 }
202
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000203 setOperationAction(ISD::SETCC, Ty, Legal);
204 setCondCodeAction(ISD::SETNE, Ty, Expand);
205 setCondCodeAction(ISD::SETGE, Ty, Expand);
206 setCondCodeAction(ISD::SETGT, Ty, Expand);
207 setCondCodeAction(ISD::SETUGE, Ty, Expand);
208 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000209}
210
Daniel Sanders7a289d02013-09-23 12:02:46 +0000211// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000212void MipsSETargetLowering::
213addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000214 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000215
216 // Expand all builtin opcodes.
217 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
218 setOperationAction(Opc, Ty, Expand);
219
220 setOperationAction(ISD::LOAD, Ty, Legal);
221 setOperationAction(ISD::STORE, Ty, Legal);
222 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000223 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000224 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000225 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000226
227 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000228 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000229 setOperationAction(ISD::FADD, Ty, Legal);
230 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000231 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000232 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000233 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000234 setOperationAction(ISD::FMUL, Ty, Legal);
235 setOperationAction(ISD::FRINT, Ty, Legal);
236 setOperationAction(ISD::FSQRT, Ty, Legal);
237 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000238 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000239
240 setOperationAction(ISD::SETCC, Ty, Legal);
241 setCondCodeAction(ISD::SETOGE, Ty, Expand);
242 setCondCodeAction(ISD::SETOGT, Ty, Expand);
243 setCondCodeAction(ISD::SETUGE, Ty, Expand);
244 setCondCodeAction(ISD::SETUGT, Ty, Expand);
245 setCondCodeAction(ISD::SETGE, Ty, Expand);
246 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000247 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000248}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000249
250bool
Matt Arsenault25793a32014-02-05 23:15:53 +0000251MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
252 unsigned,
253 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000254 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
255
256 switch (SVT) {
257 case MVT::i64:
258 case MVT::i32:
259 if (Fast)
260 *Fast = true;
261 return true;
262 default:
263 return false;
264 }
265}
266
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000267SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
268 SelectionDAG &DAG) const {
269 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000270 case ISD::LOAD: return lowerLOAD(Op, DAG);
271 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000272 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
273 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
274 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
275 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
276 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
277 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000278 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
279 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000280 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
281 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000282 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000283 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000284 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000285 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000286 }
287
288 return MipsTargetLowering::LowerOperation(Op, DAG);
289}
290
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000291// selectMADD -
292// Transforms a subgraph in CurDAG if the following pattern is found:
293// (addc multLo, Lo0), (adde multHi, Hi0),
294// where,
295// multHi/Lo: product of multiplication
296// Lo0: initial value of Lo register
297// Hi0: initial value of Hi register
298// Return true if pattern matching was successful.
299static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
300 // ADDENode's second operand must be a flag output of an ADDC node in order
301 // for the matching to be successful.
302 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
303
304 if (ADDCNode->getOpcode() != ISD::ADDC)
305 return false;
306
307 SDValue MultHi = ADDENode->getOperand(0);
308 SDValue MultLo = ADDCNode->getOperand(0);
309 SDNode *MultNode = MultHi.getNode();
310 unsigned MultOpc = MultHi.getOpcode();
311
312 // MultHi and MultLo must be generated by the same node,
313 if (MultLo.getNode() != MultNode)
314 return false;
315
316 // and it must be a multiplication.
317 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
318 return false;
319
320 // MultLo amd MultHi must be the first and second output of MultNode
321 // respectively.
322 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
323 return false;
324
325 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
326 // of the values of MultNode, in which case MultNode will be removed in later
327 // phases.
328 // If there exist users other than ADDENode or ADDCNode, this function returns
329 // here, which will result in MultNode being mapped to a single MULT
330 // instruction node rather than a pair of MULT and MADD instructions being
331 // produced.
332 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
333 return false;
334
Andrew Trickef9de2a2013-05-25 02:42:55 +0000335 SDLoc DL(ADDENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000336
337 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000338 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000339 ADDCNode->getOperand(1),
340 ADDENode->getOperand(1));
341
342 // create MipsMAdd(u) node
343 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
344
345 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
346 MultNode->getOperand(0),// Factor 0
347 MultNode->getOperand(1),// Factor 1
348 ACCIn);
349
350 // replace uses of adde and addc here
351 if (!SDValue(ADDCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000352 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000353 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
354 }
355 if (!SDValue(ADDENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000356 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000357 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
358 }
359
360 return true;
361}
362
363// selectMSUB -
364// Transforms a subgraph in CurDAG if the following pattern is found:
365// (addc Lo0, multLo), (sube Hi0, multHi),
366// where,
367// multHi/Lo: product of multiplication
368// Lo0: initial value of Lo register
369// Hi0: initial value of Hi register
370// Return true if pattern matching was successful.
371static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
372 // SUBENode's second operand must be a flag output of an SUBC node in order
373 // for the matching to be successful.
374 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
375
376 if (SUBCNode->getOpcode() != ISD::SUBC)
377 return false;
378
379 SDValue MultHi = SUBENode->getOperand(1);
380 SDValue MultLo = SUBCNode->getOperand(1);
381 SDNode *MultNode = MultHi.getNode();
382 unsigned MultOpc = MultHi.getOpcode();
383
384 // MultHi and MultLo must be generated by the same node,
385 if (MultLo.getNode() != MultNode)
386 return false;
387
388 // and it must be a multiplication.
389 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
390 return false;
391
392 // MultLo amd MultHi must be the first and second output of MultNode
393 // respectively.
394 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
395 return false;
396
397 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
398 // of the values of MultNode, in which case MultNode will be removed in later
399 // phases.
400 // If there exist users other than SUBENode or SUBCNode, this function returns
401 // here, which will result in MultNode being mapped to a single MULT
402 // instruction node rather than a pair of MULT and MSUB instructions being
403 // produced.
404 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
405 return false;
406
Andrew Trickef9de2a2013-05-25 02:42:55 +0000407 SDLoc DL(SUBENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000408
409 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000410 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000411 SUBCNode->getOperand(0),
412 SUBENode->getOperand(0));
413
414 // create MipsSub(u) node
415 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
416
417 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
418 MultNode->getOperand(0),// Factor 0
419 MultNode->getOperand(1),// Factor 1
420 ACCIn);
421
422 // replace uses of sube and subc here
423 if (!SDValue(SUBCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000424 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000425 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
426 }
427 if (!SDValue(SUBENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000428 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000429 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
430 }
431
432 return true;
433}
434
435static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
436 TargetLowering::DAGCombinerInfo &DCI,
437 const MipsSubtarget *Subtarget) {
438 if (DCI.isBeforeLegalize())
439 return SDValue();
440
441 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
442 selectMADD(N, &DAG))
443 return SDValue(N, 0);
444
445 return SDValue();
446}
447
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000448// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
449//
450// Performs the following transformations:
451// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
452// sign/zero-extension is completely overwritten by the new one performed by
453// the ISD::AND.
454// - Removes redundant zero extensions performed by an ISD::AND.
455static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
456 TargetLowering::DAGCombinerInfo &DCI,
457 const MipsSubtarget *Subtarget) {
458 if (!Subtarget->hasMSA())
459 return SDValue();
460
461 SDValue Op0 = N->getOperand(0);
462 SDValue Op1 = N->getOperand(1);
463 unsigned Op0Opcode = Op0->getOpcode();
464
465 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
466 // where $d + 1 == 2^n and n == 32
467 // or $d + 1 == 2^n and n <= 32 and ZExt
468 // -> (MipsVExtractZExt $a, $b, $c)
469 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
470 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
471 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
472
473 if (!Mask)
474 return SDValue();
475
476 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
477
478 if (Log2IfPositive <= 0)
479 return SDValue(); // Mask+1 is not a power of 2
480
481 SDValue Op0Op2 = Op0->getOperand(2);
482 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
483 unsigned ExtendTySize = ExtendTy.getSizeInBits();
484 unsigned Log2 = Log2IfPositive;
485
486 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
487 Log2 == ExtendTySize) {
488 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
489 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
490 Op0->getVTList(), Ops, Op0->getNumOperands());
491 return Op0;
492 }
493 }
494
495 return SDValue();
496}
497
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000498// Determine if the specified node is a constant vector splat.
499//
500// Returns true and sets Imm if:
501// * N is a ISD::BUILD_VECTOR representing a constant splat
502//
503// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
504// differences are that it assumes the MSA has already been checked and the
505// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
506// must not be in order for binsri.d to be selectable).
507static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
508 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
509
510 if (Node == NULL)
511 return false;
512
513 APInt SplatValue, SplatUndef;
514 unsigned SplatBitSize;
515 bool HasAnyUndefs;
516
517 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
518 8, !IsLittleEndian))
519 return false;
520
521 Imm = SplatValue;
522
523 return true;
524}
525
Daniel Sandersab94b532013-10-30 15:20:38 +0000526// Test whether the given node is an all-ones build_vector.
527static bool isVectorAllOnes(SDValue N) {
528 // Look through bitcasts. Endianness doesn't matter because we are looking
529 // for an all-ones value.
530 if (N->getOpcode() == ISD::BITCAST)
531 N = N->getOperand(0);
532
533 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
534
535 if (!BVN)
536 return false;
537
538 APInt SplatValue, SplatUndef;
539 unsigned SplatBitSize;
540 bool HasAnyUndefs;
541
542 // Endianness doesn't matter in this context because we are looking for
543 // an all-ones value.
544 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
545 return SplatValue.isAllOnesValue();
546
547 return false;
548}
549
550// Test whether N is the bitwise inverse of OfNode.
551static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
552 if (N->getOpcode() != ISD::XOR)
553 return false;
554
555 if (isVectorAllOnes(N->getOperand(0)))
556 return N->getOperand(1) == OfNode;
557
558 if (isVectorAllOnes(N->getOperand(1)))
559 return N->getOperand(0) == OfNode;
560
561 return false;
562}
563
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000564// Perform combines where ISD::OR is the root node.
565//
566// Performs the following transformations:
567// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
568// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
569// vector type.
570static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
571 TargetLowering::DAGCombinerInfo &DCI,
572 const MipsSubtarget *Subtarget) {
573 if (!Subtarget->hasMSA())
574 return SDValue();
575
576 EVT Ty = N->getValueType(0);
577
578 if (!Ty.is128BitVector())
579 return SDValue();
580
581 SDValue Op0 = N->getOperand(0);
582 SDValue Op1 = N->getOperand(1);
583
584 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
585 SDValue Op0Op0 = Op0->getOperand(0);
586 SDValue Op0Op1 = Op0->getOperand(1);
587 SDValue Op1Op0 = Op1->getOperand(0);
588 SDValue Op1Op1 = Op1->getOperand(1);
589 bool IsLittleEndian = !Subtarget->isLittle();
590
591 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000592 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000593 APInt Mask, InvMask;
594
595 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
596 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
597 // looking.
598 // IfClr will be set if we find a valid match.
599 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
600 Cond = Op0Op0;
601 IfSet = Op0Op1;
602
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000603 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
604 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000605 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000606 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
607 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000608 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000609
610 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000611 }
612
613 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
614 // thing again using this mask.
615 // IfClr will be set if we find a valid match.
616 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
617 Cond = Op0Op1;
618 IfSet = Op0Op0;
619
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000620 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
621 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000622 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000623 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
624 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000625 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000626
627 IsConstantMask = true;
628 }
629
630 // If IfClr is not yet set, try looking for a non-constant match.
631 // IfClr will be set if we find a valid match amongst the eight
632 // possibilities.
633 if (!IfClr.getNode()) {
634 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
635 Cond = Op1Op0;
636 IfSet = Op1Op1;
637 IfClr = Op0Op1;
638 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
639 Cond = Op1Op0;
640 IfSet = Op1Op1;
641 IfClr = Op0Op0;
642 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
643 Cond = Op1Op1;
644 IfSet = Op1Op0;
645 IfClr = Op0Op1;
646 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
647 Cond = Op1Op1;
648 IfSet = Op1Op0;
649 IfClr = Op0Op0;
650 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
651 Cond = Op0Op0;
652 IfSet = Op0Op1;
653 IfClr = Op1Op1;
654 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
655 Cond = Op0Op0;
656 IfSet = Op0Op1;
657 IfClr = Op1Op0;
658 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
659 Cond = Op0Op1;
660 IfSet = Op0Op0;
661 IfClr = Op1Op1;
662 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
663 Cond = Op0Op1;
664 IfSet = Op0Op0;
665 IfClr = Op1Op0;
666 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000667 }
668
669 // At this point, IfClr will be set if we have a valid match.
670 if (!IfClr.getNode())
671 return SDValue();
672
673 assert(Cond.getNode() && IfSet.getNode());
674
675 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000676 if (IsConstantMask) {
677 if (Mask.isAllOnesValue())
678 return IfSet;
679 else if (Mask == 0)
680 return IfClr;
681 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000682
683 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000684 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000685 }
686
687 return SDValue();
688}
689
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000690static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
691 TargetLowering::DAGCombinerInfo &DCI,
692 const MipsSubtarget *Subtarget) {
693 if (DCI.isBeforeLegalize())
694 return SDValue();
695
696 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
697 selectMSUB(N, &DAG))
698 return SDValue(N, 0);
699
700 return SDValue();
701}
702
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000703static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
704 EVT ShiftTy, SelectionDAG &DAG) {
705 // Clear the upper (64 - VT.sizeInBits) bits.
706 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
707
708 // Return 0.
709 if (C == 0)
710 return DAG.getConstant(0, VT);
711
712 // Return x.
713 if (C == 1)
714 return X;
715
716 // If c is power of 2, return (shl x, log2(c)).
717 if (isPowerOf2_64(C))
718 return DAG.getNode(ISD::SHL, DL, VT, X,
719 DAG.getConstant(Log2_64(C), ShiftTy));
720
721 unsigned Log2Ceil = Log2_64_Ceil(C);
722 uint64_t Floor = 1LL << Log2_64(C);
723 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
724
725 // If |c - floor_c| <= |c - ceil_c|,
726 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
727 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
728 if (C - Floor <= Ceil - C) {
729 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
730 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
731 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
732 }
733
734 // If |c - floor_c| > |c - ceil_c|,
735 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
736 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
737 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
738 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
739}
740
741static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
742 const TargetLowering::DAGCombinerInfo &DCI,
743 const MipsSETargetLowering *TL) {
744 EVT VT = N->getValueType(0);
745
746 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
747 if (!VT.isVector())
748 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
749 VT, TL->getScalarShiftAmountTy(VT), DAG);
750
751 return SDValue(N, 0);
752}
753
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000754static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
755 SelectionDAG &DAG,
756 const MipsSubtarget *Subtarget) {
757 // See if this is a vector splat immediate node.
758 APInt SplatValue, SplatUndef;
759 unsigned SplatBitSize;
760 bool HasAnyUndefs;
761 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
762 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
763
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000764 if (!Subtarget->hasDSP())
765 return SDValue();
766
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000767 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000768 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000769 EltSize, !Subtarget->isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000770 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000771 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000772 return SDValue();
773
Andrew Trickef9de2a2013-05-25 02:42:55 +0000774 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000775 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
776}
777
778static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
779 TargetLowering::DAGCombinerInfo &DCI,
780 const MipsSubtarget *Subtarget) {
781 EVT Ty = N->getValueType(0);
782
783 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
784 return SDValue();
785
786 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
787}
788
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000789// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
790// constant splats into MipsISD::SHRA_DSP for DSPr2.
791//
792// Performs the following transformations:
793// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
794// sign/zero-extension is completely overwritten by the new one performed by
795// the ISD::SRA and ISD::SHL nodes.
796// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
797// sequence.
798//
799// See performDSPShiftCombine for more information about the transformation
800// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000801static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
802 TargetLowering::DAGCombinerInfo &DCI,
803 const MipsSubtarget *Subtarget) {
804 EVT Ty = N->getValueType(0);
805
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000806 if (Subtarget->hasMSA()) {
807 SDValue Op0 = N->getOperand(0);
808 SDValue Op1 = N->getOperand(1);
809
810 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
811 // where $d + sizeof($c) == 32
812 // or $d + sizeof($c) <= 32 and SExt
813 // -> (MipsVExtractSExt $a, $b, $c)
814 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
815 SDValue Op0Op0 = Op0->getOperand(0);
816 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
817
818 if (!ShAmount)
819 return SDValue();
820
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000821 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
822 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
823 return SDValue();
824
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000825 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
826 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
827
828 if (TotalBits == 32 ||
829 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
830 TotalBits <= 32)) {
831 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
832 Op0Op0->getOperand(2) };
833 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
834 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
835 return Op0Op0;
836 }
837 }
838 }
839
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000840 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
841 return SDValue();
842
843 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
844}
845
846
847static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
848 TargetLowering::DAGCombinerInfo &DCI,
849 const MipsSubtarget *Subtarget) {
850 EVT Ty = N->getValueType(0);
851
852 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
853 return SDValue();
854
855 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
856}
857
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000858static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
859 bool IsV216 = (Ty == MVT::v2i16);
860
861 switch (CC) {
862 case ISD::SETEQ:
863 case ISD::SETNE: return true;
864 case ISD::SETLT:
865 case ISD::SETLE:
866 case ISD::SETGT:
867 case ISD::SETGE: return IsV216;
868 case ISD::SETULT:
869 case ISD::SETULE:
870 case ISD::SETUGT:
871 case ISD::SETUGE: return !IsV216;
872 default: return false;
873 }
874}
875
876static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
877 EVT Ty = N->getValueType(0);
878
879 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
880 return SDValue();
881
882 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
883 return SDValue();
884
Andrew Trickef9de2a2013-05-25 02:42:55 +0000885 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000886 N->getOperand(1), N->getOperand(2));
887}
888
889static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
890 EVT Ty = N->getValueType(0);
891
Daniel Sanders3ce56622013-09-24 12:18:31 +0000892 if (Ty.is128BitVector() && Ty.isInteger()) {
893 // Try the following combines:
894 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
895 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
896 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
897 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
898 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
899 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
900 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
901 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
902 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
903 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
904 // legalizer.
905 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000906
Daniel Sanders3ce56622013-09-24 12:18:31 +0000907 if (Op0->getOpcode() != ISD::SETCC)
908 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000909
Daniel Sanders3ce56622013-09-24 12:18:31 +0000910 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
911 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000912
Daniel Sanders3ce56622013-09-24 12:18:31 +0000913 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
914 Signed = true;
915 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
916 Signed = false;
917 else
918 return SDValue();
919
920 SDValue Op1 = N->getOperand(1);
921 SDValue Op2 = N->getOperand(2);
922 SDValue Op0Op0 = Op0->getOperand(0);
923 SDValue Op0Op1 = Op0->getOperand(1);
924
925 if (Op1 == Op0Op0 && Op2 == Op0Op1)
926 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
927 Ty, Op1, Op2);
928 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
929 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
930 Ty, Op1, Op2);
931 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
932 SDValue SetCC = N->getOperand(0);
933
934 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
935 return SDValue();
936
937 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
938 SetCC.getOperand(0), SetCC.getOperand(1),
939 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
940 }
941
942 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000943}
944
Daniel Sandersf7456c72013-09-23 13:22:24 +0000945static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
946 const MipsSubtarget *Subtarget) {
947 EVT Ty = N->getValueType(0);
948
949 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
950 // Try the following combines:
951 // (xor (or $a, $b), (build_vector allones))
952 // (xor (or $a, $b), (bitcast (build_vector allones)))
953 SDValue Op0 = N->getOperand(0);
954 SDValue Op1 = N->getOperand(1);
955 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000956
957 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
958 NotOp = Op1;
959 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
960 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000961 else
962 return SDValue();
963
964 if (NotOp->getOpcode() == ISD::OR)
965 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
966 NotOp->getOperand(1));
967 }
968
969 return SDValue();
970}
971
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000972SDValue
973MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
974 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000975 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000976
977 switch (N->getOpcode()) {
978 case ISD::ADDE:
979 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000980 case ISD::AND:
981 Val = performANDCombine(N, DAG, DCI, Subtarget);
982 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000983 case ISD::OR:
984 Val = performORCombine(N, DAG, DCI, Subtarget);
985 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000986 case ISD::SUBE:
987 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000988 case ISD::MUL:
989 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000990 case ISD::SHL:
991 return performSHLCombine(N, DAG, DCI, Subtarget);
992 case ISD::SRA:
993 return performSRACombine(N, DAG, DCI, Subtarget);
994 case ISD::SRL:
995 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000996 case ISD::VSELECT:
997 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000998 case ISD::XOR:
999 Val = performXORCombine(N, DAG, Subtarget);
1000 break;
1001 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001002 Val = performSETCCCombine(N, DAG);
1003 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001004 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001005
Daniel Sanders62aeab82013-10-30 13:31:27 +00001006 if (Val.getNode()) {
1007 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1008 N->printrWithDepth(dbgs(), &DAG);
1009 dbgs() << "\n=> \n";
1010 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1011 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001012 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001013 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001014
1015 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001016}
1017
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001018MachineBasicBlock *
1019MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1020 MachineBasicBlock *BB) const {
1021 switch (MI->getOpcode()) {
1022 default:
1023 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1024 case Mips::BPOSGE32_PSEUDO:
1025 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001026 case Mips::SNZ_B_PSEUDO:
1027 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1028 case Mips::SNZ_H_PSEUDO:
1029 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1030 case Mips::SNZ_W_PSEUDO:
1031 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1032 case Mips::SNZ_D_PSEUDO:
1033 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1034 case Mips::SNZ_V_PSEUDO:
1035 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1036 case Mips::SZ_B_PSEUDO:
1037 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1038 case Mips::SZ_H_PSEUDO:
1039 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1040 case Mips::SZ_W_PSEUDO:
1041 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1042 case Mips::SZ_D_PSEUDO:
1043 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1044 case Mips::SZ_V_PSEUDO:
1045 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001046 case Mips::COPY_FW_PSEUDO:
1047 return emitCOPY_FW(MI, BB);
1048 case Mips::COPY_FD_PSEUDO:
1049 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001050 case Mips::INSERT_FW_PSEUDO:
1051 return emitINSERT_FW(MI, BB);
1052 case Mips::INSERT_FD_PSEUDO:
1053 return emitINSERT_FD(MI, BB);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001054 case Mips::FILL_FW_PSEUDO:
1055 return emitFILL_FW(MI, BB);
1056 case Mips::FILL_FD_PSEUDO:
1057 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001058 case Mips::FEXP2_W_1_PSEUDO:
1059 return emitFEXP2_W_1(MI, BB);
1060 case Mips::FEXP2_D_1_PSEUDO:
1061 return emitFEXP2_D_1(MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001062 }
1063}
1064
1065bool MipsSETargetLowering::
1066isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
1067 unsigned NextStackOffset,
1068 const MipsFunctionInfo& FI) const {
1069 if (!EnableMipsTailCalls)
1070 return false;
1071
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001072 // Return false if either the callee or caller has a byval argument.
1073 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
1074 return false;
1075
1076 // Return true if the callee's argument area is no larger than the
1077 // caller's.
1078 return NextStackOffset <= FI.getIncomingArgSize();
1079}
1080
1081void MipsSETargetLowering::
1082getOpndList(SmallVectorImpl<SDValue> &Ops,
1083 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
1084 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
1085 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001086 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001087 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
1088 InternalLinkage, CLI, Callee, Chain);
1089}
1090
Akira Hatanaka63791212013-09-07 00:52:30 +00001091SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1092 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1093
1094 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1095 return MipsTargetLowering::lowerLOAD(Op, DAG);
1096
1097 // Replace a double precision load with two i32 loads and a buildpair64.
1098 SDLoc DL(Op);
1099 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1100 EVT PtrVT = Ptr.getValueType();
1101
1102 // i32 load from lower address.
1103 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1104 MachinePointerInfo(), Nd.isVolatile(),
1105 Nd.isNonTemporal(), Nd.isInvariant(),
1106 Nd.getAlignment());
1107
1108 // i32 load from higher address.
1109 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1110 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1111 MachinePointerInfo(), Nd.isVolatile(),
1112 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001113 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka63791212013-09-07 00:52:30 +00001114
1115 if (!Subtarget->isLittle())
1116 std::swap(Lo, Hi);
1117
1118 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1119 SDValue Ops[2] = {BP, Hi.getValue(1)};
1120 return DAG.getMergeValues(Ops, 2, DL);
1121}
1122
1123SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1124 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1125
1126 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1127 return MipsTargetLowering::lowerSTORE(Op, DAG);
1128
1129 // Replace a double precision store with two extractelement64s and i32 stores.
1130 SDLoc DL(Op);
1131 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1132 EVT PtrVT = Ptr.getValueType();
1133 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1134 Val, DAG.getConstant(0, MVT::i32));
1135 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1136 Val, DAG.getConstant(1, MVT::i32));
1137
1138 if (!Subtarget->isLittle())
1139 std::swap(Lo, Hi);
1140
1141 // i32 store to lower address.
1142 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1143 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
1144 Nd.getTBAAInfo());
1145
1146 // i32 store to higher address.
1147 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1148 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001149 Nd.isVolatile(), Nd.isNonTemporal(),
1150 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001151}
1152
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001153SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1154 bool HasLo, bool HasHi,
1155 SelectionDAG &DAG) const {
1156 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001157 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001158 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1159 Op.getOperand(0), Op.getOperand(1));
1160 SDValue Lo, Hi;
1161
1162 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001163 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001164 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001165 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001166
1167 if (!HasLo || !HasHi)
1168 return HasLo ? Lo : Hi;
1169
1170 SDValue Vals[] = { Lo, Hi };
1171 return DAG.getMergeValues(Vals, 2, DL);
1172}
1173
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001174
Andrew Trickef9de2a2013-05-25 02:42:55 +00001175static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001176 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1177 DAG.getConstant(0, MVT::i32));
1178 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1179 DAG.getConstant(1, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001180 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001181}
1182
Andrew Trickef9de2a2013-05-25 02:42:55 +00001183static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001184 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1185 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001186 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1187}
1188
1189// This function expands mips intrinsic nodes which have 64-bit input operands
1190// or output values.
1191//
1192// out64 = intrinsic-node in64
1193// =>
1194// lo = copy (extract-element (in64, 0))
1195// hi = copy (extract-element (in64, 1))
1196// mips-specific-node
1197// v0 = copy lo
1198// v1 = copy hi
1199// out64 = merge-values (v0, v1)
1200//
1201static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001202 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001203 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1204 SmallVector<SDValue, 3> Ops;
1205 unsigned OpNo = 0;
1206
1207 // See if Op has a chain input.
1208 if (HasChainIn)
1209 Ops.push_back(Op->getOperand(OpNo++));
1210
1211 // The next operand is the intrinsic opcode.
1212 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1213
1214 // See if the next operand has type i64.
1215 SDValue Opnd = Op->getOperand(++OpNo), In64;
1216
1217 if (Opnd.getValueType() == MVT::i64)
1218 In64 = initAccumulator(Opnd, DL, DAG);
1219 else
1220 Ops.push_back(Opnd);
1221
1222 // Push the remaining operands.
1223 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1224 Ops.push_back(Op->getOperand(OpNo));
1225
1226 // Add In64 to the end of the list.
1227 if (In64.getNode())
1228 Ops.push_back(In64);
1229
1230 // Scan output.
1231 SmallVector<EVT, 2> ResTys;
1232
1233 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1234 I != E; ++I)
1235 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1236
1237 // Create node.
1238 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1239 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1240
1241 if (!HasChainIn)
1242 return Out;
1243
1244 assert(Val->getValueType(1) == MVT::Other);
1245 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1246 return DAG.getMergeValues(Vals, 2, DL);
1247}
1248
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001249// Lower an MSA copy intrinsic into the specified SelectionDAG node
1250static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1251 SDLoc DL(Op);
1252 SDValue Vec = Op->getOperand(1);
1253 SDValue Idx = Op->getOperand(2);
1254 EVT ResTy = Op->getValueType(0);
1255 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1256
1257 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1258 DAG.getValueType(EltTy));
1259
1260 return Result;
1261}
1262
Daniel Sanders50b80412013-11-15 12:56:49 +00001263static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1264 EVT ResVecTy = Op->getValueType(0);
1265 EVT ViaVecTy = ResVecTy;
1266 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001267
Daniel Sanders50b80412013-11-15 12:56:49 +00001268 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1269 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1270 // lanes.
1271 SDValue LaneA;
1272 SDValue LaneB = Op->getOperand(2);
1273
1274 if (ResVecTy == MVT::v2i64) {
1275 LaneA = DAG.getConstant(0, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001276 ViaVecTy = MVT::v4i32;
Daniel Sanders50b80412013-11-15 12:56:49 +00001277 } else
1278 LaneA = LaneB;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001279
Daniel Sanders50b80412013-11-15 12:56:49 +00001280 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1281 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001282
Daniel Sanders50b80412013-11-15 12:56:49 +00001283 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, Ops,
1284 ViaVecTy.getVectorNumElements());
1285
1286 if (ViaVecTy != ResVecTy)
1287 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001288
1289 return Result;
1290}
1291
Daniel Sanders50b80412013-11-15 12:56:49 +00001292static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1293 return DAG.getConstant(Op->getConstantOperandVal(ImmOp), Op->getValueType(0));
1294}
1295
1296static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1297 bool BigEndian, SelectionDAG &DAG) {
1298 EVT ViaVecTy = VecTy;
1299 SDValue SplatValueA = SplatValue;
1300 SDValue SplatValueB = SplatValue;
1301 SDLoc DL(SplatValue);
1302
1303 if (VecTy == MVT::v2i64) {
1304 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1305 ViaVecTy = MVT::v4i32;
1306
1307 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1308 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
1309 DAG.getConstant(32, MVT::i32));
1310 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1311 }
1312
1313 // We currently hold the parts in little endian order. Swap them if
1314 // necessary.
1315 if (BigEndian)
1316 std::swap(SplatValueA, SplatValueB);
1317
1318 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1319 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1320 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1321 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1322
1323 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, Ops,
1324 ViaVecTy.getVectorNumElements());
1325
1326 if (VecTy != ViaVecTy)
1327 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1328
1329 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001330}
1331
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001332static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1333 unsigned Opc, SDValue Imm,
1334 bool BigEndian) {
1335 EVT VecTy = Op->getValueType(0);
1336 SDValue Exp2Imm;
1337 SDLoc DL(Op);
1338
Daniel Sanders50b80412013-11-15 12:56:49 +00001339 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1340 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001341 if (VecTy == MVT::v2i64) {
1342 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1343 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1344
1345 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001346 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), MVT::i32);
1347
1348 if (BigEndian)
1349 std::swap(BitImmLoOp, BitImmHiOp);
1350
1351 Exp2Imm =
1352 DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
1353 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, BitImmLoOp,
1354 BitImmHiOp, BitImmLoOp, BitImmHiOp));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001355 }
1356 }
1357
1358 if (Exp2Imm.getNode() == NULL) {
1359 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001360
1361 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1362 // only values 0-63 are valid.
1363 if (VecTy == MVT::v2i64)
1364 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1365
1366 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1367
1368 Exp2Imm =
1369 DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, VecTy), Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001370 }
1371
1372 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1373}
1374
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001375static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1376 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001377 SDLoc DL(Op);
Daniel Sanders50b80412013-11-15 12:56:49 +00001378 SDValue One = DAG.getConstant(1, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001379 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
1380
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001381 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1382 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001383}
1384
1385static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1386 SDLoc DL(Op);
1387 EVT ResTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001388 APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
1389 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
1390 SDValue BitMask = DAG.getConstant(~BitImm, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001391
1392 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1393}
1394
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001395SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1396 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001397 SDLoc DL(Op);
1398
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001399 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1400 default:
1401 return SDValue();
1402 case Intrinsic::mips_shilo:
1403 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1404 case Intrinsic::mips_dpau_h_qbl:
1405 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1406 case Intrinsic::mips_dpau_h_qbr:
1407 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1408 case Intrinsic::mips_dpsu_h_qbl:
1409 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1410 case Intrinsic::mips_dpsu_h_qbr:
1411 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1412 case Intrinsic::mips_dpa_w_ph:
1413 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1414 case Intrinsic::mips_dps_w_ph:
1415 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1416 case Intrinsic::mips_dpax_w_ph:
1417 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1418 case Intrinsic::mips_dpsx_w_ph:
1419 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1420 case Intrinsic::mips_mulsa_w_ph:
1421 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1422 case Intrinsic::mips_mult:
1423 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1424 case Intrinsic::mips_multu:
1425 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1426 case Intrinsic::mips_madd:
1427 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1428 case Intrinsic::mips_maddu:
1429 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1430 case Intrinsic::mips_msub:
1431 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1432 case Intrinsic::mips_msubu:
1433 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001434 case Intrinsic::mips_addv_b:
1435 case Intrinsic::mips_addv_h:
1436 case Intrinsic::mips_addv_w:
1437 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001438 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1439 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001440 case Intrinsic::mips_addvi_b:
1441 case Intrinsic::mips_addvi_h:
1442 case Intrinsic::mips_addvi_w:
1443 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001444 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1445 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001446 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001447 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1448 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001449 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001450 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1451 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001452 case Intrinsic::mips_bclr_b:
1453 case Intrinsic::mips_bclr_h:
1454 case Intrinsic::mips_bclr_w:
1455 case Intrinsic::mips_bclr_d:
1456 return lowerMSABitClear(Op, DAG);
1457 case Intrinsic::mips_bclri_b:
1458 case Intrinsic::mips_bclri_h:
1459 case Intrinsic::mips_bclri_w:
1460 case Intrinsic::mips_bclri_d:
1461 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001462 case Intrinsic::mips_binsli_b:
1463 case Intrinsic::mips_binsli_h:
1464 case Intrinsic::mips_binsli_w:
1465 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001466 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001467 EVT VecTy = Op->getValueType(0);
1468 EVT EltTy = VecTy.getVectorElementType();
1469 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
1470 Op->getConstantOperandVal(3));
1471 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Daniel Sandersdf2215452014-03-12 11:54:00 +00001472 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1473 Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001474 }
1475 case Intrinsic::mips_binsri_b:
1476 case Intrinsic::mips_binsri_h:
1477 case Intrinsic::mips_binsri_w:
1478 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001479 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001480 EVT VecTy = Op->getValueType(0);
1481 EVT EltTy = VecTy.getVectorElementType();
1482 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
1483 Op->getConstantOperandVal(3));
1484 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Daniel Sandersdf2215452014-03-12 11:54:00 +00001485 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1486 Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001487 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001488 case Intrinsic::mips_bmnz_v:
1489 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1490 Op->getOperand(2), Op->getOperand(1));
1491 case Intrinsic::mips_bmnzi_b:
1492 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1493 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1494 Op->getOperand(1));
1495 case Intrinsic::mips_bmz_v:
1496 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1497 Op->getOperand(1), Op->getOperand(2));
1498 case Intrinsic::mips_bmzi_b:
1499 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1500 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1501 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001502 case Intrinsic::mips_bneg_b:
1503 case Intrinsic::mips_bneg_h:
1504 case Intrinsic::mips_bneg_w:
1505 case Intrinsic::mips_bneg_d: {
1506 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001507 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001508
1509 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1510 DAG.getNode(ISD::SHL, DL, VecTy, One,
1511 Op->getOperand(2)));
1512 }
1513 case Intrinsic::mips_bnegi_b:
1514 case Intrinsic::mips_bnegi_h:
1515 case Intrinsic::mips_bnegi_w:
1516 case Intrinsic::mips_bnegi_d:
1517 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
1518 !Subtarget->isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001519 case Intrinsic::mips_bnz_b:
1520 case Intrinsic::mips_bnz_h:
1521 case Intrinsic::mips_bnz_w:
1522 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001523 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1524 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001525 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001526 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1527 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001528 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001529 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001530 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001531 Op->getOperand(1), Op->getOperand(3),
1532 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001533 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001534 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001535 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001536 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1537 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001538 case Intrinsic::mips_bset_b:
1539 case Intrinsic::mips_bset_h:
1540 case Intrinsic::mips_bset_w:
1541 case Intrinsic::mips_bset_d: {
1542 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001543 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001544
1545 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1546 DAG.getNode(ISD::SHL, DL, VecTy, One,
1547 Op->getOperand(2)));
1548 }
1549 case Intrinsic::mips_bseti_b:
1550 case Intrinsic::mips_bseti_h:
1551 case Intrinsic::mips_bseti_w:
1552 case Intrinsic::mips_bseti_d:
1553 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
1554 !Subtarget->isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001555 case Intrinsic::mips_bz_b:
1556 case Intrinsic::mips_bz_h:
1557 case Intrinsic::mips_bz_w:
1558 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001559 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1560 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001561 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001562 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1563 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001564 case Intrinsic::mips_ceq_b:
1565 case Intrinsic::mips_ceq_h:
1566 case Intrinsic::mips_ceq_w:
1567 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001568 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001569 Op->getOperand(2), ISD::SETEQ);
1570 case Intrinsic::mips_ceqi_b:
1571 case Intrinsic::mips_ceqi_h:
1572 case Intrinsic::mips_ceqi_w:
1573 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001574 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001575 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1576 case Intrinsic::mips_cle_s_b:
1577 case Intrinsic::mips_cle_s_h:
1578 case Intrinsic::mips_cle_s_w:
1579 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001580 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001581 Op->getOperand(2), ISD::SETLE);
1582 case Intrinsic::mips_clei_s_b:
1583 case Intrinsic::mips_clei_s_h:
1584 case Intrinsic::mips_clei_s_w:
1585 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001586 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001587 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1588 case Intrinsic::mips_cle_u_b:
1589 case Intrinsic::mips_cle_u_h:
1590 case Intrinsic::mips_cle_u_w:
1591 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001592 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001593 Op->getOperand(2), ISD::SETULE);
1594 case Intrinsic::mips_clei_u_b:
1595 case Intrinsic::mips_clei_u_h:
1596 case Intrinsic::mips_clei_u_w:
1597 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001598 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001599 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1600 case Intrinsic::mips_clt_s_b:
1601 case Intrinsic::mips_clt_s_h:
1602 case Intrinsic::mips_clt_s_w:
1603 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001604 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001605 Op->getOperand(2), ISD::SETLT);
1606 case Intrinsic::mips_clti_s_b:
1607 case Intrinsic::mips_clti_s_h:
1608 case Intrinsic::mips_clti_s_w:
1609 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001610 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001611 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1612 case Intrinsic::mips_clt_u_b:
1613 case Intrinsic::mips_clt_u_h:
1614 case Intrinsic::mips_clt_u_w:
1615 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001616 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001617 Op->getOperand(2), ISD::SETULT);
1618 case Intrinsic::mips_clti_u_b:
1619 case Intrinsic::mips_clti_u_h:
1620 case Intrinsic::mips_clti_u_w:
1621 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001622 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001623 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001624 case Intrinsic::mips_copy_s_b:
1625 case Intrinsic::mips_copy_s_h:
1626 case Intrinsic::mips_copy_s_w:
1627 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001628 case Intrinsic::mips_copy_s_d:
Matheus Almeida74070322014-01-29 14:05:28 +00001629 if (HasMips64)
1630 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1631 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1632 else {
1633 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1634 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1635 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1636 Op->getValueType(0), Op->getOperand(1),
1637 Op->getOperand(2));
1638 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001639 case Intrinsic::mips_copy_u_b:
1640 case Intrinsic::mips_copy_u_h:
1641 case Intrinsic::mips_copy_u_w:
1642 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001643 case Intrinsic::mips_copy_u_d:
Matheus Almeida74070322014-01-29 14:05:28 +00001644 if (HasMips64)
1645 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1646 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1647 else {
1648 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1649 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1650 // Note: When i64 is illegal, this results in copy_s.w instructions
1651 // instead of copy_u.w instructions. This makes no difference to the
1652 // behaviour since i64 is only illegal when the register file is 32-bit.
1653 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1654 Op->getValueType(0), Op->getOperand(1),
1655 Op->getOperand(2));
1656 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001657 case Intrinsic::mips_div_s_b:
1658 case Intrinsic::mips_div_s_h:
1659 case Intrinsic::mips_div_s_w:
1660 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001661 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1662 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001663 case Intrinsic::mips_div_u_b:
1664 case Intrinsic::mips_div_u_h:
1665 case Intrinsic::mips_div_u_w:
1666 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001667 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1668 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001669 case Intrinsic::mips_fadd_w:
1670 case Intrinsic::mips_fadd_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001671 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1672 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001673 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1674 case Intrinsic::mips_fceq_w:
1675 case Intrinsic::mips_fceq_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::SETOEQ);
1678 case Intrinsic::mips_fcle_w:
1679 case Intrinsic::mips_fcle_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::SETOLE);
1682 case Intrinsic::mips_fclt_w:
1683 case Intrinsic::mips_fclt_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::SETOLT);
1686 case Intrinsic::mips_fcne_w:
1687 case Intrinsic::mips_fcne_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::SETONE);
1690 case Intrinsic::mips_fcor_w:
1691 case Intrinsic::mips_fcor_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::SETO);
1694 case Intrinsic::mips_fcueq_w:
1695 case Intrinsic::mips_fcueq_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::SETUEQ);
1698 case Intrinsic::mips_fcule_w:
1699 case Intrinsic::mips_fcule_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::SETULE);
1702 case Intrinsic::mips_fcult_w:
1703 case Intrinsic::mips_fcult_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::SETULT);
1706 case Intrinsic::mips_fcun_w:
1707 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001708 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001709 Op->getOperand(2), ISD::SETUO);
1710 case Intrinsic::mips_fcune_w:
1711 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001712 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001713 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001714 case Intrinsic::mips_fdiv_w:
1715 case Intrinsic::mips_fdiv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001716 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1717 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001718 case Intrinsic::mips_ffint_u_w:
1719 case Intrinsic::mips_ffint_u_d:
1720 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1721 Op->getOperand(1));
1722 case Intrinsic::mips_ffint_s_w:
1723 case Intrinsic::mips_ffint_s_d:
1724 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1725 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001726 case Intrinsic::mips_fill_b:
1727 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001728 case Intrinsic::mips_fill_w:
1729 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001730 SmallVector<SDValue, 16> Ops;
1731 EVT ResTy = Op->getValueType(0);
1732
1733 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1734 Ops.push_back(Op->getOperand(1));
1735
Daniel Sandersc72593e2013-09-27 13:20:41 +00001736 // If ResTy is v2i64 then the type legalizer will break this node down into
1737 // an equivalent v4i32.
1738 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, &Ops[0], Ops.size());
Daniel Sandersf49dd822013-09-24 13:33:07 +00001739 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001740 case Intrinsic::mips_fexp2_w:
1741 case Intrinsic::mips_fexp2_d: {
1742 EVT ResTy = Op->getValueType(0);
1743 return DAG.getNode(
1744 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1745 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1746 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001747 case Intrinsic::mips_flog2_w:
1748 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001749 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001750 case Intrinsic::mips_fmadd_w:
1751 case Intrinsic::mips_fmadd_d:
1752 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1753 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001754 case Intrinsic::mips_fmul_w:
1755 case Intrinsic::mips_fmul_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001756 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1757 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001758 case Intrinsic::mips_fmsub_w:
1759 case Intrinsic::mips_fmsub_d: {
1760 EVT ResTy = Op->getValueType(0);
1761 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1762 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1763 Op->getOperand(2), Op->getOperand(3)));
1764 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001765 case Intrinsic::mips_frint_w:
1766 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001767 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001768 case Intrinsic::mips_fsqrt_w:
1769 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001770 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001771 case Intrinsic::mips_fsub_w:
1772 case Intrinsic::mips_fsub_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001773 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1774 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001775 case Intrinsic::mips_ftrunc_u_w:
1776 case Intrinsic::mips_ftrunc_u_d:
1777 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1778 Op->getOperand(1));
1779 case Intrinsic::mips_ftrunc_s_w:
1780 case Intrinsic::mips_ftrunc_s_d:
1781 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1782 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001783 case Intrinsic::mips_ilvev_b:
1784 case Intrinsic::mips_ilvev_h:
1785 case Intrinsic::mips_ilvev_w:
1786 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001787 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001788 Op->getOperand(1), Op->getOperand(2));
1789 case Intrinsic::mips_ilvl_b:
1790 case Intrinsic::mips_ilvl_h:
1791 case Intrinsic::mips_ilvl_w:
1792 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001793 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001794 Op->getOperand(1), Op->getOperand(2));
1795 case Intrinsic::mips_ilvod_b:
1796 case Intrinsic::mips_ilvod_h:
1797 case Intrinsic::mips_ilvod_w:
1798 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001799 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001800 Op->getOperand(1), Op->getOperand(2));
1801 case Intrinsic::mips_ilvr_b:
1802 case Intrinsic::mips_ilvr_h:
1803 case Intrinsic::mips_ilvr_w:
1804 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001805 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001806 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001807 case Intrinsic::mips_insert_b:
1808 case Intrinsic::mips_insert_h:
1809 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001810 case Intrinsic::mips_insert_d:
1811 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1812 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001813 case Intrinsic::mips_ldi_b:
1814 case Intrinsic::mips_ldi_h:
1815 case Intrinsic::mips_ldi_w:
1816 case Intrinsic::mips_ldi_d:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001817 return lowerMSASplatImm(Op, 1, DAG);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001818 case Intrinsic::mips_lsa:
1819 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001820 EVT ResTy = Op->getValueType(0);
1821 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1822 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1823 Op->getOperand(2), Op->getOperand(3)));
1824 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001825 case Intrinsic::mips_maddv_b:
1826 case Intrinsic::mips_maddv_h:
1827 case Intrinsic::mips_maddv_w:
1828 case Intrinsic::mips_maddv_d: {
1829 EVT ResTy = Op->getValueType(0);
1830 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1831 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1832 Op->getOperand(2), Op->getOperand(3)));
1833 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001834 case Intrinsic::mips_max_s_b:
1835 case Intrinsic::mips_max_s_h:
1836 case Intrinsic::mips_max_s_w:
1837 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001838 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1839 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001840 case Intrinsic::mips_max_u_b:
1841 case Intrinsic::mips_max_u_h:
1842 case Intrinsic::mips_max_u_w:
1843 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001844 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1845 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001846 case Intrinsic::mips_maxi_s_b:
1847 case Intrinsic::mips_maxi_s_h:
1848 case Intrinsic::mips_maxi_s_w:
1849 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001850 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1851 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001852 case Intrinsic::mips_maxi_u_b:
1853 case Intrinsic::mips_maxi_u_h:
1854 case Intrinsic::mips_maxi_u_w:
1855 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001856 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1857 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001858 case Intrinsic::mips_min_s_b:
1859 case Intrinsic::mips_min_s_h:
1860 case Intrinsic::mips_min_s_w:
1861 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001862 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1863 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001864 case Intrinsic::mips_min_u_b:
1865 case Intrinsic::mips_min_u_h:
1866 case Intrinsic::mips_min_u_w:
1867 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001868 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1869 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001870 case Intrinsic::mips_mini_s_b:
1871 case Intrinsic::mips_mini_s_h:
1872 case Intrinsic::mips_mini_s_w:
1873 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001874 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1875 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001876 case Intrinsic::mips_mini_u_b:
1877 case Intrinsic::mips_mini_u_h:
1878 case Intrinsic::mips_mini_u_w:
1879 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001880 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1881 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001882 case Intrinsic::mips_mod_s_b:
1883 case Intrinsic::mips_mod_s_h:
1884 case Intrinsic::mips_mod_s_w:
1885 case Intrinsic::mips_mod_s_d:
1886 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1887 Op->getOperand(2));
1888 case Intrinsic::mips_mod_u_b:
1889 case Intrinsic::mips_mod_u_h:
1890 case Intrinsic::mips_mod_u_w:
1891 case Intrinsic::mips_mod_u_d:
1892 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1893 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001894 case Intrinsic::mips_mulv_b:
1895 case Intrinsic::mips_mulv_h:
1896 case Intrinsic::mips_mulv_w:
1897 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001898 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1899 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001900 case Intrinsic::mips_msubv_b:
1901 case Intrinsic::mips_msubv_h:
1902 case Intrinsic::mips_msubv_w:
1903 case Intrinsic::mips_msubv_d: {
1904 EVT ResTy = Op->getValueType(0);
1905 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
1906 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1907 Op->getOperand(2), Op->getOperand(3)));
1908 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001909 case Intrinsic::mips_nlzc_b:
1910 case Intrinsic::mips_nlzc_h:
1911 case Intrinsic::mips_nlzc_w:
1912 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001913 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001914 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001915 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1916 Op->getOperand(1), Op->getOperand(2));
1917 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001918 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001919 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001920 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1921 Op->getOperand(1),
1922 lowerMSASplatImm(Op, 2, DAG));
1923 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001924 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001925 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001926 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
1927 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001928 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001929 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1930 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001931 case Intrinsic::mips_pckev_b:
1932 case Intrinsic::mips_pckev_h:
1933 case Intrinsic::mips_pckev_w:
1934 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001935 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001936 Op->getOperand(1), Op->getOperand(2));
1937 case Intrinsic::mips_pckod_b:
1938 case Intrinsic::mips_pckod_h:
1939 case Intrinsic::mips_pckod_w:
1940 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001941 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001942 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00001943 case Intrinsic::mips_pcnt_b:
1944 case Intrinsic::mips_pcnt_h:
1945 case Intrinsic::mips_pcnt_w:
1946 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001947 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sanders26307182013-09-24 14:20:00 +00001948 case Intrinsic::mips_shf_b:
1949 case Intrinsic::mips_shf_h:
1950 case Intrinsic::mips_shf_w:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001951 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00001952 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001953 case Intrinsic::mips_sll_b:
1954 case Intrinsic::mips_sll_h:
1955 case Intrinsic::mips_sll_w:
1956 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001957 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
1958 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001959 case Intrinsic::mips_slli_b:
1960 case Intrinsic::mips_slli_h:
1961 case Intrinsic::mips_slli_w:
1962 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001963 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
1964 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00001965 case Intrinsic::mips_splat_b:
1966 case Intrinsic::mips_splat_h:
1967 case Intrinsic::mips_splat_w:
1968 case Intrinsic::mips_splat_d:
1969 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
1970 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
1971 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
1972 // Instead we lower to MipsISD::VSHF and match from there.
1973 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00001974 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00001975 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00001976 case Intrinsic::mips_splati_b:
1977 case Intrinsic::mips_splati_h:
1978 case Intrinsic::mips_splati_w:
1979 case Intrinsic::mips_splati_d:
1980 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1981 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1982 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001983 case Intrinsic::mips_sra_b:
1984 case Intrinsic::mips_sra_h:
1985 case Intrinsic::mips_sra_w:
1986 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001987 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
1988 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001989 case Intrinsic::mips_srai_b:
1990 case Intrinsic::mips_srai_h:
1991 case Intrinsic::mips_srai_w:
1992 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001993 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
1994 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001995 case Intrinsic::mips_srl_b:
1996 case Intrinsic::mips_srl_h:
1997 case Intrinsic::mips_srl_w:
1998 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001999 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
2000 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002001 case Intrinsic::mips_srli_b:
2002 case Intrinsic::mips_srli_h:
2003 case Intrinsic::mips_srli_w:
2004 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002005 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2006 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002007 case Intrinsic::mips_subv_b:
2008 case Intrinsic::mips_subv_h:
2009 case Intrinsic::mips_subv_w:
2010 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002011 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2012 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002013 case Intrinsic::mips_subvi_b:
2014 case Intrinsic::mips_subvi_h:
2015 case Intrinsic::mips_subvi_w:
2016 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002017 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2018 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002019 case Intrinsic::mips_vshf_b:
2020 case Intrinsic::mips_vshf_h:
2021 case Intrinsic::mips_vshf_w:
2022 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002023 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002024 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002025 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002026 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2027 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002028 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002029 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2030 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002031 }
2032}
2033
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002034static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2035 SDLoc DL(Op);
2036 SDValue ChainIn = Op->getOperand(0);
2037 SDValue Address = Op->getOperand(2);
2038 SDValue Offset = Op->getOperand(3);
2039 EVT ResTy = Op->getValueType(0);
2040 EVT PtrTy = Address->getValueType(0);
2041
2042 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2043
2044 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
2045 false, false, 16);
2046}
2047
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002048SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2049 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002050 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2051 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002052 default:
2053 return SDValue();
2054 case Intrinsic::mips_extp:
2055 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2056 case Intrinsic::mips_extpdp:
2057 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2058 case Intrinsic::mips_extr_w:
2059 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2060 case Intrinsic::mips_extr_r_w:
2061 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2062 case Intrinsic::mips_extr_rs_w:
2063 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2064 case Intrinsic::mips_extr_s_h:
2065 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2066 case Intrinsic::mips_mthlip:
2067 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2068 case Intrinsic::mips_mulsaq_s_w_ph:
2069 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2070 case Intrinsic::mips_maq_s_w_phl:
2071 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2072 case Intrinsic::mips_maq_s_w_phr:
2073 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2074 case Intrinsic::mips_maq_sa_w_phl:
2075 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2076 case Intrinsic::mips_maq_sa_w_phr:
2077 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2078 case Intrinsic::mips_dpaq_s_w_ph:
2079 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2080 case Intrinsic::mips_dpsq_s_w_ph:
2081 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2082 case Intrinsic::mips_dpaq_sa_l_w:
2083 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2084 case Intrinsic::mips_dpsq_sa_l_w:
2085 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2086 case Intrinsic::mips_dpaqx_s_w_ph:
2087 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2088 case Intrinsic::mips_dpaqx_sa_w_ph:
2089 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2090 case Intrinsic::mips_dpsqx_s_w_ph:
2091 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2092 case Intrinsic::mips_dpsqx_sa_w_ph:
2093 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002094 case Intrinsic::mips_ld_b:
2095 case Intrinsic::mips_ld_h:
2096 case Intrinsic::mips_ld_w:
2097 case Intrinsic::mips_ld_d:
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002098 return lowerMSALoadIntr(Op, DAG, Intr);
2099 }
2100}
2101
2102static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2103 SDLoc DL(Op);
2104 SDValue ChainIn = Op->getOperand(0);
2105 SDValue Value = Op->getOperand(2);
2106 SDValue Address = Op->getOperand(3);
2107 SDValue Offset = Op->getOperand(4);
2108 EVT PtrTy = Address->getValueType(0);
2109
2110 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2111
2112 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
2113 false, 16);
2114}
2115
2116SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2117 SelectionDAG &DAG) const {
2118 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2119 switch (Intr) {
2120 default:
2121 return SDValue();
2122 case Intrinsic::mips_st_b:
2123 case Intrinsic::mips_st_h:
2124 case Intrinsic::mips_st_w:
2125 case Intrinsic::mips_st_d:
Daniel Sandersce09d072013-08-28 12:14:50 +00002126 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002127 }
2128}
2129
Daniel Sanders7a289d02013-09-23 12:02:46 +00002130/// \brief Check if the given BuildVectorSDNode is a splat.
2131/// This method currently relies on DAG nodes being reused when equivalent,
2132/// so it's possible for this to return false even when isConstantSplat returns
2133/// true.
2134static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002135 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002136 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002137
2138 SDValue Operand0 = N->getOperand(0);
2139
2140 for (unsigned int i = 1; i < nOps; ++i) {
2141 if (N->getOperand(i) != Operand0)
2142 return false;
2143 }
2144
2145 return true;
2146}
2147
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002148// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2149//
2150// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2151// choose to sign-extend but we could have equally chosen zero-extend. The
2152// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2153// result into this node later (possibly changing it to a zero-extend in the
2154// process).
2155SDValue MipsSETargetLowering::
2156lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2157 SDLoc DL(Op);
2158 EVT ResTy = Op->getValueType(0);
2159 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002160 EVT VecTy = Op0->getValueType(0);
2161
2162 if (!VecTy.is128BitVector())
2163 return SDValue();
2164
2165 if (ResTy.isInteger()) {
2166 SDValue Op1 = Op->getOperand(1);
2167 EVT EltTy = VecTy.getVectorElementType();
2168 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2169 DAG.getValueType(EltTy));
2170 }
2171
2172 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002173}
2174
Daniel Sandersf49dd822013-09-24 13:33:07 +00002175static bool isConstantOrUndef(const SDValue Op) {
2176 if (Op->getOpcode() == ISD::UNDEF)
2177 return true;
2178 if (dyn_cast<ConstantSDNode>(Op))
2179 return true;
2180 if (dyn_cast<ConstantFPSDNode>(Op))
2181 return true;
2182 return false;
2183}
2184
2185static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2186 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2187 if (isConstantOrUndef(Op->getOperand(i)))
2188 return true;
2189 return false;
2190}
2191
Daniel Sanders7a289d02013-09-23 12:02:46 +00002192// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2193// backend.
2194//
2195// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002196// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2197// 2 less than or equal to 64 and the value fits into a signed 10-bit
2198// immediate
2199// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2200// is a power of 2 less than or equal to 64 and the value does not fit into a
2201// signed 10-bit immediate
2202// - Non-constant splats are legal as-is.
2203// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2204// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002205SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2206 SelectionDAG &DAG) const {
2207 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2208 EVT ResTy = Op->getValueType(0);
2209 SDLoc DL(Op);
2210 APInt SplatValue, SplatUndef;
2211 unsigned SplatBitSize;
2212 bool HasAnyUndefs;
2213
2214 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
2215 return SDValue();
2216
2217 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2218 HasAnyUndefs, 8,
Daniel Sandersf49dd822013-09-24 13:33:07 +00002219 !Subtarget->isLittle()) && SplatBitSize <= 64) {
2220 // We can only cope with 8, 16, 32, or 64-bit elements
2221 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2222 SplatBitSize != 64)
2223 return SDValue();
2224
2225 // If the value fits into a simm10 then we can use ldi.[bhwd]
Daniel Sandersfd8e4162013-11-22 11:24:50 +00002226 // However, if it isn't an integer type we will have to bitcast from an
Daniel Sandersd40aea82013-11-22 13:22:52 +00002227 // integer type first. Also, if there are any undefs, we must lower them
Daniel Sanders630dbe02013-11-22 13:14:06 +00002228 // to defined values first.
2229 if (ResTy.isInteger() && !HasAnyUndefs && SplatValue.isSignedIntN(10))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002230 return Op;
2231
2232 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002233
2234 switch (SplatBitSize) {
2235 default:
2236 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002237 case 8:
2238 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002239 break;
2240 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002241 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002242 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002243 case 32:
2244 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002245 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002246 case 64:
2247 // There's no fill.d to fall back on for 64-bit values
2248 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002249 }
2250
Daniel Sanders50b80412013-11-15 12:56:49 +00002251 // SelectionDAG::getConstant will promote SplatValue appropriately.
2252 SDValue Result = DAG.getConstant(SplatValue, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002253
Daniel Sanders50b80412013-11-15 12:56:49 +00002254 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002255 if (ViaVecTy != ResTy)
2256 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002257
2258 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002259 } else if (isSplatVector(Node))
2260 return Op;
2261 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002262 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2263 // The resulting code is the same length as the expansion, but it doesn't
2264 // use memory operations
2265 EVT ResTy = Node->getValueType(0);
2266
2267 assert(ResTy.isVector());
2268
2269 unsigned NumElts = ResTy.getVectorNumElements();
2270 SDValue Vector = DAG.getUNDEF(ResTy);
2271 for (unsigned i = 0; i < NumElts; ++i) {
2272 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2273 Node->getOperand(i),
2274 DAG.getConstant(i, MVT::i32));
2275 }
2276 return Vector;
2277 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002278
2279 return SDValue();
2280}
2281
Daniel Sanders26307182013-09-24 14:20:00 +00002282// Lower VECTOR_SHUFFLE into SHF (if possible).
2283//
2284// SHF splits the vector into blocks of four elements, then shuffles these
2285// elements according to a <4 x i2> constant (encoded as an integer immediate).
2286//
2287// It is therefore possible to lower into SHF when the mask takes the form:
2288// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2289// When undef's appear they are treated as if they were whatever value is
2290// necessary in order to fit the above form.
2291//
2292// For example:
2293// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2294// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2295// i32 7, i32 6, i32 5, i32 4>
2296// is lowered to:
2297// (SHF_H $w0, $w1, 27)
2298// where the 27 comes from:
2299// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2300static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2301 SmallVector<int, 16> Indices,
2302 SelectionDAG &DAG) {
2303 int SHFIndices[4] = { -1, -1, -1, -1 };
2304
2305 if (Indices.size() < 4)
2306 return SDValue();
2307
2308 for (unsigned i = 0; i < 4; ++i) {
2309 for (unsigned j = i; j < Indices.size(); j += 4) {
2310 int Idx = Indices[j];
2311
2312 // Convert from vector index to 4-element subvector index
2313 // If an index refers to an element outside of the subvector then give up
2314 if (Idx != -1) {
2315 Idx -= 4 * (j / 4);
2316 if (Idx < 0 || Idx >= 4)
2317 return SDValue();
2318 }
2319
2320 // If the mask has an undef, replace it with the current index.
2321 // Note that it might still be undef if the current index is also undef
2322 if (SHFIndices[i] == -1)
2323 SHFIndices[i] = Idx;
2324
2325 // Check that non-undef values are the same as in the mask. If they
2326 // aren't then give up
2327 if (!(Idx == -1 || Idx == SHFIndices[i]))
2328 return SDValue();
2329 }
2330 }
2331
2332 // Calculate the immediate. Replace any remaining undefs with zero
2333 APInt Imm(32, 0);
2334 for (int i = 3; i >= 0; --i) {
2335 int Idx = SHFIndices[i];
2336
2337 if (Idx == -1)
2338 Idx = 0;
2339
2340 Imm <<= 2;
2341 Imm |= Idx & 0x3;
2342 }
2343
2344 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
2345 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
2346}
2347
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002348// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2349//
2350// ILVEV interleaves the even elements from each vector.
2351//
2352// It is possible to lower into ILVEV when the mask takes the form:
2353// <0, n, 2, n+2, 4, n+4, ...>
2354// where n is the number of elements in the vector.
2355//
2356// When undef's appear in the mask they are treated as if they were whatever
2357// value is necessary in order to fit the above form.
2358static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2359 SmallVector<int, 16> Indices,
2360 SelectionDAG &DAG) {
2361 assert ((Indices.size() % 2) == 0);
2362 int WsIdx = 0;
2363 int WtIdx = ResTy.getVectorNumElements();
2364
2365 for (unsigned i = 0; i < Indices.size(); i += 2) {
2366 if (Indices[i] != -1 && Indices[i] != WsIdx)
2367 return SDValue();
2368 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2369 return SDValue();
2370 WsIdx += 2;
2371 WtIdx += 2;
2372 }
2373
2374 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
2375 Op->getOperand(1));
2376}
2377
2378// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2379//
2380// ILVOD interleaves the odd elements from each vector.
2381//
2382// It is possible to lower into ILVOD when the mask takes the form:
2383// <1, n+1, 3, n+3, 5, n+5, ...>
2384// where n is the number of elements in the vector.
2385//
2386// When undef's appear in the mask they are treated as if they were whatever
2387// value is necessary in order to fit the above form.
2388static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2389 SmallVector<int, 16> Indices,
2390 SelectionDAG &DAG) {
2391 assert ((Indices.size() % 2) == 0);
2392 int WsIdx = 1;
2393 int WtIdx = ResTy.getVectorNumElements() + 1;
2394
2395 for (unsigned i = 0; i < Indices.size(); i += 2) {
2396 if (Indices[i] != -1 && Indices[i] != WsIdx)
2397 return SDValue();
2398 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2399 return SDValue();
2400 WsIdx += 2;
2401 WtIdx += 2;
2402 }
2403
2404 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
2405 Op->getOperand(1));
2406}
2407
2408// Lower VECTOR_SHUFFLE into ILVL (if possible).
2409//
2410// ILVL interleaves consecutive elements from the left half of each vector.
2411//
2412// It is possible to lower into ILVL when the mask takes the form:
2413// <0, n, 1, n+1, 2, n+2, ...>
2414// where n is the number of elements in the vector.
2415//
2416// When undef's appear in the mask they are treated as if they were whatever
2417// value is necessary in order to fit the above form.
2418static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2419 SmallVector<int, 16> Indices,
2420 SelectionDAG &DAG) {
2421 assert ((Indices.size() % 2) == 0);
2422 int WsIdx = 0;
2423 int WtIdx = ResTy.getVectorNumElements();
2424
2425 for (unsigned i = 0; i < Indices.size(); i += 2) {
2426 if (Indices[i] != -1 && Indices[i] != WsIdx)
2427 return SDValue();
2428 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2429 return SDValue();
2430 WsIdx ++;
2431 WtIdx ++;
2432 }
2433
2434 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2435 Op->getOperand(1));
2436}
2437
2438// Lower VECTOR_SHUFFLE into ILVR (if possible).
2439//
2440// ILVR interleaves consecutive elements from the right half of each vector.
2441//
2442// It is possible to lower into ILVR when the mask takes the form:
2443// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2444// where n is the number of elements in the vector and x is half n.
2445//
2446// When undef's appear in the mask they are treated as if they were whatever
2447// value is necessary in order to fit the above form.
2448static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2449 SmallVector<int, 16> Indices,
2450 SelectionDAG &DAG) {
2451 assert ((Indices.size() % 2) == 0);
2452 unsigned NumElts = ResTy.getVectorNumElements();
2453 int WsIdx = NumElts / 2;
2454 int WtIdx = NumElts + NumElts / 2;
2455
2456 for (unsigned i = 0; i < Indices.size(); i += 2) {
2457 if (Indices[i] != -1 && Indices[i] != WsIdx)
2458 return SDValue();
2459 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2460 return SDValue();
2461 WsIdx ++;
2462 WtIdx ++;
2463 }
2464
2465 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2466 Op->getOperand(1));
2467}
2468
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002469// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2470//
2471// PCKEV copies the even elements of each vector into the result vector.
2472//
2473// It is possible to lower into PCKEV when the mask takes the form:
2474// <0, 2, 4, ..., n, n+2, n+4, ...>
2475// where n is the number of elements in the vector.
2476//
2477// When undef's appear in the mask they are treated as if they were whatever
2478// value is necessary in order to fit the above form.
2479static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2480 SmallVector<int, 16> Indices,
2481 SelectionDAG &DAG) {
2482 assert ((Indices.size() % 2) == 0);
2483 int Idx = 0;
2484
2485 for (unsigned i = 0; i < Indices.size(); ++i) {
2486 if (Indices[i] != -1 && Indices[i] != Idx)
2487 return SDValue();
2488 Idx += 2;
2489 }
2490
2491 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2492 Op->getOperand(1));
2493}
2494
2495// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2496//
2497// PCKOD copies the odd elements of each vector into the result vector.
2498//
2499// It is possible to lower into PCKOD when the mask takes the form:
2500// <1, 3, 5, ..., n+1, n+3, n+5, ...>
2501// where n is the number of elements in the vector.
2502//
2503// When undef's appear in the mask they are treated as if they were whatever
2504// value is necessary in order to fit the above form.
2505static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2506 SmallVector<int, 16> Indices,
2507 SelectionDAG &DAG) {
2508 assert ((Indices.size() % 2) == 0);
2509 int Idx = 1;
2510
2511 for (unsigned i = 0; i < Indices.size(); ++i) {
2512 if (Indices[i] != -1 && Indices[i] != Idx)
2513 return SDValue();
2514 Idx += 2;
2515 }
2516
2517 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2518 Op->getOperand(1));
2519}
2520
Daniel Sanderse5087042013-09-24 14:02:15 +00002521// Lower VECTOR_SHUFFLE into VSHF.
2522//
2523// This mostly consists of converting the shuffle indices in Indices into a
2524// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2525// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2526// if the type is v8i16 and all the indices are less than 8 then the second
2527// operand is unused and can be replaced with anything. We choose to replace it
2528// with the used operand since this reduces the number of instructions overall.
2529static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2530 SmallVector<int, 16> Indices,
2531 SelectionDAG &DAG) {
2532 SmallVector<SDValue, 16> Ops;
2533 SDValue Op0;
2534 SDValue Op1;
2535 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2536 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2537 bool Using1stVec = false;
2538 bool Using2ndVec = false;
2539 SDLoc DL(Op);
2540 int ResTyNumElts = ResTy.getVectorNumElements();
2541
2542 for (int i = 0; i < ResTyNumElts; ++i) {
2543 // Idx == -1 means UNDEF
2544 int Idx = Indices[i];
2545
2546 if (0 <= Idx && Idx < ResTyNumElts)
2547 Using1stVec = true;
2548 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2549 Using2ndVec = true;
2550 }
2551
2552 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2553 ++I)
2554 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2555
2556 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, &Ops[0],
2557 Ops.size());
2558
2559 if (Using1stVec && Using2ndVec) {
2560 Op0 = Op->getOperand(0);
2561 Op1 = Op->getOperand(1);
2562 } else if (Using1stVec)
2563 Op0 = Op1 = Op->getOperand(0);
2564 else if (Using2ndVec)
2565 Op0 = Op1 = Op->getOperand(1);
2566 else
2567 llvm_unreachable("shuffle vector mask references neither vector operand?");
2568
2569 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op0, Op1);
2570}
2571
2572// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2573// indices in the shuffle.
2574SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2575 SelectionDAG &DAG) const {
2576 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2577 EVT ResTy = Op->getValueType(0);
2578
2579 if (!ResTy.is128BitVector())
2580 return SDValue();
2581
2582 int ResTyNumElts = ResTy.getVectorNumElements();
2583 SmallVector<int, 16> Indices;
2584
2585 for (int i = 0; i < ResTyNumElts; ++i)
2586 Indices.push_back(Node->getMaskElt(i));
2587
Daniel Sanders26307182013-09-24 14:20:00 +00002588 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2589 if (Result.getNode())
2590 return Result;
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002591 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2592 if (Result.getNode())
2593 return Result;
2594 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2595 if (Result.getNode())
2596 return Result;
2597 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2598 if (Result.getNode())
2599 return Result;
2600 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2601 if (Result.getNode())
2602 return Result;
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002603 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2604 if (Result.getNode())
2605 return Result;
2606 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2607 if (Result.getNode())
2608 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002609 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2610}
2611
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002612MachineBasicBlock * MipsSETargetLowering::
2613emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2614 // $bb:
2615 // bposge32_pseudo $vr0
2616 // =>
2617 // $bb:
2618 // bposge32 $tbb
2619 // $fbb:
2620 // li $vr2, 0
2621 // b $sink
2622 // $tbb:
2623 // li $vr1, 1
2624 // $sink:
2625 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2626
2627 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2628 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002629 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002630 DebugLoc DL = MI->getDebugLoc();
2631 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002632 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002633 MachineFunction *F = BB->getParent();
2634 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2635 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2636 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2637 F->insert(It, FBB);
2638 F->insert(It, TBB);
2639 F->insert(It, Sink);
2640
2641 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002642 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002643 BB->end());
2644 Sink->transferSuccessorsAndUpdatePHIs(BB);
2645
2646 // Add successors.
2647 BB->addSuccessor(FBB);
2648 BB->addSuccessor(TBB);
2649 FBB->addSuccessor(Sink);
2650 TBB->addSuccessor(Sink);
2651
2652 // Insert the real bposge32 instruction to $BB.
2653 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2654
2655 // Fill $FBB.
2656 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2657 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2658 .addReg(Mips::ZERO).addImm(0);
2659 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2660
2661 // Fill $TBB.
2662 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2663 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2664 .addReg(Mips::ZERO).addImm(1);
2665
2666 // Insert phi function to $Sink.
2667 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2668 MI->getOperand(0).getReg())
2669 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2670
2671 MI->eraseFromParent(); // The pseudo instruction is gone now.
2672 return Sink;
2673}
Daniel Sandersce09d072013-08-28 12:14:50 +00002674
2675MachineBasicBlock * MipsSETargetLowering::
2676emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2677 unsigned BranchOp) const{
2678 // $bb:
2679 // vany_nonzero $rd, $ws
2680 // =>
2681 // $bb:
2682 // bnz.b $ws, $tbb
2683 // b $fbb
2684 // $fbb:
2685 // li $rd1, 0
2686 // b $sink
2687 // $tbb:
2688 // li $rd2, 1
2689 // $sink:
2690 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2691
2692 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2693 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2694 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2695 DebugLoc DL = MI->getDebugLoc();
2696 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002697 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00002698 MachineFunction *F = BB->getParent();
2699 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2700 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2701 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2702 F->insert(It, FBB);
2703 F->insert(It, TBB);
2704 F->insert(It, Sink);
2705
2706 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002707 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00002708 BB->end());
2709 Sink->transferSuccessorsAndUpdatePHIs(BB);
2710
2711 // Add successors.
2712 BB->addSuccessor(FBB);
2713 BB->addSuccessor(TBB);
2714 FBB->addSuccessor(Sink);
2715 TBB->addSuccessor(Sink);
2716
2717 // Insert the real bnz.b instruction to $BB.
2718 BuildMI(BB, DL, TII->get(BranchOp))
2719 .addReg(MI->getOperand(1).getReg())
2720 .addMBB(TBB);
2721
2722 // Fill $FBB.
2723 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2724 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2725 .addReg(Mips::ZERO).addImm(0);
2726 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2727
2728 // Fill $TBB.
2729 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2730 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2731 .addReg(Mips::ZERO).addImm(1);
2732
2733 // Insert phi function to $Sink.
2734 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2735 MI->getOperand(0).getReg())
2736 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2737
2738 MI->eraseFromParent(); // The pseudo instruction is gone now.
2739 return Sink;
2740}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002741
2742// Emit the COPY_FW pseudo instruction.
2743//
2744// copy_fw_pseudo $fd, $ws, n
2745// =>
2746// copy_u_w $rt, $ws, $n
2747// mtc1 $rt, $fd
2748//
2749// When n is zero, the equivalent operation can be performed with (potentially)
2750// zero instructions due to register overlaps. This optimization is never valid
2751// for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2752MachineBasicBlock * MipsSETargetLowering::
2753emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2754 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2755 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2756 DebugLoc DL = MI->getDebugLoc();
2757 unsigned Fd = MI->getOperand(0).getReg();
2758 unsigned Ws = MI->getOperand(1).getReg();
2759 unsigned Lane = MI->getOperand(2).getImm();
2760
2761 if (Lane == 0)
2762 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2763 else {
2764 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2765
Daniel Sandersd9207702014-03-04 13:54:30 +00002766 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002767 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2768 }
2769
2770 MI->eraseFromParent(); // The pseudo instruction is gone now.
2771 return BB;
2772}
2773
2774// Emit the COPY_FD pseudo instruction.
2775//
2776// copy_fd_pseudo $fd, $ws, n
2777// =>
2778// splati.d $wt, $ws, $n
2779// copy $fd, $wt:sub_64
2780//
2781// When n is zero, the equivalent operation can be performed with (potentially)
2782// zero instructions due to register overlaps. This optimization is always
2783// valid because FR=1 mode which is the only supported mode in MSA.
2784MachineBasicBlock * MipsSETargetLowering::
2785emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
2786 assert(Subtarget->isFP64bit());
2787
2788 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2789 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2790 unsigned Fd = MI->getOperand(0).getReg();
2791 unsigned Ws = MI->getOperand(1).getReg();
2792 unsigned Lane = MI->getOperand(2).getImm() * 2;
2793 DebugLoc DL = MI->getDebugLoc();
2794
2795 if (Lane == 0)
2796 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2797 else {
2798 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2799
2800 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2801 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2802 }
2803
2804 MI->eraseFromParent(); // The pseudo instruction is gone now.
2805 return BB;
2806}
Daniel Sandersa5150702013-09-27 12:31:32 +00002807
2808// Emit the INSERT_FW pseudo instruction.
2809//
2810// insert_fw_pseudo $wd, $wd_in, $n, $fs
2811// =>
2812// subreg_to_reg $wt:sub_lo, $fs
2813// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002814MachineBasicBlock *
2815MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2816 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002817 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2818 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2819 DebugLoc DL = MI->getDebugLoc();
2820 unsigned Wd = MI->getOperand(0).getReg();
2821 unsigned Wd_in = MI->getOperand(1).getReg();
2822 unsigned Lane = MI->getOperand(2).getImm();
2823 unsigned Fs = MI->getOperand(3).getReg();
2824 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2825
2826 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002827 .addImm(0)
2828 .addReg(Fs)
2829 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00002830 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002831 .addReg(Wd_in)
2832 .addImm(Lane)
2833 .addReg(Wt);
Daniel Sandersa5150702013-09-27 12:31:32 +00002834
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002835 MI->eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00002836 return BB;
2837}
2838
2839// Emit the INSERT_FD pseudo instruction.
2840//
2841// insert_fd_pseudo $wd, $fs, n
2842// =>
2843// subreg_to_reg $wt:sub_64, $fs
2844// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002845MachineBasicBlock *
2846MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2847 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002848 assert(Subtarget->isFP64bit());
2849
2850 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2851 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2852 DebugLoc DL = MI->getDebugLoc();
2853 unsigned Wd = MI->getOperand(0).getReg();
2854 unsigned Wd_in = MI->getOperand(1).getReg();
2855 unsigned Lane = MI->getOperand(2).getImm();
2856 unsigned Fs = MI->getOperand(3).getReg();
2857 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2858
2859 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002860 .addImm(0)
2861 .addReg(Fs)
2862 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00002863 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002864 .addReg(Wd_in)
2865 .addImm(Lane)
2866 .addReg(Wt);
2867
2868 MI->eraseFromParent(); // The pseudo instruction is gone now.
2869 return BB;
2870}
2871
2872// Emit the FILL_FW pseudo instruction.
2873//
2874// fill_fw_pseudo $wd, $fs
2875// =>
2876// implicit_def $wt1
2877// insert_subreg $wt2:subreg_lo, $wt1, $fs
2878// splati.w $wd, $wt2[0]
2879MachineBasicBlock *
2880MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
2881 MachineBasicBlock *BB) const {
2882 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2883 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2884 DebugLoc DL = MI->getDebugLoc();
2885 unsigned Wd = MI->getOperand(0).getReg();
2886 unsigned Fs = MI->getOperand(1).getReg();
2887 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2888 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2889
2890 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2891 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2892 .addReg(Wt1)
2893 .addReg(Fs)
2894 .addImm(Mips::sub_lo);
2895 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
2896
2897 MI->eraseFromParent(); // The pseudo instruction is gone now.
2898 return BB;
2899}
2900
2901// Emit the FILL_FD pseudo instruction.
2902//
2903// fill_fd_pseudo $wd, $fs
2904// =>
2905// implicit_def $wt1
2906// insert_subreg $wt2:subreg_64, $wt1, $fs
2907// splati.d $wd, $wt2[0]
2908MachineBasicBlock *
2909MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
2910 MachineBasicBlock *BB) const {
2911 assert(Subtarget->isFP64bit());
2912
2913 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2914 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2915 DebugLoc DL = MI->getDebugLoc();
2916 unsigned Wd = MI->getOperand(0).getReg();
2917 unsigned Fs = MI->getOperand(1).getReg();
2918 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2919 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2920
2921 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2922 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2923 .addReg(Wt1)
2924 .addReg(Fs)
2925 .addImm(Mips::sub_64);
2926 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00002927
2928 MI->eraseFromParent(); // The pseudo instruction is gone now.
2929 return BB;
2930}
Daniel Sandersa9521602013-10-23 10:36:52 +00002931
2932// Emit the FEXP2_W_1 pseudo instructions.
2933//
2934// fexp2_w_1_pseudo $wd, $wt
2935// =>
2936// ldi.w $ws, 1
2937// fexp2.w $wd, $ws, $wt
2938MachineBasicBlock *
2939MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
2940 MachineBasicBlock *BB) const {
2941 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2942 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2943 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
2944 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2945 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2946 DebugLoc DL = MI->getDebugLoc();
2947
2948 // Splat 1.0 into a vector
2949 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
2950 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
2951
2952 // Emit 1.0 * fexp2(Wt)
2953 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
2954 .addReg(Ws2)
2955 .addReg(MI->getOperand(1).getReg());
2956
2957 MI->eraseFromParent(); // The pseudo instruction is gone now.
2958 return BB;
2959}
2960
2961// Emit the FEXP2_D_1 pseudo instructions.
2962//
2963// fexp2_d_1_pseudo $wd, $wt
2964// =>
2965// ldi.d $ws, 1
2966// fexp2.d $wd, $ws, $wt
2967MachineBasicBlock *
2968MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
2969 MachineBasicBlock *BB) const {
2970 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2971 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2972 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
2973 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2974 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2975 DebugLoc DL = MI->getDebugLoc();
2976
2977 // Splat 1.0 into a vector
2978 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
2979 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
2980
2981 // Emit 1.0 * fexp2(Wt)
2982 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
2983 .addReg(Ws2)
2984 .addReg(MI->getOperand(1).getReg());
2985
2986 MI->eraseFromParent(); // The pseudo instruction is gone now.
2987 return BB;
2988}