blob: 45d7f94f1d1950a4ef311a677f20a643aaf0bf29 [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001//===- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface -------------===//
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Subclass of MipsTargetLowering specialized for mips32/64.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenko79220eae2017-08-03 22:12:30 +000013
Akira Hatanaka96ca1822013-03-13 00:54:29 +000014#include "MipsSEISelLowering.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000015#include "MipsMachineFunction.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000016#include "MipsRegisterInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000017#include "MipsSubtarget.h"
Simon Dardis548a53f2017-01-10 16:40:57 +000018#include "llvm/ADT/APInt.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000019#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/Triple.h"
23#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/ISDOpcodes.h"
25#include "llvm/CodeGen/MachineBasicBlock.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstr.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000028#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000029#include "llvm/CodeGen/MachineMemOperand.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000031#include "llvm/CodeGen/MachineValueType.h"
32#include "llvm/CodeGen/SelectionDAG.h"
33#include "llvm/CodeGen/SelectionDAGNodes.h"
34#include "llvm/CodeGen/ValueTypes.h"
35#include "llvm/IR/DebugLoc.h"
Akira Hatanakaa6bbde52013-04-13 02:13:30 +000036#include "llvm/IR/Intrinsics.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000037#include "llvm/Support/Casting.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000038#include "llvm/Support/CommandLine.h"
Daniel Sanders62aeab82013-10-30 13:31:27 +000039#include "llvm/Support/Debug.h"
Simon Dardis548a53f2017-01-10 16:40:57 +000040#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000041#include "llvm/Support/MathExtras.h"
Hans Wennborg3e9b1c12013-10-30 16:10:10 +000042#include "llvm/Support/raw_ostream.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000043#include "llvm/Target/TargetInstrInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000044#include "llvm/Target/TargetSubtargetInfo.h"
45#include <algorithm>
46#include <cassert>
47#include <cstdint>
48#include <iterator>
49#include <utility>
Akira Hatanaka96ca1822013-03-13 00:54:29 +000050
51using namespace llvm;
52
Chandler Carruth84e68b22014-04-22 02:41:26 +000053#define DEBUG_TYPE "mips-isel"
54
Akira Hatanaka96ca1822013-03-13 00:54:29 +000055static cl::opt<bool>
Simon Dardis57f4ae42016-08-04 09:17:07 +000056UseMipsTailCalls("mips-tail-calls", cl::Hidden,
Simon Dardisd2ed8ab2016-09-27 13:15:54 +000057 cl::desc("MIPS: permit tail calls."), cl::init(false));
Akira Hatanaka96ca1822013-03-13 00:54:29 +000058
Akira Hatanaka63791212013-09-07 00:52:30 +000059static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
60 cl::desc("Expand double precision loads and "
61 "stores to their single precision "
62 "counterparts"));
63
Eric Christopherb1526602014-09-19 23:30:42 +000064MipsSETargetLowering::MipsSETargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +000065 const MipsSubtarget &STI)
66 : MipsTargetLowering(TM, STI) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000067 // Set up the register classes
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000068 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000069
Eric Christopher1c29a652014-07-18 22:55:25 +000070 if (Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000071 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000072
Eric Christopher1c29a652014-07-18 22:55:25 +000073 if (Subtarget.hasDSP() || Subtarget.hasMSA()) {
Daniel Sanders36c671e2013-09-27 09:44:59 +000074 // Expand all truncating stores and extending loads.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000075 for (MVT VT0 : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000076 for (MVT VT1 : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000077 setTruncStoreAction(VT0, VT1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000078 setLoadExtAction(ISD::SEXTLOAD, VT0, VT1, Expand);
79 setLoadExtAction(ISD::ZEXTLOAD, VT0, VT1, Expand);
80 setLoadExtAction(ISD::EXTLOAD, VT0, VT1, Expand);
81 }
Daniel Sanders36c671e2013-09-27 09:44:59 +000082 }
83 }
84
Eric Christopher1c29a652014-07-18 22:55:25 +000085 if (Subtarget.hasDSP()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000086 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
87
88 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
Akira Hatanaka654655f2013-08-14 00:53:38 +000089 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000090
91 // Expand all builtin opcodes.
92 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
93 setOperationAction(Opc, VecTys[i], Expand);
94
Akira Hatanaka2f088222013-04-13 00:55:41 +000095 setOperationAction(ISD::ADD, VecTys[i], Legal);
96 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000097 setOperationAction(ISD::LOAD, VecTys[i], Legal);
98 setOperationAction(ISD::STORE, VecTys[i], Legal);
99 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
100 }
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000101
102 setTargetDAGCombine(ISD::SHL);
103 setTargetDAGCombine(ISD::SRA);
104 setTargetDAGCombine(ISD::SRL);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000105 setTargetDAGCombine(ISD::SETCC);
106 setTargetDAGCombine(ISD::VSELECT);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000107 }
108
Eric Christopher1c29a652014-07-18 22:55:25 +0000109 if (Subtarget.hasDSPR2())
Akira Hatanaka2f088222013-04-13 00:55:41 +0000110 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
111
Eric Christopher1c29a652014-07-18 22:55:25 +0000112 if (Subtarget.hasMSA()) {
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000113 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
114 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
115 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
116 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
117 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
118 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
119 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000120
Simon Dardis0e2ee3b2016-11-18 16:17:44 +0000121 // f16 is a storage-only type, always promote it to f32.
122 addRegisterClass(MVT::f16, &Mips::MSA128HRegClass);
123 setOperationAction(ISD::SETCC, MVT::f16, Promote);
124 setOperationAction(ISD::BR_CC, MVT::f16, Promote);
125 setOperationAction(ISD::SELECT_CC, MVT::f16, Promote);
126 setOperationAction(ISD::SELECT, MVT::f16, Promote);
127 setOperationAction(ISD::FADD, MVT::f16, Promote);
128 setOperationAction(ISD::FSUB, MVT::f16, Promote);
129 setOperationAction(ISD::FMUL, MVT::f16, Promote);
130 setOperationAction(ISD::FDIV, MVT::f16, Promote);
131 setOperationAction(ISD::FREM, MVT::f16, Promote);
132 setOperationAction(ISD::FMA, MVT::f16, Promote);
133 setOperationAction(ISD::FNEG, MVT::f16, Promote);
134 setOperationAction(ISD::FABS, MVT::f16, Promote);
135 setOperationAction(ISD::FCEIL, MVT::f16, Promote);
136 setOperationAction(ISD::FCOPYSIGN, MVT::f16, Promote);
137 setOperationAction(ISD::FCOS, MVT::f16, Promote);
138 setOperationAction(ISD::FP_EXTEND, MVT::f16, Promote);
139 setOperationAction(ISD::FFLOOR, MVT::f16, Promote);
140 setOperationAction(ISD::FNEARBYINT, MVT::f16, Promote);
141 setOperationAction(ISD::FPOW, MVT::f16, Promote);
142 setOperationAction(ISD::FPOWI, MVT::f16, Promote);
143 setOperationAction(ISD::FRINT, MVT::f16, Promote);
144 setOperationAction(ISD::FSIN, MVT::f16, Promote);
145 setOperationAction(ISD::FSINCOS, MVT::f16, Promote);
146 setOperationAction(ISD::FSQRT, MVT::f16, Promote);
147 setOperationAction(ISD::FEXP, MVT::f16, Promote);
148 setOperationAction(ISD::FEXP2, MVT::f16, Promote);
149 setOperationAction(ISD::FLOG, MVT::f16, Promote);
150 setOperationAction(ISD::FLOG2, MVT::f16, Promote);
151 setOperationAction(ISD::FLOG10, MVT::f16, Promote);
152 setOperationAction(ISD::FROUND, MVT::f16, Promote);
153 setOperationAction(ISD::FTRUNC, MVT::f16, Promote);
154 setOperationAction(ISD::FMINNUM, MVT::f16, Promote);
155 setOperationAction(ISD::FMAXNUM, MVT::f16, Promote);
156 setOperationAction(ISD::FMINNAN, MVT::f16, Promote);
157 setOperationAction(ISD::FMAXNAN, MVT::f16, Promote);
158
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000159 setTargetDAGCombine(ISD::AND);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000160 setTargetDAGCombine(ISD::OR);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000161 setTargetDAGCombine(ISD::SRA);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000162 setTargetDAGCombine(ISD::VSELECT);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000163 setTargetDAGCombine(ISD::XOR);
Jack Carter3a2c2d42013-08-13 20:54:07 +0000164 }
165
Eric Christophere8ae3e32015-05-07 23:10:21 +0000166 if (!Subtarget.useSoftFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000167 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
168
169 // When dealing with single precision only, use libcalls
Eric Christopher1c29a652014-07-18 22:55:25 +0000170 if (!Subtarget.isSingleFloat()) {
171 if (Subtarget.isFP64bit())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000172 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
173 else
174 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
175 }
176 }
177
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000178 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
179 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
180 setOperationAction(ISD::MULHS, MVT::i32, Custom);
181 setOperationAction(ISD::MULHU, MVT::i32, Custom);
182
Eric Christopher1c29a652014-07-18 22:55:25 +0000183 if (Subtarget.hasCnMips())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000184 setOperationAction(ISD::MUL, MVT::i64, Legal);
Eric Christopher1c29a652014-07-18 22:55:25 +0000185 else if (Subtarget.isGP64bit())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000186 setOperationAction(ISD::MUL, MVT::i64, Custom);
187
Eric Christopher1c29a652014-07-18 22:55:25 +0000188 if (Subtarget.isGP64bit()) {
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000189 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Custom);
190 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000191 setOperationAction(ISD::MULHS, MVT::i64, Custom);
192 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Jan Vesely54468a5a2014-10-17 14:45:28 +0000193 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
194 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000195 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000196
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000197 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
198 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
199
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000200 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
201 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000202 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
203 setOperationAction(ISD::LOAD, MVT::i32, Custom);
204 setOperationAction(ISD::STORE, MVT::i32, Custom);
205
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000206 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000207
Daniel Sandersce09d072013-08-28 12:14:50 +0000208 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000209 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
210 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
211
Akira Hatanaka63791212013-09-07 00:52:30 +0000212 if (NoDPLoadStore) {
213 setOperationAction(ISD::LOAD, MVT::f64, Custom);
214 setOperationAction(ISD::STORE, MVT::f64, Custom);
215 }
216
Eric Christopher1c29a652014-07-18 22:55:25 +0000217 if (Subtarget.hasMips32r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000218 // MIPS32r6 replaces the accumulator-based multiplies with a three register
219 // instruction
Daniel Sanders826f8b32014-06-12 10:54:16 +0000220 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
221 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000222 setOperationAction(ISD::MUL, MVT::i32, Legal);
223 setOperationAction(ISD::MULHS, MVT::i32, Legal);
224 setOperationAction(ISD::MULHU, MVT::i32, Legal);
225
226 // MIPS32r6 replaces the accumulator-based division/remainder with separate
227 // three register division and remainder instructions.
228 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
229 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
230 setOperationAction(ISD::SDIV, MVT::i32, Legal);
231 setOperationAction(ISD::UDIV, MVT::i32, Legal);
232 setOperationAction(ISD::SREM, MVT::i32, Legal);
233 setOperationAction(ISD::UREM, MVT::i32, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000234
235 // MIPS32r6 replaces conditional moves with an equivalent that removes the
236 // need for three GPR read ports.
237 setOperationAction(ISD::SETCC, MVT::i32, Legal);
238 setOperationAction(ISD::SELECT, MVT::i32, Legal);
239 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
240
241 setOperationAction(ISD::SETCC, MVT::f32, Legal);
242 setOperationAction(ISD::SELECT, MVT::f32, Legal);
243 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
244
Eric Christopher1c29a652014-07-18 22:55:25 +0000245 assert(Subtarget.isFP64bit() && "FR=1 is required for MIPS32r6");
Daniel Sanders0fa60412014-06-12 13:39:06 +0000246 setOperationAction(ISD::SETCC, MVT::f64, Legal);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000247 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000248 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
249
Daniel Sanders3d3ea532014-06-12 15:00:17 +0000250 setOperationAction(ISD::BRCOND, MVT::Other, Legal);
251
Daniel Sanders0fa60412014-06-12 13:39:06 +0000252 // Floating point > and >= are supported via < and <=
253 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
254 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
255 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
256 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
257
258 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
259 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
260 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
261 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000262 }
263
Eric Christopher1c29a652014-07-18 22:55:25 +0000264 if (Subtarget.hasMips64r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000265 // MIPS64r6 replaces the accumulator-based multiplies with a three register
266 // instruction
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000267 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
268 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000269 setOperationAction(ISD::MUL, MVT::i64, Legal);
270 setOperationAction(ISD::MULHS, MVT::i64, Legal);
271 setOperationAction(ISD::MULHU, MVT::i64, Legal);
272
273 // MIPS32r6 replaces the accumulator-based division/remainder with separate
274 // three register division and remainder instructions.
275 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
276 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
277 setOperationAction(ISD::SDIV, MVT::i64, Legal);
278 setOperationAction(ISD::UDIV, MVT::i64, Legal);
279 setOperationAction(ISD::SREM, MVT::i64, Legal);
280 setOperationAction(ISD::UREM, MVT::i64, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000281
282 // MIPS64r6 replaces conditional moves with an equivalent that removes the
283 // need for three GPR read ports.
284 setOperationAction(ISD::SETCC, MVT::i64, Legal);
285 setOperationAction(ISD::SELECT, MVT::i64, Legal);
286 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000287 }
288
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000289 computeRegisterProperties(Subtarget.getRegisterInfo());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000290}
291
292const MipsTargetLowering *
Eric Christopherb1526602014-09-19 23:30:42 +0000293llvm::createMipsSETargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000294 const MipsSubtarget &STI) {
295 return new MipsSETargetLowering(TM, STI);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000296}
297
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000298const TargetRegisterClass *
299MipsSETargetLowering::getRepRegClassFor(MVT VT) const {
300 if (VT == MVT::Untyped)
Eric Christopher1c29a652014-07-18 22:55:25 +0000301 return Subtarget.hasDSP() ? &Mips::ACC64DSPRegClass : &Mips::ACC64RegClass;
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000302
303 return TargetLowering::getRepRegClassFor(VT);
304}
305
Daniel Sanders7a289d02013-09-23 12:02:46 +0000306// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000307void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000308addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
309 addRegisterClass(Ty, RC);
310
311 // Expand all builtin opcodes.
312 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
313 setOperationAction(Opc, Ty, Expand);
314
315 setOperationAction(ISD::BITCAST, Ty, Legal);
316 setOperationAction(ISD::LOAD, Ty, Legal);
317 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000318 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
319 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000320 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000321
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000322 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000323 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000324 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000325 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000326 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000327 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000328 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000329 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000330 setOperationAction(ISD::SHL, Ty, Legal);
331 setOperationAction(ISD::SRA, Ty, Legal);
332 setOperationAction(ISD::SRL, Ty, Legal);
333 setOperationAction(ISD::SUB, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000334 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000335 setOperationAction(ISD::UREM, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000336 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000337 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000338 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000339
Daniel Sanders015972b2013-10-11 10:00:06 +0000340 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
341 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
342 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
343 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
344 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
345 }
346
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000347 setOperationAction(ISD::SETCC, Ty, Legal);
348 setCondCodeAction(ISD::SETNE, Ty, Expand);
349 setCondCodeAction(ISD::SETGE, Ty, Expand);
350 setCondCodeAction(ISD::SETGT, Ty, Expand);
351 setCondCodeAction(ISD::SETUGE, Ty, Expand);
352 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000353}
354
Daniel Sanders7a289d02013-09-23 12:02:46 +0000355// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000356void MipsSETargetLowering::
357addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000358 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000359
360 // Expand all builtin opcodes.
361 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
362 setOperationAction(Opc, Ty, Expand);
363
364 setOperationAction(ISD::LOAD, Ty, Legal);
365 setOperationAction(ISD::STORE, Ty, Legal);
366 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000367 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000368 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000369 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000370
371 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000372 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000373 setOperationAction(ISD::FADD, Ty, Legal);
374 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000375 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000376 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000377 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000378 setOperationAction(ISD::FMUL, Ty, Legal);
379 setOperationAction(ISD::FRINT, Ty, Legal);
380 setOperationAction(ISD::FSQRT, Ty, Legal);
381 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000382 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000383
384 setOperationAction(ISD::SETCC, Ty, Legal);
385 setCondCodeAction(ISD::SETOGE, Ty, Expand);
386 setCondCodeAction(ISD::SETOGT, Ty, Expand);
387 setCondCodeAction(ISD::SETUGE, Ty, Expand);
388 setCondCodeAction(ISD::SETUGT, Ty, Expand);
389 setCondCodeAction(ISD::SETGE, Ty, Expand);
390 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000391 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000392}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000393
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000394SDValue MipsSETargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000395 if(!Subtarget.hasMips32r6())
396 return MipsTargetLowering::LowerOperation(Op, DAG);
397
398 EVT ResTy = Op->getValueType(0);
399 SDLoc DL(Op);
400
401 // Although MTC1_D64 takes an i32 and writes an f64, the upper 32 bits of the
402 // floating point register are undefined. Not really an issue as sel.d, which
403 // is produced from an FSELECT node, only looks at bit 0.
404 SDValue Tmp = DAG.getNode(MipsISD::MTC1_D64, DL, MVT::f64, Op->getOperand(0));
405 return DAG.getNode(MipsISD::FSELECT, DL, ResTy, Tmp, Op->getOperand(1),
406 Op->getOperand(2));
407}
408
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000409bool
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000410MipsSETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
411 unsigned,
412 unsigned,
413 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000414 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
415
Eric Christopher1c29a652014-07-18 22:55:25 +0000416 if (Subtarget.systemSupportsUnalignedAccess()) {
Daniel Sandersac272632014-05-23 13:18:02 +0000417 // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
418 // implementation defined whether this is handled by hardware, software, or
419 // a hybrid of the two but it's expected that most implementations will
420 // handle the majority of cases in hardware.
421 if (Fast)
422 *Fast = true;
423 return true;
424 }
425
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000426 switch (SVT) {
427 case MVT::i64:
428 case MVT::i32:
429 if (Fast)
430 *Fast = true;
431 return true;
432 default:
433 return false;
434 }
435}
436
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000437SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
438 SelectionDAG &DAG) const {
439 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000440 case ISD::LOAD: return lowerLOAD(Op, DAG);
441 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000442 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
443 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
444 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
445 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
446 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
447 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000448 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
449 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000450 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
451 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000452 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000453 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000454 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000455 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000456 case ISD::SELECT: return lowerSELECT(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000457 }
458
459 return MipsTargetLowering::LowerOperation(Op, DAG);
460}
461
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000462// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
463//
464// Performs the following transformations:
465// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
466// sign/zero-extension is completely overwritten by the new one performed by
467// the ISD::AND.
468// - Removes redundant zero extensions performed by an ISD::AND.
469static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
470 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000471 const MipsSubtarget &Subtarget) {
472 if (!Subtarget.hasMSA())
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000473 return SDValue();
474
475 SDValue Op0 = N->getOperand(0);
476 SDValue Op1 = N->getOperand(1);
477 unsigned Op0Opcode = Op0->getOpcode();
478
479 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
480 // where $d + 1 == 2^n and n == 32
481 // or $d + 1 == 2^n and n <= 32 and ZExt
482 // -> (MipsVExtractZExt $a, $b, $c)
483 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
484 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
485 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
486
487 if (!Mask)
488 return SDValue();
489
490 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
491
492 if (Log2IfPositive <= 0)
493 return SDValue(); // Mask+1 is not a power of 2
494
495 SDValue Op0Op2 = Op0->getOperand(2);
496 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
497 unsigned ExtendTySize = ExtendTy.getSizeInBits();
498 unsigned Log2 = Log2IfPositive;
499
500 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
501 Log2 == ExtendTySize) {
502 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000503 return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
504 Op0->getVTList(),
505 makeArrayRef(Ops, Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000506 }
507 }
508
509 return SDValue();
510}
511
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000512// Determine if the specified node is a constant vector splat.
513//
514// Returns true and sets Imm if:
515// * N is a ISD::BUILD_VECTOR representing a constant splat
516//
517// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
518// differences are that it assumes the MSA has already been checked and the
519// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
520// must not be in order for binsri.d to be selectable).
521static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
522 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
523
Craig Topper062a2ba2014-04-25 05:30:21 +0000524 if (!Node)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000525 return false;
526
527 APInt SplatValue, SplatUndef;
528 unsigned SplatBitSize;
529 bool HasAnyUndefs;
530
531 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
532 8, !IsLittleEndian))
533 return false;
534
535 Imm = SplatValue;
536
537 return true;
538}
539
Daniel Sandersab94b532013-10-30 15:20:38 +0000540// Test whether the given node is an all-ones build_vector.
541static bool isVectorAllOnes(SDValue N) {
542 // Look through bitcasts. Endianness doesn't matter because we are looking
543 // for an all-ones value.
544 if (N->getOpcode() == ISD::BITCAST)
545 N = N->getOperand(0);
546
547 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
548
549 if (!BVN)
550 return false;
551
552 APInt SplatValue, SplatUndef;
553 unsigned SplatBitSize;
554 bool HasAnyUndefs;
555
556 // Endianness doesn't matter in this context because we are looking for
557 // an all-ones value.
558 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
559 return SplatValue.isAllOnesValue();
560
561 return false;
562}
563
564// Test whether N is the bitwise inverse of OfNode.
565static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
566 if (N->getOpcode() != ISD::XOR)
567 return false;
568
569 if (isVectorAllOnes(N->getOperand(0)))
570 return N->getOperand(1) == OfNode;
571
572 if (isVectorAllOnes(N->getOperand(1)))
573 return N->getOperand(0) == OfNode;
574
575 return false;
576}
577
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000578// Perform combines where ISD::OR is the root node.
579//
580// Performs the following transformations:
581// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
582// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
583// vector type.
584static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
585 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000586 const MipsSubtarget &Subtarget) {
587 if (!Subtarget.hasMSA())
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000588 return SDValue();
589
590 EVT Ty = N->getValueType(0);
591
592 if (!Ty.is128BitVector())
593 return SDValue();
594
595 SDValue Op0 = N->getOperand(0);
596 SDValue Op1 = N->getOperand(1);
597
598 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
599 SDValue Op0Op0 = Op0->getOperand(0);
600 SDValue Op0Op1 = Op0->getOperand(1);
601 SDValue Op1Op0 = Op1->getOperand(0);
602 SDValue Op1Op1 = Op1->getOperand(1);
Eric Christopher1c29a652014-07-18 22:55:25 +0000603 bool IsLittleEndian = !Subtarget.isLittle();
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000604
605 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000606 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000607 APInt Mask, InvMask;
608
609 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
610 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
611 // looking.
612 // IfClr will be set if we find a valid match.
613 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
614 Cond = Op0Op0;
615 IfSet = Op0Op1;
616
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000617 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
618 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000619 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000620 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
621 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000622 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000623
624 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000625 }
626
627 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
628 // thing again using this mask.
629 // IfClr will be set if we find a valid match.
630 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
631 Cond = Op0Op1;
632 IfSet = Op0Op0;
633
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000634 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
635 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000636 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000637 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
638 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000639 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000640
641 IsConstantMask = true;
642 }
643
644 // If IfClr is not yet set, try looking for a non-constant match.
645 // IfClr will be set if we find a valid match amongst the eight
646 // possibilities.
647 if (!IfClr.getNode()) {
648 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
649 Cond = Op1Op0;
650 IfSet = Op1Op1;
651 IfClr = Op0Op1;
652 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
653 Cond = Op1Op0;
654 IfSet = Op1Op1;
655 IfClr = Op0Op0;
656 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
657 Cond = Op1Op1;
658 IfSet = Op1Op0;
659 IfClr = Op0Op1;
660 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
661 Cond = Op1Op1;
662 IfSet = Op1Op0;
663 IfClr = Op0Op0;
664 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
665 Cond = Op0Op0;
666 IfSet = Op0Op1;
667 IfClr = Op1Op1;
668 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
669 Cond = Op0Op0;
670 IfSet = Op0Op1;
671 IfClr = Op1Op0;
672 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
673 Cond = Op0Op1;
674 IfSet = Op0Op0;
675 IfClr = Op1Op1;
676 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
677 Cond = Op0Op1;
678 IfSet = Op0Op0;
679 IfClr = Op1Op0;
680 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000681 }
682
683 // At this point, IfClr will be set if we have a valid match.
684 if (!IfClr.getNode())
685 return SDValue();
686
687 assert(Cond.getNode() && IfSet.getNode());
688
689 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000690 if (IsConstantMask) {
691 if (Mask.isAllOnesValue())
692 return IfSet;
693 else if (Mask == 0)
694 return IfClr;
695 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000696
697 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000698 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000699 }
700
701 return SDValue();
702}
703
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000704static SDValue genConstMult(SDValue X, uint64_t C, const SDLoc &DL, EVT VT,
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000705 EVT ShiftTy, SelectionDAG &DAG) {
706 // Clear the upper (64 - VT.sizeInBits) bits.
707 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
708
709 // Return 0.
710 if (C == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000711 return DAG.getConstant(0, DL, VT);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000712
713 // Return x.
714 if (C == 1)
715 return X;
716
717 // If c is power of 2, return (shl x, log2(c)).
718 if (isPowerOf2_64(C))
719 return DAG.getNode(ISD::SHL, DL, VT, X,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000720 DAG.getConstant(Log2_64(C), DL, ShiftTy));
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000721
722 unsigned Log2Ceil = Log2_64_Ceil(C);
723 uint64_t Floor = 1LL << Log2_64(C);
724 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
725
726 // If |c - floor_c| <= |c - ceil_c|,
727 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
728 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
729 if (C - Floor <= Ceil - C) {
730 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
731 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
732 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
733 }
734
735 // If |c - floor_c| > |c - ceil_c|,
736 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
737 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
738 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
739 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
740}
741
742static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
743 const TargetLowering::DAGCombinerInfo &DCI,
744 const MipsSETargetLowering *TL) {
745 EVT VT = N->getValueType(0);
746
747 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
748 if (!VT.isVector())
Mehdi Amini9639d652015-07-09 02:09:20 +0000749 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N), VT,
Mehdi Aminieaabc512015-07-09 15:12:23 +0000750 TL->getScalarShiftAmountTy(DAG.getDataLayout(), VT),
751 DAG);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000752
753 return SDValue(N, 0);
754}
755
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000756static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
757 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000758 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000759 // See if this is a vector splat immediate node.
760 APInt SplatValue, SplatUndef;
761 unsigned SplatBitSize;
762 bool HasAnyUndefs;
Sanjay Patel1ed771f2016-09-14 16:37:15 +0000763 unsigned EltSize = Ty.getScalarSizeInBits();
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000764 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
765
Eric Christopher1c29a652014-07-18 22:55:25 +0000766 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000767 return SDValue();
768
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000769 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000770 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000771 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000772 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000773 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000774 return SDValue();
775
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000776 SDLoc DL(N);
777 return DAG.getNode(Opc, DL, Ty, N->getOperand(0),
778 DAG.getConstant(SplatValue.getZExtValue(), DL, MVT::i32));
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000779}
780
781static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
782 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000783 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000784 EVT Ty = N->getValueType(0);
785
786 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
787 return SDValue();
788
789 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
790}
791
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000792// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
793// constant splats into MipsISD::SHRA_DSP for DSPr2.
794//
795// Performs the following transformations:
796// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
797// sign/zero-extension is completely overwritten by the new one performed by
798// the ISD::SRA and ISD::SHL nodes.
799// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
800// sequence.
801//
802// See performDSPShiftCombine for more information about the transformation
803// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000804static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
805 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000806 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000807 EVT Ty = N->getValueType(0);
808
Eric Christopher1c29a652014-07-18 22:55:25 +0000809 if (Subtarget.hasMSA()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000810 SDValue Op0 = N->getOperand(0);
811 SDValue Op1 = N->getOperand(1);
812
813 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
814 // where $d + sizeof($c) == 32
815 // or $d + sizeof($c) <= 32 and SExt
816 // -> (MipsVExtractSExt $a, $b, $c)
817 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
818 SDValue Op0Op0 = Op0->getOperand(0);
819 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
820
821 if (!ShAmount)
822 return SDValue();
823
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000824 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
825 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
826 return SDValue();
827
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000828 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
829 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
830
831 if (TotalBits == 32 ||
832 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
833 TotalBits <= 32)) {
834 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
835 Op0Op0->getOperand(2) };
Chandler Carruth356665a2014-08-01 22:09:43 +0000836 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
837 Op0Op0->getVTList(),
838 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000839 }
840 }
841 }
842
Eric Christopher1c29a652014-07-18 22:55:25 +0000843 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000844 return SDValue();
845
846 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
847}
848
849
850static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
851 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000852 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000853 EVT Ty = N->getValueType(0);
854
Eric Christopher1c29a652014-07-18 22:55:25 +0000855 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000856 return SDValue();
857
858 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
859}
860
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000861static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
862 bool IsV216 = (Ty == MVT::v2i16);
863
864 switch (CC) {
865 case ISD::SETEQ:
866 case ISD::SETNE: return true;
867 case ISD::SETLT:
868 case ISD::SETLE:
869 case ISD::SETGT:
870 case ISD::SETGE: return IsV216;
871 case ISD::SETULT:
872 case ISD::SETULE:
873 case ISD::SETUGT:
874 case ISD::SETUGE: return !IsV216;
875 default: return false;
876 }
877}
878
879static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
880 EVT Ty = N->getValueType(0);
881
882 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
883 return SDValue();
884
885 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
886 return SDValue();
887
Andrew Trickef9de2a2013-05-25 02:42:55 +0000888 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000889 N->getOperand(1), N->getOperand(2));
890}
891
892static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
893 EVT Ty = N->getValueType(0);
894
Daniel Sanders3ce56622013-09-24 12:18:31 +0000895 if (Ty.is128BitVector() && Ty.isInteger()) {
896 // Try the following combines:
897 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
898 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
899 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
900 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
901 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
902 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
903 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
904 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
905 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
906 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
907 // legalizer.
908 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000909
Daniel Sanders3ce56622013-09-24 12:18:31 +0000910 if (Op0->getOpcode() != ISD::SETCC)
911 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000912
Daniel Sanders3ce56622013-09-24 12:18:31 +0000913 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
914 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000915
Daniel Sanders3ce56622013-09-24 12:18:31 +0000916 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
917 Signed = true;
918 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
919 Signed = false;
920 else
921 return SDValue();
922
923 SDValue Op1 = N->getOperand(1);
924 SDValue Op2 = N->getOperand(2);
925 SDValue Op0Op0 = Op0->getOperand(0);
926 SDValue Op0Op1 = Op0->getOperand(1);
927
928 if (Op1 == Op0Op0 && Op2 == Op0Op1)
929 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
930 Ty, Op1, Op2);
931 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
932 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
933 Ty, Op1, Op2);
934 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
935 SDValue SetCC = N->getOperand(0);
936
937 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
938 return SDValue();
939
940 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
941 SetCC.getOperand(0), SetCC.getOperand(1),
942 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
943 }
944
945 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000946}
947
Daniel Sandersf7456c72013-09-23 13:22:24 +0000948static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000949 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000950 EVT Ty = N->getValueType(0);
951
Eric Christopher1c29a652014-07-18 22:55:25 +0000952 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000953 // Try the following combines:
954 // (xor (or $a, $b), (build_vector allones))
955 // (xor (or $a, $b), (bitcast (build_vector allones)))
956 SDValue Op0 = N->getOperand(0);
957 SDValue Op1 = N->getOperand(1);
958 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000959
960 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
961 NotOp = Op1;
962 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
963 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000964 else
965 return SDValue();
966
967 if (NotOp->getOpcode() == ISD::OR)
968 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
969 NotOp->getOperand(1));
970 }
971
972 return SDValue();
973}
974
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000975SDValue
976MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
977 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000978 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000979
980 switch (N->getOpcode()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000981 case ISD::AND:
982 Val = performANDCombine(N, DAG, DCI, Subtarget);
983 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000984 case ISD::OR:
985 Val = performORCombine(N, DAG, DCI, Subtarget);
986 break;
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000987 case ISD::MUL:
988 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000989 case ISD::SHL:
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000990 Val = performSHLCombine(N, DAG, DCI, Subtarget);
991 break;
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000992 case ISD::SRA:
993 return performSRACombine(N, DAG, DCI, Subtarget);
994 case ISD::SRL:
995 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000996 case ISD::VSELECT:
997 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000998 case ISD::XOR:
999 Val = performXORCombine(N, DAG, Subtarget);
1000 break;
1001 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001002 Val = performSETCCCombine(N, DAG);
1003 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001004 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001005
Daniel Sanders62aeab82013-10-30 13:31:27 +00001006 if (Val.getNode()) {
1007 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1008 N->printrWithDepth(dbgs(), &DAG);
1009 dbgs() << "\n=> \n";
1010 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1011 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001012 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001013 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001014
1015 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001016}
1017
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001018MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001019MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001020 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001021 switch (MI.getOpcode()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001022 default:
1023 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1024 case Mips::BPOSGE32_PSEUDO:
1025 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001026 case Mips::SNZ_B_PSEUDO:
1027 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1028 case Mips::SNZ_H_PSEUDO:
1029 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1030 case Mips::SNZ_W_PSEUDO:
1031 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1032 case Mips::SNZ_D_PSEUDO:
1033 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1034 case Mips::SNZ_V_PSEUDO:
1035 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1036 case Mips::SZ_B_PSEUDO:
1037 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1038 case Mips::SZ_H_PSEUDO:
1039 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1040 case Mips::SZ_W_PSEUDO:
1041 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1042 case Mips::SZ_D_PSEUDO:
1043 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1044 case Mips::SZ_V_PSEUDO:
1045 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001046 case Mips::COPY_FW_PSEUDO:
1047 return emitCOPY_FW(MI, BB);
1048 case Mips::COPY_FD_PSEUDO:
1049 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001050 case Mips::INSERT_FW_PSEUDO:
1051 return emitINSERT_FW(MI, BB);
1052 case Mips::INSERT_FD_PSEUDO:
1053 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001054 case Mips::INSERT_B_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001055 case Mips::INSERT_B_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001056 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1057 case Mips::INSERT_H_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001058 case Mips::INSERT_H_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001059 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1060 case Mips::INSERT_W_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001061 case Mips::INSERT_W_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001062 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1063 case Mips::INSERT_D_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001064 case Mips::INSERT_D_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001065 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1066 case Mips::INSERT_FW_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001067 case Mips::INSERT_FW_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001068 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1069 case Mips::INSERT_FD_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001070 case Mips::INSERT_FD_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001071 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001072 case Mips::FILL_FW_PSEUDO:
1073 return emitFILL_FW(MI, BB);
1074 case Mips::FILL_FD_PSEUDO:
1075 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001076 case Mips::FEXP2_W_1_PSEUDO:
1077 return emitFEXP2_W_1(MI, BB);
1078 case Mips::FEXP2_D_1_PSEUDO:
1079 return emitFEXP2_D_1(MI, BB);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00001080 case Mips::ST_F16:
1081 return emitST_F16_PSEUDO(MI, BB);
1082 case Mips::LD_F16:
1083 return emitLD_F16_PSEUDO(MI, BB);
1084 case Mips::MSA_FP_EXTEND_W_PSEUDO:
1085 return emitFPEXTEND_PSEUDO(MI, BB, false);
1086 case Mips::MSA_FP_ROUND_W_PSEUDO:
1087 return emitFPROUND_PSEUDO(MI, BB, false);
1088 case Mips::MSA_FP_EXTEND_D_PSEUDO:
1089 return emitFPEXTEND_PSEUDO(MI, BB, true);
1090 case Mips::MSA_FP_ROUND_D_PSEUDO:
1091 return emitFPROUND_PSEUDO(MI, BB, true);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001092 }
1093}
1094
Daniel Sanders23e98772014-11-02 16:09:29 +00001095bool MipsSETargetLowering::isEligibleForTailCallOptimization(
1096 const CCState &CCInfo, unsigned NextStackOffset,
1097 const MipsFunctionInfo &FI) const {
Simon Dardis57f4ae42016-08-04 09:17:07 +00001098 if (!UseMipsTailCalls)
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001099 return false;
1100
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00001101 // Exception has to be cleared with eret.
1102 if (FI.isISR())
1103 return false;
1104
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001105 // Return false if either the callee or caller has a byval argument.
Daniel Sanders23e98772014-11-02 16:09:29 +00001106 if (CCInfo.getInRegsParamsCount() > 0 || FI.hasByvalArg())
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001107 return false;
1108
1109 // Return true if the callee's argument area is no larger than the
1110 // caller's.
1111 return NextStackOffset <= FI.getIncomingArgSize();
1112}
1113
1114void MipsSETargetLowering::
1115getOpndList(SmallVectorImpl<SDValue> &Ops,
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001116 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001117 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001118 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
1119 SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001120 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001121 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001122 InternalLinkage, IsCallReloc, CLI, Callee,
1123 Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001124}
1125
Akira Hatanaka63791212013-09-07 00:52:30 +00001126SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1127 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1128
1129 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1130 return MipsTargetLowering::lowerLOAD(Op, DAG);
1131
1132 // Replace a double precision load with two i32 loads and a buildpair64.
1133 SDLoc DL(Op);
1134 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1135 EVT PtrVT = Ptr.getValueType();
1136
1137 // i32 load from lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001138 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr, MachinePointerInfo(),
1139 Nd.getAlignment(), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001140
1141 // i32 load from higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001142 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001143 SDValue Hi = DAG.getLoad(
1144 MVT::i32, DL, Lo.getValue(1), Ptr, MachinePointerInfo(),
1145 std::min(Nd.getAlignment(), 4U), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001146
Eric Christopher1c29a652014-07-18 22:55:25 +00001147 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001148 std::swap(Lo, Hi);
1149
1150 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1151 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001152 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001153}
1154
1155SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1156 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1157
1158 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1159 return MipsTargetLowering::lowerSTORE(Op, DAG);
1160
1161 // Replace a double precision store with two extractelement64s and i32 stores.
1162 SDLoc DL(Op);
1163 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1164 EVT PtrVT = Ptr.getValueType();
1165 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001166 Val, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001167 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001168 Val, DAG.getConstant(1, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001169
Eric Christopher1c29a652014-07-18 22:55:25 +00001170 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001171 std::swap(Lo, Hi);
1172
1173 // i32 store to lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001174 Chain =
1175 DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(), Nd.getAlignment(),
1176 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001177
1178 // i32 store to higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001179 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Akira Hatanaka63791212013-09-07 00:52:30 +00001180 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001181 std::min(Nd.getAlignment(), 4U),
1182 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001183}
1184
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001185SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1186 bool HasLo, bool HasHi,
1187 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001188 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001189 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001190
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001191 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001192 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001193 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1194 Op.getOperand(0), Op.getOperand(1));
1195 SDValue Lo, Hi;
1196
1197 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001198 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001199 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001200 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001201
1202 if (!HasLo || !HasHi)
1203 return HasLo ? Lo : Hi;
1204
1205 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001206 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001207}
1208
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001209static SDValue initAccumulator(SDValue In, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001210 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001211 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001212 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001213 DAG.getConstant(1, DL, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001214 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001215}
1216
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001217static SDValue extractLOHI(SDValue Op, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001218 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1219 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001220 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1221}
1222
1223// This function expands mips intrinsic nodes which have 64-bit input operands
1224// or output values.
1225//
1226// out64 = intrinsic-node in64
1227// =>
1228// lo = copy (extract-element (in64, 0))
1229// hi = copy (extract-element (in64, 1))
1230// mips-specific-node
1231// v0 = copy lo
1232// v1 = copy hi
1233// out64 = merge-values (v0, v1)
1234//
1235static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001236 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001237 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1238 SmallVector<SDValue, 3> Ops;
1239 unsigned OpNo = 0;
1240
1241 // See if Op has a chain input.
1242 if (HasChainIn)
1243 Ops.push_back(Op->getOperand(OpNo++));
1244
1245 // The next operand is the intrinsic opcode.
1246 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1247
1248 // See if the next operand has type i64.
1249 SDValue Opnd = Op->getOperand(++OpNo), In64;
1250
1251 if (Opnd.getValueType() == MVT::i64)
1252 In64 = initAccumulator(Opnd, DL, DAG);
1253 else
1254 Ops.push_back(Opnd);
1255
1256 // Push the remaining operands.
1257 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1258 Ops.push_back(Op->getOperand(OpNo));
1259
1260 // Add In64 to the end of the list.
1261 if (In64.getNode())
1262 Ops.push_back(In64);
1263
1264 // Scan output.
1265 SmallVector<EVT, 2> ResTys;
1266
1267 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1268 I != E; ++I)
1269 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1270
1271 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001272 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001273 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1274
1275 if (!HasChainIn)
1276 return Out;
1277
1278 assert(Val->getValueType(1) == MVT::Other);
1279 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001280 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001281}
1282
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001283// Lower an MSA copy intrinsic into the specified SelectionDAG node
1284static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1285 SDLoc DL(Op);
1286 SDValue Vec = Op->getOperand(1);
1287 SDValue Idx = Op->getOperand(2);
1288 EVT ResTy = Op->getValueType(0);
1289 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1290
1291 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1292 DAG.getValueType(EltTy));
1293
1294 return Result;
1295}
1296
Daniel Sanders50b80412013-11-15 12:56:49 +00001297static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1298 EVT ResVecTy = Op->getValueType(0);
1299 EVT ViaVecTy = ResVecTy;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001300 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
Daniel Sanders50b80412013-11-15 12:56:49 +00001301 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001302
Daniel Sanders50b80412013-11-15 12:56:49 +00001303 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1304 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1305 // lanes.
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001306 SDValue LaneA = Op->getOperand(OpNr);
1307 SDValue LaneB;
Daniel Sanders50b80412013-11-15 12:56:49 +00001308
1309 if (ResVecTy == MVT::v2i64) {
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001310 LaneB = DAG.getConstant(0, DL, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001311 ViaVecTy = MVT::v4i32;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001312 if(BigEndian)
1313 std::swap(LaneA, LaneB);
Daniel Sanders50b80412013-11-15 12:56:49 +00001314 } else
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001315 LaneB = LaneA;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001316
Daniel Sanders50b80412013-11-15 12:56:49 +00001317 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1318 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001319
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001320 SDValue Result = DAG.getBuildVector(
1321 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001322
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001323 if (ViaVecTy != ResVecTy) {
1324 SDValue One = DAG.getConstant(1, DL, ViaVecTy);
1325 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy,
1326 DAG.getNode(ISD::AND, DL, ViaVecTy, Result, One));
1327 }
Daniel Sandersf49dd822013-09-24 13:33:07 +00001328
1329 return Result;
1330}
1331
Simon Dardis548a53f2017-01-10 16:40:57 +00001332static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG,
1333 bool IsSigned = false) {
1334 return DAG.getConstant(
1335 APInt(Op->getValueType(0).getScalarType().getSizeInBits(),
1336 Op->getConstantOperandVal(ImmOp), IsSigned),
1337 SDLoc(Op), Op->getValueType(0));
Daniel Sanders50b80412013-11-15 12:56:49 +00001338}
1339
1340static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1341 bool BigEndian, SelectionDAG &DAG) {
1342 EVT ViaVecTy = VecTy;
1343 SDValue SplatValueA = SplatValue;
1344 SDValue SplatValueB = SplatValue;
1345 SDLoc DL(SplatValue);
1346
1347 if (VecTy == MVT::v2i64) {
1348 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1349 ViaVecTy = MVT::v4i32;
1350
1351 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1352 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001353 DAG.getConstant(32, DL, MVT::i32));
Daniel Sanders50b80412013-11-15 12:56:49 +00001354 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1355 }
1356
1357 // We currently hold the parts in little endian order. Swap them if
1358 // necessary.
1359 if (BigEndian)
1360 std::swap(SplatValueA, SplatValueB);
1361
1362 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1363 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1364 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1365 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1366
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001367 SDValue Result = DAG.getBuildVector(
1368 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001369
1370 if (VecTy != ViaVecTy)
1371 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1372
1373 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001374}
1375
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001376static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1377 unsigned Opc, SDValue Imm,
1378 bool BigEndian) {
1379 EVT VecTy = Op->getValueType(0);
1380 SDValue Exp2Imm;
1381 SDLoc DL(Op);
1382
Daniel Sanders50b80412013-11-15 12:56:49 +00001383 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1384 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001385 if (VecTy == MVT::v2i64) {
1386 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1387 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1388
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001389 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), DL,
1390 MVT::i32);
1391 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), DL, MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001392
1393 if (BigEndian)
1394 std::swap(BitImmLoOp, BitImmHiOp);
1395
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001396 Exp2Imm = DAG.getNode(
1397 ISD::BITCAST, DL, MVT::v2i64,
1398 DAG.getBuildVector(MVT::v4i32, DL,
1399 {BitImmLoOp, BitImmHiOp, BitImmLoOp, BitImmHiOp}));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001400 }
1401 }
1402
Craig Topper062a2ba2014-04-25 05:30:21 +00001403 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001404 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001405
1406 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1407 // only values 0-63 are valid.
1408 if (VecTy == MVT::v2i64)
1409 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1410
1411 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1412
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001413 Exp2Imm = DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, DL, VecTy),
1414 Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001415 }
1416
1417 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1418}
1419
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001420static SDValue truncateVecElts(SDValue Op, SelectionDAG &DAG) {
1421 SDLoc DL(Op);
1422 EVT ResTy = Op->getValueType(0);
1423 SDValue Vec = Op->getOperand(2);
1424 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
1425 MVT ResEltTy = ResTy == MVT::v2i64 ? MVT::i64 : MVT::i32;
1426 SDValue ConstValue = DAG.getConstant(Vec.getScalarValueSizeInBits() - 1,
1427 DL, ResEltTy);
1428 SDValue SplatVec = getBuildVectorSplat(ResTy, ConstValue, BigEndian, DAG);
1429
1430 return DAG.getNode(ISD::AND, DL, ResTy, Vec, SplatVec);
1431}
1432
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001433static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1434 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001435 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001436 SDValue One = DAG.getConstant(1, DL, ResTy);
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001437 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, truncateVecElts(Op, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001438
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001439 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1440 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001441}
1442
1443static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1444 SDLoc DL(Op);
1445 EVT ResTy = Op->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001446 APInt BitImm = APInt(ResTy.getScalarSizeInBits(), 1)
Daniel Sanders50b80412013-11-15 12:56:49 +00001447 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001448 SDValue BitMask = DAG.getConstant(~BitImm, DL, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001449
1450 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1451}
1452
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001453SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1454 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001455 SDLoc DL(Op);
Simon Dardis548a53f2017-01-10 16:40:57 +00001456 unsigned Intrinsic = cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue();
1457 switch (Intrinsic) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001458 default:
1459 return SDValue();
1460 case Intrinsic::mips_shilo:
1461 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1462 case Intrinsic::mips_dpau_h_qbl:
1463 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1464 case Intrinsic::mips_dpau_h_qbr:
1465 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1466 case Intrinsic::mips_dpsu_h_qbl:
1467 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1468 case Intrinsic::mips_dpsu_h_qbr:
1469 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1470 case Intrinsic::mips_dpa_w_ph:
1471 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1472 case Intrinsic::mips_dps_w_ph:
1473 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1474 case Intrinsic::mips_dpax_w_ph:
1475 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1476 case Intrinsic::mips_dpsx_w_ph:
1477 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1478 case Intrinsic::mips_mulsa_w_ph:
1479 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1480 case Intrinsic::mips_mult:
1481 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1482 case Intrinsic::mips_multu:
1483 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1484 case Intrinsic::mips_madd:
1485 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1486 case Intrinsic::mips_maddu:
1487 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1488 case Intrinsic::mips_msub:
1489 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1490 case Intrinsic::mips_msubu:
1491 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001492 case Intrinsic::mips_addv_b:
1493 case Intrinsic::mips_addv_h:
1494 case Intrinsic::mips_addv_w:
1495 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001496 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1497 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001498 case Intrinsic::mips_addvi_b:
1499 case Intrinsic::mips_addvi_h:
1500 case Intrinsic::mips_addvi_w:
1501 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001502 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1503 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001504 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001505 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1506 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001507 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001508 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1509 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001510 case Intrinsic::mips_bclr_b:
1511 case Intrinsic::mips_bclr_h:
1512 case Intrinsic::mips_bclr_w:
1513 case Intrinsic::mips_bclr_d:
1514 return lowerMSABitClear(Op, DAG);
1515 case Intrinsic::mips_bclri_b:
1516 case Intrinsic::mips_bclri_h:
1517 case Intrinsic::mips_bclri_w:
1518 case Intrinsic::mips_bclri_d:
1519 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001520 case Intrinsic::mips_binsli_b:
1521 case Intrinsic::mips_binsli_h:
1522 case Intrinsic::mips_binsli_w:
1523 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001524 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001525 EVT VecTy = Op->getValueType(0);
1526 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001527 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1528 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001529 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001530 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001531 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001532 DAG.getConstant(Mask, DL, VecTy, true),
1533 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001534 }
1535 case Intrinsic::mips_binsri_b:
1536 case Intrinsic::mips_binsri_h:
1537 case Intrinsic::mips_binsri_w:
1538 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001539 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001540 EVT VecTy = Op->getValueType(0);
1541 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001542 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1543 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001544 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001545 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001546 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001547 DAG.getConstant(Mask, DL, VecTy, true),
1548 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001549 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001550 case Intrinsic::mips_bmnz_v:
1551 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1552 Op->getOperand(2), Op->getOperand(1));
1553 case Intrinsic::mips_bmnzi_b:
1554 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1555 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1556 Op->getOperand(1));
1557 case Intrinsic::mips_bmz_v:
1558 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1559 Op->getOperand(1), Op->getOperand(2));
1560 case Intrinsic::mips_bmzi_b:
1561 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1562 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1563 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001564 case Intrinsic::mips_bneg_b:
1565 case Intrinsic::mips_bneg_h:
1566 case Intrinsic::mips_bneg_w:
1567 case Intrinsic::mips_bneg_d: {
1568 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001569 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001570
1571 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1572 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001573 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001574 }
1575 case Intrinsic::mips_bnegi_b:
1576 case Intrinsic::mips_bnegi_h:
1577 case Intrinsic::mips_bnegi_w:
1578 case Intrinsic::mips_bnegi_d:
1579 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001580 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001581 case Intrinsic::mips_bnz_b:
1582 case Intrinsic::mips_bnz_h:
1583 case Intrinsic::mips_bnz_w:
1584 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001585 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1586 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001587 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001588 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1589 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001590 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001591 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001592 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001593 Op->getOperand(1), Op->getOperand(3),
1594 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001595 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001596 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001597 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001598 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1599 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001600 case Intrinsic::mips_bset_b:
1601 case Intrinsic::mips_bset_h:
1602 case Intrinsic::mips_bset_w:
1603 case Intrinsic::mips_bset_d: {
1604 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001605 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001606
1607 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1608 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001609 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001610 }
1611 case Intrinsic::mips_bseti_b:
1612 case Intrinsic::mips_bseti_h:
1613 case Intrinsic::mips_bseti_w:
1614 case Intrinsic::mips_bseti_d:
1615 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001616 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001617 case Intrinsic::mips_bz_b:
1618 case Intrinsic::mips_bz_h:
1619 case Intrinsic::mips_bz_w:
1620 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001621 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1622 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001623 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001624 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1625 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001626 case Intrinsic::mips_ceq_b:
1627 case Intrinsic::mips_ceq_h:
1628 case Intrinsic::mips_ceq_w:
1629 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001630 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001631 Op->getOperand(2), ISD::SETEQ);
1632 case Intrinsic::mips_ceqi_b:
1633 case Intrinsic::mips_ceqi_h:
1634 case Intrinsic::mips_ceqi_w:
1635 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001636 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001637 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETEQ);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001638 case Intrinsic::mips_cle_s_b:
1639 case Intrinsic::mips_cle_s_h:
1640 case Intrinsic::mips_cle_s_w:
1641 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001642 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001643 Op->getOperand(2), ISD::SETLE);
1644 case Intrinsic::mips_clei_s_b:
1645 case Intrinsic::mips_clei_s_h:
1646 case Intrinsic::mips_clei_s_w:
1647 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001648 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001649 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLE);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001650 case Intrinsic::mips_cle_u_b:
1651 case Intrinsic::mips_cle_u_h:
1652 case Intrinsic::mips_cle_u_w:
1653 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001654 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001655 Op->getOperand(2), ISD::SETULE);
1656 case Intrinsic::mips_clei_u_b:
1657 case Intrinsic::mips_clei_u_h:
1658 case Intrinsic::mips_clei_u_w:
1659 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001660 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001661 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1662 case Intrinsic::mips_clt_s_b:
1663 case Intrinsic::mips_clt_s_h:
1664 case Intrinsic::mips_clt_s_w:
1665 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001666 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001667 Op->getOperand(2), ISD::SETLT);
1668 case Intrinsic::mips_clti_s_b:
1669 case Intrinsic::mips_clti_s_h:
1670 case Intrinsic::mips_clti_s_w:
1671 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001672 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001673 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLT);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001674 case Intrinsic::mips_clt_u_b:
1675 case Intrinsic::mips_clt_u_h:
1676 case Intrinsic::mips_clt_u_w:
1677 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001678 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001679 Op->getOperand(2), ISD::SETULT);
1680 case Intrinsic::mips_clti_u_b:
1681 case Intrinsic::mips_clti_u_h:
1682 case Intrinsic::mips_clti_u_w:
1683 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001684 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001685 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001686 case Intrinsic::mips_copy_s_b:
1687 case Intrinsic::mips_copy_s_h:
1688 case Intrinsic::mips_copy_s_w:
1689 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001690 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001691 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001692 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1693 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1694 else {
1695 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1696 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1697 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1698 Op->getValueType(0), Op->getOperand(1),
1699 Op->getOperand(2));
1700 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001701 case Intrinsic::mips_copy_u_b:
1702 case Intrinsic::mips_copy_u_h:
1703 case Intrinsic::mips_copy_u_w:
1704 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001705 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001706 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001707 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1708 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1709 else {
1710 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1711 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1712 // Note: When i64 is illegal, this results in copy_s.w instructions
1713 // instead of copy_u.w instructions. This makes no difference to the
1714 // behaviour since i64 is only illegal when the register file is 32-bit.
1715 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1716 Op->getValueType(0), Op->getOperand(1),
1717 Op->getOperand(2));
1718 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001719 case Intrinsic::mips_div_s_b:
1720 case Intrinsic::mips_div_s_h:
1721 case Intrinsic::mips_div_s_w:
1722 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001723 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1724 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001725 case Intrinsic::mips_div_u_b:
1726 case Intrinsic::mips_div_u_h:
1727 case Intrinsic::mips_div_u_w:
1728 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001729 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1730 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001731 case Intrinsic::mips_fadd_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001732 case Intrinsic::mips_fadd_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001733 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001734 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1735 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001736 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1737 case Intrinsic::mips_fceq_w:
1738 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001739 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001740 Op->getOperand(2), ISD::SETOEQ);
1741 case Intrinsic::mips_fcle_w:
1742 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001743 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001744 Op->getOperand(2), ISD::SETOLE);
1745 case Intrinsic::mips_fclt_w:
1746 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001747 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001748 Op->getOperand(2), ISD::SETOLT);
1749 case Intrinsic::mips_fcne_w:
1750 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001751 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001752 Op->getOperand(2), ISD::SETONE);
1753 case Intrinsic::mips_fcor_w:
1754 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001755 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001756 Op->getOperand(2), ISD::SETO);
1757 case Intrinsic::mips_fcueq_w:
1758 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001759 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001760 Op->getOperand(2), ISD::SETUEQ);
1761 case Intrinsic::mips_fcule_w:
1762 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001763 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001764 Op->getOperand(2), ISD::SETULE);
1765 case Intrinsic::mips_fcult_w:
1766 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001767 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001768 Op->getOperand(2), ISD::SETULT);
1769 case Intrinsic::mips_fcun_w:
1770 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001771 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001772 Op->getOperand(2), ISD::SETUO);
1773 case Intrinsic::mips_fcune_w:
1774 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001775 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001776 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001777 case Intrinsic::mips_fdiv_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001778 case Intrinsic::mips_fdiv_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001779 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001780 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1781 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001782 case Intrinsic::mips_ffint_u_w:
1783 case Intrinsic::mips_ffint_u_d:
1784 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1785 Op->getOperand(1));
1786 case Intrinsic::mips_ffint_s_w:
1787 case Intrinsic::mips_ffint_s_d:
1788 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1789 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001790 case Intrinsic::mips_fill_b:
1791 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001792 case Intrinsic::mips_fill_w:
1793 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001794 EVT ResTy = Op->getValueType(0);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001795 SmallVector<SDValue, 16> Ops(ResTy.getVectorNumElements(),
1796 Op->getOperand(1));
Daniel Sandersf49dd822013-09-24 13:33:07 +00001797
Daniel Sandersc72593e2013-09-27 13:20:41 +00001798 // If ResTy is v2i64 then the type legalizer will break this node down into
1799 // an equivalent v4i32.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001800 return DAG.getBuildVector(ResTy, DL, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001801 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001802 case Intrinsic::mips_fexp2_w:
1803 case Intrinsic::mips_fexp2_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001804 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sandersa9521602013-10-23 10:36:52 +00001805 EVT ResTy = Op->getValueType(0);
1806 return DAG.getNode(
1807 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1808 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1809 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001810 case Intrinsic::mips_flog2_w:
1811 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001812 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001813 case Intrinsic::mips_fmadd_w:
1814 case Intrinsic::mips_fmadd_d:
1815 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1816 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001817 case Intrinsic::mips_fmul_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001818 case Intrinsic::mips_fmul_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001819 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001820 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1821 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001822 case Intrinsic::mips_fmsub_w:
1823 case Intrinsic::mips_fmsub_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001824 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanderse67bd872013-10-11 10:27:32 +00001825 EVT ResTy = Op->getValueType(0);
1826 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1827 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1828 Op->getOperand(2), Op->getOperand(3)));
1829 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001830 case Intrinsic::mips_frint_w:
1831 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001832 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001833 case Intrinsic::mips_fsqrt_w:
1834 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001835 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001836 case Intrinsic::mips_fsub_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001837 case Intrinsic::mips_fsub_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001838 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001839 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1840 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001841 case Intrinsic::mips_ftrunc_u_w:
1842 case Intrinsic::mips_ftrunc_u_d:
1843 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1844 Op->getOperand(1));
1845 case Intrinsic::mips_ftrunc_s_w:
1846 case Intrinsic::mips_ftrunc_s_d:
1847 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1848 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001849 case Intrinsic::mips_ilvev_b:
1850 case Intrinsic::mips_ilvev_h:
1851 case Intrinsic::mips_ilvev_w:
1852 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001853 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001854 Op->getOperand(1), Op->getOperand(2));
1855 case Intrinsic::mips_ilvl_b:
1856 case Intrinsic::mips_ilvl_h:
1857 case Intrinsic::mips_ilvl_w:
1858 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001859 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001860 Op->getOperand(1), Op->getOperand(2));
1861 case Intrinsic::mips_ilvod_b:
1862 case Intrinsic::mips_ilvod_h:
1863 case Intrinsic::mips_ilvod_w:
1864 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001865 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001866 Op->getOperand(1), Op->getOperand(2));
1867 case Intrinsic::mips_ilvr_b:
1868 case Intrinsic::mips_ilvr_h:
1869 case Intrinsic::mips_ilvr_w:
1870 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001871 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001872 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001873 case Intrinsic::mips_insert_b:
1874 case Intrinsic::mips_insert_h:
1875 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001876 case Intrinsic::mips_insert_d:
1877 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1878 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001879 case Intrinsic::mips_insve_b:
1880 case Intrinsic::mips_insve_h:
1881 case Intrinsic::mips_insve_w:
Simon Dardis548a53f2017-01-10 16:40:57 +00001882 case Intrinsic::mips_insve_d: {
1883 // Report an error for out of range values.
1884 int64_t Max;
1885 switch (Intrinsic) {
1886 case Intrinsic::mips_insve_b: Max = 15; break;
1887 case Intrinsic::mips_insve_h: Max = 7; break;
1888 case Intrinsic::mips_insve_w: Max = 3; break;
1889 case Intrinsic::mips_insve_d: Max = 1; break;
1890 default: llvm_unreachable("Unmatched intrinsic");
1891 }
1892 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
1893 if (Value < 0 || Value > Max)
1894 report_fatal_error("Immediate out of range");
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001895 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1896 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001897 DAG.getConstant(0, DL, MVT::i32));
Simon Dardis548a53f2017-01-10 16:40:57 +00001898 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00001899 case Intrinsic::mips_ldi_b:
1900 case Intrinsic::mips_ldi_h:
1901 case Intrinsic::mips_ldi_w:
1902 case Intrinsic::mips_ldi_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00001903 return lowerMSASplatImm(Op, 1, DAG, true);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001904 case Intrinsic::mips_lsa:
1905 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001906 EVT ResTy = Op->getValueType(0);
1907 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1908 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1909 Op->getOperand(2), Op->getOperand(3)));
1910 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001911 case Intrinsic::mips_maddv_b:
1912 case Intrinsic::mips_maddv_h:
1913 case Intrinsic::mips_maddv_w:
1914 case Intrinsic::mips_maddv_d: {
1915 EVT ResTy = Op->getValueType(0);
1916 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1917 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1918 Op->getOperand(2), Op->getOperand(3)));
1919 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001920 case Intrinsic::mips_max_s_b:
1921 case Intrinsic::mips_max_s_h:
1922 case Intrinsic::mips_max_s_w:
1923 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001924 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1925 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001926 case Intrinsic::mips_max_u_b:
1927 case Intrinsic::mips_max_u_h:
1928 case Intrinsic::mips_max_u_w:
1929 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001930 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1931 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001932 case Intrinsic::mips_maxi_s_b:
1933 case Intrinsic::mips_maxi_s_h:
1934 case Intrinsic::mips_maxi_s_w:
1935 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001936 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00001937 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001938 case Intrinsic::mips_maxi_u_b:
1939 case Intrinsic::mips_maxi_u_h:
1940 case Intrinsic::mips_maxi_u_w:
1941 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001942 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1943 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001944 case Intrinsic::mips_min_s_b:
1945 case Intrinsic::mips_min_s_h:
1946 case Intrinsic::mips_min_s_w:
1947 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001948 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1949 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001950 case Intrinsic::mips_min_u_b:
1951 case Intrinsic::mips_min_u_h:
1952 case Intrinsic::mips_min_u_w:
1953 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001954 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1955 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001956 case Intrinsic::mips_mini_s_b:
1957 case Intrinsic::mips_mini_s_h:
1958 case Intrinsic::mips_mini_s_w:
1959 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001960 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00001961 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001962 case Intrinsic::mips_mini_u_b:
1963 case Intrinsic::mips_mini_u_h:
1964 case Intrinsic::mips_mini_u_w:
1965 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001966 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1967 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001968 case Intrinsic::mips_mod_s_b:
1969 case Intrinsic::mips_mod_s_h:
1970 case Intrinsic::mips_mod_s_w:
1971 case Intrinsic::mips_mod_s_d:
1972 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1973 Op->getOperand(2));
1974 case Intrinsic::mips_mod_u_b:
1975 case Intrinsic::mips_mod_u_h:
1976 case Intrinsic::mips_mod_u_w:
1977 case Intrinsic::mips_mod_u_d:
1978 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1979 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001980 case Intrinsic::mips_mulv_b:
1981 case Intrinsic::mips_mulv_h:
1982 case Intrinsic::mips_mulv_w:
1983 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001984 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1985 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001986 case Intrinsic::mips_msubv_b:
1987 case Intrinsic::mips_msubv_h:
1988 case Intrinsic::mips_msubv_w:
1989 case Intrinsic::mips_msubv_d: {
1990 EVT ResTy = Op->getValueType(0);
1991 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
1992 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1993 Op->getOperand(2), Op->getOperand(3)));
1994 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001995 case Intrinsic::mips_nlzc_b:
1996 case Intrinsic::mips_nlzc_h:
1997 case Intrinsic::mips_nlzc_w:
1998 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001999 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002000 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002001 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2002 Op->getOperand(1), Op->getOperand(2));
2003 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002004 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002005 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002006 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2007 Op->getOperand(1),
2008 lowerMSASplatImm(Op, 2, DAG));
2009 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002010 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002011 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002012 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2013 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002014 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002015 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2016 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002017 case Intrinsic::mips_pckev_b:
2018 case Intrinsic::mips_pckev_h:
2019 case Intrinsic::mips_pckev_w:
2020 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002021 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002022 Op->getOperand(1), Op->getOperand(2));
2023 case Intrinsic::mips_pckod_b:
2024 case Intrinsic::mips_pckod_h:
2025 case Intrinsic::mips_pckod_w:
2026 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002027 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002028 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002029 case Intrinsic::mips_pcnt_b:
2030 case Intrinsic::mips_pcnt_h:
2031 case Intrinsic::mips_pcnt_w:
2032 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002033 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002034 case Intrinsic::mips_sat_s_b:
2035 case Intrinsic::mips_sat_s_h:
2036 case Intrinsic::mips_sat_s_w:
2037 case Intrinsic::mips_sat_s_d:
2038 case Intrinsic::mips_sat_u_b:
2039 case Intrinsic::mips_sat_u_h:
2040 case Intrinsic::mips_sat_u_w:
2041 case Intrinsic::mips_sat_u_d: {
2042 // Report an error for out of range values.
2043 int64_t Max;
2044 switch (Intrinsic) {
2045 case Intrinsic::mips_sat_s_b:
2046 case Intrinsic::mips_sat_u_b: Max = 7; break;
2047 case Intrinsic::mips_sat_s_h:
2048 case Intrinsic::mips_sat_u_h: Max = 15; break;
2049 case Intrinsic::mips_sat_s_w:
2050 case Intrinsic::mips_sat_u_w: Max = 31; break;
2051 case Intrinsic::mips_sat_s_d:
2052 case Intrinsic::mips_sat_u_d: Max = 63; break;
2053 default: llvm_unreachable("Unmatched intrinsic");
2054 }
2055 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2056 if (Value < 0 || Value > Max)
2057 report_fatal_error("Immediate out of range");
2058 return SDValue();
2059 }
Daniel Sanders26307182013-09-24 14:20:00 +00002060 case Intrinsic::mips_shf_b:
2061 case Intrinsic::mips_shf_h:
Simon Dardis548a53f2017-01-10 16:40:57 +00002062 case Intrinsic::mips_shf_w: {
2063 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2064 if (Value < 0 || Value > 255)
2065 report_fatal_error("Immediate out of range");
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002066 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002067 Op->getOperand(2), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002068 }
2069 case Intrinsic::mips_sldi_b:
2070 case Intrinsic::mips_sldi_h:
2071 case Intrinsic::mips_sldi_w:
2072 case Intrinsic::mips_sldi_d: {
2073 // Report an error for out of range values.
2074 int64_t Max;
2075 switch (Intrinsic) {
2076 case Intrinsic::mips_sldi_b: Max = 15; break;
2077 case Intrinsic::mips_sldi_h: Max = 7; break;
2078 case Intrinsic::mips_sldi_w: Max = 3; break;
2079 case Intrinsic::mips_sldi_d: Max = 1; break;
2080 default: llvm_unreachable("Unmatched intrinsic");
2081 }
2082 int64_t Value = cast<ConstantSDNode>(Op->getOperand(3))->getSExtValue();
2083 if (Value < 0 || Value > Max)
2084 report_fatal_error("Immediate out of range");
2085 return SDValue();
2086 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002087 case Intrinsic::mips_sll_b:
2088 case Intrinsic::mips_sll_h:
2089 case Intrinsic::mips_sll_w:
2090 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002091 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002092 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002093 case Intrinsic::mips_slli_b:
2094 case Intrinsic::mips_slli_h:
2095 case Intrinsic::mips_slli_w:
2096 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002097 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2098 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002099 case Intrinsic::mips_splat_b:
2100 case Intrinsic::mips_splat_h:
2101 case Intrinsic::mips_splat_w:
2102 case Intrinsic::mips_splat_d:
2103 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2104 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2105 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2106 // Instead we lower to MipsISD::VSHF and match from there.
2107 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002108 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002109 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002110 case Intrinsic::mips_splati_b:
2111 case Intrinsic::mips_splati_h:
2112 case Intrinsic::mips_splati_w:
2113 case Intrinsic::mips_splati_d:
2114 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2115 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2116 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002117 case Intrinsic::mips_sra_b:
2118 case Intrinsic::mips_sra_h:
2119 case Intrinsic::mips_sra_w:
2120 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002121 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002122 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002123 case Intrinsic::mips_srai_b:
2124 case Intrinsic::mips_srai_h:
2125 case Intrinsic::mips_srai_w:
2126 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002127 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2128 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002129 case Intrinsic::mips_srari_b:
2130 case Intrinsic::mips_srari_h:
2131 case Intrinsic::mips_srari_w:
2132 case Intrinsic::mips_srari_d: {
2133 // Report an error for out of range values.
2134 int64_t Max;
2135 switch (Intrinsic) {
2136 case Intrinsic::mips_srari_b: Max = 7; break;
2137 case Intrinsic::mips_srari_h: Max = 15; break;
2138 case Intrinsic::mips_srari_w: Max = 31; break;
2139 case Intrinsic::mips_srari_d: Max = 63; break;
2140 default: llvm_unreachable("Unmatched intrinsic");
2141 }
2142 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2143 if (Value < 0 || Value > Max)
2144 report_fatal_error("Immediate out of range");
2145 return SDValue();
2146 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002147 case Intrinsic::mips_srl_b:
2148 case Intrinsic::mips_srl_h:
2149 case Intrinsic::mips_srl_w:
2150 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002151 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002152 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002153 case Intrinsic::mips_srli_b:
2154 case Intrinsic::mips_srli_h:
2155 case Intrinsic::mips_srli_w:
2156 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002157 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2158 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002159 case Intrinsic::mips_srlri_b:
2160 case Intrinsic::mips_srlri_h:
2161 case Intrinsic::mips_srlri_w:
2162 case Intrinsic::mips_srlri_d: {
2163 // Report an error for out of range values.
2164 int64_t Max;
2165 switch (Intrinsic) {
2166 case Intrinsic::mips_srlri_b: Max = 7; break;
2167 case Intrinsic::mips_srlri_h: Max = 15; break;
2168 case Intrinsic::mips_srlri_w: Max = 31; break;
2169 case Intrinsic::mips_srlri_d: Max = 63; break;
2170 default: llvm_unreachable("Unmatched intrinsic");
2171 }
2172 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2173 if (Value < 0 || Value > Max)
2174 report_fatal_error("Immediate out of range");
2175 return SDValue();
2176 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002177 case Intrinsic::mips_subv_b:
2178 case Intrinsic::mips_subv_h:
2179 case Intrinsic::mips_subv_w:
2180 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002181 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2182 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002183 case Intrinsic::mips_subvi_b:
2184 case Intrinsic::mips_subvi_h:
2185 case Intrinsic::mips_subvi_w:
2186 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002187 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2188 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002189 case Intrinsic::mips_vshf_b:
2190 case Intrinsic::mips_vshf_h:
2191 case Intrinsic::mips_vshf_w:
2192 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002193 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002194 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002195 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002196 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2197 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002198 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002199 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2200 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Marcin Koscielnicki7efdca52016-04-27 17:21:49 +00002201 case Intrinsic::thread_pointer: {
2202 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2203 return DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2204 }
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002205 }
2206}
2207
Simon Dardis548a53f2017-01-10 16:40:57 +00002208static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2209 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002210 SDLoc DL(Op);
2211 SDValue ChainIn = Op->getOperand(0);
2212 SDValue Address = Op->getOperand(2);
2213 SDValue Offset = Op->getOperand(3);
2214 EVT ResTy = Op->getValueType(0);
2215 EVT PtrTy = Address->getValueType(0);
2216
Simon Dardis548a53f2017-01-10 16:40:57 +00002217 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2218 // however takes an i32 signed constant offset. The actual type of the
2219 // intrinsic is a scaled signed i10.
2220 if (Subtarget.isABI_N64())
2221 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2222
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002223 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002224 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(),
2225 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002226}
2227
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002228SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2229 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002230 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2231 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002232 default:
2233 return SDValue();
2234 case Intrinsic::mips_extp:
2235 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2236 case Intrinsic::mips_extpdp:
2237 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2238 case Intrinsic::mips_extr_w:
2239 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2240 case Intrinsic::mips_extr_r_w:
2241 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2242 case Intrinsic::mips_extr_rs_w:
2243 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2244 case Intrinsic::mips_extr_s_h:
2245 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2246 case Intrinsic::mips_mthlip:
2247 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2248 case Intrinsic::mips_mulsaq_s_w_ph:
2249 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2250 case Intrinsic::mips_maq_s_w_phl:
2251 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2252 case Intrinsic::mips_maq_s_w_phr:
2253 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2254 case Intrinsic::mips_maq_sa_w_phl:
2255 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2256 case Intrinsic::mips_maq_sa_w_phr:
2257 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2258 case Intrinsic::mips_dpaq_s_w_ph:
2259 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2260 case Intrinsic::mips_dpsq_s_w_ph:
2261 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2262 case Intrinsic::mips_dpaq_sa_l_w:
2263 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2264 case Intrinsic::mips_dpsq_sa_l_w:
2265 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2266 case Intrinsic::mips_dpaqx_s_w_ph:
2267 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2268 case Intrinsic::mips_dpaqx_sa_w_ph:
2269 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2270 case Intrinsic::mips_dpsqx_s_w_ph:
2271 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2272 case Intrinsic::mips_dpsqx_sa_w_ph:
2273 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002274 case Intrinsic::mips_ld_b:
2275 case Intrinsic::mips_ld_h:
2276 case Intrinsic::mips_ld_w:
2277 case Intrinsic::mips_ld_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002278 return lowerMSALoadIntr(Op, DAG, Intr, Subtarget);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002279 }
2280}
2281
Simon Dardis548a53f2017-01-10 16:40:57 +00002282static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2283 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002284 SDLoc DL(Op);
2285 SDValue ChainIn = Op->getOperand(0);
2286 SDValue Value = Op->getOperand(2);
2287 SDValue Address = Op->getOperand(3);
2288 SDValue Offset = Op->getOperand(4);
2289 EVT PtrTy = Address->getValueType(0);
2290
Simon Dardis548a53f2017-01-10 16:40:57 +00002291 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2292 // however takes an i32 signed constant offset. The actual type of the
2293 // intrinsic is a scaled signed i10.
2294 if (Subtarget.isABI_N64())
2295 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2296
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002297 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2298
Justin Lebar9c375812016-07-15 18:27:10 +00002299 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(),
2300 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002301}
2302
2303SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2304 SelectionDAG &DAG) const {
2305 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2306 switch (Intr) {
2307 default:
2308 return SDValue();
2309 case Intrinsic::mips_st_b:
2310 case Intrinsic::mips_st_h:
2311 case Intrinsic::mips_st_w:
2312 case Intrinsic::mips_st_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002313 return lowerMSAStoreIntr(Op, DAG, Intr, Subtarget);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002314 }
2315}
2316
Daniel Sanders7a289d02013-09-23 12:02:46 +00002317/// \brief Check if the given BuildVectorSDNode is a splat.
2318/// This method currently relies on DAG nodes being reused when equivalent,
2319/// so it's possible for this to return false even when isConstantSplat returns
2320/// true.
2321static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002322 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002323 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002324
2325 SDValue Operand0 = N->getOperand(0);
2326
2327 for (unsigned int i = 1; i < nOps; ++i) {
2328 if (N->getOperand(i) != Operand0)
2329 return false;
2330 }
2331
2332 return true;
2333}
2334
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002335// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2336//
2337// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2338// choose to sign-extend but we could have equally chosen zero-extend. The
2339// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2340// result into this node later (possibly changing it to a zero-extend in the
2341// process).
2342SDValue MipsSETargetLowering::
2343lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2344 SDLoc DL(Op);
2345 EVT ResTy = Op->getValueType(0);
2346 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002347 EVT VecTy = Op0->getValueType(0);
2348
2349 if (!VecTy.is128BitVector())
2350 return SDValue();
2351
2352 if (ResTy.isInteger()) {
2353 SDValue Op1 = Op->getOperand(1);
2354 EVT EltTy = VecTy.getVectorElementType();
2355 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2356 DAG.getValueType(EltTy));
2357 }
2358
2359 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002360}
2361
Daniel Sandersf49dd822013-09-24 13:33:07 +00002362static bool isConstantOrUndef(const SDValue Op) {
Sanjay Patel57195842016-03-14 17:28:46 +00002363 if (Op->isUndef())
Daniel Sandersf49dd822013-09-24 13:33:07 +00002364 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002365 if (isa<ConstantSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002366 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002367 if (isa<ConstantFPSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002368 return true;
2369 return false;
2370}
2371
2372static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2373 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2374 if (isConstantOrUndef(Op->getOperand(i)))
2375 return true;
2376 return false;
2377}
2378
Daniel Sanders7a289d02013-09-23 12:02:46 +00002379// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2380// backend.
2381//
2382// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002383// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2384// 2 less than or equal to 64 and the value fits into a signed 10-bit
2385// immediate
2386// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2387// is a power of 2 less than or equal to 64 and the value does not fit into a
2388// signed 10-bit immediate
2389// - Non-constant splats are legal as-is.
2390// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2391// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002392SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2393 SelectionDAG &DAG) const {
2394 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2395 EVT ResTy = Op->getValueType(0);
2396 SDLoc DL(Op);
2397 APInt SplatValue, SplatUndef;
2398 unsigned SplatBitSize;
2399 bool HasAnyUndefs;
2400
Eric Christopher1c29a652014-07-18 22:55:25 +00002401 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002402 return SDValue();
2403
2404 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2405 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002406 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002407 // We can only cope with 8, 16, 32, or 64-bit elements
2408 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2409 SplatBitSize != 64)
2410 return SDValue();
2411
Simon Dardis7090d142017-03-10 13:27:14 +00002412 // If the value isn't an integer type we will have to bitcast
2413 // from an integer type first. Also, if there are any undefs, we must
2414 // lower them to defined values first.
2415 if (ResTy.isInteger() && !HasAnyUndefs)
Daniel Sandersf49dd822013-09-24 13:33:07 +00002416 return Op;
2417
2418 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002419
2420 switch (SplatBitSize) {
2421 default:
2422 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002423 case 8:
2424 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002425 break;
2426 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002427 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002428 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002429 case 32:
2430 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002431 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002432 case 64:
2433 // There's no fill.d to fall back on for 64-bit values
2434 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002435 }
2436
Daniel Sanders50b80412013-11-15 12:56:49 +00002437 // SelectionDAG::getConstant will promote SplatValue appropriately.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002438 SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002439
Daniel Sanders50b80412013-11-15 12:56:49 +00002440 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002441 if (ViaVecTy != ResTy)
2442 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002443
2444 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002445 } else if (isSplatVector(Node))
2446 return Op;
2447 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002448 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2449 // The resulting code is the same length as the expansion, but it doesn't
2450 // use memory operations
2451 EVT ResTy = Node->getValueType(0);
2452
2453 assert(ResTy.isVector());
2454
2455 unsigned NumElts = ResTy.getVectorNumElements();
2456 SDValue Vector = DAG.getUNDEF(ResTy);
2457 for (unsigned i = 0; i < NumElts; ++i) {
2458 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2459 Node->getOperand(i),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002460 DAG.getConstant(i, DL, MVT::i32));
Daniel Sandersf86622b2013-09-24 13:16:15 +00002461 }
2462 return Vector;
2463 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002464
2465 return SDValue();
2466}
2467
Daniel Sanders26307182013-09-24 14:20:00 +00002468// Lower VECTOR_SHUFFLE into SHF (if possible).
2469//
2470// SHF splits the vector into blocks of four elements, then shuffles these
2471// elements according to a <4 x i2> constant (encoded as an integer immediate).
2472//
2473// It is therefore possible to lower into SHF when the mask takes the form:
2474// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2475// When undef's appear they are treated as if they were whatever value is
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002476// necessary in order to fit the above forms.
Daniel Sanders26307182013-09-24 14:20:00 +00002477//
2478// For example:
2479// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2480// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2481// i32 7, i32 6, i32 5, i32 4>
2482// is lowered to:
2483// (SHF_H $w0, $w1, 27)
2484// where the 27 comes from:
2485// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2486static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2487 SmallVector<int, 16> Indices,
2488 SelectionDAG &DAG) {
2489 int SHFIndices[4] = { -1, -1, -1, -1 };
2490
2491 if (Indices.size() < 4)
2492 return SDValue();
2493
2494 for (unsigned i = 0; i < 4; ++i) {
2495 for (unsigned j = i; j < Indices.size(); j += 4) {
2496 int Idx = Indices[j];
2497
2498 // Convert from vector index to 4-element subvector index
2499 // If an index refers to an element outside of the subvector then give up
2500 if (Idx != -1) {
2501 Idx -= 4 * (j / 4);
2502 if (Idx < 0 || Idx >= 4)
2503 return SDValue();
2504 }
2505
2506 // If the mask has an undef, replace it with the current index.
2507 // Note that it might still be undef if the current index is also undef
2508 if (SHFIndices[i] == -1)
2509 SHFIndices[i] = Idx;
2510
2511 // Check that non-undef values are the same as in the mask. If they
2512 // aren't then give up
2513 if (!(Idx == -1 || Idx == SHFIndices[i]))
2514 return SDValue();
2515 }
2516 }
2517
2518 // Calculate the immediate. Replace any remaining undefs with zero
2519 APInt Imm(32, 0);
2520 for (int i = 3; i >= 0; --i) {
2521 int Idx = SHFIndices[i];
2522
2523 if (Idx == -1)
2524 Idx = 0;
2525
2526 Imm <<= 2;
2527 Imm |= Idx & 0x3;
2528 }
2529
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002530 SDLoc DL(Op);
2531 return DAG.getNode(MipsISD::SHF, DL, ResTy,
2532 DAG.getConstant(Imm, DL, MVT::i32), Op->getOperand(0));
Daniel Sanders26307182013-09-24 14:20:00 +00002533}
2534
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002535/// Determine whether a range fits a regular pattern of values.
2536/// This function accounts for the possibility of jumping over the End iterator.
2537template <typename ValType>
2538static bool
2539fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,
2540 unsigned CheckStride,
2541 typename SmallVectorImpl<ValType>::const_iterator End,
2542 ValType ExpectedIndex, unsigned ExpectedIndexStride) {
2543 auto &I = Begin;
2544
2545 while (I != End) {
2546 if (*I != -1 && *I != ExpectedIndex)
2547 return false;
2548 ExpectedIndex += ExpectedIndexStride;
2549
2550 // Incrementing past End is undefined behaviour so we must increment one
2551 // step at a time and check for End at each step.
2552 for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
2553 ; // Empty loop body.
2554 }
2555 return true;
2556}
2557
2558// Determine whether VECTOR_SHUFFLE is a SPLATI.
2559//
2560// It is a SPLATI when the mask is:
2561// <x, x, x, ...>
2562// where x is any valid index.
2563//
2564// When undef's appear in the mask they are treated as if they were whatever
2565// value is necessary in order to fit the above form.
2566static bool isVECTOR_SHUFFLE_SPLATI(SDValue Op, EVT ResTy,
2567 SmallVector<int, 16> Indices,
2568 SelectionDAG &DAG) {
2569 assert((Indices.size() % 2) == 0);
2570
2571 int SplatIndex = -1;
2572 for (const auto &V : Indices) {
2573 if (V != -1) {
2574 SplatIndex = V;
2575 break;
2576 }
2577 }
2578
2579 return fitsRegularPattern<int>(Indices.begin(), 1, Indices.end(), SplatIndex,
2580 0);
2581}
2582
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002583// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2584//
2585// ILVEV interleaves the even elements from each vector.
2586//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002587// It is possible to lower into ILVEV when the mask consists of two of the
2588// following forms interleaved:
2589// <0, 2, 4, ...>
2590// <n, n+2, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002591// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002592// For example:
2593// <0, 0, 2, 2, 4, 4, ...>
2594// <0, n, 2, n+2, 4, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002595//
2596// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002597// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002598static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2599 SmallVector<int, 16> Indices,
2600 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002601 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002602
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002603 SDValue Wt;
2604 SDValue Ws;
2605 const auto &Begin = Indices.begin();
2606 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002607
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002608 // Check even elements are taken from the even elements of one half or the
2609 // other and pick an operand accordingly.
2610 if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
2611 Wt = Op->getOperand(0);
2612 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 2))
2613 Wt = Op->getOperand(1);
2614 else
2615 return SDValue();
2616
2617 // Check odd elements are taken from the even elements of one half or the
2618 // other and pick an operand accordingly.
2619 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
2620 Ws = Op->getOperand(0);
2621 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 2))
2622 Ws = Op->getOperand(1);
2623 else
2624 return SDValue();
2625
2626 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002627}
2628
2629// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2630//
2631// ILVOD interleaves the odd elements from each vector.
2632//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002633// It is possible to lower into ILVOD when the mask consists of two of the
2634// following forms interleaved:
2635// <1, 3, 5, ...>
2636// <n+1, n+3, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002637// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002638// For example:
2639// <1, 1, 3, 3, 5, 5, ...>
2640// <1, n+1, 3, n+3, 5, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002641//
2642// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002643// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002644static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2645 SmallVector<int, 16> Indices,
2646 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002647 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002648
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002649 SDValue Wt;
2650 SDValue Ws;
2651 const auto &Begin = Indices.begin();
2652 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002653
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002654 // Check even elements are taken from the odd elements of one half or the
2655 // other and pick an operand accordingly.
2656 if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
2657 Wt = Op->getOperand(0);
2658 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + 1, 2))
2659 Wt = Op->getOperand(1);
2660 else
2661 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002662
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002663 // Check odd elements are taken from the odd elements of one half or the
2664 // other and pick an operand accordingly.
2665 if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
2666 Ws = Op->getOperand(0);
2667 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + 1, 2))
2668 Ws = Op->getOperand(1);
2669 else
2670 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002671
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002672 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Wt, Ws);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002673}
2674
2675// Lower VECTOR_SHUFFLE into ILVR (if possible).
2676//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002677// ILVR interleaves consecutive elements from the right (lowest-indexed) half of
2678// each vector.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002679//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002680// It is possible to lower into ILVR when the mask consists of two of the
2681// following forms interleaved:
2682// <0, 1, 2, ...>
2683// <n, n+1, n+2, ...>
2684// where n is the number of elements in the vector.
2685// For example:
2686// <0, 0, 1, 1, 2, 2, ...>
2687// <0, n, 1, n+1, 2, n+2, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002688//
2689// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002690// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002691static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2692 SmallVector<int, 16> Indices,
2693 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002694 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002695
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002696 SDValue Wt;
2697 SDValue Ws;
2698 const auto &Begin = Indices.begin();
2699 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002700
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002701 // Check even elements are taken from the right (lowest-indexed) elements of
2702 // one half or the other and pick an operand accordingly.
2703 if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
2704 Wt = Op->getOperand(0);
2705 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 1))
2706 Wt = Op->getOperand(1);
2707 else
2708 return SDValue();
2709
2710 // Check odd elements are taken from the right (lowest-indexed) elements of
2711 // one half or the other and pick an operand accordingly.
2712 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
2713 Ws = Op->getOperand(0);
2714 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 1))
2715 Ws = Op->getOperand(1);
2716 else
2717 return SDValue();
2718
2719 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Ws, Wt);
2720}
2721
2722// Lower VECTOR_SHUFFLE into ILVL (if possible).
2723//
2724// ILVL interleaves consecutive elements from the left (highest-indexed) half
2725// of each vector.
2726//
2727// It is possible to lower into ILVL when the mask consists of two of the
2728// following forms interleaved:
2729// <x, x+1, x+2, ...>
2730// <n+x, n+x+1, n+x+2, ...>
2731// where n is the number of elements in the vector and x is half n.
2732// For example:
2733// <x, x, x+1, x+1, x+2, x+2, ...>
2734// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2735//
2736// When undef's appear in the mask they are treated as if they were whatever
2737// value is necessary in order to fit the above forms.
2738static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2739 SmallVector<int, 16> Indices,
2740 SelectionDAG &DAG) {
2741 assert((Indices.size() % 2) == 0);
2742
2743 unsigned HalfSize = Indices.size() / 2;
2744 SDValue Wt;
2745 SDValue Ws;
2746 const auto &Begin = Indices.begin();
2747 const auto &End = Indices.end();
2748
2749 // Check even elements are taken from the left (highest-indexed) elements of
2750 // one half or the other and pick an operand accordingly.
2751 if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
2752 Wt = Op->getOperand(0);
2753 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + HalfSize, 1))
2754 Wt = Op->getOperand(1);
2755 else
2756 return SDValue();
2757
2758 // Check odd elements are taken from the left (highest-indexed) elements of
2759 // one half or the other and pick an operand accordingly.
2760 if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
2761 Ws = Op->getOperand(0);
2762 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + HalfSize,
2763 1))
2764 Ws = Op->getOperand(1);
2765 else
2766 return SDValue();
2767
2768 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002769}
2770
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002771// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2772//
2773// PCKEV copies the even elements of each vector into the result vector.
2774//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002775// It is possible to lower into PCKEV when the mask consists of two of the
2776// following forms concatenated:
2777// <0, 2, 4, ...>
2778// <n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002779// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002780// For example:
2781// <0, 2, 4, ..., 0, 2, 4, ...>
2782// <0, 2, 4, ..., n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002783//
2784// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002785// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002786static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2787 SmallVector<int, 16> Indices,
2788 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002789 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002790
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002791 SDValue Wt;
2792 SDValue Ws;
2793 const auto &Begin = Indices.begin();
2794 const auto &Mid = Indices.begin() + Indices.size() / 2;
2795 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002796
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002797 if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
2798 Wt = Op->getOperand(0);
2799 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size(), 2))
2800 Wt = Op->getOperand(1);
2801 else
2802 return SDValue();
2803
2804 if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
2805 Ws = Op->getOperand(0);
2806 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size(), 2))
2807 Ws = Op->getOperand(1);
2808 else
2809 return SDValue();
2810
2811 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002812}
2813
2814// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2815//
2816// PCKOD copies the odd elements of each vector into the result vector.
2817//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002818// It is possible to lower into PCKOD when the mask consists of two of the
2819// following forms concatenated:
2820// <1, 3, 5, ...>
2821// <n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002822// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002823// For example:
2824// <1, 3, 5, ..., 1, 3, 5, ...>
2825// <1, 3, 5, ..., n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002826//
2827// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002828// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002829static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2830 SmallVector<int, 16> Indices,
2831 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002832 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002833
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002834 SDValue Wt;
2835 SDValue Ws;
2836 const auto &Begin = Indices.begin();
2837 const auto &Mid = Indices.begin() + Indices.size() / 2;
2838 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002839
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002840 if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
2841 Wt = Op->getOperand(0);
2842 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size() + 1, 2))
2843 Wt = Op->getOperand(1);
2844 else
2845 return SDValue();
2846
2847 if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
2848 Ws = Op->getOperand(0);
2849 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size() + 1, 2))
2850 Ws = Op->getOperand(1);
2851 else
2852 return SDValue();
2853
2854 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002855}
2856
Daniel Sanderse5087042013-09-24 14:02:15 +00002857// Lower VECTOR_SHUFFLE into VSHF.
2858//
2859// This mostly consists of converting the shuffle indices in Indices into a
2860// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2861// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2862// if the type is v8i16 and all the indices are less than 8 then the second
2863// operand is unused and can be replaced with anything. We choose to replace it
2864// with the used operand since this reduces the number of instructions overall.
2865static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2866 SmallVector<int, 16> Indices,
2867 SelectionDAG &DAG) {
2868 SmallVector<SDValue, 16> Ops;
2869 SDValue Op0;
2870 SDValue Op1;
2871 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2872 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2873 bool Using1stVec = false;
2874 bool Using2ndVec = false;
2875 SDLoc DL(Op);
2876 int ResTyNumElts = ResTy.getVectorNumElements();
2877
2878 for (int i = 0; i < ResTyNumElts; ++i) {
2879 // Idx == -1 means UNDEF
2880 int Idx = Indices[i];
2881
2882 if (0 <= Idx && Idx < ResTyNumElts)
2883 Using1stVec = true;
2884 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2885 Using2ndVec = true;
2886 }
2887
2888 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2889 ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002890 Ops.push_back(DAG.getTargetConstant(*I, DL, MaskEltTy));
Daniel Sanderse5087042013-09-24 14:02:15 +00002891
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002892 SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002893
2894 if (Using1stVec && Using2ndVec) {
2895 Op0 = Op->getOperand(0);
2896 Op1 = Op->getOperand(1);
2897 } else if (Using1stVec)
2898 Op0 = Op1 = Op->getOperand(0);
2899 else if (Using2ndVec)
2900 Op0 = Op1 = Op->getOperand(1);
2901 else
2902 llvm_unreachable("shuffle vector mask references neither vector operand?");
2903
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002904 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2905 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2906 // VSHF concatenates the vectors in a bitwise fashion:
2907 // <0b00, 0b01> + <0b10, 0b11> ->
2908 // 0b0100 + 0b1110 -> 0b01001110
2909 // <0b10, 0b11, 0b00, 0b01>
2910 // We must therefore swap the operands to get the correct result.
2911 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002912}
2913
2914// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2915// indices in the shuffle.
2916SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2917 SelectionDAG &DAG) const {
2918 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2919 EVT ResTy = Op->getValueType(0);
2920
2921 if (!ResTy.is128BitVector())
2922 return SDValue();
2923
2924 int ResTyNumElts = ResTy.getVectorNumElements();
2925 SmallVector<int, 16> Indices;
2926
2927 for (int i = 0; i < ResTyNumElts; ++i)
2928 Indices.push_back(Node->getMaskElt(i));
2929
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002930 // splati.[bhwd] is preferable to the others but is matched from
2931 // MipsISD::VSHF.
2932 if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
2933 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002934 SDValue Result;
2935 if ((Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002936 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002937 if ((Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002938 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002939 if ((Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002940 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002941 if ((Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002942 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002943 if ((Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002944 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002945 if ((Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002946 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002947 if ((Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG)))
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002948 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002949 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2950}
2951
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00002952MachineBasicBlock *
2953MipsSETargetLowering::emitBPOSGE32(MachineInstr &MI,
2954 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002955 // $bb:
2956 // bposge32_pseudo $vr0
2957 // =>
2958 // $bb:
2959 // bposge32 $tbb
2960 // $fbb:
2961 // li $vr2, 0
2962 // b $sink
2963 // $tbb:
2964 // li $vr1, 1
2965 // $sink:
2966 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2967
2968 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002969 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002970 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00002971 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002972 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002973 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002974 MachineFunction *F = BB->getParent();
2975 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2976 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2977 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2978 F->insert(It, FBB);
2979 F->insert(It, TBB);
2980 F->insert(It, Sink);
2981
2982 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002983 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002984 BB->end());
2985 Sink->transferSuccessorsAndUpdatePHIs(BB);
2986
2987 // Add successors.
2988 BB->addSuccessor(FBB);
2989 BB->addSuccessor(TBB);
2990 FBB->addSuccessor(Sink);
2991 TBB->addSuccessor(Sink);
2992
2993 // Insert the real bposge32 instruction to $BB.
2994 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +00002995 // Insert the real bposge32c instruction to $BB.
2996 BuildMI(BB, DL, TII->get(Mips::BPOSGE32C_MMR3)).addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002997
2998 // Fill $FBB.
2999 unsigned VR2 = RegInfo.createVirtualRegister(RC);
3000 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
3001 .addReg(Mips::ZERO).addImm(0);
3002 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3003
3004 // Fill $TBB.
3005 unsigned VR1 = RegInfo.createVirtualRegister(RC);
3006 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
3007 .addReg(Mips::ZERO).addImm(1);
3008
3009 // Insert phi function to $Sink.
3010 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003011 MI.getOperand(0).getReg())
3012 .addReg(VR2)
3013 .addMBB(FBB)
3014 .addReg(VR1)
3015 .addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003016
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003017 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003018 return Sink;
3019}
Daniel Sandersce09d072013-08-28 12:14:50 +00003020
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003021MachineBasicBlock *MipsSETargetLowering::emitMSACBranchPseudo(
3022 MachineInstr &MI, MachineBasicBlock *BB, unsigned BranchOp) const {
Daniel Sandersce09d072013-08-28 12:14:50 +00003023 // $bb:
3024 // vany_nonzero $rd, $ws
3025 // =>
3026 // $bb:
3027 // bnz.b $ws, $tbb
3028 // b $fbb
3029 // $fbb:
3030 // li $rd1, 0
3031 // b $sink
3032 // $tbb:
3033 // li $rd2, 1
3034 // $sink:
3035 // $rd = phi($rd1, $fbb, $rd2, $tbb)
3036
3037 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003038 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersce09d072013-08-28 12:14:50 +00003039 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003040 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersce09d072013-08-28 12:14:50 +00003041 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003042 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00003043 MachineFunction *F = BB->getParent();
3044 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3045 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3046 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3047 F->insert(It, FBB);
3048 F->insert(It, TBB);
3049 F->insert(It, Sink);
3050
3051 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003052 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00003053 BB->end());
3054 Sink->transferSuccessorsAndUpdatePHIs(BB);
3055
3056 // Add successors.
3057 BB->addSuccessor(FBB);
3058 BB->addSuccessor(TBB);
3059 FBB->addSuccessor(Sink);
3060 TBB->addSuccessor(Sink);
3061
3062 // Insert the real bnz.b instruction to $BB.
3063 BuildMI(BB, DL, TII->get(BranchOp))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003064 .addReg(MI.getOperand(1).getReg())
3065 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003066
3067 // Fill $FBB.
3068 unsigned RD1 = RegInfo.createVirtualRegister(RC);
3069 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
3070 .addReg(Mips::ZERO).addImm(0);
3071 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3072
3073 // Fill $TBB.
3074 unsigned RD2 = RegInfo.createVirtualRegister(RC);
3075 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
3076 .addReg(Mips::ZERO).addImm(1);
3077
3078 // Insert phi function to $Sink.
3079 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003080 MI.getOperand(0).getReg())
3081 .addReg(RD1)
3082 .addMBB(FBB)
3083 .addReg(RD2)
3084 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003085
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003086 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersce09d072013-08-28 12:14:50 +00003087 return Sink;
3088}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003089
3090// Emit the COPY_FW pseudo instruction.
3091//
3092// copy_fw_pseudo $fd, $ws, n
3093// =>
3094// copy_u_w $rt, $ws, $n
3095// mtc1 $rt, $fd
3096//
3097// When n is zero, the equivalent operation can be performed with (potentially)
3098// zero instructions due to register overlaps. This optimization is never valid
3099// for lane 1 because it would require FR=0 mode which isn't supported by MSA.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003100MachineBasicBlock *
3101MipsSETargetLowering::emitCOPY_FW(MachineInstr &MI,
3102 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003103 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003104 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003105 DebugLoc DL = MI.getDebugLoc();
3106 unsigned Fd = MI.getOperand(0).getReg();
3107 unsigned Ws = MI.getOperand(1).getReg();
3108 unsigned Lane = MI.getOperand(2).getImm();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003109
Daniel Sandersafe27c72015-02-23 17:22:16 +00003110 if (Lane == 0) {
3111 unsigned Wt = Ws;
3112 if (!Subtarget.useOddSPReg()) {
3113 // We must copy to an even-numbered MSA register so that the
3114 // single-precision sub-register is also guaranteed to be even-numbered.
3115 Wt = RegInfo.createVirtualRegister(&Mips::MSA128WEvensRegClass);
3116
3117 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Wt).addReg(Ws);
3118 }
3119
3120 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3121 } else {
3122 unsigned Wt = RegInfo.createVirtualRegister(
3123 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3124 &Mips::MSA128WEvensRegClass);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003125
Daniel Sandersd9207702014-03-04 13:54:30 +00003126 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003127 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3128 }
3129
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003130 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003131 return BB;
3132}
3133
3134// Emit the COPY_FD pseudo instruction.
3135//
3136// copy_fd_pseudo $fd, $ws, n
3137// =>
3138// splati.d $wt, $ws, $n
3139// copy $fd, $wt:sub_64
3140//
3141// When n is zero, the equivalent operation can be performed with (potentially)
3142// zero instructions due to register overlaps. This optimization is always
3143// valid because FR=1 mode which is the only supported mode in MSA.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003144MachineBasicBlock *
3145MipsSETargetLowering::emitCOPY_FD(MachineInstr &MI,
3146 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003147 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003148
Eric Christopher96e72c62015-01-29 23:27:36 +00003149 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003150 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003151 unsigned Fd = MI.getOperand(0).getReg();
3152 unsigned Ws = MI.getOperand(1).getReg();
3153 unsigned Lane = MI.getOperand(2).getImm() * 2;
3154 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003155
3156 if (Lane == 0)
3157 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
3158 else {
3159 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3160
3161 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
3162 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
3163 }
3164
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003165 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003166 return BB;
3167}
Daniel Sandersa5150702013-09-27 12:31:32 +00003168
3169// Emit the INSERT_FW pseudo instruction.
3170//
3171// insert_fw_pseudo $wd, $wd_in, $n, $fs
3172// =>
3173// subreg_to_reg $wt:sub_lo, $fs
3174// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003175MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003176MipsSETargetLowering::emitINSERT_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003177 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003178 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003179 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003180 DebugLoc DL = MI.getDebugLoc();
3181 unsigned Wd = MI.getOperand(0).getReg();
3182 unsigned Wd_in = MI.getOperand(1).getReg();
3183 unsigned Lane = MI.getOperand(2).getImm();
3184 unsigned Fs = MI.getOperand(3).getReg();
Daniel Sandersafe27c72015-02-23 17:22:16 +00003185 unsigned Wt = RegInfo.createVirtualRegister(
3186 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3187 &Mips::MSA128WEvensRegClass);
Daniel Sandersa5150702013-09-27 12:31:32 +00003188
3189 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003190 .addImm(0)
3191 .addReg(Fs)
3192 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00003193 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003194 .addReg(Wd_in)
3195 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003196 .addReg(Wt)
3197 .addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003198
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003199 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003200 return BB;
3201}
3202
3203// Emit the INSERT_FD pseudo instruction.
3204//
3205// insert_fd_pseudo $wd, $fs, n
3206// =>
3207// subreg_to_reg $wt:sub_64, $fs
3208// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003209MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003210MipsSETargetLowering::emitINSERT_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003211 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003212 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00003213
Eric Christopher96e72c62015-01-29 23:27:36 +00003214 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003215 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003216 DebugLoc DL = MI.getDebugLoc();
3217 unsigned Wd = MI.getOperand(0).getReg();
3218 unsigned Wd_in = MI.getOperand(1).getReg();
3219 unsigned Lane = MI.getOperand(2).getImm();
3220 unsigned Fs = MI.getOperand(3).getReg();
Daniel Sandersa5150702013-09-27 12:31:32 +00003221 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3222
3223 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003224 .addImm(0)
3225 .addReg(Fs)
3226 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00003227 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003228 .addReg(Wd_in)
3229 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003230 .addReg(Wt)
3231 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003232
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003233 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003234 return BB;
3235}
3236
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003237// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3238//
3239// For integer:
3240// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3241// =>
3242// (SLL $lanetmp1, $lane, <log2size)
3243// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3244// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3245// (NEG $lanetmp2, $lanetmp1)
3246// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3247//
3248// For floating point:
3249// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3250// =>
3251// (SUBREG_TO_REG $wt, $fs, <subreg>)
3252// (SLL $lanetmp1, $lane, <log2size)
3253// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3254// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3255// (NEG $lanetmp2, $lanetmp1)
3256// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003257MachineBasicBlock *MipsSETargetLowering::emitINSERT_DF_VIDX(
3258 MachineInstr &MI, MachineBasicBlock *BB, unsigned EltSizeInBytes,
3259 bool IsFP) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003260 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003261 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003262 DebugLoc DL = MI.getDebugLoc();
3263 unsigned Wd = MI.getOperand(0).getReg();
3264 unsigned SrcVecReg = MI.getOperand(1).getReg();
3265 unsigned LaneReg = MI.getOperand(2).getReg();
3266 unsigned SrcValReg = MI.getOperand(3).getReg();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003267
3268 const TargetRegisterClass *VecRC = nullptr;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003269 // FIXME: This should be true for N32 too.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003270 const TargetRegisterClass *GPRRC =
Daniel Sanders4160c802015-05-05 08:48:35 +00003271 Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003272 unsigned SubRegIdx = Subtarget.isABI_N64() ? Mips::sub_32 : 0;
3273 unsigned ShiftOp = Subtarget.isABI_N64() ? Mips::DSLL : Mips::SLL;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003274 unsigned EltLog2Size;
3275 unsigned InsertOp = 0;
3276 unsigned InsveOp = 0;
3277 switch (EltSizeInBytes) {
3278 default:
3279 llvm_unreachable("Unexpected size");
3280 case 1:
3281 EltLog2Size = 0;
3282 InsertOp = Mips::INSERT_B;
3283 InsveOp = Mips::INSVE_B;
3284 VecRC = &Mips::MSA128BRegClass;
3285 break;
3286 case 2:
3287 EltLog2Size = 1;
3288 InsertOp = Mips::INSERT_H;
3289 InsveOp = Mips::INSVE_H;
3290 VecRC = &Mips::MSA128HRegClass;
3291 break;
3292 case 4:
3293 EltLog2Size = 2;
3294 InsertOp = Mips::INSERT_W;
3295 InsveOp = Mips::INSVE_W;
3296 VecRC = &Mips::MSA128WRegClass;
3297 break;
3298 case 8:
3299 EltLog2Size = 3;
3300 InsertOp = Mips::INSERT_D;
3301 InsveOp = Mips::INSVE_D;
3302 VecRC = &Mips::MSA128DRegClass;
3303 break;
3304 }
3305
3306 if (IsFP) {
3307 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3308 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3309 .addImm(0)
3310 .addReg(SrcValReg)
3311 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3312 SrcValReg = Wt;
3313 }
3314
3315 // Convert the lane index into a byte index
3316 if (EltSizeInBytes != 1) {
3317 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003318 BuildMI(*BB, MI, DL, TII->get(ShiftOp), LaneTmp1)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003319 .addReg(LaneReg)
3320 .addImm(EltLog2Size);
3321 LaneReg = LaneTmp1;
3322 }
3323
3324 // Rotate bytes around so that the desired lane is element zero
3325 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3326 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3327 .addReg(SrcVecReg)
3328 .addReg(SrcVecReg)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003329 .addReg(LaneReg, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003330
3331 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3332 if (IsFP) {
3333 // Use insve.df to insert to element zero
3334 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3335 .addReg(WdTmp1)
3336 .addImm(0)
3337 .addReg(SrcValReg)
3338 .addImm(0);
3339 } else {
3340 // Use insert.df to insert to element zero
3341 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3342 .addReg(WdTmp1)
3343 .addReg(SrcValReg)
3344 .addImm(0);
3345 }
3346
3347 // Rotate elements the rest of the way for a full rotation.
3348 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3349 // the lane index to do this.
3350 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sanders4160c802015-05-05 08:48:35 +00003351 BuildMI(*BB, MI, DL, TII->get(Subtarget.isABI_N64() ? Mips::DSUB : Mips::SUB),
3352 LaneTmp2)
3353 .addReg(Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003354 .addReg(LaneReg);
3355 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3356 .addReg(WdTmp2)
3357 .addReg(WdTmp2)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003358 .addReg(LaneTmp2, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003359
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003360 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003361 return BB;
3362}
3363
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003364// Emit the FILL_FW pseudo instruction.
3365//
3366// fill_fw_pseudo $wd, $fs
3367// =>
3368// implicit_def $wt1
3369// insert_subreg $wt2:subreg_lo, $wt1, $fs
3370// splati.w $wd, $wt2[0]
3371MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003372MipsSETargetLowering::emitFILL_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003373 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003374 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003375 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003376 DebugLoc DL = MI.getDebugLoc();
3377 unsigned Wd = MI.getOperand(0).getReg();
3378 unsigned Fs = MI.getOperand(1).getReg();
Simon Dardis0e9e2372017-01-10 15:53:10 +00003379 unsigned Wt1 = RegInfo.createVirtualRegister(
3380 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3381 : &Mips::MSA128WEvensRegClass);
3382 unsigned Wt2 = RegInfo.createVirtualRegister(
3383 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3384 : &Mips::MSA128WEvensRegClass);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003385
3386 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3387 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3388 .addReg(Wt1)
3389 .addReg(Fs)
3390 .addImm(Mips::sub_lo);
3391 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3392
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003393 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003394 return BB;
3395}
3396
3397// Emit the FILL_FD pseudo instruction.
3398//
3399// fill_fd_pseudo $wd, $fs
3400// =>
3401// implicit_def $wt1
3402// insert_subreg $wt2:subreg_64, $wt1, $fs
3403// splati.d $wd, $wt2[0]
3404MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003405MipsSETargetLowering::emitFILL_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003406 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003407 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003408
Eric Christopher96e72c62015-01-29 23:27:36 +00003409 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003410 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003411 DebugLoc DL = MI.getDebugLoc();
3412 unsigned Wd = MI.getOperand(0).getReg();
3413 unsigned Fs = MI.getOperand(1).getReg();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003414 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3415 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3416
3417 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3418 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3419 .addReg(Wt1)
3420 .addReg(Fs)
3421 .addImm(Mips::sub_64);
3422 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003423
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003424 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003425 return BB;
3426}
Daniel Sandersa9521602013-10-23 10:36:52 +00003427
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003428// Emit the ST_F16_PSEDUO instruction to store a f16 value from an MSA
3429// register.
3430//
3431// STF16 MSA128F16:$wd, mem_simm10:$addr
3432// =>
3433// copy_u.h $rtemp,$wd[0]
3434// sh $rtemp, $addr
3435//
3436// Safety: We can't use st.h & co as they would over write the memory after
3437// the destination. It would require half floats be allocated 16 bytes(!) of
3438// space.
3439MachineBasicBlock *
3440MipsSETargetLowering::emitST_F16_PSEUDO(MachineInstr &MI,
3441 MachineBasicBlock *BB) const {
3442
3443 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3444 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3445 DebugLoc DL = MI.getDebugLoc();
3446 unsigned Ws = MI.getOperand(0).getReg();
3447 unsigned Rt = MI.getOperand(1).getReg();
3448 const MachineMemOperand &MMO = **MI.memoperands_begin();
3449 unsigned Imm = MMO.getOffset();
3450
3451 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3452 // spill and reload can expand as a GPR64 operand. Examine the
3453 // operand in detail and default to ABI.
3454 const TargetRegisterClass *RC =
3455 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3456 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3457 : &Mips::GPR64RegClass);
3458 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003459 unsigned Rs = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003460
3461 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_U_H), Rs).addReg(Ws).addImm(0);
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003462 if(!UsingMips32) {
3463 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR64RegClass);
3464 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Tmp)
3465 .addImm(0)
3466 .addReg(Rs)
3467 .addImm(Mips::sub_32);
3468 Rs = Tmp;
3469 }
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003470 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::SH : Mips::SH64))
3471 .addReg(Rs)
3472 .addReg(Rt)
3473 .addImm(Imm)
3474 .addMemOperand(BB->getParent()->getMachineMemOperand(
3475 &MMO, MMO.getOffset(), MMO.getSize()));
3476
3477 MI.eraseFromParent();
3478 return BB;
3479}
3480
3481// Emit the LD_F16_PSEDUO instruction to load a f16 value into an MSA register.
3482//
3483// LD_F16 MSA128F16:$wd, mem_simm10:$addr
3484// =>
3485// lh $rtemp, $addr
3486// fill.h $wd, $rtemp
3487//
3488// Safety: We can't use ld.h & co as they over-read from the source.
3489// Additionally, if the address is not modulo 16, 2 cases can occur:
3490// a) Segmentation fault as the load instruction reads from a memory page
3491// memory it's not supposed to.
3492// b) The load crosses an implementation specific boundary, requiring OS
3493// intervention.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003494MachineBasicBlock *
3495MipsSETargetLowering::emitLD_F16_PSEUDO(MachineInstr &MI,
3496 MachineBasicBlock *BB) const {
3497
3498 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3499 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3500 DebugLoc DL = MI.getDebugLoc();
3501 unsigned Wd = MI.getOperand(0).getReg();
3502
3503 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3504 // spill and reload can expand as a GPR64 operand. Examine the
3505 // operand in detail and default to ABI.
3506 const TargetRegisterClass *RC =
3507 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3508 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3509 : &Mips::GPR64RegClass);
3510
3511 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
3512 unsigned Rt = RegInfo.createVirtualRegister(RC);
3513
3514 MachineInstrBuilder MIB =
3515 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::LH : Mips::LH64), Rt);
3516 for (unsigned i = 1; i < MI.getNumOperands(); i++)
Diana Picus116bbab2017-01-13 09:58:52 +00003517 MIB.add(MI.getOperand(i));
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003518
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003519 if(!UsingMips32) {
3520 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
3521 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Tmp).addReg(Rt, 0, Mips::sub_32);
3522 Rt = Tmp;
3523 }
3524
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003525 BuildMI(*BB, MI, DL, TII->get(Mips::FILL_H), Wd).addReg(Rt);
3526
3527 MI.eraseFromParent();
3528 return BB;
3529}
3530
3531// Emit the FPROUND_PSEUDO instruction.
3532//
3533// Round an FGR64Opnd, FGR32Opnd to an f16.
3534//
3535// Safety: Cycle the operand through the GPRs so the result always ends up
3536// the correct MSA register.
3537//
3538// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fs
3539// / FGR64Opnd:$Fs and MSA128F16:$Wd to the same physical register
3540// (which they can be, as the MSA registers are defined to alias the
3541// FPU's 64 bit and 32 bit registers) the result can be accessed using
3542// the correct register class. That requires operands be tie-able across
3543// register classes which have a sub/super register class relationship.
3544//
3545// For FPG32Opnd:
3546//
3547// FPROUND MSA128F16:$wd, FGR32Opnd:$fs
3548// =>
3549// mfc1 $rtemp, $fs
3550// fill.w $rtemp, $wtemp
3551// fexdo.w $wd, $wtemp, $wtemp
3552//
3553// For FPG64Opnd on mips32r2+:
3554//
3555// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3556// =>
3557// mfc1 $rtemp, $fs
3558// fill.w $rtemp, $wtemp
3559// mfhc1 $rtemp2, $fs
3560// insert.w $wtemp[1], $rtemp2
3561// insert.w $wtemp[3], $rtemp2
3562// fexdo.w $wtemp2, $wtemp, $wtemp
3563// fexdo.h $wd, $temp2, $temp2
3564//
3565// For FGR64Opnd on mips64r2+:
3566//
3567// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3568// =>
3569// dmfc1 $rtemp, $fs
3570// fill.d $rtemp, $wtemp
3571// fexdo.w $wtemp2, $wtemp, $wtemp
3572// fexdo.h $wd, $wtemp2, $wtemp2
3573//
3574// Safety note: As $wtemp is UNDEF, we may provoke a spurious exception if the
3575// undef bits are "just right" and the exception enable bits are
3576// set. By using fill.w to replicate $fs into all elements over
3577// insert.w for one element, we avoid that potiential case. If
3578// fexdo.[hw] causes an exception in, the exception is valid and it
3579// occurs for all elements.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003580MachineBasicBlock *
3581MipsSETargetLowering::emitFPROUND_PSEUDO(MachineInstr &MI,
3582 MachineBasicBlock *BB,
3583 bool IsFGR64) const {
3584
3585 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3586 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3587 // it.
3588 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3589
3590 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003591 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003592
3593 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3594 DebugLoc DL = MI.getDebugLoc();
3595 unsigned Wd = MI.getOperand(0).getReg();
3596 unsigned Fs = MI.getOperand(1).getReg();
3597
3598 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3599 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3600 const TargetRegisterClass *GPRRC =
3601 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003602 unsigned MFC1Opc = IsFGR64onMips64
3603 ? Mips::DMFC1
3604 : (IsFGR64onMips32 ? Mips::MFC1_D64 : Mips::MFC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003605 unsigned FILLOpc = IsFGR64onMips64 ? Mips::FILL_D : Mips::FILL_W;
3606
3607 // Perform the register class copy as mentioned above.
3608 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3609 BuildMI(*BB, MI, DL, TII->get(MFC1Opc), Rtemp).addReg(Fs);
3610 BuildMI(*BB, MI, DL, TII->get(FILLOpc), Wtemp).addReg(Rtemp);
3611 unsigned WPHI = Wtemp;
3612
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003613 if (IsFGR64onMips32) {
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003614 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3615 BuildMI(*BB, MI, DL, TII->get(Mips::MFHC1_D64), Rtemp2).addReg(Fs);
3616 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3617 unsigned Wtemp3 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3618 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp2)
3619 .addReg(Wtemp)
3620 .addReg(Rtemp2)
3621 .addImm(1);
3622 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp3)
3623 .addReg(Wtemp2)
3624 .addReg(Rtemp2)
3625 .addImm(3);
3626 WPHI = Wtemp3;
3627 }
3628
3629 if (IsFGR64) {
3630 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3631 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_W), Wtemp2)
3632 .addReg(WPHI)
3633 .addReg(WPHI);
3634 WPHI = Wtemp2;
3635 }
3636
3637 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_H), Wd).addReg(WPHI).addReg(WPHI);
3638
3639 MI.eraseFromParent();
3640 return BB;
3641}
3642
3643// Emit the FPEXTEND_PSEUDO instruction.
3644//
3645// Expand an f16 to either a FGR32Opnd or FGR64Opnd.
3646//
3647// Safety: Cycle the result through the GPRs so the result always ends up
3648// the correct floating point register.
3649//
3650// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fd
3651// / FGR64Opnd:$Fd and MSA128F16:$Ws to the same physical register
3652// (which they can be, as the MSA registers are defined to alias the
3653// FPU's 64 bit and 32 bit registers) the result can be accessed using
3654// the correct register class. That requires operands be tie-able across
3655// register classes which have a sub/super register class relationship. I
3656// haven't checked.
3657//
3658// For FGR32Opnd:
3659//
3660// FPEXTEND FGR32Opnd:$fd, MSA128F16:$ws
3661// =>
3662// fexupr.w $wtemp, $ws
3663// copy_s.w $rtemp, $ws[0]
3664// mtc1 $rtemp, $fd
3665//
3666// For FGR64Opnd on Mips64:
3667//
3668// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3669// =>
3670// fexupr.w $wtemp, $ws
3671// fexupr.d $wtemp2, $wtemp
3672// copy_s.d $rtemp, $wtemp2s[0]
3673// dmtc1 $rtemp, $fd
3674//
3675// For FGR64Opnd on Mips32:
3676//
3677// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3678// =>
3679// fexupr.w $wtemp, $ws
3680// fexupr.d $wtemp2, $wtemp
3681// copy_s.w $rtemp, $wtemp2[0]
3682// mtc1 $rtemp, $ftemp
3683// copy_s.w $rtemp2, $wtemp2[1]
3684// $fd = mthc1 $rtemp2, $ftemp
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003685MachineBasicBlock *
3686MipsSETargetLowering::emitFPEXTEND_PSEUDO(MachineInstr &MI,
3687 MachineBasicBlock *BB,
3688 bool IsFGR64) const {
3689
3690 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3691 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3692 // it.
3693 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3694
3695 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
3696 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
3697
3698 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3699 DebugLoc DL = MI.getDebugLoc();
3700 unsigned Fd = MI.getOperand(0).getReg();
3701 unsigned Ws = MI.getOperand(1).getReg();
3702
3703 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3704 const TargetRegisterClass *GPRRC =
3705 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003706 unsigned MTC1Opc = IsFGR64onMips64
3707 ? Mips::DMTC1
3708 : (IsFGR64onMips32 ? Mips::MTC1_D64 : Mips::MTC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003709 unsigned COPYOpc = IsFGR64onMips64 ? Mips::COPY_S_D : Mips::COPY_S_W;
3710
3711 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3712 unsigned WPHI = Wtemp;
3713
3714 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_W), Wtemp).addReg(Ws);
3715 if (IsFGR64) {
3716 WPHI = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3717 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_D), WPHI).addReg(Wtemp);
3718 }
3719
3720 // Perform the safety regclass copy mentioned above.
3721 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3722 unsigned FPRPHI = IsFGR64onMips32
3723 ? RegInfo.createVirtualRegister(&Mips::FGR64RegClass)
3724 : Fd;
3725 BuildMI(*BB, MI, DL, TII->get(COPYOpc), Rtemp).addReg(WPHI).addImm(0);
3726 BuildMI(*BB, MI, DL, TII->get(MTC1Opc), FPRPHI).addReg(Rtemp);
3727
3728 if (IsFGR64onMips32) {
3729 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3730 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_S_W), Rtemp2)
3731 .addReg(WPHI)
3732 .addImm(1);
3733 BuildMI(*BB, MI, DL, TII->get(Mips::MTHC1_D64), Fd)
3734 .addReg(FPRPHI)
3735 .addReg(Rtemp2);
3736 }
3737
3738 MI.eraseFromParent();
3739 return BB;
3740}
3741
Daniel Sandersa9521602013-10-23 10:36:52 +00003742// Emit the FEXP2_W_1 pseudo instructions.
3743//
3744// fexp2_w_1_pseudo $wd, $wt
3745// =>
3746// ldi.w $ws, 1
3747// fexp2.w $wd, $ws, $wt
3748MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003749MipsSETargetLowering::emitFEXP2_W_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003750 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003751 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003752 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3753 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3754 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3755 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003756 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003757
3758 // Splat 1.0 into a vector
3759 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3760 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3761
3762 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003763 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003764 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003765 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003766
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003767 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003768 return BB;
3769}
3770
3771// Emit the FEXP2_D_1 pseudo instructions.
3772//
3773// fexp2_d_1_pseudo $wd, $wt
3774// =>
3775// ldi.d $ws, 1
3776// fexp2.d $wd, $ws, $wt
3777MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003778MipsSETargetLowering::emitFEXP2_D_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003779 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003780 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003781 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3782 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3783 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3784 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003785 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003786
3787 // Splat 1.0 into a vector
3788 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3789 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3790
3791 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003792 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003793 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003794 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003795
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003796 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003797 return BB;
3798}