blob: 9bd3be2d43152963b6a55316e9aa3659f31e4158 [file] [log] [blame]
Akira Hatanaka042b7962013-03-14 19:09:52 +00001//===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Subclass of MipsTargetLowering specialized for mips32/64.
11//
12//===----------------------------------------------------------------------===//
13#include "MipsSEISelLowering.h"
14#include "MipsRegisterInfo.h"
15#include "MipsTargetMachine.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
Akira Hatanaka4e0980a2013-04-13 02:13:30 +000018#include "llvm/IR/Intrinsics.h"
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000019#include "llvm/Support/CommandLine.h"
20#include "llvm/Target/TargetInstrInfo.h"
21
22using namespace llvm;
23
24static cl::opt<bool>
25EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
26 cl::desc("MIPS: Enable tail calls."), cl::init(false));
27
Akira Hatanaka3e675852013-09-07 00:52:30 +000028static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
29 cl::desc("Expand double precision loads and "
30 "stores to their single precision "
31 "counterparts"));
32
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000033MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
34 : MipsTargetLowering(TM) {
35 // Set up the register classes
Reed Kotlera430cb62013-04-09 19:46:01 +000036
37 clearRegisterClasses();
38
Akira Hatanaka18587862013-08-06 23:08:38 +000039 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000040
41 if (HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +000042 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000043
44 if (Subtarget->hasDSP()) {
45 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
46
47 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
Akira Hatanaka7d635522013-08-14 00:53:38 +000048 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000049
50 // Expand all builtin opcodes.
51 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
52 setOperationAction(Opc, VecTys[i], Expand);
53
Akira Hatanaka3d602412013-04-13 00:55:41 +000054 setOperationAction(ISD::ADD, VecTys[i], Legal);
55 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000056 setOperationAction(ISD::LOAD, VecTys[i], Legal);
57 setOperationAction(ISD::STORE, VecTys[i], Legal);
58 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
59 }
Akira Hatanaka97a62bf2013-04-19 23:21:32 +000060
Akira Hatanaka5e795092013-08-02 19:23:33 +000061 // Expand all truncating stores and extending loads.
62 unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
63 unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
64
65 for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
66 for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
67 setTruncStoreAction((MVT::SimpleValueType)VT0,
68 (MVT::SimpleValueType)VT1, Expand);
69
70 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
71 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
72 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
73 }
74
Akira Hatanaka97a62bf2013-04-19 23:21:32 +000075 setTargetDAGCombine(ISD::SHL);
76 setTargetDAGCombine(ISD::SRA);
77 setTargetDAGCombine(ISD::SRL);
Akira Hatanakacd6c5792013-04-30 22:37:26 +000078 setTargetDAGCombine(ISD::SETCC);
79 setTargetDAGCombine(ISD::VSELECT);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000080 }
81
Akira Hatanaka3d602412013-04-13 00:55:41 +000082 if (Subtarget->hasDSPR2())
83 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
84
Jack Carter3f70e902013-08-13 20:54:07 +000085 if (Subtarget->hasMSA()) {
Daniel Sandersddfbd582013-09-11 10:15:48 +000086 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
87 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
88 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
89 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
90 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
91 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
92 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
Daniel Sanders915432c2013-09-23 13:22:24 +000093
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +000094 setTargetDAGCombine(ISD::AND);
95 setTargetDAGCombine(ISD::SRA);
Daniel Sanders915432c2013-09-23 13:22:24 +000096 setTargetDAGCombine(ISD::XOR);
Jack Carter3f70e902013-08-13 20:54:07 +000097 }
98
Reed Kotlerc673f9c2013-08-30 19:40:56 +000099 if (!Subtarget->mipsSEUsesSoftFloat()) {
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000100 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
101
102 // When dealing with single precision only, use libcalls
103 if (!Subtarget->isSingleFloat()) {
Akira Hatanakaad341d42013-08-20 23:38:40 +0000104 if (Subtarget->isFP64bit())
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000105 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
106 else
107 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
108 }
109 }
110
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000111 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
112 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
113 setOperationAction(ISD::MULHS, MVT::i32, Custom);
114 setOperationAction(ISD::MULHU, MVT::i32, Custom);
115
Akira Hatanakafc82e4d2013-04-11 19:29:26 +0000116 if (HasMips64) {
117 setOperationAction(ISD::MULHS, MVT::i64, Custom);
118 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000119 setOperationAction(ISD::MUL, MVT::i64, Custom);
Akira Hatanakafc82e4d2013-04-11 19:29:26 +0000120 }
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000121
Akira Hatanaka4e0980a2013-04-13 02:13:30 +0000122 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
123 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
124
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000125 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
126 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
127 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
128 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000129 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
130 setOperationAction(ISD::LOAD, MVT::i32, Custom);
131 setOperationAction(ISD::STORE, MVT::i32, Custom);
132
Akira Hatanakad593a772013-03-30 01:42:24 +0000133 setTargetDAGCombine(ISD::ADDE);
134 setTargetDAGCombine(ISD::SUBE);
Akira Hatanaka9a308df2013-06-26 18:48:17 +0000135 setTargetDAGCombine(ISD::MUL);
Akira Hatanakad593a772013-03-30 01:42:24 +0000136
Daniel Sanders3c380d52013-08-28 12:14:50 +0000137 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanders2fd3e672013-08-28 12:04:29 +0000138 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
139 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
140
Akira Hatanaka3e675852013-09-07 00:52:30 +0000141 if (NoDPLoadStore) {
142 setOperationAction(ISD::LOAD, MVT::f64, Custom);
143 setOperationAction(ISD::STORE, MVT::f64, Custom);
144 }
145
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000146 computeRegisterProperties();
147}
148
149const MipsTargetLowering *
150llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
151 return new MipsSETargetLowering(TM);
152}
153
Daniel Sandersda521cc2013-09-23 12:02:46 +0000154// Enable MSA support for the given integer type and Register class.
Daniel Sandersc73488a2013-08-23 10:10:13 +0000155void MipsSETargetLowering::
Daniel Sandersddfbd582013-09-11 10:15:48 +0000156addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
157 addRegisterClass(Ty, RC);
158
159 // Expand all builtin opcodes.
160 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
161 setOperationAction(Opc, Ty, Expand);
162
163 setOperationAction(ISD::BITCAST, Ty, Legal);
164 setOperationAction(ISD::LOAD, Ty, Legal);
165 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000166 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
167 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sandersda521cc2013-09-23 12:02:46 +0000168 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersddfbd582013-09-11 10:15:48 +0000169
Daniel Sanders68831cb2013-09-11 10:28:16 +0000170 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders4e812c12013-09-23 12:57:42 +0000171 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersf2eb1e42013-09-11 11:58:30 +0000172 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sandersa399d692013-09-23 13:40:21 +0000173 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersf2eb1e42013-09-11 11:58:30 +0000174 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders4e812c12013-09-23 12:57:42 +0000175 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sandersece929d2013-09-11 10:38:58 +0000176 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sandersf2eb1e42013-09-11 11:58:30 +0000177 setOperationAction(ISD::SHL, Ty, Legal);
178 setOperationAction(ISD::SRA, Ty, Legal);
179 setOperationAction(ISD::SRL, Ty, Legal);
180 setOperationAction(ISD::SUB, Ty, Legal);
Daniel Sandersece929d2013-09-11 10:38:58 +0000181 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders4e812c12013-09-23 12:57:42 +0000182 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersddfbd582013-09-11 10:15:48 +0000183}
184
Daniel Sandersda521cc2013-09-23 12:02:46 +0000185// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersddfbd582013-09-11 10:15:48 +0000186void MipsSETargetLowering::
187addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sandersc73488a2013-08-23 10:10:13 +0000188 addRegisterClass(Ty, RC);
Jack Cartere2a93762013-08-15 12:24:57 +0000189
190 // Expand all builtin opcodes.
191 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
192 setOperationAction(Opc, Ty, Expand);
193
194 setOperationAction(ISD::LOAD, Ty, Legal);
195 setOperationAction(ISD::STORE, Ty, Legal);
196 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000197 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sanders2ac12822013-09-11 10:51:30 +0000198
199 if (Ty != MVT::v8f16) {
200 setOperationAction(ISD::FADD, Ty, Legal);
201 setOperationAction(ISD::FDIV, Ty, Legal);
202 setOperationAction(ISD::FLOG2, Ty, Legal);
203 setOperationAction(ISD::FMUL, Ty, Legal);
204 setOperationAction(ISD::FRINT, Ty, Legal);
205 setOperationAction(ISD::FSQRT, Ty, Legal);
206 setOperationAction(ISD::FSUB, Ty, Legal);
207 }
Jack Cartere2a93762013-08-15 12:24:57 +0000208}
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000209
210bool
211MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
212 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
213
214 switch (SVT) {
215 case MVT::i64:
216 case MVT::i32:
217 if (Fast)
218 *Fast = true;
219 return true;
220 default:
221 return false;
222 }
223}
224
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000225SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
226 SelectionDAG &DAG) const {
227 switch(Op.getOpcode()) {
Akira Hatanaka3e675852013-09-07 00:52:30 +0000228 case ISD::LOAD: return lowerLOAD(Op, DAG);
229 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000230 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
231 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
232 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
233 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
234 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
235 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakab109ea82013-04-22 20:13:37 +0000236 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
237 DAG);
Akira Hatanaka4e0980a2013-04-13 02:13:30 +0000238 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
239 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanders2fd3e672013-08-28 12:04:29 +0000240 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000241 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sandersda521cc2013-09-23 12:02:46 +0000242 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000243 }
244
245 return MipsTargetLowering::LowerOperation(Op, DAG);
246}
247
Akira Hatanakad593a772013-03-30 01:42:24 +0000248// selectMADD -
249// Transforms a subgraph in CurDAG if the following pattern is found:
250// (addc multLo, Lo0), (adde multHi, Hi0),
251// where,
252// multHi/Lo: product of multiplication
253// Lo0: initial value of Lo register
254// Hi0: initial value of Hi register
255// Return true if pattern matching was successful.
256static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
257 // ADDENode's second operand must be a flag output of an ADDC node in order
258 // for the matching to be successful.
259 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
260
261 if (ADDCNode->getOpcode() != ISD::ADDC)
262 return false;
263
264 SDValue MultHi = ADDENode->getOperand(0);
265 SDValue MultLo = ADDCNode->getOperand(0);
266 SDNode *MultNode = MultHi.getNode();
267 unsigned MultOpc = MultHi.getOpcode();
268
269 // MultHi and MultLo must be generated by the same node,
270 if (MultLo.getNode() != MultNode)
271 return false;
272
273 // and it must be a multiplication.
274 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
275 return false;
276
277 // MultLo amd MultHi must be the first and second output of MultNode
278 // respectively.
279 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
280 return false;
281
282 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
283 // of the values of MultNode, in which case MultNode will be removed in later
284 // phases.
285 // If there exist users other than ADDENode or ADDCNode, this function returns
286 // here, which will result in MultNode being mapped to a single MULT
287 // instruction node rather than a pair of MULT and MADD instructions being
288 // produced.
289 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
290 return false;
291
Andrew Trickac6d9be2013-05-25 02:42:55 +0000292 SDLoc DL(ADDENode);
Akira Hatanakad593a772013-03-30 01:42:24 +0000293
294 // Initialize accumulator.
295 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
296 ADDCNode->getOperand(1),
297 ADDENode->getOperand(1));
298
299 // create MipsMAdd(u) node
300 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
301
302 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
303 MultNode->getOperand(0),// Factor 0
304 MultNode->getOperand(1),// Factor 1
305 ACCIn);
306
307 // replace uses of adde and addc here
308 if (!SDValue(ADDCNode, 0).use_empty()) {
309 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
310 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
311 LoIdx);
312 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
313 }
314 if (!SDValue(ADDENode, 0).use_empty()) {
315 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
316 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
317 HiIdx);
318 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
319 }
320
321 return true;
322}
323
324// selectMSUB -
325// Transforms a subgraph in CurDAG if the following pattern is found:
326// (addc Lo0, multLo), (sube Hi0, multHi),
327// where,
328// multHi/Lo: product of multiplication
329// Lo0: initial value of Lo register
330// Hi0: initial value of Hi register
331// Return true if pattern matching was successful.
332static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
333 // SUBENode's second operand must be a flag output of an SUBC node in order
334 // for the matching to be successful.
335 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
336
337 if (SUBCNode->getOpcode() != ISD::SUBC)
338 return false;
339
340 SDValue MultHi = SUBENode->getOperand(1);
341 SDValue MultLo = SUBCNode->getOperand(1);
342 SDNode *MultNode = MultHi.getNode();
343 unsigned MultOpc = MultHi.getOpcode();
344
345 // MultHi and MultLo must be generated by the same node,
346 if (MultLo.getNode() != MultNode)
347 return false;
348
349 // and it must be a multiplication.
350 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
351 return false;
352
353 // MultLo amd MultHi must be the first and second output of MultNode
354 // respectively.
355 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
356 return false;
357
358 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
359 // of the values of MultNode, in which case MultNode will be removed in later
360 // phases.
361 // If there exist users other than SUBENode or SUBCNode, this function returns
362 // here, which will result in MultNode being mapped to a single MULT
363 // instruction node rather than a pair of MULT and MSUB instructions being
364 // produced.
365 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
366 return false;
367
Andrew Trickac6d9be2013-05-25 02:42:55 +0000368 SDLoc DL(SUBENode);
Akira Hatanakad593a772013-03-30 01:42:24 +0000369
370 // Initialize accumulator.
371 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
372 SUBCNode->getOperand(0),
373 SUBENode->getOperand(0));
374
375 // create MipsSub(u) node
376 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
377
378 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
379 MultNode->getOperand(0),// Factor 0
380 MultNode->getOperand(1),// Factor 1
381 ACCIn);
382
383 // replace uses of sube and subc here
384 if (!SDValue(SUBCNode, 0).use_empty()) {
385 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
386 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
387 LoIdx);
388 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
389 }
390 if (!SDValue(SUBENode, 0).use_empty()) {
391 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
392 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
393 HiIdx);
394 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
395 }
396
397 return true;
398}
399
400static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
401 TargetLowering::DAGCombinerInfo &DCI,
402 const MipsSubtarget *Subtarget) {
403 if (DCI.isBeforeLegalize())
404 return SDValue();
405
406 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
407 selectMADD(N, &DAG))
408 return SDValue(N, 0);
409
410 return SDValue();
411}
412
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000413// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
414//
415// Performs the following transformations:
416// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
417// sign/zero-extension is completely overwritten by the new one performed by
418// the ISD::AND.
419// - Removes redundant zero extensions performed by an ISD::AND.
420static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
421 TargetLowering::DAGCombinerInfo &DCI,
422 const MipsSubtarget *Subtarget) {
423 if (!Subtarget->hasMSA())
424 return SDValue();
425
426 SDValue Op0 = N->getOperand(0);
427 SDValue Op1 = N->getOperand(1);
428 unsigned Op0Opcode = Op0->getOpcode();
429
430 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
431 // where $d + 1 == 2^n and n == 32
432 // or $d + 1 == 2^n and n <= 32 and ZExt
433 // -> (MipsVExtractZExt $a, $b, $c)
434 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
435 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
436 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
437
438 if (!Mask)
439 return SDValue();
440
441 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
442
443 if (Log2IfPositive <= 0)
444 return SDValue(); // Mask+1 is not a power of 2
445
446 SDValue Op0Op2 = Op0->getOperand(2);
447 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
448 unsigned ExtendTySize = ExtendTy.getSizeInBits();
449 unsigned Log2 = Log2IfPositive;
450
451 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
452 Log2 == ExtendTySize) {
453 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
454 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
455 Op0->getVTList(), Ops, Op0->getNumOperands());
456 return Op0;
457 }
458 }
459
460 return SDValue();
461}
462
Akira Hatanakad593a772013-03-30 01:42:24 +0000463static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
464 TargetLowering::DAGCombinerInfo &DCI,
465 const MipsSubtarget *Subtarget) {
466 if (DCI.isBeforeLegalize())
467 return SDValue();
468
469 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
470 selectMSUB(N, &DAG))
471 return SDValue(N, 0);
472
473 return SDValue();
474}
475
Akira Hatanaka9a308df2013-06-26 18:48:17 +0000476static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
477 EVT ShiftTy, SelectionDAG &DAG) {
478 // Clear the upper (64 - VT.sizeInBits) bits.
479 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
480
481 // Return 0.
482 if (C == 0)
483 return DAG.getConstant(0, VT);
484
485 // Return x.
486 if (C == 1)
487 return X;
488
489 // If c is power of 2, return (shl x, log2(c)).
490 if (isPowerOf2_64(C))
491 return DAG.getNode(ISD::SHL, DL, VT, X,
492 DAG.getConstant(Log2_64(C), ShiftTy));
493
494 unsigned Log2Ceil = Log2_64_Ceil(C);
495 uint64_t Floor = 1LL << Log2_64(C);
496 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
497
498 // If |c - floor_c| <= |c - ceil_c|,
499 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
500 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
501 if (C - Floor <= Ceil - C) {
502 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
503 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
504 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
505 }
506
507 // If |c - floor_c| > |c - ceil_c|,
508 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
509 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
510 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
511 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
512}
513
514static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
515 const TargetLowering::DAGCombinerInfo &DCI,
516 const MipsSETargetLowering *TL) {
517 EVT VT = N->getValueType(0);
518
519 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
520 if (!VT.isVector())
521 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
522 VT, TL->getScalarShiftAmountTy(VT), DAG);
523
524 return SDValue(N, 0);
525}
526
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000527static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
528 SelectionDAG &DAG,
529 const MipsSubtarget *Subtarget) {
530 // See if this is a vector splat immediate node.
531 APInt SplatValue, SplatUndef;
532 unsigned SplatBitSize;
533 bool HasAnyUndefs;
534 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
535 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
536
Akira Hatanakad5972632013-04-22 19:58:23 +0000537 if (!BV ||
Akira Hatanakab109ea82013-04-22 20:13:37 +0000538 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Akira Hatanakae311b002013-04-23 18:09:42 +0000539 EltSize, !Subtarget->isLittle()) ||
Akira Hatanakad5972632013-04-22 19:58:23 +0000540 (SplatBitSize != EltSize) ||
Akira Hatanakae311b002013-04-23 18:09:42 +0000541 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000542 return SDValue();
543
Andrew Trickac6d9be2013-05-25 02:42:55 +0000544 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000545 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
546}
547
548static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
549 TargetLowering::DAGCombinerInfo &DCI,
550 const MipsSubtarget *Subtarget) {
551 EVT Ty = N->getValueType(0);
552
553 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
554 return SDValue();
555
556 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
557}
558
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000559// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
560// constant splats into MipsISD::SHRA_DSP for DSPr2.
561//
562// Performs the following transformations:
563// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
564// sign/zero-extension is completely overwritten by the new one performed by
565// the ISD::SRA and ISD::SHL nodes.
566// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
567// sequence.
568//
569// See performDSPShiftCombine for more information about the transformation
570// used for DSPr2.
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000571static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
572 TargetLowering::DAGCombinerInfo &DCI,
573 const MipsSubtarget *Subtarget) {
574 EVT Ty = N->getValueType(0);
575
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000576 if (Subtarget->hasMSA()) {
577 SDValue Op0 = N->getOperand(0);
578 SDValue Op1 = N->getOperand(1);
579
580 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
581 // where $d + sizeof($c) == 32
582 // or $d + sizeof($c) <= 32 and SExt
583 // -> (MipsVExtractSExt $a, $b, $c)
584 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
585 SDValue Op0Op0 = Op0->getOperand(0);
586 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
587
588 if (!ShAmount)
589 return SDValue();
590
591 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
592 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
593
594 if (TotalBits == 32 ||
595 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
596 TotalBits <= 32)) {
597 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
598 Op0Op0->getOperand(2) };
599 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
600 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
601 return Op0Op0;
602 }
603 }
604 }
605
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000606 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
607 return SDValue();
608
609 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
610}
611
612
613static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
614 TargetLowering::DAGCombinerInfo &DCI,
615 const MipsSubtarget *Subtarget) {
616 EVT Ty = N->getValueType(0);
617
618 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
619 return SDValue();
620
621 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
622}
623
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000624static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
625 bool IsV216 = (Ty == MVT::v2i16);
626
627 switch (CC) {
628 case ISD::SETEQ:
629 case ISD::SETNE: return true;
630 case ISD::SETLT:
631 case ISD::SETLE:
632 case ISD::SETGT:
633 case ISD::SETGE: return IsV216;
634 case ISD::SETULT:
635 case ISD::SETULE:
636 case ISD::SETUGT:
637 case ISD::SETUGE: return !IsV216;
638 default: return false;
639 }
640}
641
642static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
643 EVT Ty = N->getValueType(0);
644
645 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
646 return SDValue();
647
648 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
649 return SDValue();
650
Andrew Trickac6d9be2013-05-25 02:42:55 +0000651 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000652 N->getOperand(1), N->getOperand(2));
653}
654
655static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
656 EVT Ty = N->getValueType(0);
657
658 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
659 return SDValue();
660
661 SDValue SetCC = N->getOperand(0);
662
663 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
664 return SDValue();
665
Andrew Trickac6d9be2013-05-25 02:42:55 +0000666 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000667 SetCC.getOperand(0), SetCC.getOperand(1), N->getOperand(1),
668 N->getOperand(2), SetCC.getOperand(2));
669}
670
Daniel Sanders915432c2013-09-23 13:22:24 +0000671static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
672 const MipsSubtarget *Subtarget) {
673 EVT Ty = N->getValueType(0);
674
675 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
676 // Try the following combines:
677 // (xor (or $a, $b), (build_vector allones))
678 // (xor (or $a, $b), (bitcast (build_vector allones)))
679 SDValue Op0 = N->getOperand(0);
680 SDValue Op1 = N->getOperand(1);
681 SDValue NotOp;
682 ConstantSDNode *Const;
683
684 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
685 NotOp = Op1;
686 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
687 NotOp = Op0;
688 else if ((Op0->getOpcode() == MipsISD::VSPLAT ||
689 Op0->getOpcode() == MipsISD::VSPLATD) &&
690 (Const = dyn_cast<ConstantSDNode>(Op0->getOperand(0))) &&
691 Const->isAllOnesValue())
692 NotOp = Op1;
693 else if ((Op1->getOpcode() == MipsISD::VSPLAT ||
694 Op1->getOpcode() == MipsISD::VSPLATD) &&
695 (Const = dyn_cast<ConstantSDNode>(Op1->getOperand(0))) &&
696 Const->isAllOnesValue())
697 NotOp = Op0;
698 else
699 return SDValue();
700
701 if (NotOp->getOpcode() == ISD::OR)
702 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
703 NotOp->getOperand(1));
704 }
705
706 return SDValue();
707}
708
Akira Hatanakad593a772013-03-30 01:42:24 +0000709SDValue
710MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
711 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000712 SDValue Val;
Akira Hatanakad593a772013-03-30 01:42:24 +0000713
714 switch (N->getOpcode()) {
715 case ISD::ADDE:
716 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000717 case ISD::AND:
718 Val = performANDCombine(N, DAG, DCI, Subtarget);
719 break;
Akira Hatanakad593a772013-03-30 01:42:24 +0000720 case ISD::SUBE:
721 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka9a308df2013-06-26 18:48:17 +0000722 case ISD::MUL:
723 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000724 case ISD::SHL:
725 return performSHLCombine(N, DAG, DCI, Subtarget);
726 case ISD::SRA:
727 return performSRACombine(N, DAG, DCI, Subtarget);
728 case ISD::SRL:
729 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000730 case ISD::VSELECT:
731 return performVSELECTCombine(N, DAG);
Daniel Sanders915432c2013-09-23 13:22:24 +0000732 case ISD::XOR:
733 Val = performXORCombine(N, DAG, Subtarget);
734 break;
735 case ISD::SETCC:
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000736 Val = performSETCCCombine(N, DAG);
737 break;
Akira Hatanakad593a772013-03-30 01:42:24 +0000738 }
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000739
740 if (Val.getNode())
741 return Val;
742
743 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanakad593a772013-03-30 01:42:24 +0000744}
745
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000746MachineBasicBlock *
747MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
748 MachineBasicBlock *BB) const {
749 switch (MI->getOpcode()) {
750 default:
751 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
752 case Mips::BPOSGE32_PSEUDO:
753 return emitBPOSGE32(MI, BB);
Daniel Sanders3c380d52013-08-28 12:14:50 +0000754 case Mips::SNZ_B_PSEUDO:
755 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
756 case Mips::SNZ_H_PSEUDO:
757 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
758 case Mips::SNZ_W_PSEUDO:
759 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
760 case Mips::SNZ_D_PSEUDO:
761 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
762 case Mips::SNZ_V_PSEUDO:
763 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
764 case Mips::SZ_B_PSEUDO:
765 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
766 case Mips::SZ_H_PSEUDO:
767 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
768 case Mips::SZ_W_PSEUDO:
769 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
770 case Mips::SZ_D_PSEUDO:
771 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
772 case Mips::SZ_V_PSEUDO:
773 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000774 }
775}
776
777bool MipsSETargetLowering::
778isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
779 unsigned NextStackOffset,
780 const MipsFunctionInfo& FI) const {
781 if (!EnableMipsTailCalls)
782 return false;
783
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000784 // Return false if either the callee or caller has a byval argument.
785 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
786 return false;
787
788 // Return true if the callee's argument area is no larger than the
789 // caller's.
790 return NextStackOffset <= FI.getIncomingArgSize();
791}
792
793void MipsSETargetLowering::
794getOpndList(SmallVectorImpl<SDValue> &Ops,
795 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
796 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
797 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
798 // T9 should contain the address of the callee function if
799 // -reloction-model=pic or it is an indirect call.
800 if (IsPICCall || !GlobalOrExternal) {
801 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
802 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
803 } else
804 Ops.push_back(Callee);
805
806 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
807 InternalLinkage, CLI, Callee, Chain);
808}
809
Akira Hatanaka3e675852013-09-07 00:52:30 +0000810SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
811 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
812
813 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
814 return MipsTargetLowering::lowerLOAD(Op, DAG);
815
816 // Replace a double precision load with two i32 loads and a buildpair64.
817 SDLoc DL(Op);
818 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
819 EVT PtrVT = Ptr.getValueType();
820
821 // i32 load from lower address.
822 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
823 MachinePointerInfo(), Nd.isVolatile(),
824 Nd.isNonTemporal(), Nd.isInvariant(),
825 Nd.getAlignment());
826
827 // i32 load from higher address.
828 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
829 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
830 MachinePointerInfo(), Nd.isVolatile(),
831 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka2dd3afc2013-09-09 17:59:32 +0000832 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka3e675852013-09-07 00:52:30 +0000833
834 if (!Subtarget->isLittle())
835 std::swap(Lo, Hi);
836
837 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
838 SDValue Ops[2] = {BP, Hi.getValue(1)};
839 return DAG.getMergeValues(Ops, 2, DL);
840}
841
842SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
843 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
844
845 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
846 return MipsTargetLowering::lowerSTORE(Op, DAG);
847
848 // Replace a double precision store with two extractelement64s and i32 stores.
849 SDLoc DL(Op);
850 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
851 EVT PtrVT = Ptr.getValueType();
852 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
853 Val, DAG.getConstant(0, MVT::i32));
854 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
855 Val, DAG.getConstant(1, MVT::i32));
856
857 if (!Subtarget->isLittle())
858 std::swap(Lo, Hi);
859
860 // i32 store to lower address.
861 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
862 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
863 Nd.getTBAAInfo());
864
865 // i32 store to higher address.
866 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
867 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka2dd3afc2013-09-09 17:59:32 +0000868 Nd.isVolatile(), Nd.isNonTemporal(),
869 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
Akira Hatanaka3e675852013-09-07 00:52:30 +0000870}
871
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000872SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
873 bool HasLo, bool HasHi,
874 SelectionDAG &DAG) const {
875 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000876 SDLoc DL(Op);
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000877 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
878 Op.getOperand(0), Op.getOperand(1));
879 SDValue Lo, Hi;
880
881 if (HasLo)
882 Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
883 DAG.getConstant(Mips::sub_lo, MVT::i32));
884 if (HasHi)
885 Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
886 DAG.getConstant(Mips::sub_hi, MVT::i32));
887
888 if (!HasLo || !HasHi)
889 return HasLo ? Lo : Hi;
890
891 SDValue Vals[] = { Lo, Hi };
892 return DAG.getMergeValues(Vals, 2, DL);
893}
894
Akira Hatanaka4e0980a2013-04-13 02:13:30 +0000895
Andrew Trickac6d9be2013-05-25 02:42:55 +0000896static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanaka4e0980a2013-04-13 02:13:30 +0000897 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
898 DAG.getConstant(0, MVT::i32));
899 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
900 DAG.getConstant(1, MVT::i32));
901 return DAG.getNode(MipsISD::InsertLOHI, DL, MVT::Untyped, InLo, InHi);
902}
903
Andrew Trickac6d9be2013-05-25 02:42:55 +0000904static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanaka4e0980a2013-04-13 02:13:30 +0000905 SDValue Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
906 DAG.getConstant(Mips::sub_lo, MVT::i32));
907 SDValue Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
908 DAG.getConstant(Mips::sub_hi, MVT::i32));
909 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
910}
911
912// This function expands mips intrinsic nodes which have 64-bit input operands
913// or output values.
914//
915// out64 = intrinsic-node in64
916// =>
917// lo = copy (extract-element (in64, 0))
918// hi = copy (extract-element (in64, 1))
919// mips-specific-node
920// v0 = copy lo
921// v1 = copy hi
922// out64 = merge-values (v0, v1)
923//
924static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000925 SDLoc DL(Op);
Akira Hatanaka4e0980a2013-04-13 02:13:30 +0000926 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
927 SmallVector<SDValue, 3> Ops;
928 unsigned OpNo = 0;
929
930 // See if Op has a chain input.
931 if (HasChainIn)
932 Ops.push_back(Op->getOperand(OpNo++));
933
934 // The next operand is the intrinsic opcode.
935 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
936
937 // See if the next operand has type i64.
938 SDValue Opnd = Op->getOperand(++OpNo), In64;
939
940 if (Opnd.getValueType() == MVT::i64)
941 In64 = initAccumulator(Opnd, DL, DAG);
942 else
943 Ops.push_back(Opnd);
944
945 // Push the remaining operands.
946 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
947 Ops.push_back(Op->getOperand(OpNo));
948
949 // Add In64 to the end of the list.
950 if (In64.getNode())
951 Ops.push_back(In64);
952
953 // Scan output.
954 SmallVector<EVT, 2> ResTys;
955
956 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
957 I != E; ++I)
958 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
959
960 // Create node.
961 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
962 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
963
964 if (!HasChainIn)
965 return Out;
966
967 assert(Val->getValueType(1) == MVT::Other);
968 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
969 return DAG.getMergeValues(Vals, 2, DL);
970}
971
Daniel Sanders68831cb2013-09-11 10:28:16 +0000972static SDValue lowerMSABinaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
973 SDLoc DL(Op);
974 SDValue LHS = Op->getOperand(1);
975 SDValue RHS = Op->getOperand(2);
976 EVT ResTy = Op->getValueType(0);
977
978 SDValue Result = DAG.getNode(Opc, DL, ResTy, LHS, RHS);
979
980 return Result;
981}
982
Daniel Sanderse0187e52013-09-23 14:29:55 +0000983static SDValue lowerMSABinaryImmIntr(SDValue Op, SelectionDAG &DAG,
984 unsigned Opc, SDValue RHS) {
985 SDValue LHS = Op->getOperand(1);
986 EVT ResTy = Op->getValueType(0);
987
988 return DAG.getNode(Opc, SDLoc(Op), ResTy, LHS, RHS);
989}
990
Daniel Sanders3c380d52013-08-28 12:14:50 +0000991static SDValue lowerMSABranchIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
992 SDLoc DL(Op);
993 SDValue Value = Op->getOperand(1);
994 EVT ResTy = Op->getValueType(0);
995
996 SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
997
998 return Result;
999}
1000
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +00001001// Lower an MSA copy intrinsic into the specified SelectionDAG node
1002static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1003 SDLoc DL(Op);
1004 SDValue Vec = Op->getOperand(1);
1005 SDValue Idx = Op->getOperand(2);
1006 EVT ResTy = Op->getValueType(0);
1007 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1008
1009 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1010 DAG.getValueType(EltTy));
1011
1012 return Result;
1013}
1014
1015// Lower an MSA insert intrinsic into the specified SelectionDAG node
1016static SDValue lowerMSAInsertIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1017 SDLoc DL(Op);
1018 SDValue Op0 = Op->getOperand(1);
1019 SDValue Op1 = Op->getOperand(2);
1020 SDValue Op2 = Op->getOperand(3);
1021 EVT ResTy = Op->getValueType(0);
1022
1023 SDValue Result = DAG.getNode(Opc, DL, ResTy, Op0, Op2, Op1);
1024
1025 return Result;
1026}
1027
Daniel Sanderse0187e52013-09-23 14:29:55 +00001028static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1029 EVT ResTy = Op->getValueType(0);
1030
1031 unsigned SplatOp = MipsISD::VSPLAT;
1032 if (ResTy == MVT::v2i64)
1033 SplatOp = MipsISD::VSPLATD;
1034
1035 return DAG.getNode(SplatOp, SDLoc(Op), ResTy, Op->getOperand(ImmOp));
1036}
1037
Daniel Sanders2ac12822013-09-11 10:51:30 +00001038static SDValue lowerMSAUnaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1039 SDLoc DL(Op);
1040 SDValue Value = Op->getOperand(1);
1041 EVT ResTy = Op->getValueType(0);
1042
1043 SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
1044
1045 return Result;
1046}
1047
Akira Hatanaka4e0980a2013-04-13 02:13:30 +00001048SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1049 SelectionDAG &DAG) const {
1050 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1051 default:
1052 return SDValue();
1053 case Intrinsic::mips_shilo:
1054 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1055 case Intrinsic::mips_dpau_h_qbl:
1056 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1057 case Intrinsic::mips_dpau_h_qbr:
1058 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1059 case Intrinsic::mips_dpsu_h_qbl:
1060 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1061 case Intrinsic::mips_dpsu_h_qbr:
1062 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1063 case Intrinsic::mips_dpa_w_ph:
1064 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1065 case Intrinsic::mips_dps_w_ph:
1066 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1067 case Intrinsic::mips_dpax_w_ph:
1068 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1069 case Intrinsic::mips_dpsx_w_ph:
1070 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1071 case Intrinsic::mips_mulsa_w_ph:
1072 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1073 case Intrinsic::mips_mult:
1074 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1075 case Intrinsic::mips_multu:
1076 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1077 case Intrinsic::mips_madd:
1078 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1079 case Intrinsic::mips_maddu:
1080 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1081 case Intrinsic::mips_msub:
1082 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1083 case Intrinsic::mips_msubu:
1084 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sanders68831cb2013-09-11 10:28:16 +00001085 case Intrinsic::mips_addv_b:
1086 case Intrinsic::mips_addv_h:
1087 case Intrinsic::mips_addv_w:
1088 case Intrinsic::mips_addv_d:
1089 return lowerMSABinaryIntr(Op, DAG, ISD::ADD);
Daniel Sanderse0187e52013-09-23 14:29:55 +00001090 case Intrinsic::mips_addvi_b:
1091 case Intrinsic::mips_addvi_h:
1092 case Intrinsic::mips_addvi_w:
1093 case Intrinsic::mips_addvi_d:
1094 return lowerMSABinaryImmIntr(Op, DAG, ISD::ADD,
1095 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders4e812c12013-09-23 12:57:42 +00001096 case Intrinsic::mips_and_v:
1097 return lowerMSABinaryIntr(Op, DAG, ISD::AND);
Daniel Sanders3c380d52013-08-28 12:14:50 +00001098 case Intrinsic::mips_bnz_b:
1099 case Intrinsic::mips_bnz_h:
1100 case Intrinsic::mips_bnz_w:
1101 case Intrinsic::mips_bnz_d:
1102 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_NONZERO);
1103 case Intrinsic::mips_bnz_v:
1104 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_NONZERO);
1105 case Intrinsic::mips_bz_b:
1106 case Intrinsic::mips_bz_h:
1107 case Intrinsic::mips_bz_w:
1108 case Intrinsic::mips_bz_d:
1109 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_ZERO);
1110 case Intrinsic::mips_bz_v:
1111 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_ZERO);
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +00001112 case Intrinsic::mips_copy_s_b:
1113 case Intrinsic::mips_copy_s_h:
1114 case Intrinsic::mips_copy_s_w:
1115 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1116 case Intrinsic::mips_copy_u_b:
1117 case Intrinsic::mips_copy_u_h:
1118 case Intrinsic::mips_copy_u_w:
1119 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sandersece929d2013-09-11 10:38:58 +00001120 case Intrinsic::mips_div_s_b:
1121 case Intrinsic::mips_div_s_h:
1122 case Intrinsic::mips_div_s_w:
1123 case Intrinsic::mips_div_s_d:
1124 return lowerMSABinaryIntr(Op, DAG, ISD::SDIV);
1125 case Intrinsic::mips_div_u_b:
1126 case Intrinsic::mips_div_u_h:
1127 case Intrinsic::mips_div_u_w:
1128 case Intrinsic::mips_div_u_d:
1129 return lowerMSABinaryIntr(Op, DAG, ISD::UDIV);
Daniel Sanders2ac12822013-09-11 10:51:30 +00001130 case Intrinsic::mips_fadd_w:
1131 case Intrinsic::mips_fadd_d:
1132 return lowerMSABinaryIntr(Op, DAG, ISD::FADD);
1133 case Intrinsic::mips_fdiv_w:
1134 case Intrinsic::mips_fdiv_d:
1135 return lowerMSABinaryIntr(Op, DAG, ISD::FDIV);
Daniel Sandersda521cc2013-09-23 12:02:46 +00001136 case Intrinsic::mips_fill_b:
1137 case Intrinsic::mips_fill_h:
1138 case Intrinsic::mips_fill_w:
1139 return lowerMSAUnaryIntr(Op, DAG, MipsISD::VSPLAT);
Daniel Sanders2ac12822013-09-11 10:51:30 +00001140 case Intrinsic::mips_flog2_w:
1141 case Intrinsic::mips_flog2_d:
1142 return lowerMSAUnaryIntr(Op, DAG, ISD::FLOG2);
1143 case Intrinsic::mips_fmul_w:
1144 case Intrinsic::mips_fmul_d:
1145 return lowerMSABinaryIntr(Op, DAG, ISD::FMUL);
1146 case Intrinsic::mips_frint_w:
1147 case Intrinsic::mips_frint_d:
1148 return lowerMSAUnaryIntr(Op, DAG, ISD::FRINT);
1149 case Intrinsic::mips_fsqrt_w:
1150 case Intrinsic::mips_fsqrt_d:
1151 return lowerMSAUnaryIntr(Op, DAG, ISD::FSQRT);
1152 case Intrinsic::mips_fsub_w:
1153 case Intrinsic::mips_fsub_d:
1154 return lowerMSABinaryIntr(Op, DAG, ISD::FSUB);
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +00001155 case Intrinsic::mips_insert_b:
1156 case Intrinsic::mips_insert_h:
1157 case Intrinsic::mips_insert_w:
1158 return lowerMSAInsertIntr(Op, DAG, ISD::INSERT_VECTOR_ELT);
Daniel Sandersda521cc2013-09-23 12:02:46 +00001159 case Intrinsic::mips_ldi_b:
1160 case Intrinsic::mips_ldi_h:
1161 case Intrinsic::mips_ldi_w:
1162 case Intrinsic::mips_ldi_d:
1163 return lowerMSAUnaryIntr(Op, DAG, MipsISD::VSPLAT);
Daniel Sandersf2eb1e42013-09-11 11:58:30 +00001164 case Intrinsic::mips_mulv_b:
1165 case Intrinsic::mips_mulv_h:
1166 case Intrinsic::mips_mulv_w:
1167 case Intrinsic::mips_mulv_d:
1168 return lowerMSABinaryIntr(Op, DAG, ISD::MUL);
1169 case Intrinsic::mips_nlzc_b:
1170 case Intrinsic::mips_nlzc_h:
1171 case Intrinsic::mips_nlzc_w:
1172 case Intrinsic::mips_nlzc_d:
1173 return lowerMSAUnaryIntr(Op, DAG, ISD::CTLZ);
Daniel Sanders915432c2013-09-23 13:22:24 +00001174 case Intrinsic::mips_nor_v: {
1175 SDValue Res = lowerMSABinaryIntr(Op, DAG, ISD::OR);
1176 return DAG.getNOT(SDLoc(Op), Res, Res->getValueType(0));
1177 }
Daniel Sanders4e812c12013-09-23 12:57:42 +00001178 case Intrinsic::mips_or_v:
1179 return lowerMSABinaryIntr(Op, DAG, ISD::OR);
Daniel Sandersa399d692013-09-23 13:40:21 +00001180 case Intrinsic::mips_pcnt_b:
1181 case Intrinsic::mips_pcnt_h:
1182 case Intrinsic::mips_pcnt_w:
1183 case Intrinsic::mips_pcnt_d:
1184 return lowerMSAUnaryIntr(Op, DAG, ISD::CTPOP);
Daniel Sandersf2eb1e42013-09-11 11:58:30 +00001185 case Intrinsic::mips_sll_b:
1186 case Intrinsic::mips_sll_h:
1187 case Intrinsic::mips_sll_w:
1188 case Intrinsic::mips_sll_d:
1189 return lowerMSABinaryIntr(Op, DAG, ISD::SHL);
Daniel Sanderscfb1e172013-09-24 10:28:18 +00001190 case Intrinsic::mips_slli_b:
1191 case Intrinsic::mips_slli_h:
1192 case Intrinsic::mips_slli_w:
1193 case Intrinsic::mips_slli_d:
1194 return lowerMSABinaryImmIntr(Op, DAG, ISD::SHL,
1195 lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersf2eb1e42013-09-11 11:58:30 +00001196 case Intrinsic::mips_sra_b:
1197 case Intrinsic::mips_sra_h:
1198 case Intrinsic::mips_sra_w:
1199 case Intrinsic::mips_sra_d:
1200 return lowerMSABinaryIntr(Op, DAG, ISD::SRA);
Daniel Sanderscfb1e172013-09-24 10:28:18 +00001201 case Intrinsic::mips_srai_b:
1202 case Intrinsic::mips_srai_h:
1203 case Intrinsic::mips_srai_w:
1204 case Intrinsic::mips_srai_d:
1205 return lowerMSABinaryImmIntr(Op, DAG, ISD::SRA,
1206 lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersf2eb1e42013-09-11 11:58:30 +00001207 case Intrinsic::mips_srl_b:
1208 case Intrinsic::mips_srl_h:
1209 case Intrinsic::mips_srl_w:
1210 case Intrinsic::mips_srl_d:
1211 return lowerMSABinaryIntr(Op, DAG, ISD::SRL);
Daniel Sanderscfb1e172013-09-24 10:28:18 +00001212 case Intrinsic::mips_srli_b:
1213 case Intrinsic::mips_srli_h:
1214 case Intrinsic::mips_srli_w:
1215 case Intrinsic::mips_srli_d:
1216 return lowerMSABinaryImmIntr(Op, DAG, ISD::SRL,
1217 lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersf2eb1e42013-09-11 11:58:30 +00001218 case Intrinsic::mips_subv_b:
1219 case Intrinsic::mips_subv_h:
1220 case Intrinsic::mips_subv_w:
1221 case Intrinsic::mips_subv_d:
1222 return lowerMSABinaryIntr(Op, DAG, ISD::SUB);
Daniel Sanderse0187e52013-09-23 14:29:55 +00001223 case Intrinsic::mips_subvi_b:
1224 case Intrinsic::mips_subvi_h:
1225 case Intrinsic::mips_subvi_w:
1226 case Intrinsic::mips_subvi_d:
1227 return lowerMSABinaryImmIntr(Op, DAG, ISD::SUB,
1228 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders4e812c12013-09-23 12:57:42 +00001229 case Intrinsic::mips_xor_v:
1230 return lowerMSABinaryIntr(Op, DAG, ISD::XOR);
Akira Hatanaka4e0980a2013-04-13 02:13:30 +00001231 }
1232}
1233
Daniel Sanders2fd3e672013-08-28 12:04:29 +00001234static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1235 SDLoc DL(Op);
1236 SDValue ChainIn = Op->getOperand(0);
1237 SDValue Address = Op->getOperand(2);
1238 SDValue Offset = Op->getOperand(3);
1239 EVT ResTy = Op->getValueType(0);
1240 EVT PtrTy = Address->getValueType(0);
1241
1242 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1243
1244 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
1245 false, false, 16);
1246}
1247
Akira Hatanaka4e0980a2013-04-13 02:13:30 +00001248SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
1249 SelectionDAG &DAG) const {
Daniel Sanders2fd3e672013-08-28 12:04:29 +00001250 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1251 switch (Intr) {
Akira Hatanaka4e0980a2013-04-13 02:13:30 +00001252 default:
1253 return SDValue();
1254 case Intrinsic::mips_extp:
1255 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
1256 case Intrinsic::mips_extpdp:
1257 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
1258 case Intrinsic::mips_extr_w:
1259 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
1260 case Intrinsic::mips_extr_r_w:
1261 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
1262 case Intrinsic::mips_extr_rs_w:
1263 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
1264 case Intrinsic::mips_extr_s_h:
1265 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
1266 case Intrinsic::mips_mthlip:
1267 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
1268 case Intrinsic::mips_mulsaq_s_w_ph:
1269 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
1270 case Intrinsic::mips_maq_s_w_phl:
1271 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
1272 case Intrinsic::mips_maq_s_w_phr:
1273 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
1274 case Intrinsic::mips_maq_sa_w_phl:
1275 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
1276 case Intrinsic::mips_maq_sa_w_phr:
1277 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
1278 case Intrinsic::mips_dpaq_s_w_ph:
1279 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
1280 case Intrinsic::mips_dpsq_s_w_ph:
1281 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
1282 case Intrinsic::mips_dpaq_sa_l_w:
1283 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
1284 case Intrinsic::mips_dpsq_sa_l_w:
1285 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
1286 case Intrinsic::mips_dpaqx_s_w_ph:
1287 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
1288 case Intrinsic::mips_dpaqx_sa_w_ph:
1289 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
1290 case Intrinsic::mips_dpsqx_s_w_ph:
1291 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
1292 case Intrinsic::mips_dpsqx_sa_w_ph:
1293 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanders2fd3e672013-08-28 12:04:29 +00001294 case Intrinsic::mips_ld_b:
1295 case Intrinsic::mips_ld_h:
1296 case Intrinsic::mips_ld_w:
1297 case Intrinsic::mips_ld_d:
1298 case Intrinsic::mips_ldx_b:
1299 case Intrinsic::mips_ldx_h:
1300 case Intrinsic::mips_ldx_w:
1301 case Intrinsic::mips_ldx_d:
1302 return lowerMSALoadIntr(Op, DAG, Intr);
1303 }
1304}
1305
1306static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1307 SDLoc DL(Op);
1308 SDValue ChainIn = Op->getOperand(0);
1309 SDValue Value = Op->getOperand(2);
1310 SDValue Address = Op->getOperand(3);
1311 SDValue Offset = Op->getOperand(4);
1312 EVT PtrTy = Address->getValueType(0);
1313
1314 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1315
1316 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
1317 false, 16);
1318}
1319
1320SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
1321 SelectionDAG &DAG) const {
1322 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1323 switch (Intr) {
1324 default:
1325 return SDValue();
1326 case Intrinsic::mips_st_b:
1327 case Intrinsic::mips_st_h:
1328 case Intrinsic::mips_st_w:
1329 case Intrinsic::mips_st_d:
1330 case Intrinsic::mips_stx_b:
1331 case Intrinsic::mips_stx_h:
1332 case Intrinsic::mips_stx_w:
1333 case Intrinsic::mips_stx_d:
Daniel Sanders3c380d52013-08-28 12:14:50 +00001334 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanaka4e0980a2013-04-13 02:13:30 +00001335 }
1336}
1337
Daniel Sandersda521cc2013-09-23 12:02:46 +00001338/// \brief Check if the given BuildVectorSDNode is a splat.
1339/// This method currently relies on DAG nodes being reused when equivalent,
1340/// so it's possible for this to return false even when isConstantSplat returns
1341/// true.
1342static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sandersda521cc2013-09-23 12:02:46 +00001343 unsigned int nOps = N->getNumOperands();
1344 assert(nOps > 1 && "isSplat has 0 or 1 sized build vector");
1345
1346 SDValue Operand0 = N->getOperand(0);
1347
1348 for (unsigned int i = 1; i < nOps; ++i) {
1349 if (N->getOperand(i) != Operand0)
1350 return false;
1351 }
1352
1353 return true;
1354}
1355
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +00001356// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
1357//
1358// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
1359// choose to sign-extend but we could have equally chosen zero-extend. The
1360// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
1361// result into this node later (possibly changing it to a zero-extend in the
1362// process).
1363SDValue MipsSETargetLowering::
1364lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
1365 SDLoc DL(Op);
1366 EVT ResTy = Op->getValueType(0);
1367 SDValue Op0 = Op->getOperand(0);
1368 SDValue Op1 = Op->getOperand(1);
1369 EVT EltTy = Op0->getValueType(0).getVectorElementType();
1370 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
1371 DAG.getValueType(EltTy));
1372}
1373
Daniel Sandersda521cc2013-09-23 12:02:46 +00001374// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
1375// backend.
1376//
1377// Lowers according to the following rules:
1378// - Vectors of 128-bits may be legal subject to the other rules. Other sizes
1379// are not legal.
1380// - Non-constant splats are legal and are lowered to MipsISD::VSPLAT.
1381// - Constant splats with an element size of 32-bits or less are legal and are
1382// lowered to MipsISD::VSPLAT.
1383// - Constant splats with an element size of 64-bits but whose value would fit
1384// within a 10 bit immediate are legal and are lowered to MipsISD::VSPLATD.
1385// - All other ISD::BUILD_VECTORS are not legal
1386SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
1387 SelectionDAG &DAG) const {
1388 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
1389 EVT ResTy = Op->getValueType(0);
1390 SDLoc DL(Op);
1391 APInt SplatValue, SplatUndef;
1392 unsigned SplatBitSize;
1393 bool HasAnyUndefs;
1394
1395 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
1396 return SDValue();
1397
1398 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1399 HasAnyUndefs, 8,
1400 !Subtarget->isLittle())) {
1401 SDValue Result;
1402 EVT TmpVecTy;
1403 EVT ConstTy = MVT::i32;
1404 unsigned SplatOp = MipsISD::VSPLAT;
1405
1406 switch (SplatBitSize) {
1407 default:
1408 return SDValue();
1409 case 64:
1410 TmpVecTy = MVT::v2i64;
1411
1412 // i64 is an illegal type on Mips32, but if it the constant fits into a
1413 // signed 10-bit value then we can still handle it using VSPLATD and an
1414 // i32 constant
1415 if (HasMips64)
1416 ConstTy = MVT::i64;
1417 else if (isInt<10>(SplatValue.getSExtValue())) {
1418 SplatValue = SplatValue.trunc(32);
1419 SplatOp = MipsISD::VSPLATD;
1420 } else
1421 return SDValue();
1422 break;
1423 case 32:
1424 TmpVecTy = MVT::v4i32;
1425 break;
1426 case 16:
1427 TmpVecTy = MVT::v8i16;
1428 SplatValue = SplatValue.sext(32);
1429 break;
1430 case 8:
1431 TmpVecTy = MVT::v16i8;
1432 SplatValue = SplatValue.sext(32);
1433 break;
1434 }
1435
1436 Result = DAG.getNode(SplatOp, DL, TmpVecTy,
1437 DAG.getConstant(SplatValue, ConstTy));
1438 if (ResTy != Result.getValueType())
1439 Result = DAG.getNode(ISD::BITCAST, DL, ResTy, Result);
1440
1441 return Result;
1442 }
1443 else if (isSplatVector(Node))
1444 return DAG.getNode(MipsISD::VSPLAT, DL, ResTy, Op->getOperand(0));
1445
1446 return SDValue();
1447}
1448
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00001449MachineBasicBlock * MipsSETargetLowering::
1450emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
1451 // $bb:
1452 // bposge32_pseudo $vr0
1453 // =>
1454 // $bb:
1455 // bposge32 $tbb
1456 // $fbb:
1457 // li $vr2, 0
1458 // b $sink
1459 // $tbb:
1460 // li $vr1, 1
1461 // $sink:
1462 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
1463
1464 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1465 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka18587862013-08-06 23:08:38 +00001466 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00001467 DebugLoc DL = MI->getDebugLoc();
1468 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1469 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1470 MachineFunction *F = BB->getParent();
1471 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1472 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1473 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1474 F->insert(It, FBB);
1475 F->insert(It, TBB);
1476 F->insert(It, Sink);
1477
1478 // Transfer the remainder of BB and its successor edges to Sink.
1479 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1480 BB->end());
1481 Sink->transferSuccessorsAndUpdatePHIs(BB);
1482
1483 // Add successors.
1484 BB->addSuccessor(FBB);
1485 BB->addSuccessor(TBB);
1486 FBB->addSuccessor(Sink);
1487 TBB->addSuccessor(Sink);
1488
1489 // Insert the real bposge32 instruction to $BB.
1490 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1491
1492 // Fill $FBB.
1493 unsigned VR2 = RegInfo.createVirtualRegister(RC);
1494 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1495 .addReg(Mips::ZERO).addImm(0);
1496 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1497
1498 // Fill $TBB.
1499 unsigned VR1 = RegInfo.createVirtualRegister(RC);
1500 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1501 .addReg(Mips::ZERO).addImm(1);
1502
1503 // Insert phi function to $Sink.
1504 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1505 MI->getOperand(0).getReg())
1506 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1507
1508 MI->eraseFromParent(); // The pseudo instruction is gone now.
1509 return Sink;
1510}
Daniel Sanders3c380d52013-08-28 12:14:50 +00001511
1512MachineBasicBlock * MipsSETargetLowering::
1513emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
1514 unsigned BranchOp) const{
1515 // $bb:
1516 // vany_nonzero $rd, $ws
1517 // =>
1518 // $bb:
1519 // bnz.b $ws, $tbb
1520 // b $fbb
1521 // $fbb:
1522 // li $rd1, 0
1523 // b $sink
1524 // $tbb:
1525 // li $rd2, 1
1526 // $sink:
1527 // $rd = phi($rd1, $fbb, $rd2, $tbb)
1528
1529 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1530 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1531 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1532 DebugLoc DL = MI->getDebugLoc();
1533 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1534 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1535 MachineFunction *F = BB->getParent();
1536 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1537 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1538 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1539 F->insert(It, FBB);
1540 F->insert(It, TBB);
1541 F->insert(It, Sink);
1542
1543 // Transfer the remainder of BB and its successor edges to Sink.
1544 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1545 BB->end());
1546 Sink->transferSuccessorsAndUpdatePHIs(BB);
1547
1548 // Add successors.
1549 BB->addSuccessor(FBB);
1550 BB->addSuccessor(TBB);
1551 FBB->addSuccessor(Sink);
1552 TBB->addSuccessor(Sink);
1553
1554 // Insert the real bnz.b instruction to $BB.
1555 BuildMI(BB, DL, TII->get(BranchOp))
1556 .addReg(MI->getOperand(1).getReg())
1557 .addMBB(TBB);
1558
1559 // Fill $FBB.
1560 unsigned RD1 = RegInfo.createVirtualRegister(RC);
1561 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
1562 .addReg(Mips::ZERO).addImm(0);
1563 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1564
1565 // Fill $TBB.
1566 unsigned RD2 = RegInfo.createVirtualRegister(RC);
1567 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
1568 .addReg(Mips::ZERO).addImm(1);
1569
1570 // Insert phi function to $Sink.
1571 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1572 MI->getOperand(0).getReg())
1573 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
1574
1575 MI->eraseFromParent(); // The pseudo instruction is gone now.
1576 return Sink;
1577}