blob: cb6a5574f022b3c5572f87f0fd8412676829e56d [file] [log] [blame]
Akira Hatanaka44ebe002013-03-14 19:09:52 +00001//===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Subclass of MipsTargetLowering specialized for mips32/64.
11//
12//===----------------------------------------------------------------------===//
Daniel Sanders62aeab82013-10-30 13:31:27 +000013#define DEBUG_TYPE "mips-isel"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000014#include "MipsSEISelLowering.h"
15#include "MipsRegisterInfo.h"
16#include "MipsTargetMachine.h"
17#include "llvm/CodeGen/MachineInstrBuilder.h"
18#include "llvm/CodeGen/MachineRegisterInfo.h"
Akira Hatanakaa6bbde52013-04-13 02:13:30 +000019#include "llvm/IR/Intrinsics.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000020#include "llvm/Support/CommandLine.h"
Daniel Sanders62aeab82013-10-30 13:31:27 +000021#include "llvm/Support/Debug.h"
Hans Wennborg3e9b1c12013-10-30 16:10:10 +000022#include "llvm/Support/raw_ostream.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000023#include "llvm/Target/TargetInstrInfo.h"
24
25using namespace llvm;
26
27static cl::opt<bool>
28EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
29 cl::desc("MIPS: Enable tail calls."), cl::init(false));
30
Akira Hatanaka63791212013-09-07 00:52:30 +000031static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
32 cl::desc("Expand double precision loads and "
33 "stores to their single precision "
34 "counterparts"));
35
Akira Hatanaka96ca1822013-03-13 00:54:29 +000036MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
37 : MipsTargetLowering(TM) {
38 // Set up the register classes
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000039 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000040
41 if (HasMips64)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000042 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000043
Daniel Sanders36c671e2013-09-27 09:44:59 +000044 if (Subtarget->hasDSP() || Subtarget->hasMSA()) {
45 // Expand all truncating stores and extending loads.
46 unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
47 unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
48
49 for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
50 for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
51 setTruncStoreAction((MVT::SimpleValueType)VT0,
52 (MVT::SimpleValueType)VT1, Expand);
53
54 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
55 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
56 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
57 }
58 }
59
Akira Hatanaka96ca1822013-03-13 00:54:29 +000060 if (Subtarget->hasDSP()) {
61 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
62
63 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
Akira Hatanaka654655f2013-08-14 00:53:38 +000064 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000065
66 // Expand all builtin opcodes.
67 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
68 setOperationAction(Opc, VecTys[i], Expand);
69
Akira Hatanaka2f088222013-04-13 00:55:41 +000070 setOperationAction(ISD::ADD, VecTys[i], Legal);
71 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000072 setOperationAction(ISD::LOAD, VecTys[i], Legal);
73 setOperationAction(ISD::STORE, VecTys[i], Legal);
74 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
75 }
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +000076
77 setTargetDAGCombine(ISD::SHL);
78 setTargetDAGCombine(ISD::SRA);
79 setTargetDAGCombine(ISD::SRL);
Akira Hatanaka68741cc2013-04-30 22:37:26 +000080 setTargetDAGCombine(ISD::SETCC);
81 setTargetDAGCombine(ISD::VSELECT);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000082 }
83
Akira Hatanaka2f088222013-04-13 00:55:41 +000084 if (Subtarget->hasDSPR2())
85 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
86
Jack Carter3a2c2d42013-08-13 20:54:07 +000087 if (Subtarget->hasMSA()) {
Daniel Sandersc65f58a2013-09-11 10:15:48 +000088 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
89 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
90 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
91 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
92 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
93 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
94 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
Daniel Sandersf7456c72013-09-23 13:22:24 +000095
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +000096 setTargetDAGCombine(ISD::AND);
Daniel Sanders53fe6c42013-10-30 13:51:01 +000097 setTargetDAGCombine(ISD::OR);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +000098 setTargetDAGCombine(ISD::SRA);
Daniel Sanderse1d24352013-09-24 12:04:44 +000099 setTargetDAGCombine(ISD::VSELECT);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000100 setTargetDAGCombine(ISD::XOR);
Jack Carter3a2c2d42013-08-13 20:54:07 +0000101 }
102
Reed Kotlerc03807a2013-08-30 19:40:56 +0000103 if (!Subtarget->mipsSEUsesSoftFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000104 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
105
106 // When dealing with single precision only, use libcalls
107 if (!Subtarget->isSingleFloat()) {
Akira Hatanakabfb66242013-08-20 23:38:40 +0000108 if (Subtarget->isFP64bit())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000109 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
110 else
111 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
112 }
113 }
114
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000115 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
116 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
117 setOperationAction(ISD::MULHS, MVT::i32, Custom);
118 setOperationAction(ISD::MULHU, MVT::i32, Custom);
119
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000120 if (HasMips64) {
121 setOperationAction(ISD::MULHS, MVT::i64, Custom);
122 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000123 setOperationAction(ISD::MUL, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000124 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000125
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
127 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
128
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000129 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
130 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
131 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
132 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000133 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
134 setOperationAction(ISD::LOAD, MVT::i32, Custom);
135 setOperationAction(ISD::STORE, MVT::i32, Custom);
136
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000137 setTargetDAGCombine(ISD::ADDE);
138 setTargetDAGCombine(ISD::SUBE);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000139 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000140
Daniel Sandersce09d072013-08-28 12:14:50 +0000141 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000142 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
143 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
144
Akira Hatanaka63791212013-09-07 00:52:30 +0000145 if (NoDPLoadStore) {
146 setOperationAction(ISD::LOAD, MVT::f64, Custom);
147 setOperationAction(ISD::STORE, MVT::f64, Custom);
148 }
149
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000150 computeRegisterProperties();
151}
152
153const MipsTargetLowering *
154llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
155 return new MipsSETargetLowering(TM);
156}
157
Daniel Sanders7a289d02013-09-23 12:02:46 +0000158// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000159void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000160addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
161 addRegisterClass(Ty, RC);
162
163 // Expand all builtin opcodes.
164 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
165 setOperationAction(Opc, Ty, Expand);
166
167 setOperationAction(ISD::BITCAST, Ty, Legal);
168 setOperationAction(ISD::LOAD, Ty, Legal);
169 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000170 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
171 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000172 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000173
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000174 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000175 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000176 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000177 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000178 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000179 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000180 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000181 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000182 setOperationAction(ISD::SHL, Ty, Legal);
183 setOperationAction(ISD::SRA, Ty, Legal);
184 setOperationAction(ISD::SRL, Ty, Legal);
185 setOperationAction(ISD::SUB, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000186 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000187 setOperationAction(ISD::UREM, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000188 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000189 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000190 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000191
Daniel Sanders015972b2013-10-11 10:00:06 +0000192 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
193 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
194 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
195 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
196 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
197 }
198
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000199 setOperationAction(ISD::SETCC, Ty, Legal);
200 setCondCodeAction(ISD::SETNE, Ty, Expand);
201 setCondCodeAction(ISD::SETGE, Ty, Expand);
202 setCondCodeAction(ISD::SETGT, Ty, Expand);
203 setCondCodeAction(ISD::SETUGE, Ty, Expand);
204 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000205}
206
Daniel Sanders7a289d02013-09-23 12:02:46 +0000207// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000208void MipsSETargetLowering::
209addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000210 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000211
212 // Expand all builtin opcodes.
213 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
214 setOperationAction(Opc, Ty, Expand);
215
216 setOperationAction(ISD::LOAD, Ty, Legal);
217 setOperationAction(ISD::STORE, Ty, Legal);
218 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000219 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000220 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000221 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000222
223 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000224 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000225 setOperationAction(ISD::FADD, Ty, Legal);
226 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000227 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000228 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000229 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000230 setOperationAction(ISD::FMUL, Ty, Legal);
231 setOperationAction(ISD::FRINT, Ty, Legal);
232 setOperationAction(ISD::FSQRT, Ty, Legal);
233 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000234 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000235
236 setOperationAction(ISD::SETCC, Ty, Legal);
237 setCondCodeAction(ISD::SETOGE, Ty, Expand);
238 setCondCodeAction(ISD::SETOGT, Ty, Expand);
239 setCondCodeAction(ISD::SETUGE, Ty, Expand);
240 setCondCodeAction(ISD::SETUGT, Ty, Expand);
241 setCondCodeAction(ISD::SETGE, Ty, Expand);
242 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000243 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000244}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000245
246bool
Matt Arsenault25793a32014-02-05 23:15:53 +0000247MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
248 unsigned,
249 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000250 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
251
252 switch (SVT) {
253 case MVT::i64:
254 case MVT::i32:
255 if (Fast)
256 *Fast = true;
257 return true;
258 default:
259 return false;
260 }
261}
262
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000263SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
264 SelectionDAG &DAG) const {
265 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000266 case ISD::LOAD: return lowerLOAD(Op, DAG);
267 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000268 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
269 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
270 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
271 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
272 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
273 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000274 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
275 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000276 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
277 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000278 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000279 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000280 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000281 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000282 }
283
284 return MipsTargetLowering::LowerOperation(Op, DAG);
285}
286
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000287// selectMADD -
288// Transforms a subgraph in CurDAG if the following pattern is found:
289// (addc multLo, Lo0), (adde multHi, Hi0),
290// where,
291// multHi/Lo: product of multiplication
292// Lo0: initial value of Lo register
293// Hi0: initial value of Hi register
294// Return true if pattern matching was successful.
295static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
296 // ADDENode's second operand must be a flag output of an ADDC node in order
297 // for the matching to be successful.
298 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
299
300 if (ADDCNode->getOpcode() != ISD::ADDC)
301 return false;
302
303 SDValue MultHi = ADDENode->getOperand(0);
304 SDValue MultLo = ADDCNode->getOperand(0);
305 SDNode *MultNode = MultHi.getNode();
306 unsigned MultOpc = MultHi.getOpcode();
307
308 // MultHi and MultLo must be generated by the same node,
309 if (MultLo.getNode() != MultNode)
310 return false;
311
312 // and it must be a multiplication.
313 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
314 return false;
315
316 // MultLo amd MultHi must be the first and second output of MultNode
317 // respectively.
318 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
319 return false;
320
321 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
322 // of the values of MultNode, in which case MultNode will be removed in later
323 // phases.
324 // If there exist users other than ADDENode or ADDCNode, this function returns
325 // here, which will result in MultNode being mapped to a single MULT
326 // instruction node rather than a pair of MULT and MADD instructions being
327 // produced.
328 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
329 return false;
330
Andrew Trickef9de2a2013-05-25 02:42:55 +0000331 SDLoc DL(ADDENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000332
333 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000334 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000335 ADDCNode->getOperand(1),
336 ADDENode->getOperand(1));
337
338 // create MipsMAdd(u) node
339 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
340
341 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
342 MultNode->getOperand(0),// Factor 0
343 MultNode->getOperand(1),// Factor 1
344 ACCIn);
345
346 // replace uses of adde and addc here
347 if (!SDValue(ADDCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000348 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000349 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
350 }
351 if (!SDValue(ADDENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000352 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000353 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
354 }
355
356 return true;
357}
358
359// selectMSUB -
360// Transforms a subgraph in CurDAG if the following pattern is found:
361// (addc Lo0, multLo), (sube Hi0, multHi),
362// where,
363// multHi/Lo: product of multiplication
364// Lo0: initial value of Lo register
365// Hi0: initial value of Hi register
366// Return true if pattern matching was successful.
367static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
368 // SUBENode's second operand must be a flag output of an SUBC node in order
369 // for the matching to be successful.
370 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
371
372 if (SUBCNode->getOpcode() != ISD::SUBC)
373 return false;
374
375 SDValue MultHi = SUBENode->getOperand(1);
376 SDValue MultLo = SUBCNode->getOperand(1);
377 SDNode *MultNode = MultHi.getNode();
378 unsigned MultOpc = MultHi.getOpcode();
379
380 // MultHi and MultLo must be generated by the same node,
381 if (MultLo.getNode() != MultNode)
382 return false;
383
384 // and it must be a multiplication.
385 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
386 return false;
387
388 // MultLo amd MultHi must be the first and second output of MultNode
389 // respectively.
390 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
391 return false;
392
393 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
394 // of the values of MultNode, in which case MultNode will be removed in later
395 // phases.
396 // If there exist users other than SUBENode or SUBCNode, this function returns
397 // here, which will result in MultNode being mapped to a single MULT
398 // instruction node rather than a pair of MULT and MSUB instructions being
399 // produced.
400 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
401 return false;
402
Andrew Trickef9de2a2013-05-25 02:42:55 +0000403 SDLoc DL(SUBENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000404
405 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000406 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000407 SUBCNode->getOperand(0),
408 SUBENode->getOperand(0));
409
410 // create MipsSub(u) node
411 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
412
413 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
414 MultNode->getOperand(0),// Factor 0
415 MultNode->getOperand(1),// Factor 1
416 ACCIn);
417
418 // replace uses of sube and subc here
419 if (!SDValue(SUBCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000420 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000421 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
422 }
423 if (!SDValue(SUBENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000424 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000425 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
426 }
427
428 return true;
429}
430
431static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
432 TargetLowering::DAGCombinerInfo &DCI,
433 const MipsSubtarget *Subtarget) {
434 if (DCI.isBeforeLegalize())
435 return SDValue();
436
437 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
438 selectMADD(N, &DAG))
439 return SDValue(N, 0);
440
441 return SDValue();
442}
443
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000444// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
445//
446// Performs the following transformations:
447// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
448// sign/zero-extension is completely overwritten by the new one performed by
449// the ISD::AND.
450// - Removes redundant zero extensions performed by an ISD::AND.
451static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
452 TargetLowering::DAGCombinerInfo &DCI,
453 const MipsSubtarget *Subtarget) {
454 if (!Subtarget->hasMSA())
455 return SDValue();
456
457 SDValue Op0 = N->getOperand(0);
458 SDValue Op1 = N->getOperand(1);
459 unsigned Op0Opcode = Op0->getOpcode();
460
461 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
462 // where $d + 1 == 2^n and n == 32
463 // or $d + 1 == 2^n and n <= 32 and ZExt
464 // -> (MipsVExtractZExt $a, $b, $c)
465 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
466 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
467 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
468
469 if (!Mask)
470 return SDValue();
471
472 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
473
474 if (Log2IfPositive <= 0)
475 return SDValue(); // Mask+1 is not a power of 2
476
477 SDValue Op0Op2 = Op0->getOperand(2);
478 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
479 unsigned ExtendTySize = ExtendTy.getSizeInBits();
480 unsigned Log2 = Log2IfPositive;
481
482 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
483 Log2 == ExtendTySize) {
484 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
485 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
486 Op0->getVTList(), Ops, Op0->getNumOperands());
487 return Op0;
488 }
489 }
490
491 return SDValue();
492}
493
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000494// Determine if the specified node is a constant vector splat.
495//
496// Returns true and sets Imm if:
497// * N is a ISD::BUILD_VECTOR representing a constant splat
498//
499// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
500// differences are that it assumes the MSA has already been checked and the
501// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
502// must not be in order for binsri.d to be selectable).
503static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
504 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
505
506 if (Node == NULL)
507 return false;
508
509 APInt SplatValue, SplatUndef;
510 unsigned SplatBitSize;
511 bool HasAnyUndefs;
512
513 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
514 8, !IsLittleEndian))
515 return false;
516
517 Imm = SplatValue;
518
519 return true;
520}
521
Daniel Sandersab94b532013-10-30 15:20:38 +0000522// Test whether the given node is an all-ones build_vector.
523static bool isVectorAllOnes(SDValue N) {
524 // Look through bitcasts. Endianness doesn't matter because we are looking
525 // for an all-ones value.
526 if (N->getOpcode() == ISD::BITCAST)
527 N = N->getOperand(0);
528
529 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
530
531 if (!BVN)
532 return false;
533
534 APInt SplatValue, SplatUndef;
535 unsigned SplatBitSize;
536 bool HasAnyUndefs;
537
538 // Endianness doesn't matter in this context because we are looking for
539 // an all-ones value.
540 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
541 return SplatValue.isAllOnesValue();
542
543 return false;
544}
545
546// Test whether N is the bitwise inverse of OfNode.
547static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
548 if (N->getOpcode() != ISD::XOR)
549 return false;
550
551 if (isVectorAllOnes(N->getOperand(0)))
552 return N->getOperand(1) == OfNode;
553
554 if (isVectorAllOnes(N->getOperand(1)))
555 return N->getOperand(0) == OfNode;
556
557 return false;
558}
559
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000560// Perform combines where ISD::OR is the root node.
561//
562// Performs the following transformations:
563// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
564// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
565// vector type.
566static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
567 TargetLowering::DAGCombinerInfo &DCI,
568 const MipsSubtarget *Subtarget) {
569 if (!Subtarget->hasMSA())
570 return SDValue();
571
572 EVT Ty = N->getValueType(0);
573
574 if (!Ty.is128BitVector())
575 return SDValue();
576
577 SDValue Op0 = N->getOperand(0);
578 SDValue Op1 = N->getOperand(1);
579
580 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
581 SDValue Op0Op0 = Op0->getOperand(0);
582 SDValue Op0Op1 = Op0->getOperand(1);
583 SDValue Op1Op0 = Op1->getOperand(0);
584 SDValue Op1Op1 = Op1->getOperand(1);
585 bool IsLittleEndian = !Subtarget->isLittle();
586
587 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000588 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000589 APInt Mask, InvMask;
590
591 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
592 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
593 // looking.
594 // IfClr will be set if we find a valid match.
595 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
596 Cond = Op0Op0;
597 IfSet = Op0Op1;
598
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000599 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
600 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000601 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000602 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
603 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000604 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000605
606 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000607 }
608
609 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
610 // thing again using this mask.
611 // IfClr will be set if we find a valid match.
612 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
613 Cond = Op0Op1;
614 IfSet = Op0Op0;
615
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000616 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
617 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000618 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000619 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
620 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000621 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000622
623 IsConstantMask = true;
624 }
625
626 // If IfClr is not yet set, try looking for a non-constant match.
627 // IfClr will be set if we find a valid match amongst the eight
628 // possibilities.
629 if (!IfClr.getNode()) {
630 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
631 Cond = Op1Op0;
632 IfSet = Op1Op1;
633 IfClr = Op0Op1;
634 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
635 Cond = Op1Op0;
636 IfSet = Op1Op1;
637 IfClr = Op0Op0;
638 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
639 Cond = Op1Op1;
640 IfSet = Op1Op0;
641 IfClr = Op0Op1;
642 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
643 Cond = Op1Op1;
644 IfSet = Op1Op0;
645 IfClr = Op0Op0;
646 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
647 Cond = Op0Op0;
648 IfSet = Op0Op1;
649 IfClr = Op1Op1;
650 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
651 Cond = Op0Op0;
652 IfSet = Op0Op1;
653 IfClr = Op1Op0;
654 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
655 Cond = Op0Op1;
656 IfSet = Op0Op0;
657 IfClr = Op1Op1;
658 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
659 Cond = Op0Op1;
660 IfSet = Op0Op0;
661 IfClr = Op1Op0;
662 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000663 }
664
665 // At this point, IfClr will be set if we have a valid match.
666 if (!IfClr.getNode())
667 return SDValue();
668
669 assert(Cond.getNode() && IfSet.getNode());
670
671 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000672 if (IsConstantMask) {
673 if (Mask.isAllOnesValue())
674 return IfSet;
675 else if (Mask == 0)
676 return IfClr;
677 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000678
679 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000680 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000681 }
682
683 return SDValue();
684}
685
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000686static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
687 TargetLowering::DAGCombinerInfo &DCI,
688 const MipsSubtarget *Subtarget) {
689 if (DCI.isBeforeLegalize())
690 return SDValue();
691
692 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
693 selectMSUB(N, &DAG))
694 return SDValue(N, 0);
695
696 return SDValue();
697}
698
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000699static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
700 EVT ShiftTy, SelectionDAG &DAG) {
701 // Clear the upper (64 - VT.sizeInBits) bits.
702 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
703
704 // Return 0.
705 if (C == 0)
706 return DAG.getConstant(0, VT);
707
708 // Return x.
709 if (C == 1)
710 return X;
711
712 // If c is power of 2, return (shl x, log2(c)).
713 if (isPowerOf2_64(C))
714 return DAG.getNode(ISD::SHL, DL, VT, X,
715 DAG.getConstant(Log2_64(C), ShiftTy));
716
717 unsigned Log2Ceil = Log2_64_Ceil(C);
718 uint64_t Floor = 1LL << Log2_64(C);
719 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
720
721 // If |c - floor_c| <= |c - ceil_c|,
722 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
723 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
724 if (C - Floor <= Ceil - C) {
725 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
726 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
727 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
728 }
729
730 // If |c - floor_c| > |c - ceil_c|,
731 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
732 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
733 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
734 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
735}
736
737static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
738 const TargetLowering::DAGCombinerInfo &DCI,
739 const MipsSETargetLowering *TL) {
740 EVT VT = N->getValueType(0);
741
742 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
743 if (!VT.isVector())
744 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
745 VT, TL->getScalarShiftAmountTy(VT), DAG);
746
747 return SDValue(N, 0);
748}
749
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000750static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
751 SelectionDAG &DAG,
752 const MipsSubtarget *Subtarget) {
753 // See if this is a vector splat immediate node.
754 APInt SplatValue, SplatUndef;
755 unsigned SplatBitSize;
756 bool HasAnyUndefs;
757 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
758 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
759
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000760 if (!Subtarget->hasDSP())
761 return SDValue();
762
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000763 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000764 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000765 EltSize, !Subtarget->isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000766 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000767 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000768 return SDValue();
769
Andrew Trickef9de2a2013-05-25 02:42:55 +0000770 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000771 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
772}
773
774static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
775 TargetLowering::DAGCombinerInfo &DCI,
776 const MipsSubtarget *Subtarget) {
777 EVT Ty = N->getValueType(0);
778
779 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
780 return SDValue();
781
782 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
783}
784
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000785// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
786// constant splats into MipsISD::SHRA_DSP for DSPr2.
787//
788// Performs the following transformations:
789// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
790// sign/zero-extension is completely overwritten by the new one performed by
791// the ISD::SRA and ISD::SHL nodes.
792// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
793// sequence.
794//
795// See performDSPShiftCombine for more information about the transformation
796// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000797static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
798 TargetLowering::DAGCombinerInfo &DCI,
799 const MipsSubtarget *Subtarget) {
800 EVT Ty = N->getValueType(0);
801
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000802 if (Subtarget->hasMSA()) {
803 SDValue Op0 = N->getOperand(0);
804 SDValue Op1 = N->getOperand(1);
805
806 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
807 // where $d + sizeof($c) == 32
808 // or $d + sizeof($c) <= 32 and SExt
809 // -> (MipsVExtractSExt $a, $b, $c)
810 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
811 SDValue Op0Op0 = Op0->getOperand(0);
812 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
813
814 if (!ShAmount)
815 return SDValue();
816
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000817 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
818 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
819 return SDValue();
820
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000821 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
822 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
823
824 if (TotalBits == 32 ||
825 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
826 TotalBits <= 32)) {
827 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
828 Op0Op0->getOperand(2) };
829 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
830 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
831 return Op0Op0;
832 }
833 }
834 }
835
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000836 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
837 return SDValue();
838
839 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
840}
841
842
843static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
844 TargetLowering::DAGCombinerInfo &DCI,
845 const MipsSubtarget *Subtarget) {
846 EVT Ty = N->getValueType(0);
847
848 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
849 return SDValue();
850
851 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
852}
853
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000854static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
855 bool IsV216 = (Ty == MVT::v2i16);
856
857 switch (CC) {
858 case ISD::SETEQ:
859 case ISD::SETNE: return true;
860 case ISD::SETLT:
861 case ISD::SETLE:
862 case ISD::SETGT:
863 case ISD::SETGE: return IsV216;
864 case ISD::SETULT:
865 case ISD::SETULE:
866 case ISD::SETUGT:
867 case ISD::SETUGE: return !IsV216;
868 default: return false;
869 }
870}
871
872static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
873 EVT Ty = N->getValueType(0);
874
875 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
876 return SDValue();
877
878 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
879 return SDValue();
880
Andrew Trickef9de2a2013-05-25 02:42:55 +0000881 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000882 N->getOperand(1), N->getOperand(2));
883}
884
885static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
886 EVT Ty = N->getValueType(0);
887
Daniel Sanders3ce56622013-09-24 12:18:31 +0000888 if (Ty.is128BitVector() && Ty.isInteger()) {
889 // Try the following combines:
890 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
891 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
892 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
893 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
894 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
895 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
896 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
897 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
898 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
899 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
900 // legalizer.
901 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000902
Daniel Sanders3ce56622013-09-24 12:18:31 +0000903 if (Op0->getOpcode() != ISD::SETCC)
904 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000905
Daniel Sanders3ce56622013-09-24 12:18:31 +0000906 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
907 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000908
Daniel Sanders3ce56622013-09-24 12:18:31 +0000909 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
910 Signed = true;
911 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
912 Signed = false;
913 else
914 return SDValue();
915
916 SDValue Op1 = N->getOperand(1);
917 SDValue Op2 = N->getOperand(2);
918 SDValue Op0Op0 = Op0->getOperand(0);
919 SDValue Op0Op1 = Op0->getOperand(1);
920
921 if (Op1 == Op0Op0 && Op2 == Op0Op1)
922 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
923 Ty, Op1, Op2);
924 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
925 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
926 Ty, Op1, Op2);
927 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
928 SDValue SetCC = N->getOperand(0);
929
930 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
931 return SDValue();
932
933 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
934 SetCC.getOperand(0), SetCC.getOperand(1),
935 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
936 }
937
938 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000939}
940
Daniel Sandersf7456c72013-09-23 13:22:24 +0000941static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
942 const MipsSubtarget *Subtarget) {
943 EVT Ty = N->getValueType(0);
944
945 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
946 // Try the following combines:
947 // (xor (or $a, $b), (build_vector allones))
948 // (xor (or $a, $b), (bitcast (build_vector allones)))
949 SDValue Op0 = N->getOperand(0);
950 SDValue Op1 = N->getOperand(1);
951 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000952
953 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
954 NotOp = Op1;
955 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
956 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000957 else
958 return SDValue();
959
960 if (NotOp->getOpcode() == ISD::OR)
961 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
962 NotOp->getOperand(1));
963 }
964
965 return SDValue();
966}
967
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000968SDValue
969MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
970 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000971 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000972
973 switch (N->getOpcode()) {
974 case ISD::ADDE:
975 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000976 case ISD::AND:
977 Val = performANDCombine(N, DAG, DCI, Subtarget);
978 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000979 case ISD::OR:
980 Val = performORCombine(N, DAG, DCI, Subtarget);
981 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000982 case ISD::SUBE:
983 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000984 case ISD::MUL:
985 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000986 case ISD::SHL:
987 return performSHLCombine(N, DAG, DCI, Subtarget);
988 case ISD::SRA:
989 return performSRACombine(N, DAG, DCI, Subtarget);
990 case ISD::SRL:
991 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000992 case ISD::VSELECT:
993 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000994 case ISD::XOR:
995 Val = performXORCombine(N, DAG, Subtarget);
996 break;
997 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000998 Val = performSETCCCombine(N, DAG);
999 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001000 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001001
Daniel Sanders62aeab82013-10-30 13:31:27 +00001002 if (Val.getNode()) {
1003 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1004 N->printrWithDepth(dbgs(), &DAG);
1005 dbgs() << "\n=> \n";
1006 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1007 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001008 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001009 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001010
1011 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001012}
1013
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001014MachineBasicBlock *
1015MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1016 MachineBasicBlock *BB) const {
1017 switch (MI->getOpcode()) {
1018 default:
1019 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1020 case Mips::BPOSGE32_PSEUDO:
1021 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001022 case Mips::SNZ_B_PSEUDO:
1023 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1024 case Mips::SNZ_H_PSEUDO:
1025 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1026 case Mips::SNZ_W_PSEUDO:
1027 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1028 case Mips::SNZ_D_PSEUDO:
1029 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1030 case Mips::SNZ_V_PSEUDO:
1031 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1032 case Mips::SZ_B_PSEUDO:
1033 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1034 case Mips::SZ_H_PSEUDO:
1035 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1036 case Mips::SZ_W_PSEUDO:
1037 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1038 case Mips::SZ_D_PSEUDO:
1039 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1040 case Mips::SZ_V_PSEUDO:
1041 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001042 case Mips::COPY_FW_PSEUDO:
1043 return emitCOPY_FW(MI, BB);
1044 case Mips::COPY_FD_PSEUDO:
1045 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001046 case Mips::INSERT_FW_PSEUDO:
1047 return emitINSERT_FW(MI, BB);
1048 case Mips::INSERT_FD_PSEUDO:
1049 return emitINSERT_FD(MI, BB);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001050 case Mips::FILL_FW_PSEUDO:
1051 return emitFILL_FW(MI, BB);
1052 case Mips::FILL_FD_PSEUDO:
1053 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001054 case Mips::FEXP2_W_1_PSEUDO:
1055 return emitFEXP2_W_1(MI, BB);
1056 case Mips::FEXP2_D_1_PSEUDO:
1057 return emitFEXP2_D_1(MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001058 }
1059}
1060
1061bool MipsSETargetLowering::
1062isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
1063 unsigned NextStackOffset,
1064 const MipsFunctionInfo& FI) const {
1065 if (!EnableMipsTailCalls)
1066 return false;
1067
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001068 // Return false if either the callee or caller has a byval argument.
1069 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
1070 return false;
1071
1072 // Return true if the callee's argument area is no larger than the
1073 // caller's.
1074 return NextStackOffset <= FI.getIncomingArgSize();
1075}
1076
1077void MipsSETargetLowering::
1078getOpndList(SmallVectorImpl<SDValue> &Ops,
1079 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
1080 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
1081 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001082 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001083 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
1084 InternalLinkage, CLI, Callee, Chain);
1085}
1086
Akira Hatanaka63791212013-09-07 00:52:30 +00001087SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1088 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1089
1090 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1091 return MipsTargetLowering::lowerLOAD(Op, DAG);
1092
1093 // Replace a double precision load with two i32 loads and a buildpair64.
1094 SDLoc DL(Op);
1095 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1096 EVT PtrVT = Ptr.getValueType();
1097
1098 // i32 load from lower address.
1099 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1100 MachinePointerInfo(), Nd.isVolatile(),
1101 Nd.isNonTemporal(), Nd.isInvariant(),
1102 Nd.getAlignment());
1103
1104 // i32 load from higher address.
1105 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1106 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1107 MachinePointerInfo(), Nd.isVolatile(),
1108 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001109 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka63791212013-09-07 00:52:30 +00001110
1111 if (!Subtarget->isLittle())
1112 std::swap(Lo, Hi);
1113
1114 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1115 SDValue Ops[2] = {BP, Hi.getValue(1)};
1116 return DAG.getMergeValues(Ops, 2, DL);
1117}
1118
1119SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1120 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1121
1122 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1123 return MipsTargetLowering::lowerSTORE(Op, DAG);
1124
1125 // Replace a double precision store with two extractelement64s and i32 stores.
1126 SDLoc DL(Op);
1127 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1128 EVT PtrVT = Ptr.getValueType();
1129 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1130 Val, DAG.getConstant(0, MVT::i32));
1131 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1132 Val, DAG.getConstant(1, MVT::i32));
1133
1134 if (!Subtarget->isLittle())
1135 std::swap(Lo, Hi);
1136
1137 // i32 store to lower address.
1138 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1139 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
1140 Nd.getTBAAInfo());
1141
1142 // i32 store to higher address.
1143 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1144 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001145 Nd.isVolatile(), Nd.isNonTemporal(),
1146 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001147}
1148
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001149SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1150 bool HasLo, bool HasHi,
1151 SelectionDAG &DAG) const {
1152 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001153 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001154 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1155 Op.getOperand(0), Op.getOperand(1));
1156 SDValue Lo, Hi;
1157
1158 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001159 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001160 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001161 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001162
1163 if (!HasLo || !HasHi)
1164 return HasLo ? Lo : Hi;
1165
1166 SDValue Vals[] = { Lo, Hi };
1167 return DAG.getMergeValues(Vals, 2, DL);
1168}
1169
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001170
Andrew Trickef9de2a2013-05-25 02:42:55 +00001171static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001172 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1173 DAG.getConstant(0, MVT::i32));
1174 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1175 DAG.getConstant(1, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001176 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001177}
1178
Andrew Trickef9de2a2013-05-25 02:42:55 +00001179static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001180 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1181 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001182 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1183}
1184
1185// This function expands mips intrinsic nodes which have 64-bit input operands
1186// or output values.
1187//
1188// out64 = intrinsic-node in64
1189// =>
1190// lo = copy (extract-element (in64, 0))
1191// hi = copy (extract-element (in64, 1))
1192// mips-specific-node
1193// v0 = copy lo
1194// v1 = copy hi
1195// out64 = merge-values (v0, v1)
1196//
1197static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001198 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001199 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1200 SmallVector<SDValue, 3> Ops;
1201 unsigned OpNo = 0;
1202
1203 // See if Op has a chain input.
1204 if (HasChainIn)
1205 Ops.push_back(Op->getOperand(OpNo++));
1206
1207 // The next operand is the intrinsic opcode.
1208 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1209
1210 // See if the next operand has type i64.
1211 SDValue Opnd = Op->getOperand(++OpNo), In64;
1212
1213 if (Opnd.getValueType() == MVT::i64)
1214 In64 = initAccumulator(Opnd, DL, DAG);
1215 else
1216 Ops.push_back(Opnd);
1217
1218 // Push the remaining operands.
1219 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1220 Ops.push_back(Op->getOperand(OpNo));
1221
1222 // Add In64 to the end of the list.
1223 if (In64.getNode())
1224 Ops.push_back(In64);
1225
1226 // Scan output.
1227 SmallVector<EVT, 2> ResTys;
1228
1229 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1230 I != E; ++I)
1231 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1232
1233 // Create node.
1234 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1235 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1236
1237 if (!HasChainIn)
1238 return Out;
1239
1240 assert(Val->getValueType(1) == MVT::Other);
1241 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1242 return DAG.getMergeValues(Vals, 2, DL);
1243}
1244
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001245// Lower an MSA copy intrinsic into the specified SelectionDAG node
1246static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1247 SDLoc DL(Op);
1248 SDValue Vec = Op->getOperand(1);
1249 SDValue Idx = Op->getOperand(2);
1250 EVT ResTy = Op->getValueType(0);
1251 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1252
1253 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1254 DAG.getValueType(EltTy));
1255
1256 return Result;
1257}
1258
Daniel Sanders50b80412013-11-15 12:56:49 +00001259static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1260 EVT ResVecTy = Op->getValueType(0);
1261 EVT ViaVecTy = ResVecTy;
1262 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001263
Daniel Sanders50b80412013-11-15 12:56:49 +00001264 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1265 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1266 // lanes.
1267 SDValue LaneA;
1268 SDValue LaneB = Op->getOperand(2);
1269
1270 if (ResVecTy == MVT::v2i64) {
1271 LaneA = DAG.getConstant(0, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001272 ViaVecTy = MVT::v4i32;
Daniel Sanders50b80412013-11-15 12:56:49 +00001273 } else
1274 LaneA = LaneB;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001275
Daniel Sanders50b80412013-11-15 12:56:49 +00001276 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1277 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001278
Daniel Sanders50b80412013-11-15 12:56:49 +00001279 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, Ops,
1280 ViaVecTy.getVectorNumElements());
1281
1282 if (ViaVecTy != ResVecTy)
1283 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001284
1285 return Result;
1286}
1287
Daniel Sanders50b80412013-11-15 12:56:49 +00001288static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1289 return DAG.getConstant(Op->getConstantOperandVal(ImmOp), Op->getValueType(0));
1290}
1291
1292static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1293 bool BigEndian, SelectionDAG &DAG) {
1294 EVT ViaVecTy = VecTy;
1295 SDValue SplatValueA = SplatValue;
1296 SDValue SplatValueB = SplatValue;
1297 SDLoc DL(SplatValue);
1298
1299 if (VecTy == MVT::v2i64) {
1300 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1301 ViaVecTy = MVT::v4i32;
1302
1303 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1304 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
1305 DAG.getConstant(32, MVT::i32));
1306 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1307 }
1308
1309 // We currently hold the parts in little endian order. Swap them if
1310 // necessary.
1311 if (BigEndian)
1312 std::swap(SplatValueA, SplatValueB);
1313
1314 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1315 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1316 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1317 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1318
1319 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, Ops,
1320 ViaVecTy.getVectorNumElements());
1321
1322 if (VecTy != ViaVecTy)
1323 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1324
1325 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001326}
1327
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001328static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1329 unsigned Opc, SDValue Imm,
1330 bool BigEndian) {
1331 EVT VecTy = Op->getValueType(0);
1332 SDValue Exp2Imm;
1333 SDLoc DL(Op);
1334
Daniel Sanders50b80412013-11-15 12:56:49 +00001335 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1336 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001337 if (VecTy == MVT::v2i64) {
1338 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1339 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1340
1341 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001342 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), MVT::i32);
1343
1344 if (BigEndian)
1345 std::swap(BitImmLoOp, BitImmHiOp);
1346
1347 Exp2Imm =
1348 DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
1349 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, BitImmLoOp,
1350 BitImmHiOp, BitImmLoOp, BitImmHiOp));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001351 }
1352 }
1353
1354 if (Exp2Imm.getNode() == NULL) {
1355 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001356
1357 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1358 // only values 0-63 are valid.
1359 if (VecTy == MVT::v2i64)
1360 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1361
1362 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1363
1364 Exp2Imm =
1365 DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, VecTy), Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001366 }
1367
1368 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1369}
1370
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001371static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1372 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001373 SDLoc DL(Op);
Daniel Sanders50b80412013-11-15 12:56:49 +00001374 SDValue One = DAG.getConstant(1, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001375 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
1376
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001377 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1378 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001379}
1380
1381static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1382 SDLoc DL(Op);
1383 EVT ResTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001384 APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
1385 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
1386 SDValue BitMask = DAG.getConstant(~BitImm, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001387
1388 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1389}
1390
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001391SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1392 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001393 SDLoc DL(Op);
1394
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001395 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1396 default:
1397 return SDValue();
1398 case Intrinsic::mips_shilo:
1399 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1400 case Intrinsic::mips_dpau_h_qbl:
1401 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1402 case Intrinsic::mips_dpau_h_qbr:
1403 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1404 case Intrinsic::mips_dpsu_h_qbl:
1405 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1406 case Intrinsic::mips_dpsu_h_qbr:
1407 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1408 case Intrinsic::mips_dpa_w_ph:
1409 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1410 case Intrinsic::mips_dps_w_ph:
1411 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1412 case Intrinsic::mips_dpax_w_ph:
1413 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1414 case Intrinsic::mips_dpsx_w_ph:
1415 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1416 case Intrinsic::mips_mulsa_w_ph:
1417 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1418 case Intrinsic::mips_mult:
1419 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1420 case Intrinsic::mips_multu:
1421 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1422 case Intrinsic::mips_madd:
1423 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1424 case Intrinsic::mips_maddu:
1425 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1426 case Intrinsic::mips_msub:
1427 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1428 case Intrinsic::mips_msubu:
1429 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001430 case Intrinsic::mips_addv_b:
1431 case Intrinsic::mips_addv_h:
1432 case Intrinsic::mips_addv_w:
1433 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001434 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1435 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001436 case Intrinsic::mips_addvi_b:
1437 case Intrinsic::mips_addvi_h:
1438 case Intrinsic::mips_addvi_w:
1439 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001440 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1441 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001442 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001443 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1444 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001445 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001446 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1447 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001448 case Intrinsic::mips_bclr_b:
1449 case Intrinsic::mips_bclr_h:
1450 case Intrinsic::mips_bclr_w:
1451 case Intrinsic::mips_bclr_d:
1452 return lowerMSABitClear(Op, DAG);
1453 case Intrinsic::mips_bclri_b:
1454 case Intrinsic::mips_bclri_h:
1455 case Intrinsic::mips_bclri_w:
1456 case Intrinsic::mips_bclri_d:
1457 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001458 case Intrinsic::mips_binsli_b:
1459 case Intrinsic::mips_binsli_h:
1460 case Intrinsic::mips_binsli_w:
1461 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001462 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001463 EVT VecTy = Op->getValueType(0);
1464 EVT EltTy = VecTy.getVectorElementType();
1465 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
1466 Op->getConstantOperandVal(3));
1467 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Daniel Sandersdf2215452014-03-12 11:54:00 +00001468 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1469 Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001470 }
1471 case Intrinsic::mips_binsri_b:
1472 case Intrinsic::mips_binsri_h:
1473 case Intrinsic::mips_binsri_w:
1474 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001475 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001476 EVT VecTy = Op->getValueType(0);
1477 EVT EltTy = VecTy.getVectorElementType();
1478 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
1479 Op->getConstantOperandVal(3));
1480 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Daniel Sandersdf2215452014-03-12 11:54:00 +00001481 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1482 Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001483 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001484 case Intrinsic::mips_bmnz_v:
1485 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1486 Op->getOperand(2), Op->getOperand(1));
1487 case Intrinsic::mips_bmnzi_b:
1488 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1489 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1490 Op->getOperand(1));
1491 case Intrinsic::mips_bmz_v:
1492 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1493 Op->getOperand(1), Op->getOperand(2));
1494 case Intrinsic::mips_bmzi_b:
1495 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1496 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1497 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001498 case Intrinsic::mips_bneg_b:
1499 case Intrinsic::mips_bneg_h:
1500 case Intrinsic::mips_bneg_w:
1501 case Intrinsic::mips_bneg_d: {
1502 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001503 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001504
1505 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1506 DAG.getNode(ISD::SHL, DL, VecTy, One,
1507 Op->getOperand(2)));
1508 }
1509 case Intrinsic::mips_bnegi_b:
1510 case Intrinsic::mips_bnegi_h:
1511 case Intrinsic::mips_bnegi_w:
1512 case Intrinsic::mips_bnegi_d:
1513 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
1514 !Subtarget->isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001515 case Intrinsic::mips_bnz_b:
1516 case Intrinsic::mips_bnz_h:
1517 case Intrinsic::mips_bnz_w:
1518 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001519 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1520 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001521 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001522 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1523 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001524 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001525 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001526 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001527 Op->getOperand(1), Op->getOperand(3),
1528 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001529 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001530 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001531 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001532 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1533 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001534 case Intrinsic::mips_bset_b:
1535 case Intrinsic::mips_bset_h:
1536 case Intrinsic::mips_bset_w:
1537 case Intrinsic::mips_bset_d: {
1538 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001539 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001540
1541 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1542 DAG.getNode(ISD::SHL, DL, VecTy, One,
1543 Op->getOperand(2)));
1544 }
1545 case Intrinsic::mips_bseti_b:
1546 case Intrinsic::mips_bseti_h:
1547 case Intrinsic::mips_bseti_w:
1548 case Intrinsic::mips_bseti_d:
1549 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
1550 !Subtarget->isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001551 case Intrinsic::mips_bz_b:
1552 case Intrinsic::mips_bz_h:
1553 case Intrinsic::mips_bz_w:
1554 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001555 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1556 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001557 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001558 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1559 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001560 case Intrinsic::mips_ceq_b:
1561 case Intrinsic::mips_ceq_h:
1562 case Intrinsic::mips_ceq_w:
1563 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001564 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001565 Op->getOperand(2), ISD::SETEQ);
1566 case Intrinsic::mips_ceqi_b:
1567 case Intrinsic::mips_ceqi_h:
1568 case Intrinsic::mips_ceqi_w:
1569 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001570 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001571 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1572 case Intrinsic::mips_cle_s_b:
1573 case Intrinsic::mips_cle_s_h:
1574 case Intrinsic::mips_cle_s_w:
1575 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001576 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001577 Op->getOperand(2), ISD::SETLE);
1578 case Intrinsic::mips_clei_s_b:
1579 case Intrinsic::mips_clei_s_h:
1580 case Intrinsic::mips_clei_s_w:
1581 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001582 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001583 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1584 case Intrinsic::mips_cle_u_b:
1585 case Intrinsic::mips_cle_u_h:
1586 case Intrinsic::mips_cle_u_w:
1587 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001588 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001589 Op->getOperand(2), ISD::SETULE);
1590 case Intrinsic::mips_clei_u_b:
1591 case Intrinsic::mips_clei_u_h:
1592 case Intrinsic::mips_clei_u_w:
1593 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001594 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001595 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1596 case Intrinsic::mips_clt_s_b:
1597 case Intrinsic::mips_clt_s_h:
1598 case Intrinsic::mips_clt_s_w:
1599 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001600 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001601 Op->getOperand(2), ISD::SETLT);
1602 case Intrinsic::mips_clti_s_b:
1603 case Intrinsic::mips_clti_s_h:
1604 case Intrinsic::mips_clti_s_w:
1605 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001606 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001607 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1608 case Intrinsic::mips_clt_u_b:
1609 case Intrinsic::mips_clt_u_h:
1610 case Intrinsic::mips_clt_u_w:
1611 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001612 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001613 Op->getOperand(2), ISD::SETULT);
1614 case Intrinsic::mips_clti_u_b:
1615 case Intrinsic::mips_clti_u_h:
1616 case Intrinsic::mips_clti_u_w:
1617 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001618 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001619 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001620 case Intrinsic::mips_copy_s_b:
1621 case Intrinsic::mips_copy_s_h:
1622 case Intrinsic::mips_copy_s_w:
1623 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001624 case Intrinsic::mips_copy_s_d:
Matheus Almeida74070322014-01-29 14:05:28 +00001625 if (HasMips64)
1626 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1627 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1628 else {
1629 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1630 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1631 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1632 Op->getValueType(0), Op->getOperand(1),
1633 Op->getOperand(2));
1634 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001635 case Intrinsic::mips_copy_u_b:
1636 case Intrinsic::mips_copy_u_h:
1637 case Intrinsic::mips_copy_u_w:
1638 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001639 case Intrinsic::mips_copy_u_d:
Matheus Almeida74070322014-01-29 14:05:28 +00001640 if (HasMips64)
1641 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1642 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1643 else {
1644 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1645 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1646 // Note: When i64 is illegal, this results in copy_s.w instructions
1647 // instead of copy_u.w instructions. This makes no difference to the
1648 // behaviour since i64 is only illegal when the register file is 32-bit.
1649 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1650 Op->getValueType(0), Op->getOperand(1),
1651 Op->getOperand(2));
1652 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001653 case Intrinsic::mips_div_s_b:
1654 case Intrinsic::mips_div_s_h:
1655 case Intrinsic::mips_div_s_w:
1656 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001657 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1658 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001659 case Intrinsic::mips_div_u_b:
1660 case Intrinsic::mips_div_u_h:
1661 case Intrinsic::mips_div_u_w:
1662 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001663 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1664 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001665 case Intrinsic::mips_fadd_w:
1666 case Intrinsic::mips_fadd_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001667 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1668 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001669 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1670 case Intrinsic::mips_fceq_w:
1671 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001672 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001673 Op->getOperand(2), ISD::SETOEQ);
1674 case Intrinsic::mips_fcle_w:
1675 case Intrinsic::mips_fcle_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::SETOLE);
1678 case Intrinsic::mips_fclt_w:
1679 case Intrinsic::mips_fclt_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::SETOLT);
1682 case Intrinsic::mips_fcne_w:
1683 case Intrinsic::mips_fcne_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::SETONE);
1686 case Intrinsic::mips_fcor_w:
1687 case Intrinsic::mips_fcor_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::SETO);
1690 case Intrinsic::mips_fcueq_w:
1691 case Intrinsic::mips_fcueq_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::SETUEQ);
1694 case Intrinsic::mips_fcule_w:
1695 case Intrinsic::mips_fcule_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::SETULE);
1698 case Intrinsic::mips_fcult_w:
1699 case Intrinsic::mips_fcult_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::SETULT);
1702 case Intrinsic::mips_fcun_w:
1703 case Intrinsic::mips_fcun_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::SETUO);
1706 case Intrinsic::mips_fcune_w:
1707 case Intrinsic::mips_fcune_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::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001710 case Intrinsic::mips_fdiv_w:
1711 case Intrinsic::mips_fdiv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001712 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1713 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001714 case Intrinsic::mips_ffint_u_w:
1715 case Intrinsic::mips_ffint_u_d:
1716 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1717 Op->getOperand(1));
1718 case Intrinsic::mips_ffint_s_w:
1719 case Intrinsic::mips_ffint_s_d:
1720 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1721 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001722 case Intrinsic::mips_fill_b:
1723 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001724 case Intrinsic::mips_fill_w:
1725 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001726 SmallVector<SDValue, 16> Ops;
1727 EVT ResTy = Op->getValueType(0);
1728
1729 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1730 Ops.push_back(Op->getOperand(1));
1731
Daniel Sandersc72593e2013-09-27 13:20:41 +00001732 // If ResTy is v2i64 then the type legalizer will break this node down into
1733 // an equivalent v4i32.
1734 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, &Ops[0], Ops.size());
Daniel Sandersf49dd822013-09-24 13:33:07 +00001735 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001736 case Intrinsic::mips_fexp2_w:
1737 case Intrinsic::mips_fexp2_d: {
1738 EVT ResTy = Op->getValueType(0);
1739 return DAG.getNode(
1740 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1741 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1742 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001743 case Intrinsic::mips_flog2_w:
1744 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001745 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001746 case Intrinsic::mips_fmadd_w:
1747 case Intrinsic::mips_fmadd_d:
1748 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1749 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001750 case Intrinsic::mips_fmul_w:
1751 case Intrinsic::mips_fmul_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001752 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1753 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001754 case Intrinsic::mips_fmsub_w:
1755 case Intrinsic::mips_fmsub_d: {
1756 EVT ResTy = Op->getValueType(0);
1757 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1758 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1759 Op->getOperand(2), Op->getOperand(3)));
1760 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001761 case Intrinsic::mips_frint_w:
1762 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001763 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001764 case Intrinsic::mips_fsqrt_w:
1765 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001766 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001767 case Intrinsic::mips_fsub_w:
1768 case Intrinsic::mips_fsub_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001769 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1770 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001771 case Intrinsic::mips_ftrunc_u_w:
1772 case Intrinsic::mips_ftrunc_u_d:
1773 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1774 Op->getOperand(1));
1775 case Intrinsic::mips_ftrunc_s_w:
1776 case Intrinsic::mips_ftrunc_s_d:
1777 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1778 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001779 case Intrinsic::mips_ilvev_b:
1780 case Intrinsic::mips_ilvev_h:
1781 case Intrinsic::mips_ilvev_w:
1782 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001783 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001784 Op->getOperand(1), Op->getOperand(2));
1785 case Intrinsic::mips_ilvl_b:
1786 case Intrinsic::mips_ilvl_h:
1787 case Intrinsic::mips_ilvl_w:
1788 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001789 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001790 Op->getOperand(1), Op->getOperand(2));
1791 case Intrinsic::mips_ilvod_b:
1792 case Intrinsic::mips_ilvod_h:
1793 case Intrinsic::mips_ilvod_w:
1794 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001795 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001796 Op->getOperand(1), Op->getOperand(2));
1797 case Intrinsic::mips_ilvr_b:
1798 case Intrinsic::mips_ilvr_h:
1799 case Intrinsic::mips_ilvr_w:
1800 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001801 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001802 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001803 case Intrinsic::mips_insert_b:
1804 case Intrinsic::mips_insert_h:
1805 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001806 case Intrinsic::mips_insert_d:
1807 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1808 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001809 case Intrinsic::mips_ldi_b:
1810 case Intrinsic::mips_ldi_h:
1811 case Intrinsic::mips_ldi_w:
1812 case Intrinsic::mips_ldi_d:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001813 return lowerMSASplatImm(Op, 1, DAG);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001814 case Intrinsic::mips_lsa:
1815 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001816 EVT ResTy = Op->getValueType(0);
1817 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1818 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1819 Op->getOperand(2), Op->getOperand(3)));
1820 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001821 case Intrinsic::mips_maddv_b:
1822 case Intrinsic::mips_maddv_h:
1823 case Intrinsic::mips_maddv_w:
1824 case Intrinsic::mips_maddv_d: {
1825 EVT ResTy = Op->getValueType(0);
1826 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1827 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1828 Op->getOperand(2), Op->getOperand(3)));
1829 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001830 case Intrinsic::mips_max_s_b:
1831 case Intrinsic::mips_max_s_h:
1832 case Intrinsic::mips_max_s_w:
1833 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001834 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1835 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001836 case Intrinsic::mips_max_u_b:
1837 case Intrinsic::mips_max_u_h:
1838 case Intrinsic::mips_max_u_w:
1839 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001840 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1841 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001842 case Intrinsic::mips_maxi_s_b:
1843 case Intrinsic::mips_maxi_s_h:
1844 case Intrinsic::mips_maxi_s_w:
1845 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001846 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1847 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001848 case Intrinsic::mips_maxi_u_b:
1849 case Intrinsic::mips_maxi_u_h:
1850 case Intrinsic::mips_maxi_u_w:
1851 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001852 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1853 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001854 case Intrinsic::mips_min_s_b:
1855 case Intrinsic::mips_min_s_h:
1856 case Intrinsic::mips_min_s_w:
1857 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001858 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1859 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001860 case Intrinsic::mips_min_u_b:
1861 case Intrinsic::mips_min_u_h:
1862 case Intrinsic::mips_min_u_w:
1863 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001864 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1865 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001866 case Intrinsic::mips_mini_s_b:
1867 case Intrinsic::mips_mini_s_h:
1868 case Intrinsic::mips_mini_s_w:
1869 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001870 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1871 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001872 case Intrinsic::mips_mini_u_b:
1873 case Intrinsic::mips_mini_u_h:
1874 case Intrinsic::mips_mini_u_w:
1875 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001876 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1877 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001878 case Intrinsic::mips_mod_s_b:
1879 case Intrinsic::mips_mod_s_h:
1880 case Intrinsic::mips_mod_s_w:
1881 case Intrinsic::mips_mod_s_d:
1882 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1883 Op->getOperand(2));
1884 case Intrinsic::mips_mod_u_b:
1885 case Intrinsic::mips_mod_u_h:
1886 case Intrinsic::mips_mod_u_w:
1887 case Intrinsic::mips_mod_u_d:
1888 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1889 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001890 case Intrinsic::mips_mulv_b:
1891 case Intrinsic::mips_mulv_h:
1892 case Intrinsic::mips_mulv_w:
1893 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001894 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1895 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001896 case Intrinsic::mips_msubv_b:
1897 case Intrinsic::mips_msubv_h:
1898 case Intrinsic::mips_msubv_w:
1899 case Intrinsic::mips_msubv_d: {
1900 EVT ResTy = Op->getValueType(0);
1901 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
1902 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1903 Op->getOperand(2), Op->getOperand(3)));
1904 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001905 case Intrinsic::mips_nlzc_b:
1906 case Intrinsic::mips_nlzc_h:
1907 case Intrinsic::mips_nlzc_w:
1908 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001909 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001910 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001911 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1912 Op->getOperand(1), Op->getOperand(2));
1913 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001914 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001915 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001916 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1917 Op->getOperand(1),
1918 lowerMSASplatImm(Op, 2, DAG));
1919 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001920 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001921 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001922 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
1923 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001924 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001925 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1926 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001927 case Intrinsic::mips_pckev_b:
1928 case Intrinsic::mips_pckev_h:
1929 case Intrinsic::mips_pckev_w:
1930 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001931 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001932 Op->getOperand(1), Op->getOperand(2));
1933 case Intrinsic::mips_pckod_b:
1934 case Intrinsic::mips_pckod_h:
1935 case Intrinsic::mips_pckod_w:
1936 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001937 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00001938 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00001939 case Intrinsic::mips_pcnt_b:
1940 case Intrinsic::mips_pcnt_h:
1941 case Intrinsic::mips_pcnt_w:
1942 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001943 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sanders26307182013-09-24 14:20:00 +00001944 case Intrinsic::mips_shf_b:
1945 case Intrinsic::mips_shf_h:
1946 case Intrinsic::mips_shf_w:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001947 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00001948 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001949 case Intrinsic::mips_sll_b:
1950 case Intrinsic::mips_sll_h:
1951 case Intrinsic::mips_sll_w:
1952 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001953 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
1954 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001955 case Intrinsic::mips_slli_b:
1956 case Intrinsic::mips_slli_h:
1957 case Intrinsic::mips_slli_w:
1958 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001959 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
1960 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00001961 case Intrinsic::mips_splat_b:
1962 case Intrinsic::mips_splat_h:
1963 case Intrinsic::mips_splat_w:
1964 case Intrinsic::mips_splat_d:
1965 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
1966 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
1967 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
1968 // Instead we lower to MipsISD::VSHF and match from there.
1969 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00001970 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00001971 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00001972 case Intrinsic::mips_splati_b:
1973 case Intrinsic::mips_splati_h:
1974 case Intrinsic::mips_splati_w:
1975 case Intrinsic::mips_splati_d:
1976 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1977 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1978 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001979 case Intrinsic::mips_sra_b:
1980 case Intrinsic::mips_sra_h:
1981 case Intrinsic::mips_sra_w:
1982 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001983 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
1984 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001985 case Intrinsic::mips_srai_b:
1986 case Intrinsic::mips_srai_h:
1987 case Intrinsic::mips_srai_w:
1988 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001989 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
1990 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001991 case Intrinsic::mips_srl_b:
1992 case Intrinsic::mips_srl_h:
1993 case Intrinsic::mips_srl_w:
1994 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001995 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
1996 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00001997 case Intrinsic::mips_srli_b:
1998 case Intrinsic::mips_srli_h:
1999 case Intrinsic::mips_srli_w:
2000 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002001 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2002 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002003 case Intrinsic::mips_subv_b:
2004 case Intrinsic::mips_subv_h:
2005 case Intrinsic::mips_subv_w:
2006 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002007 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2008 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002009 case Intrinsic::mips_subvi_b:
2010 case Intrinsic::mips_subvi_h:
2011 case Intrinsic::mips_subvi_w:
2012 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002013 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2014 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002015 case Intrinsic::mips_vshf_b:
2016 case Intrinsic::mips_vshf_h:
2017 case Intrinsic::mips_vshf_w:
2018 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002019 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002020 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002021 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002022 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2023 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002024 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002025 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2026 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002027 }
2028}
2029
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002030static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2031 SDLoc DL(Op);
2032 SDValue ChainIn = Op->getOperand(0);
2033 SDValue Address = Op->getOperand(2);
2034 SDValue Offset = Op->getOperand(3);
2035 EVT ResTy = Op->getValueType(0);
2036 EVT PtrTy = Address->getValueType(0);
2037
2038 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2039
2040 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
2041 false, false, 16);
2042}
2043
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002044SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2045 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002046 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2047 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002048 default:
2049 return SDValue();
2050 case Intrinsic::mips_extp:
2051 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2052 case Intrinsic::mips_extpdp:
2053 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2054 case Intrinsic::mips_extr_w:
2055 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2056 case Intrinsic::mips_extr_r_w:
2057 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2058 case Intrinsic::mips_extr_rs_w:
2059 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2060 case Intrinsic::mips_extr_s_h:
2061 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2062 case Intrinsic::mips_mthlip:
2063 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2064 case Intrinsic::mips_mulsaq_s_w_ph:
2065 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2066 case Intrinsic::mips_maq_s_w_phl:
2067 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2068 case Intrinsic::mips_maq_s_w_phr:
2069 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2070 case Intrinsic::mips_maq_sa_w_phl:
2071 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2072 case Intrinsic::mips_maq_sa_w_phr:
2073 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2074 case Intrinsic::mips_dpaq_s_w_ph:
2075 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2076 case Intrinsic::mips_dpsq_s_w_ph:
2077 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2078 case Intrinsic::mips_dpaq_sa_l_w:
2079 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2080 case Intrinsic::mips_dpsq_sa_l_w:
2081 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2082 case Intrinsic::mips_dpaqx_s_w_ph:
2083 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2084 case Intrinsic::mips_dpaqx_sa_w_ph:
2085 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2086 case Intrinsic::mips_dpsqx_s_w_ph:
2087 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2088 case Intrinsic::mips_dpsqx_sa_w_ph:
2089 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002090 case Intrinsic::mips_ld_b:
2091 case Intrinsic::mips_ld_h:
2092 case Intrinsic::mips_ld_w:
2093 case Intrinsic::mips_ld_d:
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002094 return lowerMSALoadIntr(Op, DAG, Intr);
2095 }
2096}
2097
2098static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2099 SDLoc DL(Op);
2100 SDValue ChainIn = Op->getOperand(0);
2101 SDValue Value = Op->getOperand(2);
2102 SDValue Address = Op->getOperand(3);
2103 SDValue Offset = Op->getOperand(4);
2104 EVT PtrTy = Address->getValueType(0);
2105
2106 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2107
2108 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
2109 false, 16);
2110}
2111
2112SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2113 SelectionDAG &DAG) const {
2114 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2115 switch (Intr) {
2116 default:
2117 return SDValue();
2118 case Intrinsic::mips_st_b:
2119 case Intrinsic::mips_st_h:
2120 case Intrinsic::mips_st_w:
2121 case Intrinsic::mips_st_d:
Daniel Sandersce09d072013-08-28 12:14:50 +00002122 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002123 }
2124}
2125
Daniel Sanders7a289d02013-09-23 12:02:46 +00002126/// \brief Check if the given BuildVectorSDNode is a splat.
2127/// This method currently relies on DAG nodes being reused when equivalent,
2128/// so it's possible for this to return false even when isConstantSplat returns
2129/// true.
2130static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002131 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002132 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002133
2134 SDValue Operand0 = N->getOperand(0);
2135
2136 for (unsigned int i = 1; i < nOps; ++i) {
2137 if (N->getOperand(i) != Operand0)
2138 return false;
2139 }
2140
2141 return true;
2142}
2143
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002144// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2145//
2146// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2147// choose to sign-extend but we could have equally chosen zero-extend. The
2148// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2149// result into this node later (possibly changing it to a zero-extend in the
2150// process).
2151SDValue MipsSETargetLowering::
2152lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2153 SDLoc DL(Op);
2154 EVT ResTy = Op->getValueType(0);
2155 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002156 EVT VecTy = Op0->getValueType(0);
2157
2158 if (!VecTy.is128BitVector())
2159 return SDValue();
2160
2161 if (ResTy.isInteger()) {
2162 SDValue Op1 = Op->getOperand(1);
2163 EVT EltTy = VecTy.getVectorElementType();
2164 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2165 DAG.getValueType(EltTy));
2166 }
2167
2168 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002169}
2170
Daniel Sandersf49dd822013-09-24 13:33:07 +00002171static bool isConstantOrUndef(const SDValue Op) {
2172 if (Op->getOpcode() == ISD::UNDEF)
2173 return true;
2174 if (dyn_cast<ConstantSDNode>(Op))
2175 return true;
2176 if (dyn_cast<ConstantFPSDNode>(Op))
2177 return true;
2178 return false;
2179}
2180
2181static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2182 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2183 if (isConstantOrUndef(Op->getOperand(i)))
2184 return true;
2185 return false;
2186}
2187
Daniel Sanders7a289d02013-09-23 12:02:46 +00002188// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2189// backend.
2190//
2191// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002192// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2193// 2 less than or equal to 64 and the value fits into a signed 10-bit
2194// immediate
2195// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2196// is a power of 2 less than or equal to 64 and the value does not fit into a
2197// signed 10-bit immediate
2198// - Non-constant splats are legal as-is.
2199// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2200// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002201SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2202 SelectionDAG &DAG) const {
2203 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2204 EVT ResTy = Op->getValueType(0);
2205 SDLoc DL(Op);
2206 APInt SplatValue, SplatUndef;
2207 unsigned SplatBitSize;
2208 bool HasAnyUndefs;
2209
2210 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
2211 return SDValue();
2212
2213 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2214 HasAnyUndefs, 8,
Daniel Sandersf49dd822013-09-24 13:33:07 +00002215 !Subtarget->isLittle()) && SplatBitSize <= 64) {
2216 // We can only cope with 8, 16, 32, or 64-bit elements
2217 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2218 SplatBitSize != 64)
2219 return SDValue();
2220
2221 // If the value fits into a simm10 then we can use ldi.[bhwd]
Daniel Sandersfd8e4162013-11-22 11:24:50 +00002222 // However, if it isn't an integer type we will have to bitcast from an
Daniel Sandersd40aea82013-11-22 13:22:52 +00002223 // integer type first. Also, if there are any undefs, we must lower them
Daniel Sanders630dbe02013-11-22 13:14:06 +00002224 // to defined values first.
2225 if (ResTy.isInteger() && !HasAnyUndefs && SplatValue.isSignedIntN(10))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002226 return Op;
2227
2228 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002229
2230 switch (SplatBitSize) {
2231 default:
2232 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002233 case 8:
2234 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002235 break;
2236 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002237 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002238 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002239 case 32:
2240 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002241 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002242 case 64:
2243 // There's no fill.d to fall back on for 64-bit values
2244 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002245 }
2246
Daniel Sanders50b80412013-11-15 12:56:49 +00002247 // SelectionDAG::getConstant will promote SplatValue appropriately.
2248 SDValue Result = DAG.getConstant(SplatValue, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002249
Daniel Sanders50b80412013-11-15 12:56:49 +00002250 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002251 if (ViaVecTy != ResTy)
2252 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002253
2254 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002255 } else if (isSplatVector(Node))
2256 return Op;
2257 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002258 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2259 // The resulting code is the same length as the expansion, but it doesn't
2260 // use memory operations
2261 EVT ResTy = Node->getValueType(0);
2262
2263 assert(ResTy.isVector());
2264
2265 unsigned NumElts = ResTy.getVectorNumElements();
2266 SDValue Vector = DAG.getUNDEF(ResTy);
2267 for (unsigned i = 0; i < NumElts; ++i) {
2268 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2269 Node->getOperand(i),
2270 DAG.getConstant(i, MVT::i32));
2271 }
2272 return Vector;
2273 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002274
2275 return SDValue();
2276}
2277
Daniel Sanders26307182013-09-24 14:20:00 +00002278// Lower VECTOR_SHUFFLE into SHF (if possible).
2279//
2280// SHF splits the vector into blocks of four elements, then shuffles these
2281// elements according to a <4 x i2> constant (encoded as an integer immediate).
2282//
2283// It is therefore possible to lower into SHF when the mask takes the form:
2284// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2285// When undef's appear they are treated as if they were whatever value is
2286// necessary in order to fit the above form.
2287//
2288// For example:
2289// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2290// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2291// i32 7, i32 6, i32 5, i32 4>
2292// is lowered to:
2293// (SHF_H $w0, $w1, 27)
2294// where the 27 comes from:
2295// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2296static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2297 SmallVector<int, 16> Indices,
2298 SelectionDAG &DAG) {
2299 int SHFIndices[4] = { -1, -1, -1, -1 };
2300
2301 if (Indices.size() < 4)
2302 return SDValue();
2303
2304 for (unsigned i = 0; i < 4; ++i) {
2305 for (unsigned j = i; j < Indices.size(); j += 4) {
2306 int Idx = Indices[j];
2307
2308 // Convert from vector index to 4-element subvector index
2309 // If an index refers to an element outside of the subvector then give up
2310 if (Idx != -1) {
2311 Idx -= 4 * (j / 4);
2312 if (Idx < 0 || Idx >= 4)
2313 return SDValue();
2314 }
2315
2316 // If the mask has an undef, replace it with the current index.
2317 // Note that it might still be undef if the current index is also undef
2318 if (SHFIndices[i] == -1)
2319 SHFIndices[i] = Idx;
2320
2321 // Check that non-undef values are the same as in the mask. If they
2322 // aren't then give up
2323 if (!(Idx == -1 || Idx == SHFIndices[i]))
2324 return SDValue();
2325 }
2326 }
2327
2328 // Calculate the immediate. Replace any remaining undefs with zero
2329 APInt Imm(32, 0);
2330 for (int i = 3; i >= 0; --i) {
2331 int Idx = SHFIndices[i];
2332
2333 if (Idx == -1)
2334 Idx = 0;
2335
2336 Imm <<= 2;
2337 Imm |= Idx & 0x3;
2338 }
2339
2340 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
2341 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
2342}
2343
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002344// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2345//
2346// ILVEV interleaves the even elements from each vector.
2347//
2348// It is possible to lower into ILVEV when the mask takes the form:
2349// <0, n, 2, n+2, 4, n+4, ...>
2350// where n is the number of elements in the vector.
2351//
2352// When undef's appear in the mask they are treated as if they were whatever
2353// value is necessary in order to fit the above form.
2354static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2355 SmallVector<int, 16> Indices,
2356 SelectionDAG &DAG) {
2357 assert ((Indices.size() % 2) == 0);
2358 int WsIdx = 0;
2359 int WtIdx = ResTy.getVectorNumElements();
2360
2361 for (unsigned i = 0; i < Indices.size(); i += 2) {
2362 if (Indices[i] != -1 && Indices[i] != WsIdx)
2363 return SDValue();
2364 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2365 return SDValue();
2366 WsIdx += 2;
2367 WtIdx += 2;
2368 }
2369
2370 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
2371 Op->getOperand(1));
2372}
2373
2374// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2375//
2376// ILVOD interleaves the odd elements from each vector.
2377//
2378// It is possible to lower into ILVOD when the mask takes the form:
2379// <1, n+1, 3, n+3, 5, n+5, ...>
2380// where n is the number of elements in the vector.
2381//
2382// When undef's appear in the mask they are treated as if they were whatever
2383// value is necessary in order to fit the above form.
2384static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2385 SmallVector<int, 16> Indices,
2386 SelectionDAG &DAG) {
2387 assert ((Indices.size() % 2) == 0);
2388 int WsIdx = 1;
2389 int WtIdx = ResTy.getVectorNumElements() + 1;
2390
2391 for (unsigned i = 0; i < Indices.size(); i += 2) {
2392 if (Indices[i] != -1 && Indices[i] != WsIdx)
2393 return SDValue();
2394 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2395 return SDValue();
2396 WsIdx += 2;
2397 WtIdx += 2;
2398 }
2399
2400 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
2401 Op->getOperand(1));
2402}
2403
2404// Lower VECTOR_SHUFFLE into ILVL (if possible).
2405//
2406// ILVL interleaves consecutive elements from the left half of each vector.
2407//
2408// It is possible to lower into ILVL when the mask takes the form:
2409// <0, n, 1, n+1, 2, n+2, ...>
2410// where n is the number of elements in the vector.
2411//
2412// When undef's appear in the mask they are treated as if they were whatever
2413// value is necessary in order to fit the above form.
2414static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2415 SmallVector<int, 16> Indices,
2416 SelectionDAG &DAG) {
2417 assert ((Indices.size() % 2) == 0);
2418 int WsIdx = 0;
2419 int WtIdx = ResTy.getVectorNumElements();
2420
2421 for (unsigned i = 0; i < Indices.size(); i += 2) {
2422 if (Indices[i] != -1 && Indices[i] != WsIdx)
2423 return SDValue();
2424 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2425 return SDValue();
2426 WsIdx ++;
2427 WtIdx ++;
2428 }
2429
2430 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2431 Op->getOperand(1));
2432}
2433
2434// Lower VECTOR_SHUFFLE into ILVR (if possible).
2435//
2436// ILVR interleaves consecutive elements from the right half of each vector.
2437//
2438// It is possible to lower into ILVR when the mask takes the form:
2439// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2440// where n is the number of elements in the vector and x is half n.
2441//
2442// When undef's appear in the mask they are treated as if they were whatever
2443// value is necessary in order to fit the above form.
2444static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2445 SmallVector<int, 16> Indices,
2446 SelectionDAG &DAG) {
2447 assert ((Indices.size() % 2) == 0);
2448 unsigned NumElts = ResTy.getVectorNumElements();
2449 int WsIdx = NumElts / 2;
2450 int WtIdx = NumElts + NumElts / 2;
2451
2452 for (unsigned i = 0; i < Indices.size(); i += 2) {
2453 if (Indices[i] != -1 && Indices[i] != WsIdx)
2454 return SDValue();
2455 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2456 return SDValue();
2457 WsIdx ++;
2458 WtIdx ++;
2459 }
2460
2461 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2462 Op->getOperand(1));
2463}
2464
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002465// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2466//
2467// PCKEV copies the even elements of each vector into the result vector.
2468//
2469// It is possible to lower into PCKEV when the mask takes the form:
2470// <0, 2, 4, ..., n, n+2, n+4, ...>
2471// where n is the number of elements in the vector.
2472//
2473// When undef's appear in the mask they are treated as if they were whatever
2474// value is necessary in order to fit the above form.
2475static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2476 SmallVector<int, 16> Indices,
2477 SelectionDAG &DAG) {
2478 assert ((Indices.size() % 2) == 0);
2479 int Idx = 0;
2480
2481 for (unsigned i = 0; i < Indices.size(); ++i) {
2482 if (Indices[i] != -1 && Indices[i] != Idx)
2483 return SDValue();
2484 Idx += 2;
2485 }
2486
2487 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2488 Op->getOperand(1));
2489}
2490
2491// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2492//
2493// PCKOD copies the odd elements of each vector into the result vector.
2494//
2495// It is possible to lower into PCKOD when the mask takes the form:
2496// <1, 3, 5, ..., n+1, n+3, n+5, ...>
2497// where n is the number of elements in the vector.
2498//
2499// When undef's appear in the mask they are treated as if they were whatever
2500// value is necessary in order to fit the above form.
2501static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2502 SmallVector<int, 16> Indices,
2503 SelectionDAG &DAG) {
2504 assert ((Indices.size() % 2) == 0);
2505 int Idx = 1;
2506
2507 for (unsigned i = 0; i < Indices.size(); ++i) {
2508 if (Indices[i] != -1 && Indices[i] != Idx)
2509 return SDValue();
2510 Idx += 2;
2511 }
2512
2513 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2514 Op->getOperand(1));
2515}
2516
Daniel Sanderse5087042013-09-24 14:02:15 +00002517// Lower VECTOR_SHUFFLE into VSHF.
2518//
2519// This mostly consists of converting the shuffle indices in Indices into a
2520// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2521// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2522// if the type is v8i16 and all the indices are less than 8 then the second
2523// operand is unused and can be replaced with anything. We choose to replace it
2524// with the used operand since this reduces the number of instructions overall.
2525static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2526 SmallVector<int, 16> Indices,
2527 SelectionDAG &DAG) {
2528 SmallVector<SDValue, 16> Ops;
2529 SDValue Op0;
2530 SDValue Op1;
2531 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2532 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2533 bool Using1stVec = false;
2534 bool Using2ndVec = false;
2535 SDLoc DL(Op);
2536 int ResTyNumElts = ResTy.getVectorNumElements();
2537
2538 for (int i = 0; i < ResTyNumElts; ++i) {
2539 // Idx == -1 means UNDEF
2540 int Idx = Indices[i];
2541
2542 if (0 <= Idx && Idx < ResTyNumElts)
2543 Using1stVec = true;
2544 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2545 Using2ndVec = true;
2546 }
2547
2548 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2549 ++I)
2550 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2551
2552 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, &Ops[0],
2553 Ops.size());
2554
2555 if (Using1stVec && Using2ndVec) {
2556 Op0 = Op->getOperand(0);
2557 Op1 = Op->getOperand(1);
2558 } else if (Using1stVec)
2559 Op0 = Op1 = Op->getOperand(0);
2560 else if (Using2ndVec)
2561 Op0 = Op1 = Op->getOperand(1);
2562 else
2563 llvm_unreachable("shuffle vector mask references neither vector operand?");
2564
2565 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op0, Op1);
2566}
2567
2568// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2569// indices in the shuffle.
2570SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2571 SelectionDAG &DAG) const {
2572 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2573 EVT ResTy = Op->getValueType(0);
2574
2575 if (!ResTy.is128BitVector())
2576 return SDValue();
2577
2578 int ResTyNumElts = ResTy.getVectorNumElements();
2579 SmallVector<int, 16> Indices;
2580
2581 for (int i = 0; i < ResTyNumElts; ++i)
2582 Indices.push_back(Node->getMaskElt(i));
2583
Daniel Sanders26307182013-09-24 14:20:00 +00002584 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2585 if (Result.getNode())
2586 return Result;
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002587 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2588 if (Result.getNode())
2589 return Result;
2590 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2591 if (Result.getNode())
2592 return Result;
2593 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2594 if (Result.getNode())
2595 return Result;
2596 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2597 if (Result.getNode())
2598 return Result;
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002599 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2600 if (Result.getNode())
2601 return Result;
2602 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2603 if (Result.getNode())
2604 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002605 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2606}
2607
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002608MachineBasicBlock * MipsSETargetLowering::
2609emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2610 // $bb:
2611 // bposge32_pseudo $vr0
2612 // =>
2613 // $bb:
2614 // bposge32 $tbb
2615 // $fbb:
2616 // li $vr2, 0
2617 // b $sink
2618 // $tbb:
2619 // li $vr1, 1
2620 // $sink:
2621 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2622
2623 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2624 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002625 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002626 DebugLoc DL = MI->getDebugLoc();
2627 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002628 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002629 MachineFunction *F = BB->getParent();
2630 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2631 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2632 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2633 F->insert(It, FBB);
2634 F->insert(It, TBB);
2635 F->insert(It, Sink);
2636
2637 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002638 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002639 BB->end());
2640 Sink->transferSuccessorsAndUpdatePHIs(BB);
2641
2642 // Add successors.
2643 BB->addSuccessor(FBB);
2644 BB->addSuccessor(TBB);
2645 FBB->addSuccessor(Sink);
2646 TBB->addSuccessor(Sink);
2647
2648 // Insert the real bposge32 instruction to $BB.
2649 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2650
2651 // Fill $FBB.
2652 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2653 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2654 .addReg(Mips::ZERO).addImm(0);
2655 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2656
2657 // Fill $TBB.
2658 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2659 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2660 .addReg(Mips::ZERO).addImm(1);
2661
2662 // Insert phi function to $Sink.
2663 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2664 MI->getOperand(0).getReg())
2665 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2666
2667 MI->eraseFromParent(); // The pseudo instruction is gone now.
2668 return Sink;
2669}
Daniel Sandersce09d072013-08-28 12:14:50 +00002670
2671MachineBasicBlock * MipsSETargetLowering::
2672emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2673 unsigned BranchOp) const{
2674 // $bb:
2675 // vany_nonzero $rd, $ws
2676 // =>
2677 // $bb:
2678 // bnz.b $ws, $tbb
2679 // b $fbb
2680 // $fbb:
2681 // li $rd1, 0
2682 // b $sink
2683 // $tbb:
2684 // li $rd2, 1
2685 // $sink:
2686 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2687
2688 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2689 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2690 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2691 DebugLoc DL = MI->getDebugLoc();
2692 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002693 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00002694 MachineFunction *F = BB->getParent();
2695 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2696 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2697 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2698 F->insert(It, FBB);
2699 F->insert(It, TBB);
2700 F->insert(It, Sink);
2701
2702 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002703 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00002704 BB->end());
2705 Sink->transferSuccessorsAndUpdatePHIs(BB);
2706
2707 // Add successors.
2708 BB->addSuccessor(FBB);
2709 BB->addSuccessor(TBB);
2710 FBB->addSuccessor(Sink);
2711 TBB->addSuccessor(Sink);
2712
2713 // Insert the real bnz.b instruction to $BB.
2714 BuildMI(BB, DL, TII->get(BranchOp))
2715 .addReg(MI->getOperand(1).getReg())
2716 .addMBB(TBB);
2717
2718 // Fill $FBB.
2719 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2720 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2721 .addReg(Mips::ZERO).addImm(0);
2722 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2723
2724 // Fill $TBB.
2725 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2726 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2727 .addReg(Mips::ZERO).addImm(1);
2728
2729 // Insert phi function to $Sink.
2730 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2731 MI->getOperand(0).getReg())
2732 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2733
2734 MI->eraseFromParent(); // The pseudo instruction is gone now.
2735 return Sink;
2736}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002737
2738// Emit the COPY_FW pseudo instruction.
2739//
2740// copy_fw_pseudo $fd, $ws, n
2741// =>
2742// copy_u_w $rt, $ws, $n
2743// mtc1 $rt, $fd
2744//
2745// When n is zero, the equivalent operation can be performed with (potentially)
2746// zero instructions due to register overlaps. This optimization is never valid
2747// for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2748MachineBasicBlock * MipsSETargetLowering::
2749emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2750 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2751 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2752 DebugLoc DL = MI->getDebugLoc();
2753 unsigned Fd = MI->getOperand(0).getReg();
2754 unsigned Ws = MI->getOperand(1).getReg();
2755 unsigned Lane = MI->getOperand(2).getImm();
2756
2757 if (Lane == 0)
2758 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2759 else {
2760 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2761
Daniel Sandersd9207702014-03-04 13:54:30 +00002762 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002763 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2764 }
2765
2766 MI->eraseFromParent(); // The pseudo instruction is gone now.
2767 return BB;
2768}
2769
2770// Emit the COPY_FD pseudo instruction.
2771//
2772// copy_fd_pseudo $fd, $ws, n
2773// =>
2774// splati.d $wt, $ws, $n
2775// copy $fd, $wt:sub_64
2776//
2777// When n is zero, the equivalent operation can be performed with (potentially)
2778// zero instructions due to register overlaps. This optimization is always
2779// valid because FR=1 mode which is the only supported mode in MSA.
2780MachineBasicBlock * MipsSETargetLowering::
2781emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
2782 assert(Subtarget->isFP64bit());
2783
2784 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2785 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2786 unsigned Fd = MI->getOperand(0).getReg();
2787 unsigned Ws = MI->getOperand(1).getReg();
2788 unsigned Lane = MI->getOperand(2).getImm() * 2;
2789 DebugLoc DL = MI->getDebugLoc();
2790
2791 if (Lane == 0)
2792 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2793 else {
2794 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2795
2796 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2797 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2798 }
2799
2800 MI->eraseFromParent(); // The pseudo instruction is gone now.
2801 return BB;
2802}
Daniel Sandersa5150702013-09-27 12:31:32 +00002803
2804// Emit the INSERT_FW pseudo instruction.
2805//
2806// insert_fw_pseudo $wd, $wd_in, $n, $fs
2807// =>
2808// subreg_to_reg $wt:sub_lo, $fs
2809// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002810MachineBasicBlock *
2811MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2812 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002813 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2814 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2815 DebugLoc DL = MI->getDebugLoc();
2816 unsigned Wd = MI->getOperand(0).getReg();
2817 unsigned Wd_in = MI->getOperand(1).getReg();
2818 unsigned Lane = MI->getOperand(2).getImm();
2819 unsigned Fs = MI->getOperand(3).getReg();
2820 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2821
2822 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002823 .addImm(0)
2824 .addReg(Fs)
2825 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00002826 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002827 .addReg(Wd_in)
2828 .addImm(Lane)
2829 .addReg(Wt);
Daniel Sandersa5150702013-09-27 12:31:32 +00002830
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002831 MI->eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00002832 return BB;
2833}
2834
2835// Emit the INSERT_FD pseudo instruction.
2836//
2837// insert_fd_pseudo $wd, $fs, n
2838// =>
2839// subreg_to_reg $wt:sub_64, $fs
2840// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002841MachineBasicBlock *
2842MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2843 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002844 assert(Subtarget->isFP64bit());
2845
2846 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2847 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2848 DebugLoc DL = MI->getDebugLoc();
2849 unsigned Wd = MI->getOperand(0).getReg();
2850 unsigned Wd_in = MI->getOperand(1).getReg();
2851 unsigned Lane = MI->getOperand(2).getImm();
2852 unsigned Fs = MI->getOperand(3).getReg();
2853 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2854
2855 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002856 .addImm(0)
2857 .addReg(Fs)
2858 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00002859 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002860 .addReg(Wd_in)
2861 .addImm(Lane)
2862 .addReg(Wt);
2863
2864 MI->eraseFromParent(); // The pseudo instruction is gone now.
2865 return BB;
2866}
2867
2868// Emit the FILL_FW pseudo instruction.
2869//
2870// fill_fw_pseudo $wd, $fs
2871// =>
2872// implicit_def $wt1
2873// insert_subreg $wt2:subreg_lo, $wt1, $fs
2874// splati.w $wd, $wt2[0]
2875MachineBasicBlock *
2876MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
2877 MachineBasicBlock *BB) const {
2878 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2879 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2880 DebugLoc DL = MI->getDebugLoc();
2881 unsigned Wd = MI->getOperand(0).getReg();
2882 unsigned Fs = MI->getOperand(1).getReg();
2883 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2884 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2885
2886 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2887 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2888 .addReg(Wt1)
2889 .addReg(Fs)
2890 .addImm(Mips::sub_lo);
2891 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
2892
2893 MI->eraseFromParent(); // The pseudo instruction is gone now.
2894 return BB;
2895}
2896
2897// Emit the FILL_FD pseudo instruction.
2898//
2899// fill_fd_pseudo $wd, $fs
2900// =>
2901// implicit_def $wt1
2902// insert_subreg $wt2:subreg_64, $wt1, $fs
2903// splati.d $wd, $wt2[0]
2904MachineBasicBlock *
2905MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
2906 MachineBasicBlock *BB) const {
2907 assert(Subtarget->isFP64bit());
2908
2909 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2910 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2911 DebugLoc DL = MI->getDebugLoc();
2912 unsigned Wd = MI->getOperand(0).getReg();
2913 unsigned Fs = MI->getOperand(1).getReg();
2914 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2915 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2916
2917 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2918 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2919 .addReg(Wt1)
2920 .addReg(Fs)
2921 .addImm(Mips::sub_64);
2922 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00002923
2924 MI->eraseFromParent(); // The pseudo instruction is gone now.
2925 return BB;
2926}
Daniel Sandersa9521602013-10-23 10:36:52 +00002927
2928// Emit the FEXP2_W_1 pseudo instructions.
2929//
2930// fexp2_w_1_pseudo $wd, $wt
2931// =>
2932// ldi.w $ws, 1
2933// fexp2.w $wd, $ws, $wt
2934MachineBasicBlock *
2935MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
2936 MachineBasicBlock *BB) const {
2937 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2938 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2939 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
2940 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2941 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2942 DebugLoc DL = MI->getDebugLoc();
2943
2944 // Splat 1.0 into a vector
2945 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
2946 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
2947
2948 // Emit 1.0 * fexp2(Wt)
2949 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
2950 .addReg(Ws2)
2951 .addReg(MI->getOperand(1).getReg());
2952
2953 MI->eraseFromParent(); // The pseudo instruction is gone now.
2954 return BB;
2955}
2956
2957// Emit the FEXP2_D_1 pseudo instructions.
2958//
2959// fexp2_d_1_pseudo $wd, $wt
2960// =>
2961// ldi.d $ws, 1
2962// fexp2.d $wd, $ws, $wt
2963MachineBasicBlock *
2964MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
2965 MachineBasicBlock *BB) const {
2966 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2967 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2968 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
2969 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2970 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2971 DebugLoc DL = MI->getDebugLoc();
2972
2973 // Splat 1.0 into a vector
2974 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
2975 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
2976
2977 // Emit 1.0 * fexp2(Wt)
2978 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
2979 .addReg(Ws2)
2980 .addReg(MI->getOperand(1).getReg());
2981
2982 MI->eraseFromParent(); // The pseudo instruction is gone now.
2983 return BB;
2984}