1 //===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
2 //
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 "MipsMachineFunction.h"
15 #include "MipsRegisterInfo.h"
16 #include "MipsTargetMachine.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24
25 using namespace llvm;
26
27 #define DEBUG_TYPE "mips-isel"
28
29 static cl::opt<bool>
30 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
31 cl::desc("MIPS: Enable tail calls."), cl::init(false));
32
33 static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
34 cl::desc("Expand double precision loads and "
35 "stores to their single precision "
36 "counterparts"));
37
MipsSETargetLowering(const MipsTargetMachine & TM,const MipsSubtarget & STI)38 MipsSETargetLowering::MipsSETargetLowering(const MipsTargetMachine &TM,
39 const MipsSubtarget &STI)
40 : MipsTargetLowering(TM, STI) {
41 // Set up the register classes
42 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
43
44 if (Subtarget.isGP64bit())
45 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
46
47 if (Subtarget.hasDSP() || Subtarget.hasMSA()) {
48 // Expand all truncating stores and extending loads.
49 for (MVT VT0 : MVT::vector_valuetypes()) {
50 for (MVT VT1 : MVT::vector_valuetypes()) {
51 setTruncStoreAction(VT0, VT1, Expand);
52 setLoadExtAction(ISD::SEXTLOAD, VT0, VT1, Expand);
53 setLoadExtAction(ISD::ZEXTLOAD, VT0, VT1, Expand);
54 setLoadExtAction(ISD::EXTLOAD, VT0, VT1, Expand);
55 }
56 }
57 }
58
59 if (Subtarget.hasDSP()) {
60 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
61
62 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
63 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
64
65 // Expand all builtin opcodes.
66 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
67 setOperationAction(Opc, VecTys[i], Expand);
68
69 setOperationAction(ISD::ADD, VecTys[i], Legal);
70 setOperationAction(ISD::SUB, VecTys[i], Legal);
71 setOperationAction(ISD::LOAD, VecTys[i], Legal);
72 setOperationAction(ISD::STORE, VecTys[i], Legal);
73 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
74 }
75
76 setTargetDAGCombine(ISD::SHL);
77 setTargetDAGCombine(ISD::SRA);
78 setTargetDAGCombine(ISD::SRL);
79 setTargetDAGCombine(ISD::SETCC);
80 setTargetDAGCombine(ISD::VSELECT);
81 }
82
83 if (Subtarget.hasDSPR2())
84 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
85
86 if (Subtarget.hasMSA()) {
87 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
88 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
89 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
90 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
91 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
92 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
93 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
94
95 setTargetDAGCombine(ISD::AND);
96 setTargetDAGCombine(ISD::OR);
97 setTargetDAGCombine(ISD::SRA);
98 setTargetDAGCombine(ISD::VSELECT);
99 setTargetDAGCombine(ISD::XOR);
100 }
101
102 if (!Subtarget.useSoftFloat()) {
103 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
104
105 // When dealing with single precision only, use libcalls
106 if (!Subtarget.isSingleFloat()) {
107 if (Subtarget.isFP64bit())
108 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
109 else
110 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
111 }
112 }
113
114 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
115 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
116 setOperationAction(ISD::MULHS, MVT::i32, Custom);
117 setOperationAction(ISD::MULHU, MVT::i32, Custom);
118
119 if (Subtarget.hasCnMips())
120 setOperationAction(ISD::MUL, MVT::i64, Legal);
121 else if (Subtarget.isGP64bit())
122 setOperationAction(ISD::MUL, MVT::i64, Custom);
123
124 if (Subtarget.isGP64bit()) {
125 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Custom);
126 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Custom);
127 setOperationAction(ISD::MULHS, MVT::i64, Custom);
128 setOperationAction(ISD::MULHU, MVT::i64, Custom);
129 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
130 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
131 }
132
133 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
134 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
135
136 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
137 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
138 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
139 setOperationAction(ISD::LOAD, MVT::i32, Custom);
140 setOperationAction(ISD::STORE, MVT::i32, Custom);
141
142 setTargetDAGCombine(ISD::ADDE);
143 setTargetDAGCombine(ISD::SUBE);
144 setTargetDAGCombine(ISD::MUL);
145
146 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
147 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
148 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
149
150 if (NoDPLoadStore) {
151 setOperationAction(ISD::LOAD, MVT::f64, Custom);
152 setOperationAction(ISD::STORE, MVT::f64, Custom);
153 }
154
155 if (Subtarget.hasMips32r6()) {
156 // MIPS32r6 replaces the accumulator-based multiplies with a three register
157 // instruction
158 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
159 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
160 setOperationAction(ISD::MUL, MVT::i32, Legal);
161 setOperationAction(ISD::MULHS, MVT::i32, Legal);
162 setOperationAction(ISD::MULHU, MVT::i32, Legal);
163
164 // MIPS32r6 replaces the accumulator-based division/remainder with separate
165 // three register division and remainder instructions.
166 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
167 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
168 setOperationAction(ISD::SDIV, MVT::i32, Legal);
169 setOperationAction(ISD::UDIV, MVT::i32, Legal);
170 setOperationAction(ISD::SREM, MVT::i32, Legal);
171 setOperationAction(ISD::UREM, MVT::i32, Legal);
172
173 // MIPS32r6 replaces conditional moves with an equivalent that removes the
174 // need for three GPR read ports.
175 setOperationAction(ISD::SETCC, MVT::i32, Legal);
176 setOperationAction(ISD::SELECT, MVT::i32, Legal);
177 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
178
179 setOperationAction(ISD::SETCC, MVT::f32, Legal);
180 setOperationAction(ISD::SELECT, MVT::f32, Legal);
181 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
182
183 assert(Subtarget.isFP64bit() && "FR=1 is required for MIPS32r6");
184 setOperationAction(ISD::SETCC, MVT::f64, Legal);
185 setOperationAction(ISD::SELECT, MVT::f64, Legal);
186 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
187
188 setOperationAction(ISD::BRCOND, MVT::Other, Legal);
189
190 // Floating point > and >= are supported via < and <=
191 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
192 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
193 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
194 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
195
196 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
197 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
198 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
199 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
200 }
201
202 if (Subtarget.hasMips64r6()) {
203 // MIPS64r6 replaces the accumulator-based multiplies with a three register
204 // instruction
205 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
206 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
207 setOperationAction(ISD::MUL, MVT::i64, Legal);
208 setOperationAction(ISD::MULHS, MVT::i64, Legal);
209 setOperationAction(ISD::MULHU, MVT::i64, Legal);
210
211 // MIPS32r6 replaces the accumulator-based division/remainder with separate
212 // three register division and remainder instructions.
213 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
214 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
215 setOperationAction(ISD::SDIV, MVT::i64, Legal);
216 setOperationAction(ISD::UDIV, MVT::i64, Legal);
217 setOperationAction(ISD::SREM, MVT::i64, Legal);
218 setOperationAction(ISD::UREM, MVT::i64, Legal);
219
220 // MIPS64r6 replaces conditional moves with an equivalent that removes the
221 // need for three GPR read ports.
222 setOperationAction(ISD::SETCC, MVT::i64, Legal);
223 setOperationAction(ISD::SELECT, MVT::i64, Legal);
224 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
225 }
226
227 computeRegisterProperties(Subtarget.getRegisterInfo());
228 }
229
230 const MipsTargetLowering *
createMipsSETargetLowering(const MipsTargetMachine & TM,const MipsSubtarget & STI)231 llvm::createMipsSETargetLowering(const MipsTargetMachine &TM,
232 const MipsSubtarget &STI) {
233 return new MipsSETargetLowering(TM, STI);
234 }
235
236 const TargetRegisterClass *
getRepRegClassFor(MVT VT) const237 MipsSETargetLowering::getRepRegClassFor(MVT VT) const {
238 if (VT == MVT::Untyped)
239 return Subtarget.hasDSP() ? &Mips::ACC64DSPRegClass : &Mips::ACC64RegClass;
240
241 return TargetLowering::getRepRegClassFor(VT);
242 }
243
244 // Enable MSA support for the given integer type and Register class.
245 void MipsSETargetLowering::
addMSAIntType(MVT::SimpleValueType Ty,const TargetRegisterClass * RC)246 addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
247 addRegisterClass(Ty, RC);
248
249 // Expand all builtin opcodes.
250 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
251 setOperationAction(Opc, Ty, Expand);
252
253 setOperationAction(ISD::BITCAST, Ty, Legal);
254 setOperationAction(ISD::LOAD, Ty, Legal);
255 setOperationAction(ISD::STORE, Ty, Legal);
256 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
257 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
258 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
259
260 setOperationAction(ISD::ADD, Ty, Legal);
261 setOperationAction(ISD::AND, Ty, Legal);
262 setOperationAction(ISD::CTLZ, Ty, Legal);
263 setOperationAction(ISD::CTPOP, Ty, Legal);
264 setOperationAction(ISD::MUL, Ty, Legal);
265 setOperationAction(ISD::OR, Ty, Legal);
266 setOperationAction(ISD::SDIV, Ty, Legal);
267 setOperationAction(ISD::SREM, Ty, Legal);
268 setOperationAction(ISD::SHL, Ty, Legal);
269 setOperationAction(ISD::SRA, Ty, Legal);
270 setOperationAction(ISD::SRL, Ty, Legal);
271 setOperationAction(ISD::SUB, Ty, Legal);
272 setOperationAction(ISD::UDIV, Ty, Legal);
273 setOperationAction(ISD::UREM, Ty, Legal);
274 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
275 setOperationAction(ISD::VSELECT, Ty, Legal);
276 setOperationAction(ISD::XOR, Ty, Legal);
277
278 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
279 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
280 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
281 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
282 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
283 }
284
285 setOperationAction(ISD::SETCC, Ty, Legal);
286 setCondCodeAction(ISD::SETNE, Ty, Expand);
287 setCondCodeAction(ISD::SETGE, Ty, Expand);
288 setCondCodeAction(ISD::SETGT, Ty, Expand);
289 setCondCodeAction(ISD::SETUGE, Ty, Expand);
290 setCondCodeAction(ISD::SETUGT, Ty, Expand);
291 }
292
293 // Enable MSA support for the given floating-point type and Register class.
294 void MipsSETargetLowering::
addMSAFloatType(MVT::SimpleValueType Ty,const TargetRegisterClass * RC)295 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
296 addRegisterClass(Ty, RC);
297
298 // Expand all builtin opcodes.
299 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
300 setOperationAction(Opc, Ty, Expand);
301
302 setOperationAction(ISD::LOAD, Ty, Legal);
303 setOperationAction(ISD::STORE, Ty, Legal);
304 setOperationAction(ISD::BITCAST, Ty, Legal);
305 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
306 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
307 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
308
309 if (Ty != MVT::v8f16) {
310 setOperationAction(ISD::FABS, Ty, Legal);
311 setOperationAction(ISD::FADD, Ty, Legal);
312 setOperationAction(ISD::FDIV, Ty, Legal);
313 setOperationAction(ISD::FEXP2, Ty, Legal);
314 setOperationAction(ISD::FLOG2, Ty, Legal);
315 setOperationAction(ISD::FMA, Ty, Legal);
316 setOperationAction(ISD::FMUL, Ty, Legal);
317 setOperationAction(ISD::FRINT, Ty, Legal);
318 setOperationAction(ISD::FSQRT, Ty, Legal);
319 setOperationAction(ISD::FSUB, Ty, Legal);
320 setOperationAction(ISD::VSELECT, Ty, Legal);
321
322 setOperationAction(ISD::SETCC, Ty, Legal);
323 setCondCodeAction(ISD::SETOGE, Ty, Expand);
324 setCondCodeAction(ISD::SETOGT, Ty, Expand);
325 setCondCodeAction(ISD::SETUGE, Ty, Expand);
326 setCondCodeAction(ISD::SETUGT, Ty, Expand);
327 setCondCodeAction(ISD::SETGE, Ty, Expand);
328 setCondCodeAction(ISD::SETGT, Ty, Expand);
329 }
330 }
331
332 bool
allowsMisalignedMemoryAccesses(EVT VT,unsigned,unsigned,bool * Fast) const333 MipsSETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
334 unsigned,
335 unsigned,
336 bool *Fast) const {
337 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
338
339 if (Subtarget.systemSupportsUnalignedAccess()) {
340 // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
341 // implementation defined whether this is handled by hardware, software, or
342 // a hybrid of the two but it's expected that most implementations will
343 // handle the majority of cases in hardware.
344 if (Fast)
345 *Fast = true;
346 return true;
347 }
348
349 switch (SVT) {
350 case MVT::i64:
351 case MVT::i32:
352 if (Fast)
353 *Fast = true;
354 return true;
355 default:
356 return false;
357 }
358 }
359
LowerOperation(SDValue Op,SelectionDAG & DAG) const360 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
361 SelectionDAG &DAG) const {
362 switch(Op.getOpcode()) {
363 case ISD::LOAD: return lowerLOAD(Op, DAG);
364 case ISD::STORE: return lowerSTORE(Op, DAG);
365 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
366 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
367 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
368 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
369 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
370 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
371 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
372 DAG);
373 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
374 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
375 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
376 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
377 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
378 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
379 }
380
381 return MipsTargetLowering::LowerOperation(Op, DAG);
382 }
383
384 // selectMADD -
385 // Transforms a subgraph in CurDAG if the following pattern is found:
386 // (addc multLo, Lo0), (adde multHi, Hi0),
387 // where,
388 // multHi/Lo: product of multiplication
389 // Lo0: initial value of Lo register
390 // Hi0: initial value of Hi register
391 // Return true if pattern matching was successful.
selectMADD(SDNode * ADDENode,SelectionDAG * CurDAG)392 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
393 // ADDENode's second operand must be a flag output of an ADDC node in order
394 // for the matching to be successful.
395 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
396
397 if (ADDCNode->getOpcode() != ISD::ADDC)
398 return false;
399
400 SDValue MultHi = ADDENode->getOperand(0);
401 SDValue MultLo = ADDCNode->getOperand(0);
402 SDNode *MultNode = MultHi.getNode();
403 unsigned MultOpc = MultHi.getOpcode();
404
405 // MultHi and MultLo must be generated by the same node,
406 if (MultLo.getNode() != MultNode)
407 return false;
408
409 // and it must be a multiplication.
410 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
411 return false;
412
413 // MultLo amd MultHi must be the first and second output of MultNode
414 // respectively.
415 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
416 return false;
417
418 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
419 // of the values of MultNode, in which case MultNode will be removed in later
420 // phases.
421 // If there exist users other than ADDENode or ADDCNode, this function returns
422 // here, which will result in MultNode being mapped to a single MULT
423 // instruction node rather than a pair of MULT and MADD instructions being
424 // produced.
425 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
426 return false;
427
428 SDLoc DL(ADDENode);
429
430 // Initialize accumulator.
431 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
432 ADDCNode->getOperand(1),
433 ADDENode->getOperand(1));
434
435 // create MipsMAdd(u) node
436 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
437
438 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
439 MultNode->getOperand(0),// Factor 0
440 MultNode->getOperand(1),// Factor 1
441 ACCIn);
442
443 // replace uses of adde and addc here
444 if (!SDValue(ADDCNode, 0).use_empty()) {
445 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
446 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
447 }
448 if (!SDValue(ADDENode, 0).use_empty()) {
449 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
450 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
451 }
452
453 return true;
454 }
455
456 // selectMSUB -
457 // Transforms a subgraph in CurDAG if the following pattern is found:
458 // (addc Lo0, multLo), (sube Hi0, multHi),
459 // where,
460 // multHi/Lo: product of multiplication
461 // Lo0: initial value of Lo register
462 // Hi0: initial value of Hi register
463 // Return true if pattern matching was successful.
selectMSUB(SDNode * SUBENode,SelectionDAG * CurDAG)464 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
465 // SUBENode's second operand must be a flag output of an SUBC node in order
466 // for the matching to be successful.
467 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
468
469 if (SUBCNode->getOpcode() != ISD::SUBC)
470 return false;
471
472 SDValue MultHi = SUBENode->getOperand(1);
473 SDValue MultLo = SUBCNode->getOperand(1);
474 SDNode *MultNode = MultHi.getNode();
475 unsigned MultOpc = MultHi.getOpcode();
476
477 // MultHi and MultLo must be generated by the same node,
478 if (MultLo.getNode() != MultNode)
479 return false;
480
481 // and it must be a multiplication.
482 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
483 return false;
484
485 // MultLo amd MultHi must be the first and second output of MultNode
486 // respectively.
487 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
488 return false;
489
490 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
491 // of the values of MultNode, in which case MultNode will be removed in later
492 // phases.
493 // If there exist users other than SUBENode or SUBCNode, this function returns
494 // here, which will result in MultNode being mapped to a single MULT
495 // instruction node rather than a pair of MULT and MSUB instructions being
496 // produced.
497 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
498 return false;
499
500 SDLoc DL(SUBENode);
501
502 // Initialize accumulator.
503 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
504 SUBCNode->getOperand(0),
505 SUBENode->getOperand(0));
506
507 // create MipsSub(u) node
508 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
509
510 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
511 MultNode->getOperand(0),// Factor 0
512 MultNode->getOperand(1),// Factor 1
513 ACCIn);
514
515 // replace uses of sube and subc here
516 if (!SDValue(SUBCNode, 0).use_empty()) {
517 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
518 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
519 }
520 if (!SDValue(SUBENode, 0).use_empty()) {
521 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
522 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
523 }
524
525 return true;
526 }
527
performADDECombine(SDNode * N,SelectionDAG & DAG,TargetLowering::DAGCombinerInfo & DCI,const MipsSubtarget & Subtarget)528 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
529 TargetLowering::DAGCombinerInfo &DCI,
530 const MipsSubtarget &Subtarget) {
531 if (DCI.isBeforeLegalize())
532 return SDValue();
533
534 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
535 N->getValueType(0) == MVT::i32 && selectMADD(N, &DAG))
536 return SDValue(N, 0);
537
538 return SDValue();
539 }
540
541 // Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
542 //
543 // Performs the following transformations:
544 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
545 // sign/zero-extension is completely overwritten by the new one performed by
546 // the ISD::AND.
547 // - Removes redundant zero extensions performed by an ISD::AND.
performANDCombine(SDNode * N,SelectionDAG & DAG,TargetLowering::DAGCombinerInfo & DCI,const MipsSubtarget & Subtarget)548 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
549 TargetLowering::DAGCombinerInfo &DCI,
550 const MipsSubtarget &Subtarget) {
551 if (!Subtarget.hasMSA())
552 return SDValue();
553
554 SDValue Op0 = N->getOperand(0);
555 SDValue Op1 = N->getOperand(1);
556 unsigned Op0Opcode = Op0->getOpcode();
557
558 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
559 // where $d + 1 == 2^n and n == 32
560 // or $d + 1 == 2^n and n <= 32 and ZExt
561 // -> (MipsVExtractZExt $a, $b, $c)
562 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
563 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
564 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
565
566 if (!Mask)
567 return SDValue();
568
569 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
570
571 if (Log2IfPositive <= 0)
572 return SDValue(); // Mask+1 is not a power of 2
573
574 SDValue Op0Op2 = Op0->getOperand(2);
575 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
576 unsigned ExtendTySize = ExtendTy.getSizeInBits();
577 unsigned Log2 = Log2IfPositive;
578
579 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
580 Log2 == ExtendTySize) {
581 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
582 return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
583 Op0->getVTList(),
584 makeArrayRef(Ops, Op0->getNumOperands()));
585 }
586 }
587
588 return SDValue();
589 }
590
591 // Determine if the specified node is a constant vector splat.
592 //
593 // Returns true and sets Imm if:
594 // * N is a ISD::BUILD_VECTOR representing a constant splat
595 //
596 // This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
597 // differences are that it assumes the MSA has already been checked and the
598 // arbitrary requirement for a maximum of 32-bit integers isn't applied (and
599 // must not be in order for binsri.d to be selectable).
isVSplat(SDValue N,APInt & Imm,bool IsLittleEndian)600 static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
601 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
602
603 if (!Node)
604 return false;
605
606 APInt SplatValue, SplatUndef;
607 unsigned SplatBitSize;
608 bool HasAnyUndefs;
609
610 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
611 8, !IsLittleEndian))
612 return false;
613
614 Imm = SplatValue;
615
616 return true;
617 }
618
619 // Test whether the given node is an all-ones build_vector.
isVectorAllOnes(SDValue N)620 static bool isVectorAllOnes(SDValue N) {
621 // Look through bitcasts. Endianness doesn't matter because we are looking
622 // for an all-ones value.
623 if (N->getOpcode() == ISD::BITCAST)
624 N = N->getOperand(0);
625
626 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
627
628 if (!BVN)
629 return false;
630
631 APInt SplatValue, SplatUndef;
632 unsigned SplatBitSize;
633 bool HasAnyUndefs;
634
635 // Endianness doesn't matter in this context because we are looking for
636 // an all-ones value.
637 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
638 return SplatValue.isAllOnesValue();
639
640 return false;
641 }
642
643 // Test whether N is the bitwise inverse of OfNode.
isBitwiseInverse(SDValue N,SDValue OfNode)644 static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
645 if (N->getOpcode() != ISD::XOR)
646 return false;
647
648 if (isVectorAllOnes(N->getOperand(0)))
649 return N->getOperand(1) == OfNode;
650
651 if (isVectorAllOnes(N->getOperand(1)))
652 return N->getOperand(0) == OfNode;
653
654 return false;
655 }
656
657 // Perform combines where ISD::OR is the root node.
658 //
659 // Performs the following transformations:
660 // - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
661 // where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
662 // vector type.
performORCombine(SDNode * N,SelectionDAG & DAG,TargetLowering::DAGCombinerInfo & DCI,const MipsSubtarget & Subtarget)663 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
664 TargetLowering::DAGCombinerInfo &DCI,
665 const MipsSubtarget &Subtarget) {
666 if (!Subtarget.hasMSA())
667 return SDValue();
668
669 EVT Ty = N->getValueType(0);
670
671 if (!Ty.is128BitVector())
672 return SDValue();
673
674 SDValue Op0 = N->getOperand(0);
675 SDValue Op1 = N->getOperand(1);
676
677 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
678 SDValue Op0Op0 = Op0->getOperand(0);
679 SDValue Op0Op1 = Op0->getOperand(1);
680 SDValue Op1Op0 = Op1->getOperand(0);
681 SDValue Op1Op1 = Op1->getOperand(1);
682 bool IsLittleEndian = !Subtarget.isLittle();
683
684 SDValue IfSet, IfClr, Cond;
685 bool IsConstantMask = false;
686 APInt Mask, InvMask;
687
688 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
689 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
690 // looking.
691 // IfClr will be set if we find a valid match.
692 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
693 Cond = Op0Op0;
694 IfSet = Op0Op1;
695
696 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
697 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
698 IfClr = Op1Op1;
699 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
700 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
701 IfClr = Op1Op0;
702
703 IsConstantMask = true;
704 }
705
706 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
707 // thing again using this mask.
708 // IfClr will be set if we find a valid match.
709 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
710 Cond = Op0Op1;
711 IfSet = Op0Op0;
712
713 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
714 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
715 IfClr = Op1Op1;
716 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
717 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
718 IfClr = Op1Op0;
719
720 IsConstantMask = true;
721 }
722
723 // If IfClr is not yet set, try looking for a non-constant match.
724 // IfClr will be set if we find a valid match amongst the eight
725 // possibilities.
726 if (!IfClr.getNode()) {
727 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
728 Cond = Op1Op0;
729 IfSet = Op1Op1;
730 IfClr = Op0Op1;
731 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
732 Cond = Op1Op0;
733 IfSet = Op1Op1;
734 IfClr = Op0Op0;
735 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
736 Cond = Op1Op1;
737 IfSet = Op1Op0;
738 IfClr = Op0Op1;
739 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
740 Cond = Op1Op1;
741 IfSet = Op1Op0;
742 IfClr = Op0Op0;
743 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
744 Cond = Op0Op0;
745 IfSet = Op0Op1;
746 IfClr = Op1Op1;
747 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
748 Cond = Op0Op0;
749 IfSet = Op0Op1;
750 IfClr = Op1Op0;
751 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
752 Cond = Op0Op1;
753 IfSet = Op0Op0;
754 IfClr = Op1Op1;
755 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
756 Cond = Op0Op1;
757 IfSet = Op0Op0;
758 IfClr = Op1Op0;
759 }
760 }
761
762 // At this point, IfClr will be set if we have a valid match.
763 if (!IfClr.getNode())
764 return SDValue();
765
766 assert(Cond.getNode() && IfSet.getNode());
767
768 // Fold degenerate cases.
769 if (IsConstantMask) {
770 if (Mask.isAllOnesValue())
771 return IfSet;
772 else if (Mask == 0)
773 return IfClr;
774 }
775
776 // Transform the DAG into an equivalent VSELECT.
777 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
778 }
779
780 return SDValue();
781 }
782
performSUBECombine(SDNode * N,SelectionDAG & DAG,TargetLowering::DAGCombinerInfo & DCI,const MipsSubtarget & Subtarget)783 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
784 TargetLowering::DAGCombinerInfo &DCI,
785 const MipsSubtarget &Subtarget) {
786 if (DCI.isBeforeLegalize())
787 return SDValue();
788
789 if (Subtarget.hasMips32() && N->getValueType(0) == MVT::i32 &&
790 selectMSUB(N, &DAG))
791 return SDValue(N, 0);
792
793 return SDValue();
794 }
795
genConstMult(SDValue X,uint64_t C,const SDLoc & DL,EVT VT,EVT ShiftTy,SelectionDAG & DAG)796 static SDValue genConstMult(SDValue X, uint64_t C, const SDLoc &DL, EVT VT,
797 EVT ShiftTy, SelectionDAG &DAG) {
798 // Clear the upper (64 - VT.sizeInBits) bits.
799 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
800
801 // Return 0.
802 if (C == 0)
803 return DAG.getConstant(0, DL, VT);
804
805 // Return x.
806 if (C == 1)
807 return X;
808
809 // If c is power of 2, return (shl x, log2(c)).
810 if (isPowerOf2_64(C))
811 return DAG.getNode(ISD::SHL, DL, VT, X,
812 DAG.getConstant(Log2_64(C), DL, ShiftTy));
813
814 unsigned Log2Ceil = Log2_64_Ceil(C);
815 uint64_t Floor = 1LL << Log2_64(C);
816 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
817
818 // If |c - floor_c| <= |c - ceil_c|,
819 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
820 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
821 if (C - Floor <= Ceil - C) {
822 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
823 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
824 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
825 }
826
827 // If |c - floor_c| > |c - ceil_c|,
828 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
829 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
830 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
831 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
832 }
833
performMULCombine(SDNode * N,SelectionDAG & DAG,const TargetLowering::DAGCombinerInfo & DCI,const MipsSETargetLowering * TL)834 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
835 const TargetLowering::DAGCombinerInfo &DCI,
836 const MipsSETargetLowering *TL) {
837 EVT VT = N->getValueType(0);
838
839 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
840 if (!VT.isVector())
841 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N), VT,
842 TL->getScalarShiftAmountTy(DAG.getDataLayout(), VT),
843 DAG);
844
845 return SDValue(N, 0);
846 }
847
performDSPShiftCombine(unsigned Opc,SDNode * N,EVT Ty,SelectionDAG & DAG,const MipsSubtarget & Subtarget)848 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
849 SelectionDAG &DAG,
850 const MipsSubtarget &Subtarget) {
851 // See if this is a vector splat immediate node.
852 APInt SplatValue, SplatUndef;
853 unsigned SplatBitSize;
854 bool HasAnyUndefs;
855 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
856 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
857
858 if (!Subtarget.hasDSP())
859 return SDValue();
860
861 if (!BV ||
862 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
863 EltSize, !Subtarget.isLittle()) ||
864 (SplatBitSize != EltSize) ||
865 (SplatValue.getZExtValue() >= EltSize))
866 return SDValue();
867
868 SDLoc DL(N);
869 return DAG.getNode(Opc, DL, Ty, N->getOperand(0),
870 DAG.getConstant(SplatValue.getZExtValue(), DL, MVT::i32));
871 }
872
performSHLCombine(SDNode * N,SelectionDAG & DAG,TargetLowering::DAGCombinerInfo & DCI,const MipsSubtarget & Subtarget)873 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
874 TargetLowering::DAGCombinerInfo &DCI,
875 const MipsSubtarget &Subtarget) {
876 EVT Ty = N->getValueType(0);
877
878 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
879 return SDValue();
880
881 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
882 }
883
884 // Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
885 // constant splats into MipsISD::SHRA_DSP for DSPr2.
886 //
887 // Performs the following transformations:
888 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
889 // sign/zero-extension is completely overwritten by the new one performed by
890 // the ISD::SRA and ISD::SHL nodes.
891 // - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
892 // sequence.
893 //
894 // See performDSPShiftCombine for more information about the transformation
895 // used for DSPr2.
performSRACombine(SDNode * N,SelectionDAG & DAG,TargetLowering::DAGCombinerInfo & DCI,const MipsSubtarget & Subtarget)896 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
897 TargetLowering::DAGCombinerInfo &DCI,
898 const MipsSubtarget &Subtarget) {
899 EVT Ty = N->getValueType(0);
900
901 if (Subtarget.hasMSA()) {
902 SDValue Op0 = N->getOperand(0);
903 SDValue Op1 = N->getOperand(1);
904
905 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
906 // where $d + sizeof($c) == 32
907 // or $d + sizeof($c) <= 32 and SExt
908 // -> (MipsVExtractSExt $a, $b, $c)
909 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
910 SDValue Op0Op0 = Op0->getOperand(0);
911 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
912
913 if (!ShAmount)
914 return SDValue();
915
916 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
917 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
918 return SDValue();
919
920 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
921 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
922
923 if (TotalBits == 32 ||
924 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
925 TotalBits <= 32)) {
926 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
927 Op0Op0->getOperand(2) };
928 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
929 Op0Op0->getVTList(),
930 makeArrayRef(Ops, Op0Op0->getNumOperands()));
931 }
932 }
933 }
934
935 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
936 return SDValue();
937
938 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
939 }
940
941
performSRLCombine(SDNode * N,SelectionDAG & DAG,TargetLowering::DAGCombinerInfo & DCI,const MipsSubtarget & Subtarget)942 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
943 TargetLowering::DAGCombinerInfo &DCI,
944 const MipsSubtarget &Subtarget) {
945 EVT Ty = N->getValueType(0);
946
947 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
948 return SDValue();
949
950 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
951 }
952
isLegalDSPCondCode(EVT Ty,ISD::CondCode CC)953 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
954 bool IsV216 = (Ty == MVT::v2i16);
955
956 switch (CC) {
957 case ISD::SETEQ:
958 case ISD::SETNE: return true;
959 case ISD::SETLT:
960 case ISD::SETLE:
961 case ISD::SETGT:
962 case ISD::SETGE: return IsV216;
963 case ISD::SETULT:
964 case ISD::SETULE:
965 case ISD::SETUGT:
966 case ISD::SETUGE: return !IsV216;
967 default: return false;
968 }
969 }
970
performSETCCCombine(SDNode * N,SelectionDAG & DAG)971 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
972 EVT Ty = N->getValueType(0);
973
974 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
975 return SDValue();
976
977 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
978 return SDValue();
979
980 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
981 N->getOperand(1), N->getOperand(2));
982 }
983
performVSELECTCombine(SDNode * N,SelectionDAG & DAG)984 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
985 EVT Ty = N->getValueType(0);
986
987 if (Ty.is128BitVector() && Ty.isInteger()) {
988 // Try the following combines:
989 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
990 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
991 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
992 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
993 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
994 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
995 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
996 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
997 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
998 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
999 // legalizer.
1000 SDValue Op0 = N->getOperand(0);
1001
1002 if (Op0->getOpcode() != ISD::SETCC)
1003 return SDValue();
1004
1005 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
1006 bool Signed;
1007
1008 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
1009 Signed = true;
1010 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
1011 Signed = false;
1012 else
1013 return SDValue();
1014
1015 SDValue Op1 = N->getOperand(1);
1016 SDValue Op2 = N->getOperand(2);
1017 SDValue Op0Op0 = Op0->getOperand(0);
1018 SDValue Op0Op1 = Op0->getOperand(1);
1019
1020 if (Op1 == Op0Op0 && Op2 == Op0Op1)
1021 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
1022 Ty, Op1, Op2);
1023 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
1024 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
1025 Ty, Op1, Op2);
1026 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
1027 SDValue SetCC = N->getOperand(0);
1028
1029 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
1030 return SDValue();
1031
1032 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
1033 SetCC.getOperand(0), SetCC.getOperand(1),
1034 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
1035 }
1036
1037 return SDValue();
1038 }
1039
performXORCombine(SDNode * N,SelectionDAG & DAG,const MipsSubtarget & Subtarget)1040 static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
1041 const MipsSubtarget &Subtarget) {
1042 EVT Ty = N->getValueType(0);
1043
1044 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
1045 // Try the following combines:
1046 // (xor (or $a, $b), (build_vector allones))
1047 // (xor (or $a, $b), (bitcast (build_vector allones)))
1048 SDValue Op0 = N->getOperand(0);
1049 SDValue Op1 = N->getOperand(1);
1050 SDValue NotOp;
1051
1052 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
1053 NotOp = Op1;
1054 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
1055 NotOp = Op0;
1056 else
1057 return SDValue();
1058
1059 if (NotOp->getOpcode() == ISD::OR)
1060 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
1061 NotOp->getOperand(1));
1062 }
1063
1064 return SDValue();
1065 }
1066
1067 SDValue
PerformDAGCombine(SDNode * N,DAGCombinerInfo & DCI) const1068 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1069 SelectionDAG &DAG = DCI.DAG;
1070 SDValue Val;
1071
1072 switch (N->getOpcode()) {
1073 case ISD::ADDE:
1074 return performADDECombine(N, DAG, DCI, Subtarget);
1075 case ISD::AND:
1076 Val = performANDCombine(N, DAG, DCI, Subtarget);
1077 break;
1078 case ISD::OR:
1079 Val = performORCombine(N, DAG, DCI, Subtarget);
1080 break;
1081 case ISD::SUBE:
1082 return performSUBECombine(N, DAG, DCI, Subtarget);
1083 case ISD::MUL:
1084 return performMULCombine(N, DAG, DCI, this);
1085 case ISD::SHL:
1086 return performSHLCombine(N, DAG, DCI, Subtarget);
1087 case ISD::SRA:
1088 return performSRACombine(N, DAG, DCI, Subtarget);
1089 case ISD::SRL:
1090 return performSRLCombine(N, DAG, DCI, Subtarget);
1091 case ISD::VSELECT:
1092 return performVSELECTCombine(N, DAG);
1093 case ISD::XOR:
1094 Val = performXORCombine(N, DAG, Subtarget);
1095 break;
1096 case ISD::SETCC:
1097 Val = performSETCCCombine(N, DAG);
1098 break;
1099 }
1100
1101 if (Val.getNode()) {
1102 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1103 N->printrWithDepth(dbgs(), &DAG);
1104 dbgs() << "\n=> \n";
1105 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1106 dbgs() << "\n");
1107 return Val;
1108 }
1109
1110 return MipsTargetLowering::PerformDAGCombine(N, DCI);
1111 }
1112
1113 MachineBasicBlock *
EmitInstrWithCustomInserter(MachineInstr & MI,MachineBasicBlock * BB) const1114 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
1115 MachineBasicBlock *BB) const {
1116 switch (MI.getOpcode()) {
1117 default:
1118 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1119 case Mips::BPOSGE32_PSEUDO:
1120 return emitBPOSGE32(MI, BB);
1121 case Mips::SNZ_B_PSEUDO:
1122 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1123 case Mips::SNZ_H_PSEUDO:
1124 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1125 case Mips::SNZ_W_PSEUDO:
1126 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1127 case Mips::SNZ_D_PSEUDO:
1128 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1129 case Mips::SNZ_V_PSEUDO:
1130 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1131 case Mips::SZ_B_PSEUDO:
1132 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1133 case Mips::SZ_H_PSEUDO:
1134 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1135 case Mips::SZ_W_PSEUDO:
1136 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1137 case Mips::SZ_D_PSEUDO:
1138 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1139 case Mips::SZ_V_PSEUDO:
1140 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
1141 case Mips::COPY_FW_PSEUDO:
1142 return emitCOPY_FW(MI, BB);
1143 case Mips::COPY_FD_PSEUDO:
1144 return emitCOPY_FD(MI, BB);
1145 case Mips::INSERT_FW_PSEUDO:
1146 return emitINSERT_FW(MI, BB);
1147 case Mips::INSERT_FD_PSEUDO:
1148 return emitINSERT_FD(MI, BB);
1149 case Mips::INSERT_B_VIDX_PSEUDO:
1150 case Mips::INSERT_B_VIDX64_PSEUDO:
1151 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1152 case Mips::INSERT_H_VIDX_PSEUDO:
1153 case Mips::INSERT_H_VIDX64_PSEUDO:
1154 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1155 case Mips::INSERT_W_VIDX_PSEUDO:
1156 case Mips::INSERT_W_VIDX64_PSEUDO:
1157 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1158 case Mips::INSERT_D_VIDX_PSEUDO:
1159 case Mips::INSERT_D_VIDX64_PSEUDO:
1160 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1161 case Mips::INSERT_FW_VIDX_PSEUDO:
1162 case Mips::INSERT_FW_VIDX64_PSEUDO:
1163 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1164 case Mips::INSERT_FD_VIDX_PSEUDO:
1165 case Mips::INSERT_FD_VIDX64_PSEUDO:
1166 return emitINSERT_DF_VIDX(MI, BB, 8, true);
1167 case Mips::FILL_FW_PSEUDO:
1168 return emitFILL_FW(MI, BB);
1169 case Mips::FILL_FD_PSEUDO:
1170 return emitFILL_FD(MI, BB);
1171 case Mips::FEXP2_W_1_PSEUDO:
1172 return emitFEXP2_W_1(MI, BB);
1173 case Mips::FEXP2_D_1_PSEUDO:
1174 return emitFEXP2_D_1(MI, BB);
1175 }
1176 }
1177
isEligibleForTailCallOptimization(const CCState & CCInfo,unsigned NextStackOffset,const MipsFunctionInfo & FI) const1178 bool MipsSETargetLowering::isEligibleForTailCallOptimization(
1179 const CCState &CCInfo, unsigned NextStackOffset,
1180 const MipsFunctionInfo &FI) const {
1181 if (!EnableMipsTailCalls)
1182 return false;
1183
1184 // Exception has to be cleared with eret.
1185 if (FI.isISR())
1186 return false;
1187
1188 // Return false if either the callee or caller has a byval argument.
1189 if (CCInfo.getInRegsParamsCount() > 0 || FI.hasByvalArg())
1190 return false;
1191
1192 // Return true if the callee's argument area is no larger than the
1193 // caller's.
1194 return NextStackOffset <= FI.getIncomingArgSize();
1195 }
1196
1197 void MipsSETargetLowering::
getOpndList(SmallVectorImpl<SDValue> & Ops,std::deque<std::pair<unsigned,SDValue>> & RegsToPass,bool IsPICCall,bool GlobalOrExternal,bool InternalLinkage,bool IsCallReloc,CallLoweringInfo & CLI,SDValue Callee,SDValue Chain) const1198 getOpndList(SmallVectorImpl<SDValue> &Ops,
1199 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
1200 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
1201 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
1202 SDValue Chain) const {
1203 Ops.push_back(Callee);
1204 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
1205 InternalLinkage, IsCallReloc, CLI, Callee,
1206 Chain);
1207 }
1208
lowerLOAD(SDValue Op,SelectionDAG & DAG) const1209 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1210 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1211
1212 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1213 return MipsTargetLowering::lowerLOAD(Op, DAG);
1214
1215 // Replace a double precision load with two i32 loads and a buildpair64.
1216 SDLoc DL(Op);
1217 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1218 EVT PtrVT = Ptr.getValueType();
1219
1220 // i32 load from lower address.
1221 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1222 MachinePointerInfo(), Nd.isVolatile(),
1223 Nd.isNonTemporal(), Nd.isInvariant(),
1224 Nd.getAlignment());
1225
1226 // i32 load from higher address.
1227 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
1228 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1229 MachinePointerInfo(), Nd.isVolatile(),
1230 Nd.isNonTemporal(), Nd.isInvariant(),
1231 std::min(Nd.getAlignment(), 4U));
1232
1233 if (!Subtarget.isLittle())
1234 std::swap(Lo, Hi);
1235
1236 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1237 SDValue Ops[2] = {BP, Hi.getValue(1)};
1238 return DAG.getMergeValues(Ops, DL);
1239 }
1240
lowerSTORE(SDValue Op,SelectionDAG & DAG) const1241 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1242 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1243
1244 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1245 return MipsTargetLowering::lowerSTORE(Op, DAG);
1246
1247 // Replace a double precision store with two extractelement64s and i32 stores.
1248 SDLoc DL(Op);
1249 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1250 EVT PtrVT = Ptr.getValueType();
1251 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1252 Val, DAG.getConstant(0, DL, MVT::i32));
1253 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1254 Val, DAG.getConstant(1, DL, MVT::i32));
1255
1256 if (!Subtarget.isLittle())
1257 std::swap(Lo, Hi);
1258
1259 // i32 store to lower address.
1260 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1261 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
1262 Nd.getAAInfo());
1263
1264 // i32 store to higher address.
1265 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
1266 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
1267 Nd.isVolatile(), Nd.isNonTemporal(),
1268 std::min(Nd.getAlignment(), 4U), Nd.getAAInfo());
1269 }
1270
lowerMulDiv(SDValue Op,unsigned NewOpc,bool HasLo,bool HasHi,SelectionDAG & DAG) const1271 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1272 bool HasLo, bool HasHi,
1273 SelectionDAG &DAG) const {
1274 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
1275 assert(!Subtarget.hasMips32r6());
1276
1277 EVT Ty = Op.getOperand(0).getValueType();
1278 SDLoc DL(Op);
1279 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1280 Op.getOperand(0), Op.getOperand(1));
1281 SDValue Lo, Hi;
1282
1283 if (HasLo)
1284 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
1285 if (HasHi)
1286 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
1287
1288 if (!HasLo || !HasHi)
1289 return HasLo ? Lo : Hi;
1290
1291 SDValue Vals[] = { Lo, Hi };
1292 return DAG.getMergeValues(Vals, DL);
1293 }
1294
initAccumulator(SDValue In,const SDLoc & DL,SelectionDAG & DAG)1295 static SDValue initAccumulator(SDValue In, const SDLoc &DL, SelectionDAG &DAG) {
1296 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1297 DAG.getConstant(0, DL, MVT::i32));
1298 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1299 DAG.getConstant(1, DL, MVT::i32));
1300 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
1301 }
1302
extractLOHI(SDValue Op,const SDLoc & DL,SelectionDAG & DAG)1303 static SDValue extractLOHI(SDValue Op, const SDLoc &DL, SelectionDAG &DAG) {
1304 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1305 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
1306 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1307 }
1308
1309 // This function expands mips intrinsic nodes which have 64-bit input operands
1310 // or output values.
1311 //
1312 // out64 = intrinsic-node in64
1313 // =>
1314 // lo = copy (extract-element (in64, 0))
1315 // hi = copy (extract-element (in64, 1))
1316 // mips-specific-node
1317 // v0 = copy lo
1318 // v1 = copy hi
1319 // out64 = merge-values (v0, v1)
1320 //
lowerDSPIntr(SDValue Op,SelectionDAG & DAG,unsigned Opc)1321 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1322 SDLoc DL(Op);
1323 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1324 SmallVector<SDValue, 3> Ops;
1325 unsigned OpNo = 0;
1326
1327 // See if Op has a chain input.
1328 if (HasChainIn)
1329 Ops.push_back(Op->getOperand(OpNo++));
1330
1331 // The next operand is the intrinsic opcode.
1332 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1333
1334 // See if the next operand has type i64.
1335 SDValue Opnd = Op->getOperand(++OpNo), In64;
1336
1337 if (Opnd.getValueType() == MVT::i64)
1338 In64 = initAccumulator(Opnd, DL, DAG);
1339 else
1340 Ops.push_back(Opnd);
1341
1342 // Push the remaining operands.
1343 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1344 Ops.push_back(Op->getOperand(OpNo));
1345
1346 // Add In64 to the end of the list.
1347 if (In64.getNode())
1348 Ops.push_back(In64);
1349
1350 // Scan output.
1351 SmallVector<EVT, 2> ResTys;
1352
1353 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1354 I != E; ++I)
1355 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1356
1357 // Create node.
1358 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
1359 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1360
1361 if (!HasChainIn)
1362 return Out;
1363
1364 assert(Val->getValueType(1) == MVT::Other);
1365 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1366 return DAG.getMergeValues(Vals, DL);
1367 }
1368
1369 // Lower an MSA copy intrinsic into the specified SelectionDAG node
lowerMSACopyIntr(SDValue Op,SelectionDAG & DAG,unsigned Opc)1370 static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1371 SDLoc DL(Op);
1372 SDValue Vec = Op->getOperand(1);
1373 SDValue Idx = Op->getOperand(2);
1374 EVT ResTy = Op->getValueType(0);
1375 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1376
1377 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1378 DAG.getValueType(EltTy));
1379
1380 return Result;
1381 }
1382
lowerMSASplatZExt(SDValue Op,unsigned OpNr,SelectionDAG & DAG)1383 static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1384 EVT ResVecTy = Op->getValueType(0);
1385 EVT ViaVecTy = ResVecTy;
1386 SDLoc DL(Op);
1387
1388 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1389 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1390 // lanes.
1391 SDValue LaneA;
1392 SDValue LaneB = Op->getOperand(2);
1393
1394 if (ResVecTy == MVT::v2i64) {
1395 LaneA = DAG.getConstant(0, DL, MVT::i32);
1396 ViaVecTy = MVT::v4i32;
1397 } else
1398 LaneA = LaneB;
1399
1400 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1401 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
1402
1403 SDValue Result = DAG.getBuildVector(
1404 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
1405
1406 if (ViaVecTy != ResVecTy)
1407 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
1408
1409 return Result;
1410 }
1411
lowerMSASplatImm(SDValue Op,unsigned ImmOp,SelectionDAG & DAG)1412 static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1413 return DAG.getConstant(Op->getConstantOperandVal(ImmOp), SDLoc(Op),
1414 Op->getValueType(0));
1415 }
1416
getBuildVectorSplat(EVT VecTy,SDValue SplatValue,bool BigEndian,SelectionDAG & DAG)1417 static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1418 bool BigEndian, SelectionDAG &DAG) {
1419 EVT ViaVecTy = VecTy;
1420 SDValue SplatValueA = SplatValue;
1421 SDValue SplatValueB = SplatValue;
1422 SDLoc DL(SplatValue);
1423
1424 if (VecTy == MVT::v2i64) {
1425 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1426 ViaVecTy = MVT::v4i32;
1427
1428 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1429 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
1430 DAG.getConstant(32, DL, MVT::i32));
1431 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1432 }
1433
1434 // We currently hold the parts in little endian order. Swap them if
1435 // necessary.
1436 if (BigEndian)
1437 std::swap(SplatValueA, SplatValueB);
1438
1439 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1440 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1441 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1442 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1443
1444 SDValue Result = DAG.getBuildVector(
1445 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
1446
1447 if (VecTy != ViaVecTy)
1448 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1449
1450 return Result;
1451 }
1452
lowerMSABinaryBitImmIntr(SDValue Op,SelectionDAG & DAG,unsigned Opc,SDValue Imm,bool BigEndian)1453 static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1454 unsigned Opc, SDValue Imm,
1455 bool BigEndian) {
1456 EVT VecTy = Op->getValueType(0);
1457 SDValue Exp2Imm;
1458 SDLoc DL(Op);
1459
1460 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1461 // here for now.
1462 if (VecTy == MVT::v2i64) {
1463 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1464 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1465
1466 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), DL,
1467 MVT::i32);
1468 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), DL, MVT::i32);
1469
1470 if (BigEndian)
1471 std::swap(BitImmLoOp, BitImmHiOp);
1472
1473 Exp2Imm = DAG.getNode(
1474 ISD::BITCAST, DL, MVT::v2i64,
1475 DAG.getBuildVector(MVT::v4i32, DL,
1476 {BitImmLoOp, BitImmHiOp, BitImmLoOp, BitImmHiOp}));
1477 }
1478 }
1479
1480 if (!Exp2Imm.getNode()) {
1481 // We couldnt constant fold, do a vector shift instead
1482
1483 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1484 // only values 0-63 are valid.
1485 if (VecTy == MVT::v2i64)
1486 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1487
1488 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1489
1490 Exp2Imm = DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, DL, VecTy),
1491 Exp2Imm);
1492 }
1493
1494 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1495 }
1496
lowerMSABitClear(SDValue Op,SelectionDAG & DAG)1497 static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1498 EVT ResTy = Op->getValueType(0);
1499 SDLoc DL(Op);
1500 SDValue One = DAG.getConstant(1, DL, ResTy);
1501 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
1502
1503 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1504 DAG.getNOT(DL, Bit, ResTy));
1505 }
1506
lowerMSABitClearImm(SDValue Op,SelectionDAG & DAG)1507 static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1508 SDLoc DL(Op);
1509 EVT ResTy = Op->getValueType(0);
1510 APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
1511 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
1512 SDValue BitMask = DAG.getConstant(~BitImm, DL, ResTy);
1513
1514 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1515 }
1516
lowerINTRINSIC_WO_CHAIN(SDValue Op,SelectionDAG & DAG) const1517 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1518 SelectionDAG &DAG) const {
1519 SDLoc DL(Op);
1520
1521 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1522 default:
1523 return SDValue();
1524 case Intrinsic::mips_shilo:
1525 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1526 case Intrinsic::mips_dpau_h_qbl:
1527 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1528 case Intrinsic::mips_dpau_h_qbr:
1529 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1530 case Intrinsic::mips_dpsu_h_qbl:
1531 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1532 case Intrinsic::mips_dpsu_h_qbr:
1533 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1534 case Intrinsic::mips_dpa_w_ph:
1535 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1536 case Intrinsic::mips_dps_w_ph:
1537 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1538 case Intrinsic::mips_dpax_w_ph:
1539 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1540 case Intrinsic::mips_dpsx_w_ph:
1541 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1542 case Intrinsic::mips_mulsa_w_ph:
1543 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1544 case Intrinsic::mips_mult:
1545 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1546 case Intrinsic::mips_multu:
1547 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1548 case Intrinsic::mips_madd:
1549 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1550 case Intrinsic::mips_maddu:
1551 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1552 case Intrinsic::mips_msub:
1553 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1554 case Intrinsic::mips_msubu:
1555 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
1556 case Intrinsic::mips_addv_b:
1557 case Intrinsic::mips_addv_h:
1558 case Intrinsic::mips_addv_w:
1559 case Intrinsic::mips_addv_d:
1560 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1561 Op->getOperand(2));
1562 case Intrinsic::mips_addvi_b:
1563 case Intrinsic::mips_addvi_h:
1564 case Intrinsic::mips_addvi_w:
1565 case Intrinsic::mips_addvi_d:
1566 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1567 lowerMSASplatImm(Op, 2, DAG));
1568 case Intrinsic::mips_and_v:
1569 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1570 Op->getOperand(2));
1571 case Intrinsic::mips_andi_b:
1572 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1573 lowerMSASplatImm(Op, 2, DAG));
1574 case Intrinsic::mips_bclr_b:
1575 case Intrinsic::mips_bclr_h:
1576 case Intrinsic::mips_bclr_w:
1577 case Intrinsic::mips_bclr_d:
1578 return lowerMSABitClear(Op, DAG);
1579 case Intrinsic::mips_bclri_b:
1580 case Intrinsic::mips_bclri_h:
1581 case Intrinsic::mips_bclri_w:
1582 case Intrinsic::mips_bclri_d:
1583 return lowerMSABitClearImm(Op, DAG);
1584 case Intrinsic::mips_binsli_b:
1585 case Intrinsic::mips_binsli_h:
1586 case Intrinsic::mips_binsli_w:
1587 case Intrinsic::mips_binsli_d: {
1588 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
1589 EVT VecTy = Op->getValueType(0);
1590 EVT EltTy = VecTy.getVectorElementType();
1591 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
1592 Op->getConstantOperandVal(3));
1593 return DAG.getNode(ISD::VSELECT, DL, VecTy,
1594 DAG.getConstant(Mask, DL, VecTy, true),
1595 Op->getOperand(2), Op->getOperand(1));
1596 }
1597 case Intrinsic::mips_binsri_b:
1598 case Intrinsic::mips_binsri_h:
1599 case Intrinsic::mips_binsri_w:
1600 case Intrinsic::mips_binsri_d: {
1601 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
1602 EVT VecTy = Op->getValueType(0);
1603 EVT EltTy = VecTy.getVectorElementType();
1604 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
1605 Op->getConstantOperandVal(3));
1606 return DAG.getNode(ISD::VSELECT, DL, VecTy,
1607 DAG.getConstant(Mask, DL, VecTy, true),
1608 Op->getOperand(2), Op->getOperand(1));
1609 }
1610 case Intrinsic::mips_bmnz_v:
1611 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1612 Op->getOperand(2), Op->getOperand(1));
1613 case Intrinsic::mips_bmnzi_b:
1614 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1615 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1616 Op->getOperand(1));
1617 case Intrinsic::mips_bmz_v:
1618 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1619 Op->getOperand(1), Op->getOperand(2));
1620 case Intrinsic::mips_bmzi_b:
1621 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1622 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1623 Op->getOperand(2));
1624 case Intrinsic::mips_bneg_b:
1625 case Intrinsic::mips_bneg_h:
1626 case Intrinsic::mips_bneg_w:
1627 case Intrinsic::mips_bneg_d: {
1628 EVT VecTy = Op->getValueType(0);
1629 SDValue One = DAG.getConstant(1, DL, VecTy);
1630
1631 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1632 DAG.getNode(ISD::SHL, DL, VecTy, One,
1633 Op->getOperand(2)));
1634 }
1635 case Intrinsic::mips_bnegi_b:
1636 case Intrinsic::mips_bnegi_h:
1637 case Intrinsic::mips_bnegi_w:
1638 case Intrinsic::mips_bnegi_d:
1639 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
1640 !Subtarget.isLittle());
1641 case Intrinsic::mips_bnz_b:
1642 case Intrinsic::mips_bnz_h:
1643 case Intrinsic::mips_bnz_w:
1644 case Intrinsic::mips_bnz_d:
1645 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1646 Op->getOperand(1));
1647 case Intrinsic::mips_bnz_v:
1648 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1649 Op->getOperand(1));
1650 case Intrinsic::mips_bsel_v:
1651 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
1652 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1653 Op->getOperand(1), Op->getOperand(3),
1654 Op->getOperand(2));
1655 case Intrinsic::mips_bseli_b:
1656 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
1657 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1658 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1659 Op->getOperand(2));
1660 case Intrinsic::mips_bset_b:
1661 case Intrinsic::mips_bset_h:
1662 case Intrinsic::mips_bset_w:
1663 case Intrinsic::mips_bset_d: {
1664 EVT VecTy = Op->getValueType(0);
1665 SDValue One = DAG.getConstant(1, DL, VecTy);
1666
1667 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1668 DAG.getNode(ISD::SHL, DL, VecTy, One,
1669 Op->getOperand(2)));
1670 }
1671 case Intrinsic::mips_bseti_b:
1672 case Intrinsic::mips_bseti_h:
1673 case Intrinsic::mips_bseti_w:
1674 case Intrinsic::mips_bseti_d:
1675 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
1676 !Subtarget.isLittle());
1677 case Intrinsic::mips_bz_b:
1678 case Intrinsic::mips_bz_h:
1679 case Intrinsic::mips_bz_w:
1680 case Intrinsic::mips_bz_d:
1681 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1682 Op->getOperand(1));
1683 case Intrinsic::mips_bz_v:
1684 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1685 Op->getOperand(1));
1686 case Intrinsic::mips_ceq_b:
1687 case Intrinsic::mips_ceq_h:
1688 case Intrinsic::mips_ceq_w:
1689 case Intrinsic::mips_ceq_d:
1690 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1691 Op->getOperand(2), ISD::SETEQ);
1692 case Intrinsic::mips_ceqi_b:
1693 case Intrinsic::mips_ceqi_h:
1694 case Intrinsic::mips_ceqi_w:
1695 case Intrinsic::mips_ceqi_d:
1696 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1697 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1698 case Intrinsic::mips_cle_s_b:
1699 case Intrinsic::mips_cle_s_h:
1700 case Intrinsic::mips_cle_s_w:
1701 case Intrinsic::mips_cle_s_d:
1702 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1703 Op->getOperand(2), ISD::SETLE);
1704 case Intrinsic::mips_clei_s_b:
1705 case Intrinsic::mips_clei_s_h:
1706 case Intrinsic::mips_clei_s_w:
1707 case Intrinsic::mips_clei_s_d:
1708 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1709 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1710 case Intrinsic::mips_cle_u_b:
1711 case Intrinsic::mips_cle_u_h:
1712 case Intrinsic::mips_cle_u_w:
1713 case Intrinsic::mips_cle_u_d:
1714 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1715 Op->getOperand(2), ISD::SETULE);
1716 case Intrinsic::mips_clei_u_b:
1717 case Intrinsic::mips_clei_u_h:
1718 case Intrinsic::mips_clei_u_w:
1719 case Intrinsic::mips_clei_u_d:
1720 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1721 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1722 case Intrinsic::mips_clt_s_b:
1723 case Intrinsic::mips_clt_s_h:
1724 case Intrinsic::mips_clt_s_w:
1725 case Intrinsic::mips_clt_s_d:
1726 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1727 Op->getOperand(2), ISD::SETLT);
1728 case Intrinsic::mips_clti_s_b:
1729 case Intrinsic::mips_clti_s_h:
1730 case Intrinsic::mips_clti_s_w:
1731 case Intrinsic::mips_clti_s_d:
1732 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1733 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1734 case Intrinsic::mips_clt_u_b:
1735 case Intrinsic::mips_clt_u_h:
1736 case Intrinsic::mips_clt_u_w:
1737 case Intrinsic::mips_clt_u_d:
1738 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1739 Op->getOperand(2), ISD::SETULT);
1740 case Intrinsic::mips_clti_u_b:
1741 case Intrinsic::mips_clti_u_h:
1742 case Intrinsic::mips_clti_u_w:
1743 case Intrinsic::mips_clti_u_d:
1744 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1745 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
1746 case Intrinsic::mips_copy_s_b:
1747 case Intrinsic::mips_copy_s_h:
1748 case Intrinsic::mips_copy_s_w:
1749 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1750 case Intrinsic::mips_copy_s_d:
1751 if (Subtarget.hasMips64())
1752 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1753 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1754 else {
1755 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1756 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1757 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1758 Op->getValueType(0), Op->getOperand(1),
1759 Op->getOperand(2));
1760 }
1761 case Intrinsic::mips_copy_u_b:
1762 case Intrinsic::mips_copy_u_h:
1763 case Intrinsic::mips_copy_u_w:
1764 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1765 case Intrinsic::mips_copy_u_d:
1766 if (Subtarget.hasMips64())
1767 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1768 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1769 else {
1770 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1771 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1772 // Note: When i64 is illegal, this results in copy_s.w instructions
1773 // instead of copy_u.w instructions. This makes no difference to the
1774 // behaviour since i64 is only illegal when the register file is 32-bit.
1775 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1776 Op->getValueType(0), Op->getOperand(1),
1777 Op->getOperand(2));
1778 }
1779 case Intrinsic::mips_div_s_b:
1780 case Intrinsic::mips_div_s_h:
1781 case Intrinsic::mips_div_s_w:
1782 case Intrinsic::mips_div_s_d:
1783 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1784 Op->getOperand(2));
1785 case Intrinsic::mips_div_u_b:
1786 case Intrinsic::mips_div_u_h:
1787 case Intrinsic::mips_div_u_w:
1788 case Intrinsic::mips_div_u_d:
1789 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1790 Op->getOperand(2));
1791 case Intrinsic::mips_fadd_w:
1792 case Intrinsic::mips_fadd_d: {
1793 // TODO: If intrinsics have fast-math-flags, propagate them.
1794 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1795 Op->getOperand(2));
1796 }
1797 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1798 case Intrinsic::mips_fceq_w:
1799 case Intrinsic::mips_fceq_d:
1800 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1801 Op->getOperand(2), ISD::SETOEQ);
1802 case Intrinsic::mips_fcle_w:
1803 case Intrinsic::mips_fcle_d:
1804 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1805 Op->getOperand(2), ISD::SETOLE);
1806 case Intrinsic::mips_fclt_w:
1807 case Intrinsic::mips_fclt_d:
1808 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1809 Op->getOperand(2), ISD::SETOLT);
1810 case Intrinsic::mips_fcne_w:
1811 case Intrinsic::mips_fcne_d:
1812 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1813 Op->getOperand(2), ISD::SETONE);
1814 case Intrinsic::mips_fcor_w:
1815 case Intrinsic::mips_fcor_d:
1816 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1817 Op->getOperand(2), ISD::SETO);
1818 case Intrinsic::mips_fcueq_w:
1819 case Intrinsic::mips_fcueq_d:
1820 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1821 Op->getOperand(2), ISD::SETUEQ);
1822 case Intrinsic::mips_fcule_w:
1823 case Intrinsic::mips_fcule_d:
1824 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1825 Op->getOperand(2), ISD::SETULE);
1826 case Intrinsic::mips_fcult_w:
1827 case Intrinsic::mips_fcult_d:
1828 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1829 Op->getOperand(2), ISD::SETULT);
1830 case Intrinsic::mips_fcun_w:
1831 case Intrinsic::mips_fcun_d:
1832 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1833 Op->getOperand(2), ISD::SETUO);
1834 case Intrinsic::mips_fcune_w:
1835 case Intrinsic::mips_fcune_d:
1836 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1837 Op->getOperand(2), ISD::SETUNE);
1838 case Intrinsic::mips_fdiv_w:
1839 case Intrinsic::mips_fdiv_d: {
1840 // TODO: If intrinsics have fast-math-flags, propagate them.
1841 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1842 Op->getOperand(2));
1843 }
1844 case Intrinsic::mips_ffint_u_w:
1845 case Intrinsic::mips_ffint_u_d:
1846 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1847 Op->getOperand(1));
1848 case Intrinsic::mips_ffint_s_w:
1849 case Intrinsic::mips_ffint_s_d:
1850 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1851 Op->getOperand(1));
1852 case Intrinsic::mips_fill_b:
1853 case Intrinsic::mips_fill_h:
1854 case Intrinsic::mips_fill_w:
1855 case Intrinsic::mips_fill_d: {
1856 EVT ResTy = Op->getValueType(0);
1857 SmallVector<SDValue, 16> Ops(ResTy.getVectorNumElements(),
1858 Op->getOperand(1));
1859
1860 // If ResTy is v2i64 then the type legalizer will break this node down into
1861 // an equivalent v4i32.
1862 return DAG.getBuildVector(ResTy, DL, Ops);
1863 }
1864 case Intrinsic::mips_fexp2_w:
1865 case Intrinsic::mips_fexp2_d: {
1866 // TODO: If intrinsics have fast-math-flags, propagate them.
1867 EVT ResTy = Op->getValueType(0);
1868 return DAG.getNode(
1869 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1870 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1871 }
1872 case Intrinsic::mips_flog2_w:
1873 case Intrinsic::mips_flog2_d:
1874 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
1875 case Intrinsic::mips_fmadd_w:
1876 case Intrinsic::mips_fmadd_d:
1877 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1878 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
1879 case Intrinsic::mips_fmul_w:
1880 case Intrinsic::mips_fmul_d: {
1881 // TODO: If intrinsics have fast-math-flags, propagate them.
1882 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1883 Op->getOperand(2));
1884 }
1885 case Intrinsic::mips_fmsub_w:
1886 case Intrinsic::mips_fmsub_d: {
1887 // TODO: If intrinsics have fast-math-flags, propagate them.
1888 EVT ResTy = Op->getValueType(0);
1889 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1890 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1891 Op->getOperand(2), Op->getOperand(3)));
1892 }
1893 case Intrinsic::mips_frint_w:
1894 case Intrinsic::mips_frint_d:
1895 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
1896 case Intrinsic::mips_fsqrt_w:
1897 case Intrinsic::mips_fsqrt_d:
1898 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
1899 case Intrinsic::mips_fsub_w:
1900 case Intrinsic::mips_fsub_d: {
1901 // TODO: If intrinsics have fast-math-flags, propagate them.
1902 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1903 Op->getOperand(2));
1904 }
1905 case Intrinsic::mips_ftrunc_u_w:
1906 case Intrinsic::mips_ftrunc_u_d:
1907 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1908 Op->getOperand(1));
1909 case Intrinsic::mips_ftrunc_s_w:
1910 case Intrinsic::mips_ftrunc_s_d:
1911 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1912 Op->getOperand(1));
1913 case Intrinsic::mips_ilvev_b:
1914 case Intrinsic::mips_ilvev_h:
1915 case Intrinsic::mips_ilvev_w:
1916 case Intrinsic::mips_ilvev_d:
1917 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
1918 Op->getOperand(1), Op->getOperand(2));
1919 case Intrinsic::mips_ilvl_b:
1920 case Intrinsic::mips_ilvl_h:
1921 case Intrinsic::mips_ilvl_w:
1922 case Intrinsic::mips_ilvl_d:
1923 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
1924 Op->getOperand(1), Op->getOperand(2));
1925 case Intrinsic::mips_ilvod_b:
1926 case Intrinsic::mips_ilvod_h:
1927 case Intrinsic::mips_ilvod_w:
1928 case Intrinsic::mips_ilvod_d:
1929 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
1930 Op->getOperand(1), Op->getOperand(2));
1931 case Intrinsic::mips_ilvr_b:
1932 case Intrinsic::mips_ilvr_h:
1933 case Intrinsic::mips_ilvr_w:
1934 case Intrinsic::mips_ilvr_d:
1935 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
1936 Op->getOperand(1), Op->getOperand(2));
1937 case Intrinsic::mips_insert_b:
1938 case Intrinsic::mips_insert_h:
1939 case Intrinsic::mips_insert_w:
1940 case Intrinsic::mips_insert_d:
1941 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1942 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
1943 case Intrinsic::mips_insve_b:
1944 case Intrinsic::mips_insve_h:
1945 case Intrinsic::mips_insve_w:
1946 case Intrinsic::mips_insve_d:
1947 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1948 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
1949 DAG.getConstant(0, DL, MVT::i32));
1950 case Intrinsic::mips_ldi_b:
1951 case Intrinsic::mips_ldi_h:
1952 case Intrinsic::mips_ldi_w:
1953 case Intrinsic::mips_ldi_d:
1954 return lowerMSASplatImm(Op, 1, DAG);
1955 case Intrinsic::mips_lsa:
1956 case Intrinsic::mips_dlsa: {
1957 EVT ResTy = Op->getValueType(0);
1958 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1959 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1960 Op->getOperand(2), Op->getOperand(3)));
1961 }
1962 case Intrinsic::mips_maddv_b:
1963 case Intrinsic::mips_maddv_h:
1964 case Intrinsic::mips_maddv_w:
1965 case Intrinsic::mips_maddv_d: {
1966 EVT ResTy = Op->getValueType(0);
1967 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1968 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1969 Op->getOperand(2), Op->getOperand(3)));
1970 }
1971 case Intrinsic::mips_max_s_b:
1972 case Intrinsic::mips_max_s_h:
1973 case Intrinsic::mips_max_s_w:
1974 case Intrinsic::mips_max_s_d:
1975 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1976 Op->getOperand(1), Op->getOperand(2));
1977 case Intrinsic::mips_max_u_b:
1978 case Intrinsic::mips_max_u_h:
1979 case Intrinsic::mips_max_u_w:
1980 case Intrinsic::mips_max_u_d:
1981 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1982 Op->getOperand(1), Op->getOperand(2));
1983 case Intrinsic::mips_maxi_s_b:
1984 case Intrinsic::mips_maxi_s_h:
1985 case Intrinsic::mips_maxi_s_w:
1986 case Intrinsic::mips_maxi_s_d:
1987 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1988 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1989 case Intrinsic::mips_maxi_u_b:
1990 case Intrinsic::mips_maxi_u_h:
1991 case Intrinsic::mips_maxi_u_w:
1992 case Intrinsic::mips_maxi_u_d:
1993 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1994 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1995 case Intrinsic::mips_min_s_b:
1996 case Intrinsic::mips_min_s_h:
1997 case Intrinsic::mips_min_s_w:
1998 case Intrinsic::mips_min_s_d:
1999 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
2000 Op->getOperand(1), Op->getOperand(2));
2001 case Intrinsic::mips_min_u_b:
2002 case Intrinsic::mips_min_u_h:
2003 case Intrinsic::mips_min_u_w:
2004 case Intrinsic::mips_min_u_d:
2005 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
2006 Op->getOperand(1), Op->getOperand(2));
2007 case Intrinsic::mips_mini_s_b:
2008 case Intrinsic::mips_mini_s_h:
2009 case Intrinsic::mips_mini_s_w:
2010 case Intrinsic::mips_mini_s_d:
2011 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
2012 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2013 case Intrinsic::mips_mini_u_b:
2014 case Intrinsic::mips_mini_u_h:
2015 case Intrinsic::mips_mini_u_w:
2016 case Intrinsic::mips_mini_u_d:
2017 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
2018 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2019 case Intrinsic::mips_mod_s_b:
2020 case Intrinsic::mips_mod_s_h:
2021 case Intrinsic::mips_mod_s_w:
2022 case Intrinsic::mips_mod_s_d:
2023 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
2024 Op->getOperand(2));
2025 case Intrinsic::mips_mod_u_b:
2026 case Intrinsic::mips_mod_u_h:
2027 case Intrinsic::mips_mod_u_w:
2028 case Intrinsic::mips_mod_u_d:
2029 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
2030 Op->getOperand(2));
2031 case Intrinsic::mips_mulv_b:
2032 case Intrinsic::mips_mulv_h:
2033 case Intrinsic::mips_mulv_w:
2034 case Intrinsic::mips_mulv_d:
2035 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
2036 Op->getOperand(2));
2037 case Intrinsic::mips_msubv_b:
2038 case Intrinsic::mips_msubv_h:
2039 case Intrinsic::mips_msubv_w:
2040 case Intrinsic::mips_msubv_d: {
2041 EVT ResTy = Op->getValueType(0);
2042 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
2043 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2044 Op->getOperand(2), Op->getOperand(3)));
2045 }
2046 case Intrinsic::mips_nlzc_b:
2047 case Intrinsic::mips_nlzc_h:
2048 case Intrinsic::mips_nlzc_w:
2049 case Intrinsic::mips_nlzc_d:
2050 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
2051 case Intrinsic::mips_nor_v: {
2052 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2053 Op->getOperand(1), Op->getOperand(2));
2054 return DAG.getNOT(DL, Res, Res->getValueType(0));
2055 }
2056 case Intrinsic::mips_nori_b: {
2057 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2058 Op->getOperand(1),
2059 lowerMSASplatImm(Op, 2, DAG));
2060 return DAG.getNOT(DL, Res, Res->getValueType(0));
2061 }
2062 case Intrinsic::mips_or_v:
2063 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2064 Op->getOperand(2));
2065 case Intrinsic::mips_ori_b:
2066 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2067 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2068 case Intrinsic::mips_pckev_b:
2069 case Intrinsic::mips_pckev_h:
2070 case Intrinsic::mips_pckev_w:
2071 case Intrinsic::mips_pckev_d:
2072 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
2073 Op->getOperand(1), Op->getOperand(2));
2074 case Intrinsic::mips_pckod_b:
2075 case Intrinsic::mips_pckod_h:
2076 case Intrinsic::mips_pckod_w:
2077 case Intrinsic::mips_pckod_d:
2078 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
2079 Op->getOperand(1), Op->getOperand(2));
2080 case Intrinsic::mips_pcnt_b:
2081 case Intrinsic::mips_pcnt_h:
2082 case Intrinsic::mips_pcnt_w:
2083 case Intrinsic::mips_pcnt_d:
2084 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
2085 case Intrinsic::mips_shf_b:
2086 case Intrinsic::mips_shf_h:
2087 case Intrinsic::mips_shf_w:
2088 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
2089 Op->getOperand(2), Op->getOperand(1));
2090 case Intrinsic::mips_sll_b:
2091 case Intrinsic::mips_sll_h:
2092 case Intrinsic::mips_sll_w:
2093 case Intrinsic::mips_sll_d:
2094 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
2095 Op->getOperand(2));
2096 case Intrinsic::mips_slli_b:
2097 case Intrinsic::mips_slli_h:
2098 case Intrinsic::mips_slli_w:
2099 case Intrinsic::mips_slli_d:
2100 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2101 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2102 case Intrinsic::mips_splat_b:
2103 case Intrinsic::mips_splat_h:
2104 case Intrinsic::mips_splat_w:
2105 case Intrinsic::mips_splat_d:
2106 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2107 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2108 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2109 // Instead we lower to MipsISD::VSHF and match from there.
2110 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2111 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
2112 Op->getOperand(1));
2113 case Intrinsic::mips_splati_b:
2114 case Intrinsic::mips_splati_h:
2115 case Intrinsic::mips_splati_w:
2116 case Intrinsic::mips_splati_d:
2117 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2118 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2119 Op->getOperand(1));
2120 case Intrinsic::mips_sra_b:
2121 case Intrinsic::mips_sra_h:
2122 case Intrinsic::mips_sra_w:
2123 case Intrinsic::mips_sra_d:
2124 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
2125 Op->getOperand(2));
2126 case Intrinsic::mips_srai_b:
2127 case Intrinsic::mips_srai_h:
2128 case Intrinsic::mips_srai_w:
2129 case Intrinsic::mips_srai_d:
2130 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2131 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2132 case Intrinsic::mips_srl_b:
2133 case Intrinsic::mips_srl_h:
2134 case Intrinsic::mips_srl_w:
2135 case Intrinsic::mips_srl_d:
2136 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
2137 Op->getOperand(2));
2138 case Intrinsic::mips_srli_b:
2139 case Intrinsic::mips_srli_h:
2140 case Intrinsic::mips_srli_w:
2141 case Intrinsic::mips_srli_d:
2142 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2143 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2144 case Intrinsic::mips_subv_b:
2145 case Intrinsic::mips_subv_h:
2146 case Intrinsic::mips_subv_w:
2147 case Intrinsic::mips_subv_d:
2148 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2149 Op->getOperand(2));
2150 case Intrinsic::mips_subvi_b:
2151 case Intrinsic::mips_subvi_h:
2152 case Intrinsic::mips_subvi_w:
2153 case Intrinsic::mips_subvi_d:
2154 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2155 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2156 case Intrinsic::mips_vshf_b:
2157 case Intrinsic::mips_vshf_h:
2158 case Intrinsic::mips_vshf_w:
2159 case Intrinsic::mips_vshf_d:
2160 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2161 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
2162 case Intrinsic::mips_xor_v:
2163 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2164 Op->getOperand(2));
2165 case Intrinsic::mips_xori_b:
2166 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2167 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2168 case Intrinsic::thread_pointer: {
2169 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2170 return DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2171 }
2172 }
2173 }
2174
lowerMSALoadIntr(SDValue Op,SelectionDAG & DAG,unsigned Intr)2175 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2176 SDLoc DL(Op);
2177 SDValue ChainIn = Op->getOperand(0);
2178 SDValue Address = Op->getOperand(2);
2179 SDValue Offset = Op->getOperand(3);
2180 EVT ResTy = Op->getValueType(0);
2181 EVT PtrTy = Address->getValueType(0);
2182
2183 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2184
2185 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
2186 false, false, 16);
2187 }
2188
lowerINTRINSIC_W_CHAIN(SDValue Op,SelectionDAG & DAG) const2189 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2190 SelectionDAG &DAG) const {
2191 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2192 switch (Intr) {
2193 default:
2194 return SDValue();
2195 case Intrinsic::mips_extp:
2196 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2197 case Intrinsic::mips_extpdp:
2198 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2199 case Intrinsic::mips_extr_w:
2200 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2201 case Intrinsic::mips_extr_r_w:
2202 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2203 case Intrinsic::mips_extr_rs_w:
2204 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2205 case Intrinsic::mips_extr_s_h:
2206 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2207 case Intrinsic::mips_mthlip:
2208 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2209 case Intrinsic::mips_mulsaq_s_w_ph:
2210 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2211 case Intrinsic::mips_maq_s_w_phl:
2212 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2213 case Intrinsic::mips_maq_s_w_phr:
2214 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2215 case Intrinsic::mips_maq_sa_w_phl:
2216 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2217 case Intrinsic::mips_maq_sa_w_phr:
2218 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2219 case Intrinsic::mips_dpaq_s_w_ph:
2220 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2221 case Intrinsic::mips_dpsq_s_w_ph:
2222 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2223 case Intrinsic::mips_dpaq_sa_l_w:
2224 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2225 case Intrinsic::mips_dpsq_sa_l_w:
2226 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2227 case Intrinsic::mips_dpaqx_s_w_ph:
2228 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2229 case Intrinsic::mips_dpaqx_sa_w_ph:
2230 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2231 case Intrinsic::mips_dpsqx_s_w_ph:
2232 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2233 case Intrinsic::mips_dpsqx_sa_w_ph:
2234 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
2235 case Intrinsic::mips_ld_b:
2236 case Intrinsic::mips_ld_h:
2237 case Intrinsic::mips_ld_w:
2238 case Intrinsic::mips_ld_d:
2239 return lowerMSALoadIntr(Op, DAG, Intr);
2240 }
2241 }
2242
lowerMSAStoreIntr(SDValue Op,SelectionDAG & DAG,unsigned Intr)2243 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2244 SDLoc DL(Op);
2245 SDValue ChainIn = Op->getOperand(0);
2246 SDValue Value = Op->getOperand(2);
2247 SDValue Address = Op->getOperand(3);
2248 SDValue Offset = Op->getOperand(4);
2249 EVT PtrTy = Address->getValueType(0);
2250
2251 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2252
2253 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
2254 false, 16);
2255 }
2256
lowerINTRINSIC_VOID(SDValue Op,SelectionDAG & DAG) const2257 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2258 SelectionDAG &DAG) const {
2259 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2260 switch (Intr) {
2261 default:
2262 return SDValue();
2263 case Intrinsic::mips_st_b:
2264 case Intrinsic::mips_st_h:
2265 case Intrinsic::mips_st_w:
2266 case Intrinsic::mips_st_d:
2267 return lowerMSAStoreIntr(Op, DAG, Intr);
2268 }
2269 }
2270
2271 /// \brief Check if the given BuildVectorSDNode is a splat.
2272 /// This method currently relies on DAG nodes being reused when equivalent,
2273 /// so it's possible for this to return false even when isConstantSplat returns
2274 /// true.
isSplatVector(const BuildVectorSDNode * N)2275 static bool isSplatVector(const BuildVectorSDNode *N) {
2276 unsigned int nOps = N->getNumOperands();
2277 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
2278
2279 SDValue Operand0 = N->getOperand(0);
2280
2281 for (unsigned int i = 1; i < nOps; ++i) {
2282 if (N->getOperand(i) != Operand0)
2283 return false;
2284 }
2285
2286 return true;
2287 }
2288
2289 // Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2290 //
2291 // The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2292 // choose to sign-extend but we could have equally chosen zero-extend. The
2293 // DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2294 // result into this node later (possibly changing it to a zero-extend in the
2295 // process).
2296 SDValue MipsSETargetLowering::
lowerEXTRACT_VECTOR_ELT(SDValue Op,SelectionDAG & DAG) const2297 lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2298 SDLoc DL(Op);
2299 EVT ResTy = Op->getValueType(0);
2300 SDValue Op0 = Op->getOperand(0);
2301 EVT VecTy = Op0->getValueType(0);
2302
2303 if (!VecTy.is128BitVector())
2304 return SDValue();
2305
2306 if (ResTy.isInteger()) {
2307 SDValue Op1 = Op->getOperand(1);
2308 EVT EltTy = VecTy.getVectorElementType();
2309 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2310 DAG.getValueType(EltTy));
2311 }
2312
2313 return Op;
2314 }
2315
isConstantOrUndef(const SDValue Op)2316 static bool isConstantOrUndef(const SDValue Op) {
2317 if (Op->isUndef())
2318 return true;
2319 if (isa<ConstantSDNode>(Op))
2320 return true;
2321 if (isa<ConstantFPSDNode>(Op))
2322 return true;
2323 return false;
2324 }
2325
isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode * Op)2326 static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2327 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2328 if (isConstantOrUndef(Op->getOperand(i)))
2329 return true;
2330 return false;
2331 }
2332
2333 // Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2334 // backend.
2335 //
2336 // Lowers according to the following rules:
2337 // - Constant splats are legal as-is as long as the SplatBitSize is a power of
2338 // 2 less than or equal to 64 and the value fits into a signed 10-bit
2339 // immediate
2340 // - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2341 // is a power of 2 less than or equal to 64 and the value does not fit into a
2342 // signed 10-bit immediate
2343 // - Non-constant splats are legal as-is.
2344 // - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2345 // - All others are illegal and must be expanded.
lowerBUILD_VECTOR(SDValue Op,SelectionDAG & DAG) const2346 SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2347 SelectionDAG &DAG) const {
2348 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2349 EVT ResTy = Op->getValueType(0);
2350 SDLoc DL(Op);
2351 APInt SplatValue, SplatUndef;
2352 unsigned SplatBitSize;
2353 bool HasAnyUndefs;
2354
2355 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
2356 return SDValue();
2357
2358 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2359 HasAnyUndefs, 8,
2360 !Subtarget.isLittle()) && SplatBitSize <= 64) {
2361 // We can only cope with 8, 16, 32, or 64-bit elements
2362 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2363 SplatBitSize != 64)
2364 return SDValue();
2365
2366 // If the value fits into a simm10 then we can use ldi.[bhwd]
2367 // However, if it isn't an integer type we will have to bitcast from an
2368 // integer type first. Also, if there are any undefs, we must lower them
2369 // to defined values first.
2370 if (ResTy.isInteger() && !HasAnyUndefs && SplatValue.isSignedIntN(10))
2371 return Op;
2372
2373 EVT ViaVecTy;
2374
2375 switch (SplatBitSize) {
2376 default:
2377 return SDValue();
2378 case 8:
2379 ViaVecTy = MVT::v16i8;
2380 break;
2381 case 16:
2382 ViaVecTy = MVT::v8i16;
2383 break;
2384 case 32:
2385 ViaVecTy = MVT::v4i32;
2386 break;
2387 case 64:
2388 // There's no fill.d to fall back on for 64-bit values
2389 return SDValue();
2390 }
2391
2392 // SelectionDAG::getConstant will promote SplatValue appropriately.
2393 SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
2394
2395 // Bitcast to the type we originally wanted
2396 if (ViaVecTy != ResTy)
2397 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
2398
2399 return Result;
2400 } else if (isSplatVector(Node))
2401 return Op;
2402 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
2403 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2404 // The resulting code is the same length as the expansion, but it doesn't
2405 // use memory operations
2406 EVT ResTy = Node->getValueType(0);
2407
2408 assert(ResTy.isVector());
2409
2410 unsigned NumElts = ResTy.getVectorNumElements();
2411 SDValue Vector = DAG.getUNDEF(ResTy);
2412 for (unsigned i = 0; i < NumElts; ++i) {
2413 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2414 Node->getOperand(i),
2415 DAG.getConstant(i, DL, MVT::i32));
2416 }
2417 return Vector;
2418 }
2419
2420 return SDValue();
2421 }
2422
2423 // Lower VECTOR_SHUFFLE into SHF (if possible).
2424 //
2425 // SHF splits the vector into blocks of four elements, then shuffles these
2426 // elements according to a <4 x i2> constant (encoded as an integer immediate).
2427 //
2428 // It is therefore possible to lower into SHF when the mask takes the form:
2429 // <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2430 // When undef's appear they are treated as if they were whatever value is
2431 // necessary in order to fit the above forms.
2432 //
2433 // For example:
2434 // %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2435 // <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2436 // i32 7, i32 6, i32 5, i32 4>
2437 // is lowered to:
2438 // (SHF_H $w0, $w1, 27)
2439 // where the 27 comes from:
2440 // 3 + (2 << 2) + (1 << 4) + (0 << 6)
lowerVECTOR_SHUFFLE_SHF(SDValue Op,EVT ResTy,SmallVector<int,16> Indices,SelectionDAG & DAG)2441 static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2442 SmallVector<int, 16> Indices,
2443 SelectionDAG &DAG) {
2444 int SHFIndices[4] = { -1, -1, -1, -1 };
2445
2446 if (Indices.size() < 4)
2447 return SDValue();
2448
2449 for (unsigned i = 0; i < 4; ++i) {
2450 for (unsigned j = i; j < Indices.size(); j += 4) {
2451 int Idx = Indices[j];
2452
2453 // Convert from vector index to 4-element subvector index
2454 // If an index refers to an element outside of the subvector then give up
2455 if (Idx != -1) {
2456 Idx -= 4 * (j / 4);
2457 if (Idx < 0 || Idx >= 4)
2458 return SDValue();
2459 }
2460
2461 // If the mask has an undef, replace it with the current index.
2462 // Note that it might still be undef if the current index is also undef
2463 if (SHFIndices[i] == -1)
2464 SHFIndices[i] = Idx;
2465
2466 // Check that non-undef values are the same as in the mask. If they
2467 // aren't then give up
2468 if (!(Idx == -1 || Idx == SHFIndices[i]))
2469 return SDValue();
2470 }
2471 }
2472
2473 // Calculate the immediate. Replace any remaining undefs with zero
2474 APInt Imm(32, 0);
2475 for (int i = 3; i >= 0; --i) {
2476 int Idx = SHFIndices[i];
2477
2478 if (Idx == -1)
2479 Idx = 0;
2480
2481 Imm <<= 2;
2482 Imm |= Idx & 0x3;
2483 }
2484
2485 SDLoc DL(Op);
2486 return DAG.getNode(MipsISD::SHF, DL, ResTy,
2487 DAG.getConstant(Imm, DL, MVT::i32), Op->getOperand(0));
2488 }
2489
2490 /// Determine whether a range fits a regular pattern of values.
2491 /// This function accounts for the possibility of jumping over the End iterator.
2492 template <typename ValType>
2493 static bool
fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,unsigned CheckStride,typename SmallVectorImpl<ValType>::const_iterator End,ValType ExpectedIndex,unsigned ExpectedIndexStride)2494 fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,
2495 unsigned CheckStride,
2496 typename SmallVectorImpl<ValType>::const_iterator End,
2497 ValType ExpectedIndex, unsigned ExpectedIndexStride) {
2498 auto &I = Begin;
2499
2500 while (I != End) {
2501 if (*I != -1 && *I != ExpectedIndex)
2502 return false;
2503 ExpectedIndex += ExpectedIndexStride;
2504
2505 // Incrementing past End is undefined behaviour so we must increment one
2506 // step at a time and check for End at each step.
2507 for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
2508 ; // Empty loop body.
2509 }
2510 return true;
2511 }
2512
2513 // Determine whether VECTOR_SHUFFLE is a SPLATI.
2514 //
2515 // It is a SPLATI when the mask is:
2516 // <x, x, x, ...>
2517 // where x is any valid index.
2518 //
2519 // When undef's appear in the mask they are treated as if they were whatever
2520 // value is necessary in order to fit the above form.
isVECTOR_SHUFFLE_SPLATI(SDValue Op,EVT ResTy,SmallVector<int,16> Indices,SelectionDAG & DAG)2521 static bool isVECTOR_SHUFFLE_SPLATI(SDValue Op, EVT ResTy,
2522 SmallVector<int, 16> Indices,
2523 SelectionDAG &DAG) {
2524 assert((Indices.size() % 2) == 0);
2525
2526 int SplatIndex = -1;
2527 for (const auto &V : Indices) {
2528 if (V != -1) {
2529 SplatIndex = V;
2530 break;
2531 }
2532 }
2533
2534 return fitsRegularPattern<int>(Indices.begin(), 1, Indices.end(), SplatIndex,
2535 0);
2536 }
2537
2538 // Lower VECTOR_SHUFFLE into ILVEV (if possible).
2539 //
2540 // ILVEV interleaves the even elements from each vector.
2541 //
2542 // It is possible to lower into ILVEV when the mask consists of two of the
2543 // following forms interleaved:
2544 // <0, 2, 4, ...>
2545 // <n, n+2, n+4, ...>
2546 // where n is the number of elements in the vector.
2547 // For example:
2548 // <0, 0, 2, 2, 4, 4, ...>
2549 // <0, n, 2, n+2, 4, n+4, ...>
2550 //
2551 // When undef's appear in the mask they are treated as if they were whatever
2552 // value is necessary in order to fit the above forms.
lowerVECTOR_SHUFFLE_ILVEV(SDValue Op,EVT ResTy,SmallVector<int,16> Indices,SelectionDAG & DAG)2553 static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2554 SmallVector<int, 16> Indices,
2555 SelectionDAG &DAG) {
2556 assert((Indices.size() % 2) == 0);
2557
2558 SDValue Wt;
2559 SDValue Ws;
2560 const auto &Begin = Indices.begin();
2561 const auto &End = Indices.end();
2562
2563 // Check even elements are taken from the even elements of one half or the
2564 // other and pick an operand accordingly.
2565 if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
2566 Wt = Op->getOperand(0);
2567 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 2))
2568 Wt = Op->getOperand(1);
2569 else
2570 return SDValue();
2571
2572 // Check odd elements are taken from the even elements of one half or the
2573 // other and pick an operand accordingly.
2574 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
2575 Ws = Op->getOperand(0);
2576 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 2))
2577 Ws = Op->getOperand(1);
2578 else
2579 return SDValue();
2580
2581 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Ws, Wt);
2582 }
2583
2584 // Lower VECTOR_SHUFFLE into ILVOD (if possible).
2585 //
2586 // ILVOD interleaves the odd elements from each vector.
2587 //
2588 // It is possible to lower into ILVOD when the mask consists of two of the
2589 // following forms interleaved:
2590 // <1, 3, 5, ...>
2591 // <n+1, n+3, n+5, ...>
2592 // where n is the number of elements in the vector.
2593 // For example:
2594 // <1, 1, 3, 3, 5, 5, ...>
2595 // <1, n+1, 3, n+3, 5, n+5, ...>
2596 //
2597 // When undef's appear in the mask they are treated as if they were whatever
2598 // value is necessary in order to fit the above forms.
lowerVECTOR_SHUFFLE_ILVOD(SDValue Op,EVT ResTy,SmallVector<int,16> Indices,SelectionDAG & DAG)2599 static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2600 SmallVector<int, 16> Indices,
2601 SelectionDAG &DAG) {
2602 assert((Indices.size() % 2) == 0);
2603
2604 SDValue Wt;
2605 SDValue Ws;
2606 const auto &Begin = Indices.begin();
2607 const auto &End = Indices.end();
2608
2609 // Check even elements are taken from the odd elements of one half or the
2610 // other and pick an operand accordingly.
2611 if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
2612 Wt = Op->getOperand(0);
2613 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + 1, 2))
2614 Wt = Op->getOperand(1);
2615 else
2616 return SDValue();
2617
2618 // Check odd elements are taken from the odd elements of one half or the
2619 // other and pick an operand accordingly.
2620 if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
2621 Ws = Op->getOperand(0);
2622 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + 1, 2))
2623 Ws = Op->getOperand(1);
2624 else
2625 return SDValue();
2626
2627 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Wt, Ws);
2628 }
2629
2630 // Lower VECTOR_SHUFFLE into ILVR (if possible).
2631 //
2632 // ILVR interleaves consecutive elements from the right (lowest-indexed) half of
2633 // each vector.
2634 //
2635 // It is possible to lower into ILVR when the mask consists of two of the
2636 // following forms interleaved:
2637 // <0, 1, 2, ...>
2638 // <n, n+1, n+2, ...>
2639 // where n is the number of elements in the vector.
2640 // For example:
2641 // <0, 0, 1, 1, 2, 2, ...>
2642 // <0, n, 1, n+1, 2, n+2, ...>
2643 //
2644 // When undef's appear in the mask they are treated as if they were whatever
2645 // value is necessary in order to fit the above forms.
lowerVECTOR_SHUFFLE_ILVR(SDValue Op,EVT ResTy,SmallVector<int,16> Indices,SelectionDAG & DAG)2646 static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2647 SmallVector<int, 16> Indices,
2648 SelectionDAG &DAG) {
2649 assert((Indices.size() % 2) == 0);
2650
2651 SDValue Wt;
2652 SDValue Ws;
2653 const auto &Begin = Indices.begin();
2654 const auto &End = Indices.end();
2655
2656 // Check even elements are taken from the right (lowest-indexed) elements of
2657 // one half or the other and pick an operand accordingly.
2658 if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
2659 Wt = Op->getOperand(0);
2660 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 1))
2661 Wt = Op->getOperand(1);
2662 else
2663 return SDValue();
2664
2665 // Check odd elements are taken from the right (lowest-indexed) elements of
2666 // one half or the other and pick an operand accordingly.
2667 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
2668 Ws = Op->getOperand(0);
2669 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 1))
2670 Ws = Op->getOperand(1);
2671 else
2672 return SDValue();
2673
2674 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Ws, Wt);
2675 }
2676
2677 // Lower VECTOR_SHUFFLE into ILVL (if possible).
2678 //
2679 // ILVL interleaves consecutive elements from the left (highest-indexed) half
2680 // of each vector.
2681 //
2682 // It is possible to lower into ILVL when the mask consists of two of the
2683 // following forms interleaved:
2684 // <x, x+1, x+2, ...>
2685 // <n+x, n+x+1, n+x+2, ...>
2686 // where n is the number of elements in the vector and x is half n.
2687 // For example:
2688 // <x, x, x+1, x+1, x+2, x+2, ...>
2689 // <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2690 //
2691 // When undef's appear in the mask they are treated as if they were whatever
2692 // value is necessary in order to fit the above forms.
lowerVECTOR_SHUFFLE_ILVL(SDValue Op,EVT ResTy,SmallVector<int,16> Indices,SelectionDAG & DAG)2693 static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2694 SmallVector<int, 16> Indices,
2695 SelectionDAG &DAG) {
2696 assert((Indices.size() % 2) == 0);
2697
2698 unsigned HalfSize = Indices.size() / 2;
2699 SDValue Wt;
2700 SDValue Ws;
2701 const auto &Begin = Indices.begin();
2702 const auto &End = Indices.end();
2703
2704 // Check even elements are taken from the left (highest-indexed) elements of
2705 // one half or the other and pick an operand accordingly.
2706 if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
2707 Wt = Op->getOperand(0);
2708 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + HalfSize, 1))
2709 Wt = Op->getOperand(1);
2710 else
2711 return SDValue();
2712
2713 // Check odd elements are taken from the left (highest-indexed) elements of
2714 // one half or the other and pick an operand accordingly.
2715 if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
2716 Ws = Op->getOperand(0);
2717 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + HalfSize,
2718 1))
2719 Ws = Op->getOperand(1);
2720 else
2721 return SDValue();
2722
2723 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Ws, Wt);
2724 }
2725
2726 // Lower VECTOR_SHUFFLE into PCKEV (if possible).
2727 //
2728 // PCKEV copies the even elements of each vector into the result vector.
2729 //
2730 // It is possible to lower into PCKEV when the mask consists of two of the
2731 // following forms concatenated:
2732 // <0, 2, 4, ...>
2733 // <n, n+2, n+4, ...>
2734 // where n is the number of elements in the vector.
2735 // For example:
2736 // <0, 2, 4, ..., 0, 2, 4, ...>
2737 // <0, 2, 4, ..., n, n+2, n+4, ...>
2738 //
2739 // When undef's appear in the mask they are treated as if they were whatever
2740 // value is necessary in order to fit the above forms.
lowerVECTOR_SHUFFLE_PCKEV(SDValue Op,EVT ResTy,SmallVector<int,16> Indices,SelectionDAG & DAG)2741 static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2742 SmallVector<int, 16> Indices,
2743 SelectionDAG &DAG) {
2744 assert((Indices.size() % 2) == 0);
2745
2746 SDValue Wt;
2747 SDValue Ws;
2748 const auto &Begin = Indices.begin();
2749 const auto &Mid = Indices.begin() + Indices.size() / 2;
2750 const auto &End = Indices.end();
2751
2752 if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
2753 Wt = Op->getOperand(0);
2754 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size(), 2))
2755 Wt = Op->getOperand(1);
2756 else
2757 return SDValue();
2758
2759 if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
2760 Ws = Op->getOperand(0);
2761 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size(), 2))
2762 Ws = Op->getOperand(1);
2763 else
2764 return SDValue();
2765
2766 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Ws, Wt);
2767 }
2768
2769 // Lower VECTOR_SHUFFLE into PCKOD (if possible).
2770 //
2771 // PCKOD copies the odd elements of each vector into the result vector.
2772 //
2773 // It is possible to lower into PCKOD when the mask consists of two of the
2774 // following forms concatenated:
2775 // <1, 3, 5, ...>
2776 // <n+1, n+3, n+5, ...>
2777 // where n is the number of elements in the vector.
2778 // For example:
2779 // <1, 3, 5, ..., 1, 3, 5, ...>
2780 // <1, 3, 5, ..., n+1, n+3, n+5, ...>
2781 //
2782 // When undef's appear in the mask they are treated as if they were whatever
2783 // value is necessary in order to fit the above forms.
lowerVECTOR_SHUFFLE_PCKOD(SDValue Op,EVT ResTy,SmallVector<int,16> Indices,SelectionDAG & DAG)2784 static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2785 SmallVector<int, 16> Indices,
2786 SelectionDAG &DAG) {
2787 assert((Indices.size() % 2) == 0);
2788
2789 SDValue Wt;
2790 SDValue Ws;
2791 const auto &Begin = Indices.begin();
2792 const auto &Mid = Indices.begin() + Indices.size() / 2;
2793 const auto &End = Indices.end();
2794
2795 if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
2796 Wt = Op->getOperand(0);
2797 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size() + 1, 2))
2798 Wt = Op->getOperand(1);
2799 else
2800 return SDValue();
2801
2802 if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
2803 Ws = Op->getOperand(0);
2804 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size() + 1, 2))
2805 Ws = Op->getOperand(1);
2806 else
2807 return SDValue();
2808
2809 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Ws, Wt);
2810 }
2811
2812 // Lower VECTOR_SHUFFLE into VSHF.
2813 //
2814 // This mostly consists of converting the shuffle indices in Indices into a
2815 // BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2816 // also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2817 // if the type is v8i16 and all the indices are less than 8 then the second
2818 // operand is unused and can be replaced with anything. We choose to replace it
2819 // with the used operand since this reduces the number of instructions overall.
lowerVECTOR_SHUFFLE_VSHF(SDValue Op,EVT ResTy,SmallVector<int,16> Indices,SelectionDAG & DAG)2820 static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2821 SmallVector<int, 16> Indices,
2822 SelectionDAG &DAG) {
2823 SmallVector<SDValue, 16> Ops;
2824 SDValue Op0;
2825 SDValue Op1;
2826 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2827 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2828 bool Using1stVec = false;
2829 bool Using2ndVec = false;
2830 SDLoc DL(Op);
2831 int ResTyNumElts = ResTy.getVectorNumElements();
2832
2833 for (int i = 0; i < ResTyNumElts; ++i) {
2834 // Idx == -1 means UNDEF
2835 int Idx = Indices[i];
2836
2837 if (0 <= Idx && Idx < ResTyNumElts)
2838 Using1stVec = true;
2839 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2840 Using2ndVec = true;
2841 }
2842
2843 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2844 ++I)
2845 Ops.push_back(DAG.getTargetConstant(*I, DL, MaskEltTy));
2846
2847 SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
2848
2849 if (Using1stVec && Using2ndVec) {
2850 Op0 = Op->getOperand(0);
2851 Op1 = Op->getOperand(1);
2852 } else if (Using1stVec)
2853 Op0 = Op1 = Op->getOperand(0);
2854 else if (Using2ndVec)
2855 Op0 = Op1 = Op->getOperand(1);
2856 else
2857 llvm_unreachable("shuffle vector mask references neither vector operand?");
2858
2859 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2860 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2861 // VSHF concatenates the vectors in a bitwise fashion:
2862 // <0b00, 0b01> + <0b10, 0b11> ->
2863 // 0b0100 + 0b1110 -> 0b01001110
2864 // <0b10, 0b11, 0b00, 0b01>
2865 // We must therefore swap the operands to get the correct result.
2866 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
2867 }
2868
2869 // Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2870 // indices in the shuffle.
lowerVECTOR_SHUFFLE(SDValue Op,SelectionDAG & DAG) const2871 SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2872 SelectionDAG &DAG) const {
2873 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2874 EVT ResTy = Op->getValueType(0);
2875
2876 if (!ResTy.is128BitVector())
2877 return SDValue();
2878
2879 int ResTyNumElts = ResTy.getVectorNumElements();
2880 SmallVector<int, 16> Indices;
2881
2882 for (int i = 0; i < ResTyNumElts; ++i)
2883 Indices.push_back(Node->getMaskElt(i));
2884
2885 // splati.[bhwd] is preferable to the others but is matched from
2886 // MipsISD::VSHF.
2887 if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
2888 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2889 SDValue Result;
2890 if ((Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG)))
2891 return Result;
2892 if ((Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG)))
2893 return Result;
2894 if ((Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG)))
2895 return Result;
2896 if ((Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG)))
2897 return Result;
2898 if ((Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG)))
2899 return Result;
2900 if ((Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG)))
2901 return Result;
2902 if ((Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG)))
2903 return Result;
2904 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2905 }
2906
2907 MachineBasicBlock *
emitBPOSGE32(MachineInstr & MI,MachineBasicBlock * BB) const2908 MipsSETargetLowering::emitBPOSGE32(MachineInstr &MI,
2909 MachineBasicBlock *BB) const {
2910 // $bb:
2911 // bposge32_pseudo $vr0
2912 // =>
2913 // $bb:
2914 // bposge32 $tbb
2915 // $fbb:
2916 // li $vr2, 0
2917 // b $sink
2918 // $tbb:
2919 // li $vr1, 1
2920 // $sink:
2921 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2922
2923 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2924 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
2925 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2926 DebugLoc DL = MI.getDebugLoc();
2927 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2928 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
2929 MachineFunction *F = BB->getParent();
2930 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2931 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2932 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2933 F->insert(It, FBB);
2934 F->insert(It, TBB);
2935 F->insert(It, Sink);
2936
2937 // Transfer the remainder of BB and its successor edges to Sink.
2938 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
2939 BB->end());
2940 Sink->transferSuccessorsAndUpdatePHIs(BB);
2941
2942 // Add successors.
2943 BB->addSuccessor(FBB);
2944 BB->addSuccessor(TBB);
2945 FBB->addSuccessor(Sink);
2946 TBB->addSuccessor(Sink);
2947
2948 // Insert the real bposge32 instruction to $BB.
2949 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2950 // Insert the real bposge32c instruction to $BB.
2951 BuildMI(BB, DL, TII->get(Mips::BPOSGE32C_MMR3)).addMBB(TBB);
2952
2953 // Fill $FBB.
2954 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2955 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2956 .addReg(Mips::ZERO).addImm(0);
2957 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2958
2959 // Fill $TBB.
2960 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2961 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2962 .addReg(Mips::ZERO).addImm(1);
2963
2964 // Insert phi function to $Sink.
2965 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2966 MI.getOperand(0).getReg())
2967 .addReg(VR2)
2968 .addMBB(FBB)
2969 .addReg(VR1)
2970 .addMBB(TBB);
2971
2972 MI.eraseFromParent(); // The pseudo instruction is gone now.
2973 return Sink;
2974 }
2975
emitMSACBranchPseudo(MachineInstr & MI,MachineBasicBlock * BB,unsigned BranchOp) const2976 MachineBasicBlock *MipsSETargetLowering::emitMSACBranchPseudo(
2977 MachineInstr &MI, MachineBasicBlock *BB, unsigned BranchOp) const {
2978 // $bb:
2979 // vany_nonzero $rd, $ws
2980 // =>
2981 // $bb:
2982 // bnz.b $ws, $tbb
2983 // b $fbb
2984 // $fbb:
2985 // li $rd1, 0
2986 // b $sink
2987 // $tbb:
2988 // li $rd2, 1
2989 // $sink:
2990 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2991
2992 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2993 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
2994 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2995 DebugLoc DL = MI.getDebugLoc();
2996 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2997 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
2998 MachineFunction *F = BB->getParent();
2999 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3000 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3001 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3002 F->insert(It, FBB);
3003 F->insert(It, TBB);
3004 F->insert(It, Sink);
3005
3006 // Transfer the remainder of BB and its successor edges to Sink.
3007 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
3008 BB->end());
3009 Sink->transferSuccessorsAndUpdatePHIs(BB);
3010
3011 // Add successors.
3012 BB->addSuccessor(FBB);
3013 BB->addSuccessor(TBB);
3014 FBB->addSuccessor(Sink);
3015 TBB->addSuccessor(Sink);
3016
3017 // Insert the real bnz.b instruction to $BB.
3018 BuildMI(BB, DL, TII->get(BranchOp))
3019 .addReg(MI.getOperand(1).getReg())
3020 .addMBB(TBB);
3021
3022 // Fill $FBB.
3023 unsigned RD1 = RegInfo.createVirtualRegister(RC);
3024 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
3025 .addReg(Mips::ZERO).addImm(0);
3026 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3027
3028 // Fill $TBB.
3029 unsigned RD2 = RegInfo.createVirtualRegister(RC);
3030 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
3031 .addReg(Mips::ZERO).addImm(1);
3032
3033 // Insert phi function to $Sink.
3034 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
3035 MI.getOperand(0).getReg())
3036 .addReg(RD1)
3037 .addMBB(FBB)
3038 .addReg(RD2)
3039 .addMBB(TBB);
3040
3041 MI.eraseFromParent(); // The pseudo instruction is gone now.
3042 return Sink;
3043 }
3044
3045 // Emit the COPY_FW pseudo instruction.
3046 //
3047 // copy_fw_pseudo $fd, $ws, n
3048 // =>
3049 // copy_u_w $rt, $ws, $n
3050 // mtc1 $rt, $fd
3051 //
3052 // When n is zero, the equivalent operation can be performed with (potentially)
3053 // zero instructions due to register overlaps. This optimization is never valid
3054 // for lane 1 because it would require FR=0 mode which isn't supported by MSA.
3055 MachineBasicBlock *
emitCOPY_FW(MachineInstr & MI,MachineBasicBlock * BB) const3056 MipsSETargetLowering::emitCOPY_FW(MachineInstr &MI,
3057 MachineBasicBlock *BB) const {
3058 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3059 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3060 DebugLoc DL = MI.getDebugLoc();
3061 unsigned Fd = MI.getOperand(0).getReg();
3062 unsigned Ws = MI.getOperand(1).getReg();
3063 unsigned Lane = MI.getOperand(2).getImm();
3064
3065 if (Lane == 0) {
3066 unsigned Wt = Ws;
3067 if (!Subtarget.useOddSPReg()) {
3068 // We must copy to an even-numbered MSA register so that the
3069 // single-precision sub-register is also guaranteed to be even-numbered.
3070 Wt = RegInfo.createVirtualRegister(&Mips::MSA128WEvensRegClass);
3071
3072 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Wt).addReg(Ws);
3073 }
3074
3075 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3076 } else {
3077 unsigned Wt = RegInfo.createVirtualRegister(
3078 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3079 &Mips::MSA128WEvensRegClass);
3080
3081 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
3082 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3083 }
3084
3085 MI.eraseFromParent(); // The pseudo instruction is gone now.
3086 return BB;
3087 }
3088
3089 // Emit the COPY_FD pseudo instruction.
3090 //
3091 // copy_fd_pseudo $fd, $ws, n
3092 // =>
3093 // splati.d $wt, $ws, $n
3094 // copy $fd, $wt:sub_64
3095 //
3096 // When n is zero, the equivalent operation can be performed with (potentially)
3097 // zero instructions due to register overlaps. This optimization is always
3098 // valid because FR=1 mode which is the only supported mode in MSA.
3099 MachineBasicBlock *
emitCOPY_FD(MachineInstr & MI,MachineBasicBlock * BB) const3100 MipsSETargetLowering::emitCOPY_FD(MachineInstr &MI,
3101 MachineBasicBlock *BB) const {
3102 assert(Subtarget.isFP64bit());
3103
3104 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3105 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3106 unsigned Fd = MI.getOperand(0).getReg();
3107 unsigned Ws = MI.getOperand(1).getReg();
3108 unsigned Lane = MI.getOperand(2).getImm() * 2;
3109 DebugLoc DL = MI.getDebugLoc();
3110
3111 if (Lane == 0)
3112 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
3113 else {
3114 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3115
3116 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
3117 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
3118 }
3119
3120 MI.eraseFromParent(); // The pseudo instruction is gone now.
3121 return BB;
3122 }
3123
3124 // Emit the INSERT_FW pseudo instruction.
3125 //
3126 // insert_fw_pseudo $wd, $wd_in, $n, $fs
3127 // =>
3128 // subreg_to_reg $wt:sub_lo, $fs
3129 // insve_w $wd[$n], $wd_in, $wt[0]
3130 MachineBasicBlock *
emitINSERT_FW(MachineInstr & MI,MachineBasicBlock * BB) const3131 MipsSETargetLowering::emitINSERT_FW(MachineInstr &MI,
3132 MachineBasicBlock *BB) const {
3133 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3134 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3135 DebugLoc DL = MI.getDebugLoc();
3136 unsigned Wd = MI.getOperand(0).getReg();
3137 unsigned Wd_in = MI.getOperand(1).getReg();
3138 unsigned Lane = MI.getOperand(2).getImm();
3139 unsigned Fs = MI.getOperand(3).getReg();
3140 unsigned Wt = RegInfo.createVirtualRegister(
3141 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3142 &Mips::MSA128WEvensRegClass);
3143
3144 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3145 .addImm(0)
3146 .addReg(Fs)
3147 .addImm(Mips::sub_lo);
3148 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
3149 .addReg(Wd_in)
3150 .addImm(Lane)
3151 .addReg(Wt)
3152 .addImm(0);
3153
3154 MI.eraseFromParent(); // The pseudo instruction is gone now.
3155 return BB;
3156 }
3157
3158 // Emit the INSERT_FD pseudo instruction.
3159 //
3160 // insert_fd_pseudo $wd, $fs, n
3161 // =>
3162 // subreg_to_reg $wt:sub_64, $fs
3163 // insve_d $wd[$n], $wd_in, $wt[0]
3164 MachineBasicBlock *
emitINSERT_FD(MachineInstr & MI,MachineBasicBlock * BB) const3165 MipsSETargetLowering::emitINSERT_FD(MachineInstr &MI,
3166 MachineBasicBlock *BB) const {
3167 assert(Subtarget.isFP64bit());
3168
3169 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3170 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3171 DebugLoc DL = MI.getDebugLoc();
3172 unsigned Wd = MI.getOperand(0).getReg();
3173 unsigned Wd_in = MI.getOperand(1).getReg();
3174 unsigned Lane = MI.getOperand(2).getImm();
3175 unsigned Fs = MI.getOperand(3).getReg();
3176 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3177
3178 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3179 .addImm(0)
3180 .addReg(Fs)
3181 .addImm(Mips::sub_64);
3182 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
3183 .addReg(Wd_in)
3184 .addImm(Lane)
3185 .addReg(Wt)
3186 .addImm(0);
3187
3188 MI.eraseFromParent(); // The pseudo instruction is gone now.
3189 return BB;
3190 }
3191
3192 // Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3193 //
3194 // For integer:
3195 // (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3196 // =>
3197 // (SLL $lanetmp1, $lane, <log2size)
3198 // (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3199 // (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3200 // (NEG $lanetmp2, $lanetmp1)
3201 // (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3202 //
3203 // For floating point:
3204 // (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3205 // =>
3206 // (SUBREG_TO_REG $wt, $fs, <subreg>)
3207 // (SLL $lanetmp1, $lane, <log2size)
3208 // (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3209 // (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3210 // (NEG $lanetmp2, $lanetmp1)
3211 // (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
emitINSERT_DF_VIDX(MachineInstr & MI,MachineBasicBlock * BB,unsigned EltSizeInBytes,bool IsFP) const3212 MachineBasicBlock *MipsSETargetLowering::emitINSERT_DF_VIDX(
3213 MachineInstr &MI, MachineBasicBlock *BB, unsigned EltSizeInBytes,
3214 bool IsFP) const {
3215 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3216 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3217 DebugLoc DL = MI.getDebugLoc();
3218 unsigned Wd = MI.getOperand(0).getReg();
3219 unsigned SrcVecReg = MI.getOperand(1).getReg();
3220 unsigned LaneReg = MI.getOperand(2).getReg();
3221 unsigned SrcValReg = MI.getOperand(3).getReg();
3222
3223 const TargetRegisterClass *VecRC = nullptr;
3224 // FIXME: This should be true for N32 too.
3225 const TargetRegisterClass *GPRRC =
3226 Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
3227 unsigned SubRegIdx = Subtarget.isABI_N64() ? Mips::sub_32 : 0;
3228 unsigned ShiftOp = Subtarget.isABI_N64() ? Mips::DSLL : Mips::SLL;
3229 unsigned EltLog2Size;
3230 unsigned InsertOp = 0;
3231 unsigned InsveOp = 0;
3232 switch (EltSizeInBytes) {
3233 default:
3234 llvm_unreachable("Unexpected size");
3235 case 1:
3236 EltLog2Size = 0;
3237 InsertOp = Mips::INSERT_B;
3238 InsveOp = Mips::INSVE_B;
3239 VecRC = &Mips::MSA128BRegClass;
3240 break;
3241 case 2:
3242 EltLog2Size = 1;
3243 InsertOp = Mips::INSERT_H;
3244 InsveOp = Mips::INSVE_H;
3245 VecRC = &Mips::MSA128HRegClass;
3246 break;
3247 case 4:
3248 EltLog2Size = 2;
3249 InsertOp = Mips::INSERT_W;
3250 InsveOp = Mips::INSVE_W;
3251 VecRC = &Mips::MSA128WRegClass;
3252 break;
3253 case 8:
3254 EltLog2Size = 3;
3255 InsertOp = Mips::INSERT_D;
3256 InsveOp = Mips::INSVE_D;
3257 VecRC = &Mips::MSA128DRegClass;
3258 break;
3259 }
3260
3261 if (IsFP) {
3262 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3263 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3264 .addImm(0)
3265 .addReg(SrcValReg)
3266 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3267 SrcValReg = Wt;
3268 }
3269
3270 // Convert the lane index into a byte index
3271 if (EltSizeInBytes != 1) {
3272 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
3273 BuildMI(*BB, MI, DL, TII->get(ShiftOp), LaneTmp1)
3274 .addReg(LaneReg)
3275 .addImm(EltLog2Size);
3276 LaneReg = LaneTmp1;
3277 }
3278
3279 // Rotate bytes around so that the desired lane is element zero
3280 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3281 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3282 .addReg(SrcVecReg)
3283 .addReg(SrcVecReg)
3284 .addReg(LaneReg, 0, SubRegIdx);
3285
3286 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3287 if (IsFP) {
3288 // Use insve.df to insert to element zero
3289 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3290 .addReg(WdTmp1)
3291 .addImm(0)
3292 .addReg(SrcValReg)
3293 .addImm(0);
3294 } else {
3295 // Use insert.df to insert to element zero
3296 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3297 .addReg(WdTmp1)
3298 .addReg(SrcValReg)
3299 .addImm(0);
3300 }
3301
3302 // Rotate elements the rest of the way for a full rotation.
3303 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3304 // the lane index to do this.
3305 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
3306 BuildMI(*BB, MI, DL, TII->get(Subtarget.isABI_N64() ? Mips::DSUB : Mips::SUB),
3307 LaneTmp2)
3308 .addReg(Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO)
3309 .addReg(LaneReg);
3310 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3311 .addReg(WdTmp2)
3312 .addReg(WdTmp2)
3313 .addReg(LaneTmp2, 0, SubRegIdx);
3314
3315 MI.eraseFromParent(); // The pseudo instruction is gone now.
3316 return BB;
3317 }
3318
3319 // Emit the FILL_FW pseudo instruction.
3320 //
3321 // fill_fw_pseudo $wd, $fs
3322 // =>
3323 // implicit_def $wt1
3324 // insert_subreg $wt2:subreg_lo, $wt1, $fs
3325 // splati.w $wd, $wt2[0]
3326 MachineBasicBlock *
emitFILL_FW(MachineInstr & MI,MachineBasicBlock * BB) const3327 MipsSETargetLowering::emitFILL_FW(MachineInstr &MI,
3328 MachineBasicBlock *BB) const {
3329 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3330 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3331 DebugLoc DL = MI.getDebugLoc();
3332 unsigned Wd = MI.getOperand(0).getReg();
3333 unsigned Fs = MI.getOperand(1).getReg();
3334 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3335 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3336
3337 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3338 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3339 .addReg(Wt1)
3340 .addReg(Fs)
3341 .addImm(Mips::sub_lo);
3342 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3343
3344 MI.eraseFromParent(); // The pseudo instruction is gone now.
3345 return BB;
3346 }
3347
3348 // Emit the FILL_FD pseudo instruction.
3349 //
3350 // fill_fd_pseudo $wd, $fs
3351 // =>
3352 // implicit_def $wt1
3353 // insert_subreg $wt2:subreg_64, $wt1, $fs
3354 // splati.d $wd, $wt2[0]
3355 MachineBasicBlock *
emitFILL_FD(MachineInstr & MI,MachineBasicBlock * BB) const3356 MipsSETargetLowering::emitFILL_FD(MachineInstr &MI,
3357 MachineBasicBlock *BB) const {
3358 assert(Subtarget.isFP64bit());
3359
3360 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3361 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3362 DebugLoc DL = MI.getDebugLoc();
3363 unsigned Wd = MI.getOperand(0).getReg();
3364 unsigned Fs = MI.getOperand(1).getReg();
3365 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3366 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3367
3368 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3369 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3370 .addReg(Wt1)
3371 .addReg(Fs)
3372 .addImm(Mips::sub_64);
3373 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
3374
3375 MI.eraseFromParent(); // The pseudo instruction is gone now.
3376 return BB;
3377 }
3378
3379 // Emit the FEXP2_W_1 pseudo instructions.
3380 //
3381 // fexp2_w_1_pseudo $wd, $wt
3382 // =>
3383 // ldi.w $ws, 1
3384 // fexp2.w $wd, $ws, $wt
3385 MachineBasicBlock *
emitFEXP2_W_1(MachineInstr & MI,MachineBasicBlock * BB) const3386 MipsSETargetLowering::emitFEXP2_W_1(MachineInstr &MI,
3387 MachineBasicBlock *BB) const {
3388 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3389 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3390 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3391 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3392 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
3393 DebugLoc DL = MI.getDebugLoc();
3394
3395 // Splat 1.0 into a vector
3396 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3397 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3398
3399 // Emit 1.0 * fexp2(Wt)
3400 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI.getOperand(0).getReg())
3401 .addReg(Ws2)
3402 .addReg(MI.getOperand(1).getReg());
3403
3404 MI.eraseFromParent(); // The pseudo instruction is gone now.
3405 return BB;
3406 }
3407
3408 // Emit the FEXP2_D_1 pseudo instructions.
3409 //
3410 // fexp2_d_1_pseudo $wd, $wt
3411 // =>
3412 // ldi.d $ws, 1
3413 // fexp2.d $wd, $ws, $wt
3414 MachineBasicBlock *
emitFEXP2_D_1(MachineInstr & MI,MachineBasicBlock * BB) const3415 MipsSETargetLowering::emitFEXP2_D_1(MachineInstr &MI,
3416 MachineBasicBlock *BB) const {
3417 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3418 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3419 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3420 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3421 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
3422 DebugLoc DL = MI.getDebugLoc();
3423
3424 // Splat 1.0 into a vector
3425 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3426 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3427
3428 // Emit 1.0 * fexp2(Wt)
3429 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI.getOperand(0).getReg())
3430 .addReg(Ws2)
3431 .addReg(MI.getOperand(1).getReg());
3432
3433 MI.eraseFromParent(); // The pseudo instruction is gone now.
3434 return BB;
3435 }
3436