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