blob: 732b68b5a64dcf4d0e5dbe1b90446980f5e8258b [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 Sanders3c380d52013-08-28 12:14:50 +0000983static SDValue lowerMSABranchIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
984 SDLoc DL(Op);
985 SDValue Value = Op->getOperand(1);
986 EVT ResTy = Op->getValueType(0);
987
988 SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
989
990 return Result;
991}
992
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000993// Lower an MSA copy intrinsic into the specified SelectionDAG node
994static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
995 SDLoc DL(Op);
996 SDValue Vec = Op->getOperand(1);
997 SDValue Idx = Op->getOperand(2);
998 EVT ResTy = Op->getValueType(0);
999 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1000
1001 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1002 DAG.getValueType(EltTy));
1003
1004 return Result;
1005}
1006
1007// Lower an MSA insert intrinsic into the specified SelectionDAG node
1008static SDValue lowerMSAInsertIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1009 SDLoc DL(Op);
1010 SDValue Op0 = Op->getOperand(1);
1011 SDValue Op1 = Op->getOperand(2);
1012 SDValue Op2 = Op->getOperand(3);
1013 EVT ResTy = Op->getValueType(0);
1014
1015 SDValue Result = DAG.getNode(Opc, DL, ResTy, Op0, Op2, Op1);
1016
1017 return Result;
1018}
1019
Daniel Sanders2ac12822013-09-11 10:51:30 +00001020static SDValue lowerMSAUnaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1021 SDLoc DL(Op);
1022 SDValue Value = Op->getOperand(1);
1023 EVT ResTy = Op->getValueType(0);
1024
1025 SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
1026
1027 return Result;
1028}
1029
Akira Hatanaka4e0980a2013-04-13 02:13:30 +00001030SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1031 SelectionDAG &DAG) const {
1032 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1033 default:
1034 return SDValue();
1035 case Intrinsic::mips_shilo:
1036 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1037 case Intrinsic::mips_dpau_h_qbl:
1038 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1039 case Intrinsic::mips_dpau_h_qbr:
1040 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1041 case Intrinsic::mips_dpsu_h_qbl:
1042 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1043 case Intrinsic::mips_dpsu_h_qbr:
1044 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1045 case Intrinsic::mips_dpa_w_ph:
1046 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1047 case Intrinsic::mips_dps_w_ph:
1048 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1049 case Intrinsic::mips_dpax_w_ph:
1050 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1051 case Intrinsic::mips_dpsx_w_ph:
1052 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1053 case Intrinsic::mips_mulsa_w_ph:
1054 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1055 case Intrinsic::mips_mult:
1056 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1057 case Intrinsic::mips_multu:
1058 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1059 case Intrinsic::mips_madd:
1060 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1061 case Intrinsic::mips_maddu:
1062 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1063 case Intrinsic::mips_msub:
1064 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1065 case Intrinsic::mips_msubu:
1066 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sanders68831cb2013-09-11 10:28:16 +00001067 case Intrinsic::mips_addv_b:
1068 case Intrinsic::mips_addv_h:
1069 case Intrinsic::mips_addv_w:
1070 case Intrinsic::mips_addv_d:
1071 return lowerMSABinaryIntr(Op, DAG, ISD::ADD);
Daniel Sanders4e812c12013-09-23 12:57:42 +00001072 case Intrinsic::mips_and_v:
1073 return lowerMSABinaryIntr(Op, DAG, ISD::AND);
Daniel Sanders3c380d52013-08-28 12:14:50 +00001074 case Intrinsic::mips_bnz_b:
1075 case Intrinsic::mips_bnz_h:
1076 case Intrinsic::mips_bnz_w:
1077 case Intrinsic::mips_bnz_d:
1078 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_NONZERO);
1079 case Intrinsic::mips_bnz_v:
1080 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_NONZERO);
1081 case Intrinsic::mips_bz_b:
1082 case Intrinsic::mips_bz_h:
1083 case Intrinsic::mips_bz_w:
1084 case Intrinsic::mips_bz_d:
1085 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_ZERO);
1086 case Intrinsic::mips_bz_v:
1087 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_ZERO);
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +00001088 case Intrinsic::mips_copy_s_b:
1089 case Intrinsic::mips_copy_s_h:
1090 case Intrinsic::mips_copy_s_w:
1091 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1092 case Intrinsic::mips_copy_u_b:
1093 case Intrinsic::mips_copy_u_h:
1094 case Intrinsic::mips_copy_u_w:
1095 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sandersece929d2013-09-11 10:38:58 +00001096 case Intrinsic::mips_div_s_b:
1097 case Intrinsic::mips_div_s_h:
1098 case Intrinsic::mips_div_s_w:
1099 case Intrinsic::mips_div_s_d:
1100 return lowerMSABinaryIntr(Op, DAG, ISD::SDIV);
1101 case Intrinsic::mips_div_u_b:
1102 case Intrinsic::mips_div_u_h:
1103 case Intrinsic::mips_div_u_w:
1104 case Intrinsic::mips_div_u_d:
1105 return lowerMSABinaryIntr(Op, DAG, ISD::UDIV);
Daniel Sanders2ac12822013-09-11 10:51:30 +00001106 case Intrinsic::mips_fadd_w:
1107 case Intrinsic::mips_fadd_d:
1108 return lowerMSABinaryIntr(Op, DAG, ISD::FADD);
1109 case Intrinsic::mips_fdiv_w:
1110 case Intrinsic::mips_fdiv_d:
1111 return lowerMSABinaryIntr(Op, DAG, ISD::FDIV);
Daniel Sandersda521cc2013-09-23 12:02:46 +00001112 case Intrinsic::mips_fill_b:
1113 case Intrinsic::mips_fill_h:
1114 case Intrinsic::mips_fill_w:
1115 return lowerMSAUnaryIntr(Op, DAG, MipsISD::VSPLAT);
Daniel Sanders2ac12822013-09-11 10:51:30 +00001116 case Intrinsic::mips_flog2_w:
1117 case Intrinsic::mips_flog2_d:
1118 return lowerMSAUnaryIntr(Op, DAG, ISD::FLOG2);
1119 case Intrinsic::mips_fmul_w:
1120 case Intrinsic::mips_fmul_d:
1121 return lowerMSABinaryIntr(Op, DAG, ISD::FMUL);
1122 case Intrinsic::mips_frint_w:
1123 case Intrinsic::mips_frint_d:
1124 return lowerMSAUnaryIntr(Op, DAG, ISD::FRINT);
1125 case Intrinsic::mips_fsqrt_w:
1126 case Intrinsic::mips_fsqrt_d:
1127 return lowerMSAUnaryIntr(Op, DAG, ISD::FSQRT);
1128 case Intrinsic::mips_fsub_w:
1129 case Intrinsic::mips_fsub_d:
1130 return lowerMSABinaryIntr(Op, DAG, ISD::FSUB);
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +00001131 case Intrinsic::mips_insert_b:
1132 case Intrinsic::mips_insert_h:
1133 case Intrinsic::mips_insert_w:
1134 return lowerMSAInsertIntr(Op, DAG, ISD::INSERT_VECTOR_ELT);
Daniel Sandersda521cc2013-09-23 12:02:46 +00001135 case Intrinsic::mips_ldi_b:
1136 case Intrinsic::mips_ldi_h:
1137 case Intrinsic::mips_ldi_w:
1138 case Intrinsic::mips_ldi_d:
1139 return lowerMSAUnaryIntr(Op, DAG, MipsISD::VSPLAT);
Daniel Sandersf2eb1e42013-09-11 11:58:30 +00001140 case Intrinsic::mips_mulv_b:
1141 case Intrinsic::mips_mulv_h:
1142 case Intrinsic::mips_mulv_w:
1143 case Intrinsic::mips_mulv_d:
1144 return lowerMSABinaryIntr(Op, DAG, ISD::MUL);
1145 case Intrinsic::mips_nlzc_b:
1146 case Intrinsic::mips_nlzc_h:
1147 case Intrinsic::mips_nlzc_w:
1148 case Intrinsic::mips_nlzc_d:
1149 return lowerMSAUnaryIntr(Op, DAG, ISD::CTLZ);
Daniel Sanders915432c2013-09-23 13:22:24 +00001150 case Intrinsic::mips_nor_v: {
1151 SDValue Res = lowerMSABinaryIntr(Op, DAG, ISD::OR);
1152 return DAG.getNOT(SDLoc(Op), Res, Res->getValueType(0));
1153 }
Daniel Sanders4e812c12013-09-23 12:57:42 +00001154 case Intrinsic::mips_or_v:
1155 return lowerMSABinaryIntr(Op, DAG, ISD::OR);
Daniel Sandersa399d692013-09-23 13:40:21 +00001156 case Intrinsic::mips_pcnt_b:
1157 case Intrinsic::mips_pcnt_h:
1158 case Intrinsic::mips_pcnt_w:
1159 case Intrinsic::mips_pcnt_d:
1160 return lowerMSAUnaryIntr(Op, DAG, ISD::CTPOP);
Daniel Sandersf2eb1e42013-09-11 11:58:30 +00001161 case Intrinsic::mips_sll_b:
1162 case Intrinsic::mips_sll_h:
1163 case Intrinsic::mips_sll_w:
1164 case Intrinsic::mips_sll_d:
1165 return lowerMSABinaryIntr(Op, DAG, ISD::SHL);
1166 case Intrinsic::mips_sra_b:
1167 case Intrinsic::mips_sra_h:
1168 case Intrinsic::mips_sra_w:
1169 case Intrinsic::mips_sra_d:
1170 return lowerMSABinaryIntr(Op, DAG, ISD::SRA);
1171 case Intrinsic::mips_srl_b:
1172 case Intrinsic::mips_srl_h:
1173 case Intrinsic::mips_srl_w:
1174 case Intrinsic::mips_srl_d:
1175 return lowerMSABinaryIntr(Op, DAG, ISD::SRL);
1176 case Intrinsic::mips_subv_b:
1177 case Intrinsic::mips_subv_h:
1178 case Intrinsic::mips_subv_w:
1179 case Intrinsic::mips_subv_d:
1180 return lowerMSABinaryIntr(Op, DAG, ISD::SUB);
Daniel Sanders4e812c12013-09-23 12:57:42 +00001181 case Intrinsic::mips_xor_v:
1182 return lowerMSABinaryIntr(Op, DAG, ISD::XOR);
Akira Hatanaka4e0980a2013-04-13 02:13:30 +00001183 }
1184}
1185
Daniel Sanders2fd3e672013-08-28 12:04:29 +00001186static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1187 SDLoc DL(Op);
1188 SDValue ChainIn = Op->getOperand(0);
1189 SDValue Address = Op->getOperand(2);
1190 SDValue Offset = Op->getOperand(3);
1191 EVT ResTy = Op->getValueType(0);
1192 EVT PtrTy = Address->getValueType(0);
1193
1194 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1195
1196 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
1197 false, false, 16);
1198}
1199
Akira Hatanaka4e0980a2013-04-13 02:13:30 +00001200SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
1201 SelectionDAG &DAG) const {
Daniel Sanders2fd3e672013-08-28 12:04:29 +00001202 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1203 switch (Intr) {
Akira Hatanaka4e0980a2013-04-13 02:13:30 +00001204 default:
1205 return SDValue();
1206 case Intrinsic::mips_extp:
1207 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
1208 case Intrinsic::mips_extpdp:
1209 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
1210 case Intrinsic::mips_extr_w:
1211 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
1212 case Intrinsic::mips_extr_r_w:
1213 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
1214 case Intrinsic::mips_extr_rs_w:
1215 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
1216 case Intrinsic::mips_extr_s_h:
1217 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
1218 case Intrinsic::mips_mthlip:
1219 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
1220 case Intrinsic::mips_mulsaq_s_w_ph:
1221 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
1222 case Intrinsic::mips_maq_s_w_phl:
1223 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
1224 case Intrinsic::mips_maq_s_w_phr:
1225 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
1226 case Intrinsic::mips_maq_sa_w_phl:
1227 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
1228 case Intrinsic::mips_maq_sa_w_phr:
1229 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
1230 case Intrinsic::mips_dpaq_s_w_ph:
1231 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
1232 case Intrinsic::mips_dpsq_s_w_ph:
1233 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
1234 case Intrinsic::mips_dpaq_sa_l_w:
1235 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
1236 case Intrinsic::mips_dpsq_sa_l_w:
1237 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
1238 case Intrinsic::mips_dpaqx_s_w_ph:
1239 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
1240 case Intrinsic::mips_dpaqx_sa_w_ph:
1241 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
1242 case Intrinsic::mips_dpsqx_s_w_ph:
1243 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
1244 case Intrinsic::mips_dpsqx_sa_w_ph:
1245 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanders2fd3e672013-08-28 12:04:29 +00001246 case Intrinsic::mips_ld_b:
1247 case Intrinsic::mips_ld_h:
1248 case Intrinsic::mips_ld_w:
1249 case Intrinsic::mips_ld_d:
1250 case Intrinsic::mips_ldx_b:
1251 case Intrinsic::mips_ldx_h:
1252 case Intrinsic::mips_ldx_w:
1253 case Intrinsic::mips_ldx_d:
1254 return lowerMSALoadIntr(Op, DAG, Intr);
1255 }
1256}
1257
1258static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1259 SDLoc DL(Op);
1260 SDValue ChainIn = Op->getOperand(0);
1261 SDValue Value = Op->getOperand(2);
1262 SDValue Address = Op->getOperand(3);
1263 SDValue Offset = Op->getOperand(4);
1264 EVT PtrTy = Address->getValueType(0);
1265
1266 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1267
1268 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
1269 false, 16);
1270}
1271
1272SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
1273 SelectionDAG &DAG) const {
1274 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1275 switch (Intr) {
1276 default:
1277 return SDValue();
1278 case Intrinsic::mips_st_b:
1279 case Intrinsic::mips_st_h:
1280 case Intrinsic::mips_st_w:
1281 case Intrinsic::mips_st_d:
1282 case Intrinsic::mips_stx_b:
1283 case Intrinsic::mips_stx_h:
1284 case Intrinsic::mips_stx_w:
1285 case Intrinsic::mips_stx_d:
Daniel Sanders3c380d52013-08-28 12:14:50 +00001286 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanaka4e0980a2013-04-13 02:13:30 +00001287 }
1288}
1289
Daniel Sandersda521cc2013-09-23 12:02:46 +00001290/// \brief Check if the given BuildVectorSDNode is a splat.
1291/// This method currently relies on DAG nodes being reused when equivalent,
1292/// so it's possible for this to return false even when isConstantSplat returns
1293/// true.
1294static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sandersda521cc2013-09-23 12:02:46 +00001295 unsigned int nOps = N->getNumOperands();
1296 assert(nOps > 1 && "isSplat has 0 or 1 sized build vector");
1297
1298 SDValue Operand0 = N->getOperand(0);
1299
1300 for (unsigned int i = 1; i < nOps; ++i) {
1301 if (N->getOperand(i) != Operand0)
1302 return false;
1303 }
1304
1305 return true;
1306}
1307
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +00001308// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
1309//
1310// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
1311// choose to sign-extend but we could have equally chosen zero-extend. The
1312// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
1313// result into this node later (possibly changing it to a zero-extend in the
1314// process).
1315SDValue MipsSETargetLowering::
1316lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
1317 SDLoc DL(Op);
1318 EVT ResTy = Op->getValueType(0);
1319 SDValue Op0 = Op->getOperand(0);
1320 SDValue Op1 = Op->getOperand(1);
1321 EVT EltTy = Op0->getValueType(0).getVectorElementType();
1322 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
1323 DAG.getValueType(EltTy));
1324}
1325
Daniel Sandersda521cc2013-09-23 12:02:46 +00001326// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
1327// backend.
1328//
1329// Lowers according to the following rules:
1330// - Vectors of 128-bits may be legal subject to the other rules. Other sizes
1331// are not legal.
1332// - Non-constant splats are legal and are lowered to MipsISD::VSPLAT.
1333// - Constant splats with an element size of 32-bits or less are legal and are
1334// lowered to MipsISD::VSPLAT.
1335// - Constant splats with an element size of 64-bits but whose value would fit
1336// within a 10 bit immediate are legal and are lowered to MipsISD::VSPLATD.
1337// - All other ISD::BUILD_VECTORS are not legal
1338SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
1339 SelectionDAG &DAG) const {
1340 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
1341 EVT ResTy = Op->getValueType(0);
1342 SDLoc DL(Op);
1343 APInt SplatValue, SplatUndef;
1344 unsigned SplatBitSize;
1345 bool HasAnyUndefs;
1346
1347 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
1348 return SDValue();
1349
1350 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1351 HasAnyUndefs, 8,
1352 !Subtarget->isLittle())) {
1353 SDValue Result;
1354 EVT TmpVecTy;
1355 EVT ConstTy = MVT::i32;
1356 unsigned SplatOp = MipsISD::VSPLAT;
1357
1358 switch (SplatBitSize) {
1359 default:
1360 return SDValue();
1361 case 64:
1362 TmpVecTy = MVT::v2i64;
1363
1364 // i64 is an illegal type on Mips32, but if it the constant fits into a
1365 // signed 10-bit value then we can still handle it using VSPLATD and an
1366 // i32 constant
1367 if (HasMips64)
1368 ConstTy = MVT::i64;
1369 else if (isInt<10>(SplatValue.getSExtValue())) {
1370 SplatValue = SplatValue.trunc(32);
1371 SplatOp = MipsISD::VSPLATD;
1372 } else
1373 return SDValue();
1374 break;
1375 case 32:
1376 TmpVecTy = MVT::v4i32;
1377 break;
1378 case 16:
1379 TmpVecTy = MVT::v8i16;
1380 SplatValue = SplatValue.sext(32);
1381 break;
1382 case 8:
1383 TmpVecTy = MVT::v16i8;
1384 SplatValue = SplatValue.sext(32);
1385 break;
1386 }
1387
1388 Result = DAG.getNode(SplatOp, DL, TmpVecTy,
1389 DAG.getConstant(SplatValue, ConstTy));
1390 if (ResTy != Result.getValueType())
1391 Result = DAG.getNode(ISD::BITCAST, DL, ResTy, Result);
1392
1393 return Result;
1394 }
1395 else if (isSplatVector(Node))
1396 return DAG.getNode(MipsISD::VSPLAT, DL, ResTy, Op->getOperand(0));
1397
1398 return SDValue();
1399}
1400
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00001401MachineBasicBlock * MipsSETargetLowering::
1402emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
1403 // $bb:
1404 // bposge32_pseudo $vr0
1405 // =>
1406 // $bb:
1407 // bposge32 $tbb
1408 // $fbb:
1409 // li $vr2, 0
1410 // b $sink
1411 // $tbb:
1412 // li $vr1, 1
1413 // $sink:
1414 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
1415
1416 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1417 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka18587862013-08-06 23:08:38 +00001418 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00001419 DebugLoc DL = MI->getDebugLoc();
1420 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1421 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1422 MachineFunction *F = BB->getParent();
1423 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1424 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1425 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1426 F->insert(It, FBB);
1427 F->insert(It, TBB);
1428 F->insert(It, Sink);
1429
1430 // Transfer the remainder of BB and its successor edges to Sink.
1431 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1432 BB->end());
1433 Sink->transferSuccessorsAndUpdatePHIs(BB);
1434
1435 // Add successors.
1436 BB->addSuccessor(FBB);
1437 BB->addSuccessor(TBB);
1438 FBB->addSuccessor(Sink);
1439 TBB->addSuccessor(Sink);
1440
1441 // Insert the real bposge32 instruction to $BB.
1442 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1443
1444 // Fill $FBB.
1445 unsigned VR2 = RegInfo.createVirtualRegister(RC);
1446 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1447 .addReg(Mips::ZERO).addImm(0);
1448 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1449
1450 // Fill $TBB.
1451 unsigned VR1 = RegInfo.createVirtualRegister(RC);
1452 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1453 .addReg(Mips::ZERO).addImm(1);
1454
1455 // Insert phi function to $Sink.
1456 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1457 MI->getOperand(0).getReg())
1458 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1459
1460 MI->eraseFromParent(); // The pseudo instruction is gone now.
1461 return Sink;
1462}
Daniel Sanders3c380d52013-08-28 12:14:50 +00001463
1464MachineBasicBlock * MipsSETargetLowering::
1465emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
1466 unsigned BranchOp) const{
1467 // $bb:
1468 // vany_nonzero $rd, $ws
1469 // =>
1470 // $bb:
1471 // bnz.b $ws, $tbb
1472 // b $fbb
1473 // $fbb:
1474 // li $rd1, 0
1475 // b $sink
1476 // $tbb:
1477 // li $rd2, 1
1478 // $sink:
1479 // $rd = phi($rd1, $fbb, $rd2, $tbb)
1480
1481 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1482 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1483 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1484 DebugLoc DL = MI->getDebugLoc();
1485 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1486 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1487 MachineFunction *F = BB->getParent();
1488 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1489 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1490 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1491 F->insert(It, FBB);
1492 F->insert(It, TBB);
1493 F->insert(It, Sink);
1494
1495 // Transfer the remainder of BB and its successor edges to Sink.
1496 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1497 BB->end());
1498 Sink->transferSuccessorsAndUpdatePHIs(BB);
1499
1500 // Add successors.
1501 BB->addSuccessor(FBB);
1502 BB->addSuccessor(TBB);
1503 FBB->addSuccessor(Sink);
1504 TBB->addSuccessor(Sink);
1505
1506 // Insert the real bnz.b instruction to $BB.
1507 BuildMI(BB, DL, TII->get(BranchOp))
1508 .addReg(MI->getOperand(1).getReg())
1509 .addMBB(TBB);
1510
1511 // Fill $FBB.
1512 unsigned RD1 = RegInfo.createVirtualRegister(RC);
1513 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
1514 .addReg(Mips::ZERO).addImm(0);
1515 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1516
1517 // Fill $TBB.
1518 unsigned RD2 = RegInfo.createVirtualRegister(RC);
1519 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
1520 .addReg(Mips::ZERO).addImm(1);
1521
1522 // Insert phi function to $Sink.
1523 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1524 MI->getOperand(0).getReg())
1525 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
1526
1527 MI->eraseFromParent(); // The pseudo instruction is gone now.
1528 return Sink;
1529}