blob: 84367b240a603e74fedfc4e6f89778e4bf6757c3 [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/SelectionDAG.h"
32#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000033#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000034#include "llvm/CodeGen/TargetSubtargetInfo.h"
Craig Topper2fa14362018-03-29 17:21:10 +000035#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000036#include "llvm/IR/DebugLoc.h"
Akira Hatanakaa6bbde52013-04-13 02:13:30 +000037#include "llvm/IR/Intrinsics.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000038#include "llvm/Support/Casting.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000039#include "llvm/Support/CommandLine.h"
Daniel Sanders62aeab82013-10-30 13:31:27 +000040#include "llvm/Support/Debug.h"
Simon Dardis548a53f2017-01-10 16:40:57 +000041#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000042#include "llvm/Support/MachineValueType.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000043#include "llvm/Support/MathExtras.h"
Hans Wennborg3e9b1c12013-10-30 16:10:10 +000044#include "llvm/Support/raw_ostream.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000045#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);
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000334 setOperationAction(ISD::SMAX, Ty, Legal);
335 setOperationAction(ISD::SMIN, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000336 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000337 setOperationAction(ISD::UREM, Ty, Legal);
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000338 setOperationAction(ISD::UMAX, Ty, Legal);
339 setOperationAction(ISD::UMIN, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000340 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000341 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000342 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000343
Daniel Sanders015972b2013-10-11 10:00:06 +0000344 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
345 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
346 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
347 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
348 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
349 }
350
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000351 setOperationAction(ISD::SETCC, Ty, Legal);
352 setCondCodeAction(ISD::SETNE, Ty, Expand);
353 setCondCodeAction(ISD::SETGE, Ty, Expand);
354 setCondCodeAction(ISD::SETGT, Ty, Expand);
355 setCondCodeAction(ISD::SETUGE, Ty, Expand);
356 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000357}
358
Daniel Sanders7a289d02013-09-23 12:02:46 +0000359// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000360void MipsSETargetLowering::
361addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000362 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000363
364 // Expand all builtin opcodes.
365 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
366 setOperationAction(Opc, Ty, Expand);
367
368 setOperationAction(ISD::LOAD, Ty, Legal);
369 setOperationAction(ISD::STORE, Ty, Legal);
370 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000371 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000372 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000373 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000374
375 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000376 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000377 setOperationAction(ISD::FADD, Ty, Legal);
378 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000379 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000380 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000381 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000382 setOperationAction(ISD::FMUL, Ty, Legal);
383 setOperationAction(ISD::FRINT, Ty, Legal);
384 setOperationAction(ISD::FSQRT, Ty, Legal);
385 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000386 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000387
388 setOperationAction(ISD::SETCC, Ty, Legal);
389 setCondCodeAction(ISD::SETOGE, Ty, Expand);
390 setCondCodeAction(ISD::SETOGT, Ty, Expand);
391 setCondCodeAction(ISD::SETUGE, Ty, Expand);
392 setCondCodeAction(ISD::SETUGT, Ty, Expand);
393 setCondCodeAction(ISD::SETGE, Ty, Expand);
394 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000395 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000396}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000397
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000398SDValue MipsSETargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000399 if(!Subtarget.hasMips32r6())
400 return MipsTargetLowering::LowerOperation(Op, DAG);
401
402 EVT ResTy = Op->getValueType(0);
403 SDLoc DL(Op);
404
405 // Although MTC1_D64 takes an i32 and writes an f64, the upper 32 bits of the
406 // floating point register are undefined. Not really an issue as sel.d, which
407 // is produced from an FSELECT node, only looks at bit 0.
408 SDValue Tmp = DAG.getNode(MipsISD::MTC1_D64, DL, MVT::f64, Op->getOperand(0));
409 return DAG.getNode(MipsISD::FSELECT, DL, ResTy, Tmp, Op->getOperand(1),
410 Op->getOperand(2));
411}
412
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000413bool
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000414MipsSETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
415 unsigned,
416 unsigned,
417 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000418 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
419
Eric Christopher1c29a652014-07-18 22:55:25 +0000420 if (Subtarget.systemSupportsUnalignedAccess()) {
Daniel Sandersac272632014-05-23 13:18:02 +0000421 // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
422 // implementation defined whether this is handled by hardware, software, or
423 // a hybrid of the two but it's expected that most implementations will
424 // handle the majority of cases in hardware.
425 if (Fast)
426 *Fast = true;
427 return true;
428 }
429
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000430 switch (SVT) {
431 case MVT::i64:
432 case MVT::i32:
433 if (Fast)
434 *Fast = true;
435 return true;
436 default:
437 return false;
438 }
439}
440
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000441SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
442 SelectionDAG &DAG) const {
443 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000444 case ISD::LOAD: return lowerLOAD(Op, DAG);
445 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000446 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
447 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
448 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
449 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
450 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
451 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000452 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
453 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000454 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
455 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000456 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000457 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000458 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000459 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000460 case ISD::SELECT: return lowerSELECT(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000461 }
462
463 return MipsTargetLowering::LowerOperation(Op, DAG);
464}
465
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000466// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
467//
468// Performs the following transformations:
469// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
470// sign/zero-extension is completely overwritten by the new one performed by
471// the ISD::AND.
472// - Removes redundant zero extensions performed by an ISD::AND.
473static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
474 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000475 const MipsSubtarget &Subtarget) {
476 if (!Subtarget.hasMSA())
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000477 return SDValue();
478
479 SDValue Op0 = N->getOperand(0);
480 SDValue Op1 = N->getOperand(1);
481 unsigned Op0Opcode = Op0->getOpcode();
482
483 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
484 // where $d + 1 == 2^n and n == 32
485 // or $d + 1 == 2^n and n <= 32 and ZExt
486 // -> (MipsVExtractZExt $a, $b, $c)
487 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
488 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
489 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
490
491 if (!Mask)
492 return SDValue();
493
494 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
495
496 if (Log2IfPositive <= 0)
497 return SDValue(); // Mask+1 is not a power of 2
498
499 SDValue Op0Op2 = Op0->getOperand(2);
500 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
501 unsigned ExtendTySize = ExtendTy.getSizeInBits();
502 unsigned Log2 = Log2IfPositive;
503
504 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
505 Log2 == ExtendTySize) {
506 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000507 return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
508 Op0->getVTList(),
509 makeArrayRef(Ops, Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000510 }
511 }
512
513 return SDValue();
514}
515
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000516// Determine if the specified node is a constant vector splat.
517//
518// Returns true and sets Imm if:
519// * N is a ISD::BUILD_VECTOR representing a constant splat
520//
521// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
522// differences are that it assumes the MSA has already been checked and the
523// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
524// must not be in order for binsri.d to be selectable).
525static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
526 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
527
Craig Topper062a2ba2014-04-25 05:30:21 +0000528 if (!Node)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000529 return false;
530
531 APInt SplatValue, SplatUndef;
532 unsigned SplatBitSize;
533 bool HasAnyUndefs;
534
535 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
536 8, !IsLittleEndian))
537 return false;
538
539 Imm = SplatValue;
540
541 return true;
542}
543
Daniel Sandersab94b532013-10-30 15:20:38 +0000544// Test whether the given node is an all-ones build_vector.
545static bool isVectorAllOnes(SDValue N) {
546 // Look through bitcasts. Endianness doesn't matter because we are looking
547 // for an all-ones value.
548 if (N->getOpcode() == ISD::BITCAST)
549 N = N->getOperand(0);
550
551 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
552
553 if (!BVN)
554 return false;
555
556 APInt SplatValue, SplatUndef;
557 unsigned SplatBitSize;
558 bool HasAnyUndefs;
559
560 // Endianness doesn't matter in this context because we are looking for
561 // an all-ones value.
562 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
563 return SplatValue.isAllOnesValue();
564
565 return false;
566}
567
568// Test whether N is the bitwise inverse of OfNode.
569static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
570 if (N->getOpcode() != ISD::XOR)
571 return false;
572
573 if (isVectorAllOnes(N->getOperand(0)))
574 return N->getOperand(1) == OfNode;
575
576 if (isVectorAllOnes(N->getOperand(1)))
577 return N->getOperand(0) == OfNode;
578
579 return false;
580}
581
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000582// Perform combines where ISD::OR is the root node.
583//
584// Performs the following transformations:
585// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
586// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
587// vector type.
588static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
589 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000590 const MipsSubtarget &Subtarget) {
591 if (!Subtarget.hasMSA())
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000592 return SDValue();
593
594 EVT Ty = N->getValueType(0);
595
596 if (!Ty.is128BitVector())
597 return SDValue();
598
599 SDValue Op0 = N->getOperand(0);
600 SDValue Op1 = N->getOperand(1);
601
602 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
603 SDValue Op0Op0 = Op0->getOperand(0);
604 SDValue Op0Op1 = Op0->getOperand(1);
605 SDValue Op1Op0 = Op1->getOperand(0);
606 SDValue Op1Op1 = Op1->getOperand(1);
Eric Christopher1c29a652014-07-18 22:55:25 +0000607 bool IsLittleEndian = !Subtarget.isLittle();
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000608
609 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000610 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000611 APInt Mask, InvMask;
612
613 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
614 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
615 // looking.
616 // IfClr will be set if we find a valid match.
617 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
618 Cond = Op0Op0;
619 IfSet = Op0Op1;
620
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000621 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
622 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000623 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000624 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
625 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000626 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000627
628 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000629 }
630
631 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
632 // thing again using this mask.
633 // IfClr will be set if we find a valid match.
634 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
635 Cond = Op0Op1;
636 IfSet = Op0Op0;
637
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000638 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
639 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000640 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000641 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
642 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000643 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000644
645 IsConstantMask = true;
646 }
647
648 // If IfClr is not yet set, try looking for a non-constant match.
649 // IfClr will be set if we find a valid match amongst the eight
650 // possibilities.
651 if (!IfClr.getNode()) {
652 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
653 Cond = Op1Op0;
654 IfSet = Op1Op1;
655 IfClr = Op0Op1;
656 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
657 Cond = Op1Op0;
658 IfSet = Op1Op1;
659 IfClr = Op0Op0;
660 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
661 Cond = Op1Op1;
662 IfSet = Op1Op0;
663 IfClr = Op0Op1;
664 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
665 Cond = Op1Op1;
666 IfSet = Op1Op0;
667 IfClr = Op0Op0;
668 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
669 Cond = Op0Op0;
670 IfSet = Op0Op1;
671 IfClr = Op1Op1;
672 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
673 Cond = Op0Op0;
674 IfSet = Op0Op1;
675 IfClr = Op1Op0;
676 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
677 Cond = Op0Op1;
678 IfSet = Op0Op0;
679 IfClr = Op1Op1;
680 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
681 Cond = Op0Op1;
682 IfSet = Op0Op0;
683 IfClr = Op1Op0;
684 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000685 }
686
687 // At this point, IfClr will be set if we have a valid match.
688 if (!IfClr.getNode())
689 return SDValue();
690
691 assert(Cond.getNode() && IfSet.getNode());
692
693 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000694 if (IsConstantMask) {
695 if (Mask.isAllOnesValue())
696 return IfSet;
697 else if (Mask == 0)
698 return IfClr;
699 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000700
701 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000702 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000703 }
704
705 return SDValue();
706}
707
Simon Dardis9ec9f442018-04-13 16:09:07 +0000708static bool shouldTransformMulToShiftsAddsSubs(APInt C, EVT VT,
709 SelectionDAG &DAG,
710 const MipsSubtarget &Subtarget) {
711 // Estimate the number of operations the below transform will turn a
712 // constant multiply into. The number is approximately how many powers
713 // of two summed together that the constant can be broken down into.
714
715 SmallVector<APInt, 16> WorkStack(1, C);
716 unsigned Steps = 0;
717 unsigned BitWidth = C.getBitWidth();
718
719 while (!WorkStack.empty()) {
720 APInt Val = WorkStack.pop_back_val();
721
722 if (Val == 0 || Val == 1)
723 continue;
724
725 if (Val.isPowerOf2()) {
726 ++Steps;
727 continue;
728 }
729
730 APInt Floor = APInt(BitWidth, 1) << Val.logBase2();
731 APInt Ceil = Val.isNegative() ? APInt(BitWidth, 0)
732 : APInt(BitWidth, 1) << C.ceilLogBase2();
733
734 if ((Val - Floor).ule(Ceil - Val)) {
735 WorkStack.push_back(Floor);
736 WorkStack.push_back(Val - Floor);
737 ++Steps;
738 continue;
739 }
740
741 WorkStack.push_back(Ceil);
742 WorkStack.push_back(Ceil - Val);
743 ++Steps;
744
745 // If we have taken more than 12[1] / 8[2] steps to attempt the
746 // optimization for a native sized value, it is more than likely that this
747 // optimization will make things worse.
748 //
749 // [1] MIPS64 requires 6 instructions at most to materialize any constant,
750 // multiplication requires at least 4 cycles, but another cycle (or two)
751 // to retrieve the result from the HI/LO registers.
752 //
753 // [2] For MIPS32, more than 8 steps is expensive as the constant could be
754 // materialized in 2 instructions, multiplication requires at least 4
755 // cycles, but another cycle (or two) to retrieve the result from the
756 // HI/LO registers.
757
758 if (Steps > 12 && (Subtarget.isABI_N32() || Subtarget.isABI_N64()))
759 return false;
760
761 if (Steps > 8 && Subtarget.isABI_O32())
762 return false;
763 }
764
765 // If the value being multiplied is not supported natively, we have to pay
766 // an additional legalization cost, conservatively assume an increase in the
767 // cost of 3 instructions per step. This values for this heuristic were
768 // determined experimentally.
769 unsigned RegisterSize = DAG.getTargetLoweringInfo()
770 .getRegisterType(*DAG.getContext(), VT)
771 .getSizeInBits();
772 Steps *= (VT.getSizeInBits() != RegisterSize) * 3;
773 if (Steps > 27)
774 return false;
775
776 return true;
777}
778
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000779static SDValue genConstMult(SDValue X, APInt C, const SDLoc &DL, EVT VT,
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000780 EVT ShiftTy, SelectionDAG &DAG) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000781 // Return 0.
782 if (C == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000783 return DAG.getConstant(0, DL, VT);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000784
785 // Return x.
786 if (C == 1)
787 return X;
788
789 // If c is power of 2, return (shl x, log2(c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000790 if (C.isPowerOf2())
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000791 return DAG.getNode(ISD::SHL, DL, VT, X,
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000792 DAG.getConstant(C.logBase2(), DL, ShiftTy));
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000793
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000794 unsigned BitWidth = C.getBitWidth();
795 APInt Floor = APInt(BitWidth, 1) << C.logBase2();
796 APInt Ceil = C.isNegative() ? APInt(BitWidth, 0) :
797 APInt(BitWidth, 1) << C.ceilLogBase2();
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000798
799 // If |c - floor_c| <= |c - ceil_c|,
800 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
801 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000802 if ((C - Floor).ule(Ceil - C)) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000803 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
804 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
805 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
806 }
807
808 // If |c - floor_c| > |c - ceil_c|,
809 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
810 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
811 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
812 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
813}
814
815static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
816 const TargetLowering::DAGCombinerInfo &DCI,
Simon Dardis9ec9f442018-04-13 16:09:07 +0000817 const MipsSETargetLowering *TL,
818 const MipsSubtarget &Subtarget) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000819 EVT VT = N->getValueType(0);
820
821 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
Simon Dardis9ec9f442018-04-13 16:09:07 +0000822 if (!VT.isVector() && shouldTransformMulToShiftsAddsSubs(
823 C->getAPIntValue(), VT, DAG, Subtarget))
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000824 return genConstMult(N->getOperand(0), C->getAPIntValue(), SDLoc(N), VT,
Mehdi Aminieaabc512015-07-09 15:12:23 +0000825 TL->getScalarShiftAmountTy(DAG.getDataLayout(), VT),
826 DAG);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000827
828 return SDValue(N, 0);
829}
830
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000831static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
832 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000833 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000834 // See if this is a vector splat immediate node.
835 APInt SplatValue, SplatUndef;
836 unsigned SplatBitSize;
837 bool HasAnyUndefs;
Sanjay Patel1ed771f2016-09-14 16:37:15 +0000838 unsigned EltSize = Ty.getScalarSizeInBits();
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000839 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
840
Eric Christopher1c29a652014-07-18 22:55:25 +0000841 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000842 return SDValue();
843
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000844 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000845 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000846 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000847 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000848 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000849 return SDValue();
850
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000851 SDLoc DL(N);
852 return DAG.getNode(Opc, DL, Ty, N->getOperand(0),
853 DAG.getConstant(SplatValue.getZExtValue(), DL, MVT::i32));
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000854}
855
856static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
857 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000858 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000859 EVT Ty = N->getValueType(0);
860
861 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
862 return SDValue();
863
864 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
865}
866
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000867// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
868// constant splats into MipsISD::SHRA_DSP for DSPr2.
869//
870// Performs the following transformations:
871// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
872// sign/zero-extension is completely overwritten by the new one performed by
873// the ISD::SRA and ISD::SHL nodes.
874// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
875// sequence.
876//
877// See performDSPShiftCombine for more information about the transformation
878// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000879static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
880 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000881 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000882 EVT Ty = N->getValueType(0);
883
Eric Christopher1c29a652014-07-18 22:55:25 +0000884 if (Subtarget.hasMSA()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000885 SDValue Op0 = N->getOperand(0);
886 SDValue Op1 = N->getOperand(1);
887
888 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
889 // where $d + sizeof($c) == 32
890 // or $d + sizeof($c) <= 32 and SExt
891 // -> (MipsVExtractSExt $a, $b, $c)
892 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
893 SDValue Op0Op0 = Op0->getOperand(0);
894 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
895
896 if (!ShAmount)
897 return SDValue();
898
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000899 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
900 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
901 return SDValue();
902
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000903 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
904 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
905
906 if (TotalBits == 32 ||
907 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
908 TotalBits <= 32)) {
909 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
910 Op0Op0->getOperand(2) };
Chandler Carruth356665a2014-08-01 22:09:43 +0000911 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
912 Op0Op0->getVTList(),
913 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000914 }
915 }
916 }
917
Eric Christopher1c29a652014-07-18 22:55:25 +0000918 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000919 return SDValue();
920
921 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
922}
923
924
925static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
926 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000927 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000928 EVT Ty = N->getValueType(0);
929
Eric Christopher1c29a652014-07-18 22:55:25 +0000930 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000931 return SDValue();
932
933 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
934}
935
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000936static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
937 bool IsV216 = (Ty == MVT::v2i16);
938
939 switch (CC) {
940 case ISD::SETEQ:
941 case ISD::SETNE: return true;
942 case ISD::SETLT:
943 case ISD::SETLE:
944 case ISD::SETGT:
945 case ISD::SETGE: return IsV216;
946 case ISD::SETULT:
947 case ISD::SETULE:
948 case ISD::SETUGT:
949 case ISD::SETUGE: return !IsV216;
950 default: return false;
951 }
952}
953
954static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
955 EVT Ty = N->getValueType(0);
956
957 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
958 return SDValue();
959
960 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
961 return SDValue();
962
Andrew Trickef9de2a2013-05-25 02:42:55 +0000963 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000964 N->getOperand(1), N->getOperand(2));
965}
966
967static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
968 EVT Ty = N->getValueType(0);
969
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000970 if (Ty == MVT::v2i16 || Ty == MVT::v4i8) {
Daniel Sanders3ce56622013-09-24 12:18:31 +0000971 SDValue SetCC = N->getOperand(0);
972
973 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
974 return SDValue();
975
976 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
977 SetCC.getOperand(0), SetCC.getOperand(1),
978 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
979 }
980
981 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000982}
983
Daniel Sandersf7456c72013-09-23 13:22:24 +0000984static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000985 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000986 EVT Ty = N->getValueType(0);
987
Eric Christopher1c29a652014-07-18 22:55:25 +0000988 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000989 // Try the following combines:
990 // (xor (or $a, $b), (build_vector allones))
991 // (xor (or $a, $b), (bitcast (build_vector allones)))
992 SDValue Op0 = N->getOperand(0);
993 SDValue Op1 = N->getOperand(1);
994 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000995
996 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
997 NotOp = Op1;
998 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
999 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001000 else
1001 return SDValue();
1002
1003 if (NotOp->getOpcode() == ISD::OR)
1004 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
1005 NotOp->getOperand(1));
1006 }
1007
1008 return SDValue();
1009}
1010
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001011SDValue
1012MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1013 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001014 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001015
1016 switch (N->getOpcode()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001017 case ISD::AND:
1018 Val = performANDCombine(N, DAG, DCI, Subtarget);
1019 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +00001020 case ISD::OR:
1021 Val = performORCombine(N, DAG, DCI, Subtarget);
1022 break;
Akira Hatanaka5832fc62013-06-26 18:48:17 +00001023 case ISD::MUL:
Simon Dardis9ec9f442018-04-13 16:09:07 +00001024 return performMULCombine(N, DAG, DCI, this, Subtarget);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001025 case ISD::SHL:
Petar Jovanovicb71386a2017-03-15 13:10:08 +00001026 Val = performSHLCombine(N, DAG, DCI, Subtarget);
1027 break;
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001028 case ISD::SRA:
1029 return performSRACombine(N, DAG, DCI, Subtarget);
1030 case ISD::SRL:
1031 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001032 case ISD::VSELECT:
1033 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +00001034 case ISD::XOR:
1035 Val = performXORCombine(N, DAG, Subtarget);
1036 break;
1037 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001038 Val = performSETCCCombine(N, DAG);
1039 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001040 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001041
Daniel Sanders62aeab82013-10-30 13:31:27 +00001042 if (Val.getNode()) {
1043 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1044 N->printrWithDepth(dbgs(), &DAG);
1045 dbgs() << "\n=> \n";
1046 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1047 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001048 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001049 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001050
1051 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001052}
1053
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001054MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001055MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001056 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001057 switch (MI.getOpcode()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001058 default:
1059 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1060 case Mips::BPOSGE32_PSEUDO:
1061 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001062 case Mips::SNZ_B_PSEUDO:
1063 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1064 case Mips::SNZ_H_PSEUDO:
1065 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1066 case Mips::SNZ_W_PSEUDO:
1067 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1068 case Mips::SNZ_D_PSEUDO:
1069 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1070 case Mips::SNZ_V_PSEUDO:
1071 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1072 case Mips::SZ_B_PSEUDO:
1073 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1074 case Mips::SZ_H_PSEUDO:
1075 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1076 case Mips::SZ_W_PSEUDO:
1077 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1078 case Mips::SZ_D_PSEUDO:
1079 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1080 case Mips::SZ_V_PSEUDO:
1081 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001082 case Mips::COPY_FW_PSEUDO:
1083 return emitCOPY_FW(MI, BB);
1084 case Mips::COPY_FD_PSEUDO:
1085 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001086 case Mips::INSERT_FW_PSEUDO:
1087 return emitINSERT_FW(MI, BB);
1088 case Mips::INSERT_FD_PSEUDO:
1089 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001090 case Mips::INSERT_B_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001091 case Mips::INSERT_B_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001092 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1093 case Mips::INSERT_H_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001094 case Mips::INSERT_H_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001095 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1096 case Mips::INSERT_W_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001097 case Mips::INSERT_W_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001098 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1099 case Mips::INSERT_D_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001100 case Mips::INSERT_D_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001101 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1102 case Mips::INSERT_FW_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001103 case Mips::INSERT_FW_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001104 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1105 case Mips::INSERT_FD_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001106 case Mips::INSERT_FD_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001107 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001108 case Mips::FILL_FW_PSEUDO:
1109 return emitFILL_FW(MI, BB);
1110 case Mips::FILL_FD_PSEUDO:
1111 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001112 case Mips::FEXP2_W_1_PSEUDO:
1113 return emitFEXP2_W_1(MI, BB);
1114 case Mips::FEXP2_D_1_PSEUDO:
1115 return emitFEXP2_D_1(MI, BB);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00001116 case Mips::ST_F16:
1117 return emitST_F16_PSEUDO(MI, BB);
1118 case Mips::LD_F16:
1119 return emitLD_F16_PSEUDO(MI, BB);
1120 case Mips::MSA_FP_EXTEND_W_PSEUDO:
1121 return emitFPEXTEND_PSEUDO(MI, BB, false);
1122 case Mips::MSA_FP_ROUND_W_PSEUDO:
1123 return emitFPROUND_PSEUDO(MI, BB, false);
1124 case Mips::MSA_FP_EXTEND_D_PSEUDO:
1125 return emitFPEXTEND_PSEUDO(MI, BB, true);
1126 case Mips::MSA_FP_ROUND_D_PSEUDO:
1127 return emitFPROUND_PSEUDO(MI, BB, true);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001128 }
1129}
1130
Daniel Sanders23e98772014-11-02 16:09:29 +00001131bool MipsSETargetLowering::isEligibleForTailCallOptimization(
1132 const CCState &CCInfo, unsigned NextStackOffset,
1133 const MipsFunctionInfo &FI) const {
Simon Dardis57f4ae42016-08-04 09:17:07 +00001134 if (!UseMipsTailCalls)
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001135 return false;
1136
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00001137 // Exception has to be cleared with eret.
1138 if (FI.isISR())
1139 return false;
1140
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001141 // Return false if either the callee or caller has a byval argument.
Daniel Sanders23e98772014-11-02 16:09:29 +00001142 if (CCInfo.getInRegsParamsCount() > 0 || FI.hasByvalArg())
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001143 return false;
1144
1145 // Return true if the callee's argument area is no larger than the
1146 // caller's.
1147 return NextStackOffset <= FI.getIncomingArgSize();
1148}
1149
1150void MipsSETargetLowering::
1151getOpndList(SmallVectorImpl<SDValue> &Ops,
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001152 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001153 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001154 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
1155 SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001156 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001157 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001158 InternalLinkage, IsCallReloc, CLI, Callee,
1159 Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001160}
1161
Akira Hatanaka63791212013-09-07 00:52:30 +00001162SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1163 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1164
1165 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1166 return MipsTargetLowering::lowerLOAD(Op, DAG);
1167
1168 // Replace a double precision load with two i32 loads and a buildpair64.
1169 SDLoc DL(Op);
1170 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1171 EVT PtrVT = Ptr.getValueType();
1172
1173 // i32 load from lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001174 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr, MachinePointerInfo(),
1175 Nd.getAlignment(), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001176
1177 // i32 load from higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001178 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001179 SDValue Hi = DAG.getLoad(
1180 MVT::i32, DL, Lo.getValue(1), Ptr, MachinePointerInfo(),
1181 std::min(Nd.getAlignment(), 4U), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001182
Eric Christopher1c29a652014-07-18 22:55:25 +00001183 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001184 std::swap(Lo, Hi);
1185
1186 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1187 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001188 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001189}
1190
1191SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1192 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1193
1194 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1195 return MipsTargetLowering::lowerSTORE(Op, DAG);
1196
1197 // Replace a double precision store with two extractelement64s and i32 stores.
1198 SDLoc DL(Op);
1199 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1200 EVT PtrVT = Ptr.getValueType();
1201 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001202 Val, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001203 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001204 Val, DAG.getConstant(1, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001205
Eric Christopher1c29a652014-07-18 22:55:25 +00001206 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001207 std::swap(Lo, Hi);
1208
1209 // i32 store to lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001210 Chain =
1211 DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(), Nd.getAlignment(),
1212 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001213
1214 // i32 store to higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001215 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Akira Hatanaka63791212013-09-07 00:52:30 +00001216 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001217 std::min(Nd.getAlignment(), 4U),
1218 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001219}
1220
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001221SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1222 bool HasLo, bool HasHi,
1223 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001224 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001225 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001226
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001227 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001228 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001229 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1230 Op.getOperand(0), Op.getOperand(1));
1231 SDValue Lo, Hi;
1232
1233 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001234 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001235 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001236 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001237
1238 if (!HasLo || !HasHi)
1239 return HasLo ? Lo : Hi;
1240
1241 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001242 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001243}
1244
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001245static SDValue initAccumulator(SDValue In, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001246 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001247 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001248 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001249 DAG.getConstant(1, DL, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001250 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001251}
1252
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001253static SDValue extractLOHI(SDValue Op, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001254 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1255 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001256 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1257}
1258
1259// This function expands mips intrinsic nodes which have 64-bit input operands
1260// or output values.
1261//
1262// out64 = intrinsic-node in64
1263// =>
1264// lo = copy (extract-element (in64, 0))
1265// hi = copy (extract-element (in64, 1))
1266// mips-specific-node
1267// v0 = copy lo
1268// v1 = copy hi
1269// out64 = merge-values (v0, v1)
1270//
1271static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001272 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001273 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1274 SmallVector<SDValue, 3> Ops;
1275 unsigned OpNo = 0;
1276
1277 // See if Op has a chain input.
1278 if (HasChainIn)
1279 Ops.push_back(Op->getOperand(OpNo++));
1280
1281 // The next operand is the intrinsic opcode.
1282 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1283
1284 // See if the next operand has type i64.
1285 SDValue Opnd = Op->getOperand(++OpNo), In64;
1286
1287 if (Opnd.getValueType() == MVT::i64)
1288 In64 = initAccumulator(Opnd, DL, DAG);
1289 else
1290 Ops.push_back(Opnd);
1291
1292 // Push the remaining operands.
1293 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1294 Ops.push_back(Op->getOperand(OpNo));
1295
1296 // Add In64 to the end of the list.
1297 if (In64.getNode())
1298 Ops.push_back(In64);
1299
1300 // Scan output.
1301 SmallVector<EVT, 2> ResTys;
1302
1303 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1304 I != E; ++I)
1305 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1306
1307 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001308 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001309 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1310
1311 if (!HasChainIn)
1312 return Out;
1313
1314 assert(Val->getValueType(1) == MVT::Other);
1315 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001316 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001317}
1318
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001319// Lower an MSA copy intrinsic into the specified SelectionDAG node
1320static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1321 SDLoc DL(Op);
1322 SDValue Vec = Op->getOperand(1);
1323 SDValue Idx = Op->getOperand(2);
1324 EVT ResTy = Op->getValueType(0);
1325 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1326
1327 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1328 DAG.getValueType(EltTy));
1329
1330 return Result;
1331}
1332
Daniel Sanders50b80412013-11-15 12:56:49 +00001333static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1334 EVT ResVecTy = Op->getValueType(0);
1335 EVT ViaVecTy = ResVecTy;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001336 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
Daniel Sanders50b80412013-11-15 12:56:49 +00001337 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001338
Daniel Sanders50b80412013-11-15 12:56:49 +00001339 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1340 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1341 // lanes.
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001342 SDValue LaneA = Op->getOperand(OpNr);
1343 SDValue LaneB;
Daniel Sanders50b80412013-11-15 12:56:49 +00001344
1345 if (ResVecTy == MVT::v2i64) {
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001346 LaneB = DAG.getConstant(0, DL, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001347 ViaVecTy = MVT::v4i32;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001348 if(BigEndian)
1349 std::swap(LaneA, LaneB);
Daniel Sanders50b80412013-11-15 12:56:49 +00001350 } else
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001351 LaneB = LaneA;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001352
Daniel Sanders50b80412013-11-15 12:56:49 +00001353 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1354 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001355
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001356 SDValue Result = DAG.getBuildVector(
1357 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001358
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001359 if (ViaVecTy != ResVecTy) {
1360 SDValue One = DAG.getConstant(1, DL, ViaVecTy);
1361 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy,
1362 DAG.getNode(ISD::AND, DL, ViaVecTy, Result, One));
1363 }
Daniel Sandersf49dd822013-09-24 13:33:07 +00001364
1365 return Result;
1366}
1367
Simon Dardis548a53f2017-01-10 16:40:57 +00001368static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG,
1369 bool IsSigned = false) {
1370 return DAG.getConstant(
1371 APInt(Op->getValueType(0).getScalarType().getSizeInBits(),
1372 Op->getConstantOperandVal(ImmOp), IsSigned),
1373 SDLoc(Op), Op->getValueType(0));
Daniel Sanders50b80412013-11-15 12:56:49 +00001374}
1375
1376static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1377 bool BigEndian, SelectionDAG &DAG) {
1378 EVT ViaVecTy = VecTy;
1379 SDValue SplatValueA = SplatValue;
1380 SDValue SplatValueB = SplatValue;
1381 SDLoc DL(SplatValue);
1382
1383 if (VecTy == MVT::v2i64) {
1384 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1385 ViaVecTy = MVT::v4i32;
1386
1387 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1388 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001389 DAG.getConstant(32, DL, MVT::i32));
Daniel Sanders50b80412013-11-15 12:56:49 +00001390 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1391 }
1392
1393 // We currently hold the parts in little endian order. Swap them if
1394 // necessary.
1395 if (BigEndian)
1396 std::swap(SplatValueA, SplatValueB);
1397
1398 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1399 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1400 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1401 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1402
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001403 SDValue Result = DAG.getBuildVector(
1404 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001405
1406 if (VecTy != ViaVecTy)
1407 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1408
1409 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001410}
1411
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001412static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1413 unsigned Opc, SDValue Imm,
1414 bool BigEndian) {
1415 EVT VecTy = Op->getValueType(0);
1416 SDValue Exp2Imm;
1417 SDLoc DL(Op);
1418
Daniel Sanders50b80412013-11-15 12:56:49 +00001419 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1420 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001421 if (VecTy == MVT::v2i64) {
1422 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1423 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1424
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001425 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), DL,
1426 MVT::i32);
1427 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), DL, MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001428
1429 if (BigEndian)
1430 std::swap(BitImmLoOp, BitImmHiOp);
1431
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001432 Exp2Imm = DAG.getNode(
1433 ISD::BITCAST, DL, MVT::v2i64,
1434 DAG.getBuildVector(MVT::v4i32, DL,
1435 {BitImmLoOp, BitImmHiOp, BitImmLoOp, BitImmHiOp}));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001436 }
1437 }
1438
Craig Topper062a2ba2014-04-25 05:30:21 +00001439 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001440 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001441
1442 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1443 // only values 0-63 are valid.
1444 if (VecTy == MVT::v2i64)
1445 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1446
1447 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1448
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001449 Exp2Imm = DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, DL, VecTy),
1450 Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001451 }
1452
1453 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1454}
1455
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001456static SDValue truncateVecElts(SDValue Op, SelectionDAG &DAG) {
1457 SDLoc DL(Op);
1458 EVT ResTy = Op->getValueType(0);
1459 SDValue Vec = Op->getOperand(2);
1460 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
1461 MVT ResEltTy = ResTy == MVT::v2i64 ? MVT::i64 : MVT::i32;
1462 SDValue ConstValue = DAG.getConstant(Vec.getScalarValueSizeInBits() - 1,
1463 DL, ResEltTy);
1464 SDValue SplatVec = getBuildVectorSplat(ResTy, ConstValue, BigEndian, DAG);
1465
1466 return DAG.getNode(ISD::AND, DL, ResTy, Vec, SplatVec);
1467}
1468
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001469static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1470 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001471 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001472 SDValue One = DAG.getConstant(1, DL, ResTy);
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001473 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, truncateVecElts(Op, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001474
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001475 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1476 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001477}
1478
1479static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1480 SDLoc DL(Op);
1481 EVT ResTy = Op->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001482 APInt BitImm = APInt(ResTy.getScalarSizeInBits(), 1)
Daniel Sanders50b80412013-11-15 12:56:49 +00001483 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001484 SDValue BitMask = DAG.getConstant(~BitImm, DL, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001485
1486 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1487}
1488
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001489SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1490 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001491 SDLoc DL(Op);
Simon Dardis548a53f2017-01-10 16:40:57 +00001492 unsigned Intrinsic = cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue();
1493 switch (Intrinsic) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001494 default:
1495 return SDValue();
1496 case Intrinsic::mips_shilo:
1497 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1498 case Intrinsic::mips_dpau_h_qbl:
1499 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1500 case Intrinsic::mips_dpau_h_qbr:
1501 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1502 case Intrinsic::mips_dpsu_h_qbl:
1503 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1504 case Intrinsic::mips_dpsu_h_qbr:
1505 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1506 case Intrinsic::mips_dpa_w_ph:
1507 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1508 case Intrinsic::mips_dps_w_ph:
1509 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1510 case Intrinsic::mips_dpax_w_ph:
1511 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1512 case Intrinsic::mips_dpsx_w_ph:
1513 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1514 case Intrinsic::mips_mulsa_w_ph:
1515 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1516 case Intrinsic::mips_mult:
1517 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1518 case Intrinsic::mips_multu:
1519 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1520 case Intrinsic::mips_madd:
1521 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1522 case Intrinsic::mips_maddu:
1523 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1524 case Intrinsic::mips_msub:
1525 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1526 case Intrinsic::mips_msubu:
1527 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001528 case Intrinsic::mips_addv_b:
1529 case Intrinsic::mips_addv_h:
1530 case Intrinsic::mips_addv_w:
1531 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001532 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1533 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001534 case Intrinsic::mips_addvi_b:
1535 case Intrinsic::mips_addvi_h:
1536 case Intrinsic::mips_addvi_w:
1537 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001538 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1539 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001540 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001541 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1542 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001543 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001544 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1545 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001546 case Intrinsic::mips_bclr_b:
1547 case Intrinsic::mips_bclr_h:
1548 case Intrinsic::mips_bclr_w:
1549 case Intrinsic::mips_bclr_d:
1550 return lowerMSABitClear(Op, DAG);
1551 case Intrinsic::mips_bclri_b:
1552 case Intrinsic::mips_bclri_h:
1553 case Intrinsic::mips_bclri_w:
1554 case Intrinsic::mips_bclri_d:
1555 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001556 case Intrinsic::mips_binsli_b:
1557 case Intrinsic::mips_binsli_h:
1558 case Intrinsic::mips_binsli_w:
1559 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001560 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001561 EVT VecTy = Op->getValueType(0);
1562 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001563 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1564 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001565 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001566 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001567 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001568 DAG.getConstant(Mask, DL, VecTy, true),
1569 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001570 }
1571 case Intrinsic::mips_binsri_b:
1572 case Intrinsic::mips_binsri_h:
1573 case Intrinsic::mips_binsri_w:
1574 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001575 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001576 EVT VecTy = Op->getValueType(0);
1577 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001578 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1579 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001580 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001581 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001582 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001583 DAG.getConstant(Mask, DL, VecTy, true),
1584 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001585 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001586 case Intrinsic::mips_bmnz_v:
1587 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1588 Op->getOperand(2), Op->getOperand(1));
1589 case Intrinsic::mips_bmnzi_b:
1590 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1591 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1592 Op->getOperand(1));
1593 case Intrinsic::mips_bmz_v:
1594 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1595 Op->getOperand(1), Op->getOperand(2));
1596 case Intrinsic::mips_bmzi_b:
1597 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1598 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1599 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001600 case Intrinsic::mips_bneg_b:
1601 case Intrinsic::mips_bneg_h:
1602 case Intrinsic::mips_bneg_w:
1603 case Intrinsic::mips_bneg_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::XOR, 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_bnegi_b:
1612 case Intrinsic::mips_bnegi_h:
1613 case Intrinsic::mips_bnegi_w:
1614 case Intrinsic::mips_bnegi_d:
1615 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001616 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001617 case Intrinsic::mips_bnz_b:
1618 case Intrinsic::mips_bnz_h:
1619 case Intrinsic::mips_bnz_w:
1620 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001621 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1622 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001623 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001624 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1625 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001626 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001627 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001628 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001629 Op->getOperand(1), Op->getOperand(3),
1630 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001631 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001632 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001633 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001634 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1635 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001636 case Intrinsic::mips_bset_b:
1637 case Intrinsic::mips_bset_h:
1638 case Intrinsic::mips_bset_w:
1639 case Intrinsic::mips_bset_d: {
1640 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001641 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001642
1643 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1644 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001645 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001646 }
1647 case Intrinsic::mips_bseti_b:
1648 case Intrinsic::mips_bseti_h:
1649 case Intrinsic::mips_bseti_w:
1650 case Intrinsic::mips_bseti_d:
1651 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001652 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001653 case Intrinsic::mips_bz_b:
1654 case Intrinsic::mips_bz_h:
1655 case Intrinsic::mips_bz_w:
1656 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001657 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1658 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001659 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001660 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1661 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001662 case Intrinsic::mips_ceq_b:
1663 case Intrinsic::mips_ceq_h:
1664 case Intrinsic::mips_ceq_w:
1665 case Intrinsic::mips_ceq_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::SETEQ);
1668 case Intrinsic::mips_ceqi_b:
1669 case Intrinsic::mips_ceqi_h:
1670 case Intrinsic::mips_ceqi_w:
1671 case Intrinsic::mips_ceqi_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::SETEQ);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001674 case Intrinsic::mips_cle_s_b:
1675 case Intrinsic::mips_cle_s_h:
1676 case Intrinsic::mips_cle_s_w:
1677 case Intrinsic::mips_cle_s_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::SETLE);
1680 case Intrinsic::mips_clei_s_b:
1681 case Intrinsic::mips_clei_s_h:
1682 case Intrinsic::mips_clei_s_w:
1683 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001684 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001685 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLE);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001686 case Intrinsic::mips_cle_u_b:
1687 case Intrinsic::mips_cle_u_h:
1688 case Intrinsic::mips_cle_u_w:
1689 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001690 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001691 Op->getOperand(2), ISD::SETULE);
1692 case Intrinsic::mips_clei_u_b:
1693 case Intrinsic::mips_clei_u_h:
1694 case Intrinsic::mips_clei_u_w:
1695 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001696 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001697 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1698 case Intrinsic::mips_clt_s_b:
1699 case Intrinsic::mips_clt_s_h:
1700 case Intrinsic::mips_clt_s_w:
1701 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001702 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001703 Op->getOperand(2), ISD::SETLT);
1704 case Intrinsic::mips_clti_s_b:
1705 case Intrinsic::mips_clti_s_h:
1706 case Intrinsic::mips_clti_s_w:
1707 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001708 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001709 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLT);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001710 case Intrinsic::mips_clt_u_b:
1711 case Intrinsic::mips_clt_u_h:
1712 case Intrinsic::mips_clt_u_w:
1713 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001714 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001715 Op->getOperand(2), ISD::SETULT);
1716 case Intrinsic::mips_clti_u_b:
1717 case Intrinsic::mips_clti_u_h:
1718 case Intrinsic::mips_clti_u_w:
1719 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001720 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001721 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001722 case Intrinsic::mips_copy_s_b:
1723 case Intrinsic::mips_copy_s_h:
1724 case Intrinsic::mips_copy_s_w:
1725 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001726 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001727 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001728 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1729 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1730 else {
1731 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1732 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1733 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1734 Op->getValueType(0), Op->getOperand(1),
1735 Op->getOperand(2));
1736 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001737 case Intrinsic::mips_copy_u_b:
1738 case Intrinsic::mips_copy_u_h:
1739 case Intrinsic::mips_copy_u_w:
1740 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001741 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001742 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001743 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1744 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1745 else {
1746 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1747 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1748 // Note: When i64 is illegal, this results in copy_s.w instructions
1749 // instead of copy_u.w instructions. This makes no difference to the
1750 // behaviour since i64 is only illegal when the register file is 32-bit.
1751 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1752 Op->getValueType(0), Op->getOperand(1),
1753 Op->getOperand(2));
1754 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001755 case Intrinsic::mips_div_s_b:
1756 case Intrinsic::mips_div_s_h:
1757 case Intrinsic::mips_div_s_w:
1758 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001759 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1760 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001761 case Intrinsic::mips_div_u_b:
1762 case Intrinsic::mips_div_u_h:
1763 case Intrinsic::mips_div_u_w:
1764 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001765 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1766 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001767 case Intrinsic::mips_fadd_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001768 case Intrinsic::mips_fadd_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001769 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001770 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1771 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001772 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1773 case Intrinsic::mips_fceq_w:
1774 case Intrinsic::mips_fceq_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::SETOEQ);
1777 case Intrinsic::mips_fcle_w:
1778 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001779 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001780 Op->getOperand(2), ISD::SETOLE);
1781 case Intrinsic::mips_fclt_w:
1782 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001783 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001784 Op->getOperand(2), ISD::SETOLT);
1785 case Intrinsic::mips_fcne_w:
1786 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001787 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001788 Op->getOperand(2), ISD::SETONE);
1789 case Intrinsic::mips_fcor_w:
1790 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001791 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001792 Op->getOperand(2), ISD::SETO);
1793 case Intrinsic::mips_fcueq_w:
1794 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001795 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001796 Op->getOperand(2), ISD::SETUEQ);
1797 case Intrinsic::mips_fcule_w:
1798 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001799 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001800 Op->getOperand(2), ISD::SETULE);
1801 case Intrinsic::mips_fcult_w:
1802 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001803 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001804 Op->getOperand(2), ISD::SETULT);
1805 case Intrinsic::mips_fcun_w:
1806 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001807 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001808 Op->getOperand(2), ISD::SETUO);
1809 case Intrinsic::mips_fcune_w:
1810 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001811 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001812 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001813 case Intrinsic::mips_fdiv_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001814 case Intrinsic::mips_fdiv_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001815 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001816 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1817 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001818 case Intrinsic::mips_ffint_u_w:
1819 case Intrinsic::mips_ffint_u_d:
1820 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1821 Op->getOperand(1));
1822 case Intrinsic::mips_ffint_s_w:
1823 case Intrinsic::mips_ffint_s_d:
1824 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1825 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001826 case Intrinsic::mips_fill_b:
1827 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001828 case Intrinsic::mips_fill_w:
1829 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001830 EVT ResTy = Op->getValueType(0);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001831 SmallVector<SDValue, 16> Ops(ResTy.getVectorNumElements(),
1832 Op->getOperand(1));
Daniel Sandersf49dd822013-09-24 13:33:07 +00001833
Daniel Sandersc72593e2013-09-27 13:20:41 +00001834 // If ResTy is v2i64 then the type legalizer will break this node down into
1835 // an equivalent v4i32.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001836 return DAG.getBuildVector(ResTy, DL, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001837 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001838 case Intrinsic::mips_fexp2_w:
1839 case Intrinsic::mips_fexp2_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001840 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sandersa9521602013-10-23 10:36:52 +00001841 EVT ResTy = Op->getValueType(0);
1842 return DAG.getNode(
1843 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1844 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1845 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001846 case Intrinsic::mips_flog2_w:
1847 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001848 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001849 case Intrinsic::mips_fmadd_w:
1850 case Intrinsic::mips_fmadd_d:
1851 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1852 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001853 case Intrinsic::mips_fmul_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001854 case Intrinsic::mips_fmul_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001855 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001856 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1857 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001858 case Intrinsic::mips_fmsub_w:
1859 case Intrinsic::mips_fmsub_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001860 // TODO: If intrinsics have fast-math-flags, propagate them.
Aleksandar Beserminji3546c162018-04-27 13:30:27 +00001861 return DAG.getNode(MipsISD::FMS, SDLoc(Op), Op->getValueType(0),
1862 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001863 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001864 case Intrinsic::mips_frint_w:
1865 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001866 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001867 case Intrinsic::mips_fsqrt_w:
1868 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001869 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001870 case Intrinsic::mips_fsub_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001871 case Intrinsic::mips_fsub_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001872 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001873 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1874 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001875 case Intrinsic::mips_ftrunc_u_w:
1876 case Intrinsic::mips_ftrunc_u_d:
1877 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1878 Op->getOperand(1));
1879 case Intrinsic::mips_ftrunc_s_w:
1880 case Intrinsic::mips_ftrunc_s_d:
1881 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1882 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001883 case Intrinsic::mips_ilvev_b:
1884 case Intrinsic::mips_ilvev_h:
1885 case Intrinsic::mips_ilvev_w:
1886 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001887 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001888 Op->getOperand(1), Op->getOperand(2));
1889 case Intrinsic::mips_ilvl_b:
1890 case Intrinsic::mips_ilvl_h:
1891 case Intrinsic::mips_ilvl_w:
1892 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001893 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001894 Op->getOperand(1), Op->getOperand(2));
1895 case Intrinsic::mips_ilvod_b:
1896 case Intrinsic::mips_ilvod_h:
1897 case Intrinsic::mips_ilvod_w:
1898 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001899 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001900 Op->getOperand(1), Op->getOperand(2));
1901 case Intrinsic::mips_ilvr_b:
1902 case Intrinsic::mips_ilvr_h:
1903 case Intrinsic::mips_ilvr_w:
1904 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001905 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001906 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001907 case Intrinsic::mips_insert_b:
1908 case Intrinsic::mips_insert_h:
1909 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001910 case Intrinsic::mips_insert_d:
1911 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1912 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001913 case Intrinsic::mips_insve_b:
1914 case Intrinsic::mips_insve_h:
1915 case Intrinsic::mips_insve_w:
Simon Dardis548a53f2017-01-10 16:40:57 +00001916 case Intrinsic::mips_insve_d: {
1917 // Report an error for out of range values.
1918 int64_t Max;
1919 switch (Intrinsic) {
1920 case Intrinsic::mips_insve_b: Max = 15; break;
1921 case Intrinsic::mips_insve_h: Max = 7; break;
1922 case Intrinsic::mips_insve_w: Max = 3; break;
1923 case Intrinsic::mips_insve_d: Max = 1; break;
1924 default: llvm_unreachable("Unmatched intrinsic");
1925 }
1926 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
1927 if (Value < 0 || Value > Max)
1928 report_fatal_error("Immediate out of range");
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001929 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1930 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001931 DAG.getConstant(0, DL, MVT::i32));
Simon Dardis548a53f2017-01-10 16:40:57 +00001932 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00001933 case Intrinsic::mips_ldi_b:
1934 case Intrinsic::mips_ldi_h:
1935 case Intrinsic::mips_ldi_w:
1936 case Intrinsic::mips_ldi_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00001937 return lowerMSASplatImm(Op, 1, DAG, true);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001938 case Intrinsic::mips_lsa:
1939 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001940 EVT ResTy = Op->getValueType(0);
1941 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1942 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1943 Op->getOperand(2), Op->getOperand(3)));
1944 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001945 case Intrinsic::mips_maddv_b:
1946 case Intrinsic::mips_maddv_h:
1947 case Intrinsic::mips_maddv_w:
1948 case Intrinsic::mips_maddv_d: {
1949 EVT ResTy = Op->getValueType(0);
1950 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1951 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1952 Op->getOperand(2), Op->getOperand(3)));
1953 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001954 case Intrinsic::mips_max_s_b:
1955 case Intrinsic::mips_max_s_h:
1956 case Intrinsic::mips_max_s_w:
1957 case Intrinsic::mips_max_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001958 return DAG.getNode(ISD::SMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001959 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001960 case Intrinsic::mips_max_u_b:
1961 case Intrinsic::mips_max_u_h:
1962 case Intrinsic::mips_max_u_w:
1963 case Intrinsic::mips_max_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001964 return DAG.getNode(ISD::UMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001965 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001966 case Intrinsic::mips_maxi_s_b:
1967 case Intrinsic::mips_maxi_s_h:
1968 case Intrinsic::mips_maxi_s_w:
1969 case Intrinsic::mips_maxi_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001970 return DAG.getNode(ISD::SMAX, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00001971 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001972 case Intrinsic::mips_maxi_u_b:
1973 case Intrinsic::mips_maxi_u_h:
1974 case Intrinsic::mips_maxi_u_w:
1975 case Intrinsic::mips_maxi_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001976 return DAG.getNode(ISD::UMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001977 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001978 case Intrinsic::mips_min_s_b:
1979 case Intrinsic::mips_min_s_h:
1980 case Intrinsic::mips_min_s_w:
1981 case Intrinsic::mips_min_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001982 return DAG.getNode(ISD::SMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001983 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001984 case Intrinsic::mips_min_u_b:
1985 case Intrinsic::mips_min_u_h:
1986 case Intrinsic::mips_min_u_w:
1987 case Intrinsic::mips_min_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001988 return DAG.getNode(ISD::UMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001989 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001990 case Intrinsic::mips_mini_s_b:
1991 case Intrinsic::mips_mini_s_h:
1992 case Intrinsic::mips_mini_s_w:
1993 case Intrinsic::mips_mini_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001994 return DAG.getNode(ISD::SMIN, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00001995 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001996 case Intrinsic::mips_mini_u_b:
1997 case Intrinsic::mips_mini_u_h:
1998 case Intrinsic::mips_mini_u_w:
1999 case Intrinsic::mips_mini_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002000 return DAG.getNode(ISD::UMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002001 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00002002 case Intrinsic::mips_mod_s_b:
2003 case Intrinsic::mips_mod_s_h:
2004 case Intrinsic::mips_mod_s_w:
2005 case Intrinsic::mips_mod_s_d:
2006 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
2007 Op->getOperand(2));
2008 case Intrinsic::mips_mod_u_b:
2009 case Intrinsic::mips_mod_u_h:
2010 case Intrinsic::mips_mod_u_w:
2011 case Intrinsic::mips_mod_u_d:
2012 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
2013 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002014 case Intrinsic::mips_mulv_b:
2015 case Intrinsic::mips_mulv_h:
2016 case Intrinsic::mips_mulv_w:
2017 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002018 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
2019 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00002020 case Intrinsic::mips_msubv_b:
2021 case Intrinsic::mips_msubv_h:
2022 case Intrinsic::mips_msubv_w:
2023 case Intrinsic::mips_msubv_d: {
2024 EVT ResTy = Op->getValueType(0);
2025 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
2026 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2027 Op->getOperand(2), Op->getOperand(3)));
2028 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002029 case Intrinsic::mips_nlzc_b:
2030 case Intrinsic::mips_nlzc_h:
2031 case Intrinsic::mips_nlzc_w:
2032 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002033 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002034 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002035 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2036 Op->getOperand(1), Op->getOperand(2));
2037 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002038 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002039 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002040 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2041 Op->getOperand(1),
2042 lowerMSASplatImm(Op, 2, DAG));
2043 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002044 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002045 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002046 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2047 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002048 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002049 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2050 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002051 case Intrinsic::mips_pckev_b:
2052 case Intrinsic::mips_pckev_h:
2053 case Intrinsic::mips_pckev_w:
2054 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002055 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002056 Op->getOperand(1), Op->getOperand(2));
2057 case Intrinsic::mips_pckod_b:
2058 case Intrinsic::mips_pckod_h:
2059 case Intrinsic::mips_pckod_w:
2060 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002061 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002062 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002063 case Intrinsic::mips_pcnt_b:
2064 case Intrinsic::mips_pcnt_h:
2065 case Intrinsic::mips_pcnt_w:
2066 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002067 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002068 case Intrinsic::mips_sat_s_b:
2069 case Intrinsic::mips_sat_s_h:
2070 case Intrinsic::mips_sat_s_w:
2071 case Intrinsic::mips_sat_s_d:
2072 case Intrinsic::mips_sat_u_b:
2073 case Intrinsic::mips_sat_u_h:
2074 case Intrinsic::mips_sat_u_w:
2075 case Intrinsic::mips_sat_u_d: {
2076 // Report an error for out of range values.
2077 int64_t Max;
2078 switch (Intrinsic) {
2079 case Intrinsic::mips_sat_s_b:
2080 case Intrinsic::mips_sat_u_b: Max = 7; break;
2081 case Intrinsic::mips_sat_s_h:
2082 case Intrinsic::mips_sat_u_h: Max = 15; break;
2083 case Intrinsic::mips_sat_s_w:
2084 case Intrinsic::mips_sat_u_w: Max = 31; break;
2085 case Intrinsic::mips_sat_s_d:
2086 case Intrinsic::mips_sat_u_d: Max = 63; break;
2087 default: llvm_unreachable("Unmatched intrinsic");
2088 }
2089 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2090 if (Value < 0 || Value > Max)
2091 report_fatal_error("Immediate out of range");
2092 return SDValue();
2093 }
Daniel Sanders26307182013-09-24 14:20:00 +00002094 case Intrinsic::mips_shf_b:
2095 case Intrinsic::mips_shf_h:
Simon Dardis548a53f2017-01-10 16:40:57 +00002096 case Intrinsic::mips_shf_w: {
2097 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2098 if (Value < 0 || Value > 255)
2099 report_fatal_error("Immediate out of range");
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002100 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002101 Op->getOperand(2), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002102 }
2103 case Intrinsic::mips_sldi_b:
2104 case Intrinsic::mips_sldi_h:
2105 case Intrinsic::mips_sldi_w:
2106 case Intrinsic::mips_sldi_d: {
2107 // Report an error for out of range values.
2108 int64_t Max;
2109 switch (Intrinsic) {
2110 case Intrinsic::mips_sldi_b: Max = 15; break;
2111 case Intrinsic::mips_sldi_h: Max = 7; break;
2112 case Intrinsic::mips_sldi_w: Max = 3; break;
2113 case Intrinsic::mips_sldi_d: Max = 1; break;
2114 default: llvm_unreachable("Unmatched intrinsic");
2115 }
2116 int64_t Value = cast<ConstantSDNode>(Op->getOperand(3))->getSExtValue();
2117 if (Value < 0 || Value > Max)
2118 report_fatal_error("Immediate out of range");
2119 return SDValue();
2120 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002121 case Intrinsic::mips_sll_b:
2122 case Intrinsic::mips_sll_h:
2123 case Intrinsic::mips_sll_w:
2124 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002125 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002126 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002127 case Intrinsic::mips_slli_b:
2128 case Intrinsic::mips_slli_h:
2129 case Intrinsic::mips_slli_w:
2130 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002131 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2132 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002133 case Intrinsic::mips_splat_b:
2134 case Intrinsic::mips_splat_h:
2135 case Intrinsic::mips_splat_w:
2136 case Intrinsic::mips_splat_d:
2137 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2138 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2139 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2140 // Instead we lower to MipsISD::VSHF and match from there.
2141 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002142 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002143 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002144 case Intrinsic::mips_splati_b:
2145 case Intrinsic::mips_splati_h:
2146 case Intrinsic::mips_splati_w:
2147 case Intrinsic::mips_splati_d:
2148 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2149 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2150 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002151 case Intrinsic::mips_sra_b:
2152 case Intrinsic::mips_sra_h:
2153 case Intrinsic::mips_sra_w:
2154 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002155 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002156 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002157 case Intrinsic::mips_srai_b:
2158 case Intrinsic::mips_srai_h:
2159 case Intrinsic::mips_srai_w:
2160 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002161 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2162 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002163 case Intrinsic::mips_srari_b:
2164 case Intrinsic::mips_srari_h:
2165 case Intrinsic::mips_srari_w:
2166 case Intrinsic::mips_srari_d: {
2167 // Report an error for out of range values.
2168 int64_t Max;
2169 switch (Intrinsic) {
2170 case Intrinsic::mips_srari_b: Max = 7; break;
2171 case Intrinsic::mips_srari_h: Max = 15; break;
2172 case Intrinsic::mips_srari_w: Max = 31; break;
2173 case Intrinsic::mips_srari_d: Max = 63; break;
2174 default: llvm_unreachable("Unmatched intrinsic");
2175 }
2176 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2177 if (Value < 0 || Value > Max)
2178 report_fatal_error("Immediate out of range");
2179 return SDValue();
2180 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002181 case Intrinsic::mips_srl_b:
2182 case Intrinsic::mips_srl_h:
2183 case Intrinsic::mips_srl_w:
2184 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002185 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002186 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002187 case Intrinsic::mips_srli_b:
2188 case Intrinsic::mips_srli_h:
2189 case Intrinsic::mips_srli_w:
2190 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002191 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2192 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002193 case Intrinsic::mips_srlri_b:
2194 case Intrinsic::mips_srlri_h:
2195 case Intrinsic::mips_srlri_w:
2196 case Intrinsic::mips_srlri_d: {
2197 // Report an error for out of range values.
2198 int64_t Max;
2199 switch (Intrinsic) {
2200 case Intrinsic::mips_srlri_b: Max = 7; break;
2201 case Intrinsic::mips_srlri_h: Max = 15; break;
2202 case Intrinsic::mips_srlri_w: Max = 31; break;
2203 case Intrinsic::mips_srlri_d: Max = 63; break;
2204 default: llvm_unreachable("Unmatched intrinsic");
2205 }
2206 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2207 if (Value < 0 || Value > Max)
2208 report_fatal_error("Immediate out of range");
2209 return SDValue();
2210 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002211 case Intrinsic::mips_subv_b:
2212 case Intrinsic::mips_subv_h:
2213 case Intrinsic::mips_subv_w:
2214 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002215 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2216 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002217 case Intrinsic::mips_subvi_b:
2218 case Intrinsic::mips_subvi_h:
2219 case Intrinsic::mips_subvi_w:
2220 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002221 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2222 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002223 case Intrinsic::mips_vshf_b:
2224 case Intrinsic::mips_vshf_h:
2225 case Intrinsic::mips_vshf_w:
2226 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002227 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002228 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002229 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002230 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2231 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002232 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002233 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2234 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Marcin Koscielnicki7efdca52016-04-27 17:21:49 +00002235 case Intrinsic::thread_pointer: {
2236 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2237 return DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2238 }
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002239 }
2240}
2241
Simon Dardis548a53f2017-01-10 16:40:57 +00002242static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2243 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002244 SDLoc DL(Op);
2245 SDValue ChainIn = Op->getOperand(0);
2246 SDValue Address = Op->getOperand(2);
2247 SDValue Offset = Op->getOperand(3);
2248 EVT ResTy = Op->getValueType(0);
2249 EVT PtrTy = Address->getValueType(0);
2250
Simon Dardis548a53f2017-01-10 16:40:57 +00002251 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2252 // however takes an i32 signed constant offset. The actual type of the
2253 // intrinsic is a scaled signed i10.
2254 if (Subtarget.isABI_N64())
2255 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2256
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002257 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002258 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(),
2259 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002260}
2261
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002262SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2263 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002264 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2265 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002266 default:
2267 return SDValue();
2268 case Intrinsic::mips_extp:
2269 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2270 case Intrinsic::mips_extpdp:
2271 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2272 case Intrinsic::mips_extr_w:
2273 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2274 case Intrinsic::mips_extr_r_w:
2275 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2276 case Intrinsic::mips_extr_rs_w:
2277 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2278 case Intrinsic::mips_extr_s_h:
2279 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2280 case Intrinsic::mips_mthlip:
2281 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2282 case Intrinsic::mips_mulsaq_s_w_ph:
2283 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2284 case Intrinsic::mips_maq_s_w_phl:
2285 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2286 case Intrinsic::mips_maq_s_w_phr:
2287 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2288 case Intrinsic::mips_maq_sa_w_phl:
2289 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2290 case Intrinsic::mips_maq_sa_w_phr:
2291 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2292 case Intrinsic::mips_dpaq_s_w_ph:
2293 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2294 case Intrinsic::mips_dpsq_s_w_ph:
2295 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2296 case Intrinsic::mips_dpaq_sa_l_w:
2297 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2298 case Intrinsic::mips_dpsq_sa_l_w:
2299 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2300 case Intrinsic::mips_dpaqx_s_w_ph:
2301 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2302 case Intrinsic::mips_dpaqx_sa_w_ph:
2303 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2304 case Intrinsic::mips_dpsqx_s_w_ph:
2305 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2306 case Intrinsic::mips_dpsqx_sa_w_ph:
2307 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002308 case Intrinsic::mips_ld_b:
2309 case Intrinsic::mips_ld_h:
2310 case Intrinsic::mips_ld_w:
2311 case Intrinsic::mips_ld_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002312 return lowerMSALoadIntr(Op, DAG, Intr, Subtarget);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002313 }
2314}
2315
Simon Dardis548a53f2017-01-10 16:40:57 +00002316static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2317 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002318 SDLoc DL(Op);
2319 SDValue ChainIn = Op->getOperand(0);
2320 SDValue Value = Op->getOperand(2);
2321 SDValue Address = Op->getOperand(3);
2322 SDValue Offset = Op->getOperand(4);
2323 EVT PtrTy = Address->getValueType(0);
2324
Simon Dardis548a53f2017-01-10 16:40:57 +00002325 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2326 // however takes an i32 signed constant offset. The actual type of the
2327 // intrinsic is a scaled signed i10.
2328 if (Subtarget.isABI_N64())
2329 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2330
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002331 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2332
Justin Lebar9c375812016-07-15 18:27:10 +00002333 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(),
2334 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002335}
2336
2337SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2338 SelectionDAG &DAG) const {
2339 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2340 switch (Intr) {
2341 default:
2342 return SDValue();
2343 case Intrinsic::mips_st_b:
2344 case Intrinsic::mips_st_h:
2345 case Intrinsic::mips_st_w:
2346 case Intrinsic::mips_st_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002347 return lowerMSAStoreIntr(Op, DAG, Intr, Subtarget);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002348 }
2349}
2350
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002351/// Check if the given BuildVectorSDNode is a splat.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002352/// This method currently relies on DAG nodes being reused when equivalent,
2353/// so it's possible for this to return false even when isConstantSplat returns
2354/// true.
2355static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002356 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002357 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002358
2359 SDValue Operand0 = N->getOperand(0);
2360
2361 for (unsigned int i = 1; i < nOps; ++i) {
2362 if (N->getOperand(i) != Operand0)
2363 return false;
2364 }
2365
2366 return true;
2367}
2368
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002369// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2370//
2371// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2372// choose to sign-extend but we could have equally chosen zero-extend. The
2373// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2374// result into this node later (possibly changing it to a zero-extend in the
2375// process).
2376SDValue MipsSETargetLowering::
2377lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2378 SDLoc DL(Op);
2379 EVT ResTy = Op->getValueType(0);
2380 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002381 EVT VecTy = Op0->getValueType(0);
2382
2383 if (!VecTy.is128BitVector())
2384 return SDValue();
2385
2386 if (ResTy.isInteger()) {
2387 SDValue Op1 = Op->getOperand(1);
2388 EVT EltTy = VecTy.getVectorElementType();
2389 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2390 DAG.getValueType(EltTy));
2391 }
2392
2393 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002394}
2395
Daniel Sandersf49dd822013-09-24 13:33:07 +00002396static bool isConstantOrUndef(const SDValue Op) {
Sanjay Patel57195842016-03-14 17:28:46 +00002397 if (Op->isUndef())
Daniel Sandersf49dd822013-09-24 13:33:07 +00002398 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002399 if (isa<ConstantSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002400 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002401 if (isa<ConstantFPSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002402 return true;
2403 return false;
2404}
2405
2406static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2407 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2408 if (isConstantOrUndef(Op->getOperand(i)))
2409 return true;
2410 return false;
2411}
2412
Daniel Sanders7a289d02013-09-23 12:02:46 +00002413// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2414// backend.
2415//
2416// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002417// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2418// 2 less than or equal to 64 and the value fits into a signed 10-bit
2419// immediate
2420// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2421// is a power of 2 less than or equal to 64 and the value does not fit into a
2422// signed 10-bit immediate
2423// - Non-constant splats are legal as-is.
2424// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2425// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002426SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2427 SelectionDAG &DAG) const {
2428 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2429 EVT ResTy = Op->getValueType(0);
2430 SDLoc DL(Op);
2431 APInt SplatValue, SplatUndef;
2432 unsigned SplatBitSize;
2433 bool HasAnyUndefs;
2434
Eric Christopher1c29a652014-07-18 22:55:25 +00002435 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002436 return SDValue();
2437
2438 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2439 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002440 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002441 // We can only cope with 8, 16, 32, or 64-bit elements
2442 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2443 SplatBitSize != 64)
2444 return SDValue();
2445
Simon Dardis7090d142017-03-10 13:27:14 +00002446 // If the value isn't an integer type we will have to bitcast
2447 // from an integer type first. Also, if there are any undefs, we must
2448 // lower them to defined values first.
2449 if (ResTy.isInteger() && !HasAnyUndefs)
Daniel Sandersf49dd822013-09-24 13:33:07 +00002450 return Op;
2451
2452 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002453
2454 switch (SplatBitSize) {
2455 default:
2456 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002457 case 8:
2458 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002459 break;
2460 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002461 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002462 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002463 case 32:
2464 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002465 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002466 case 64:
2467 // There's no fill.d to fall back on for 64-bit values
2468 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002469 }
2470
Daniel Sanders50b80412013-11-15 12:56:49 +00002471 // SelectionDAG::getConstant will promote SplatValue appropriately.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002472 SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002473
Daniel Sanders50b80412013-11-15 12:56:49 +00002474 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002475 if (ViaVecTy != ResTy)
2476 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002477
2478 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002479 } else if (isSplatVector(Node))
2480 return Op;
2481 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002482 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2483 // The resulting code is the same length as the expansion, but it doesn't
2484 // use memory operations
2485 EVT ResTy = Node->getValueType(0);
2486
2487 assert(ResTy.isVector());
2488
2489 unsigned NumElts = ResTy.getVectorNumElements();
2490 SDValue Vector = DAG.getUNDEF(ResTy);
2491 for (unsigned i = 0; i < NumElts; ++i) {
2492 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2493 Node->getOperand(i),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002494 DAG.getConstant(i, DL, MVT::i32));
Daniel Sandersf86622b2013-09-24 13:16:15 +00002495 }
2496 return Vector;
2497 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002498
2499 return SDValue();
2500}
2501
Daniel Sanders26307182013-09-24 14:20:00 +00002502// Lower VECTOR_SHUFFLE into SHF (if possible).
2503//
2504// SHF splits the vector into blocks of four elements, then shuffles these
2505// elements according to a <4 x i2> constant (encoded as an integer immediate).
2506//
2507// It is therefore possible to lower into SHF when the mask takes the form:
2508// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2509// When undef's appear they are treated as if they were whatever value is
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002510// necessary in order to fit the above forms.
Daniel Sanders26307182013-09-24 14:20:00 +00002511//
2512// For example:
2513// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2514// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2515// i32 7, i32 6, i32 5, i32 4>
2516// is lowered to:
2517// (SHF_H $w0, $w1, 27)
2518// where the 27 comes from:
2519// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2520static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2521 SmallVector<int, 16> Indices,
2522 SelectionDAG &DAG) {
2523 int SHFIndices[4] = { -1, -1, -1, -1 };
2524
2525 if (Indices.size() < 4)
2526 return SDValue();
2527
2528 for (unsigned i = 0; i < 4; ++i) {
2529 for (unsigned j = i; j < Indices.size(); j += 4) {
2530 int Idx = Indices[j];
2531
2532 // Convert from vector index to 4-element subvector index
2533 // If an index refers to an element outside of the subvector then give up
2534 if (Idx != -1) {
2535 Idx -= 4 * (j / 4);
2536 if (Idx < 0 || Idx >= 4)
2537 return SDValue();
2538 }
2539
2540 // If the mask has an undef, replace it with the current index.
2541 // Note that it might still be undef if the current index is also undef
2542 if (SHFIndices[i] == -1)
2543 SHFIndices[i] = Idx;
2544
2545 // Check that non-undef values are the same as in the mask. If they
2546 // aren't then give up
2547 if (!(Idx == -1 || Idx == SHFIndices[i]))
2548 return SDValue();
2549 }
2550 }
2551
2552 // Calculate the immediate. Replace any remaining undefs with zero
2553 APInt Imm(32, 0);
2554 for (int i = 3; i >= 0; --i) {
2555 int Idx = SHFIndices[i];
2556
2557 if (Idx == -1)
2558 Idx = 0;
2559
2560 Imm <<= 2;
2561 Imm |= Idx & 0x3;
2562 }
2563
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002564 SDLoc DL(Op);
2565 return DAG.getNode(MipsISD::SHF, DL, ResTy,
2566 DAG.getConstant(Imm, DL, MVT::i32), Op->getOperand(0));
Daniel Sanders26307182013-09-24 14:20:00 +00002567}
2568
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002569/// Determine whether a range fits a regular pattern of values.
2570/// This function accounts for the possibility of jumping over the End iterator.
2571template <typename ValType>
2572static bool
2573fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,
2574 unsigned CheckStride,
2575 typename SmallVectorImpl<ValType>::const_iterator End,
2576 ValType ExpectedIndex, unsigned ExpectedIndexStride) {
2577 auto &I = Begin;
2578
2579 while (I != End) {
2580 if (*I != -1 && *I != ExpectedIndex)
2581 return false;
2582 ExpectedIndex += ExpectedIndexStride;
2583
2584 // Incrementing past End is undefined behaviour so we must increment one
2585 // step at a time and check for End at each step.
2586 for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
2587 ; // Empty loop body.
2588 }
2589 return true;
2590}
2591
2592// Determine whether VECTOR_SHUFFLE is a SPLATI.
2593//
2594// It is a SPLATI when the mask is:
2595// <x, x, x, ...>
2596// where x is any valid index.
2597//
2598// When undef's appear in the mask they are treated as if they were whatever
2599// value is necessary in order to fit the above form.
2600static bool isVECTOR_SHUFFLE_SPLATI(SDValue Op, EVT ResTy,
2601 SmallVector<int, 16> Indices,
2602 SelectionDAG &DAG) {
2603 assert((Indices.size() % 2) == 0);
2604
2605 int SplatIndex = -1;
2606 for (const auto &V : Indices) {
2607 if (V != -1) {
2608 SplatIndex = V;
2609 break;
2610 }
2611 }
2612
2613 return fitsRegularPattern<int>(Indices.begin(), 1, Indices.end(), SplatIndex,
2614 0);
2615}
2616
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002617// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2618//
2619// ILVEV interleaves the even elements from each vector.
2620//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002621// It is possible to lower into ILVEV when the mask consists of two of the
2622// following forms interleaved:
2623// <0, 2, 4, ...>
2624// <n, n+2, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002625// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002626// For example:
2627// <0, 0, 2, 2, 4, 4, ...>
2628// <0, n, 2, n+2, 4, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002629//
2630// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002631// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002632static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2633 SmallVector<int, 16> Indices,
2634 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002635 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002636
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002637 SDValue Wt;
2638 SDValue Ws;
2639 const auto &Begin = Indices.begin();
2640 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002641
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002642 // Check even elements are taken from the even elements of one half or the
2643 // other and pick an operand accordingly.
2644 if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
2645 Wt = Op->getOperand(0);
2646 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 2))
2647 Wt = Op->getOperand(1);
2648 else
2649 return SDValue();
2650
2651 // Check odd elements are taken from the even elements of one half or the
2652 // other and pick an operand accordingly.
2653 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
2654 Ws = Op->getOperand(0);
2655 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 2))
2656 Ws = Op->getOperand(1);
2657 else
2658 return SDValue();
2659
2660 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002661}
2662
2663// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2664//
2665// ILVOD interleaves the odd elements from each vector.
2666//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002667// It is possible to lower into ILVOD when the mask consists of two of the
2668// following forms interleaved:
2669// <1, 3, 5, ...>
2670// <n+1, n+3, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002671// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002672// For example:
2673// <1, 1, 3, 3, 5, 5, ...>
2674// <1, n+1, 3, n+3, 5, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002675//
2676// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002677// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002678static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2679 SmallVector<int, 16> Indices,
2680 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002681 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002682
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002683 SDValue Wt;
2684 SDValue Ws;
2685 const auto &Begin = Indices.begin();
2686 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002687
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002688 // Check even elements are taken from the odd elements of one half or the
2689 // other and pick an operand accordingly.
2690 if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
2691 Wt = Op->getOperand(0);
2692 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + 1, 2))
2693 Wt = Op->getOperand(1);
2694 else
2695 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002696
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002697 // Check odd elements are taken from the odd elements of one half or the
2698 // other and pick an operand accordingly.
2699 if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
2700 Ws = Op->getOperand(0);
2701 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + 1, 2))
2702 Ws = Op->getOperand(1);
2703 else
2704 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002705
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002706 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Wt, Ws);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002707}
2708
2709// Lower VECTOR_SHUFFLE into ILVR (if possible).
2710//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002711// ILVR interleaves consecutive elements from the right (lowest-indexed) half of
2712// each vector.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002713//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002714// It is possible to lower into ILVR when the mask consists of two of the
2715// following forms interleaved:
2716// <0, 1, 2, ...>
2717// <n, n+1, n+2, ...>
2718// where n is the number of elements in the vector.
2719// For example:
2720// <0, 0, 1, 1, 2, 2, ...>
2721// <0, n, 1, n+1, 2, n+2, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002722//
2723// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002724// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002725static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2726 SmallVector<int, 16> Indices,
2727 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002728 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002729
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002730 SDValue Wt;
2731 SDValue Ws;
2732 const auto &Begin = Indices.begin();
2733 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002734
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002735 // Check even elements are taken from the right (lowest-indexed) elements of
2736 // one half or the other and pick an operand accordingly.
2737 if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
2738 Wt = Op->getOperand(0);
2739 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 1))
2740 Wt = Op->getOperand(1);
2741 else
2742 return SDValue();
2743
2744 // Check odd elements are taken from the right (lowest-indexed) elements of
2745 // one half or the other and pick an operand accordingly.
2746 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
2747 Ws = Op->getOperand(0);
2748 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 1))
2749 Ws = Op->getOperand(1);
2750 else
2751 return SDValue();
2752
2753 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Ws, Wt);
2754}
2755
2756// Lower VECTOR_SHUFFLE into ILVL (if possible).
2757//
2758// ILVL interleaves consecutive elements from the left (highest-indexed) half
2759// of each vector.
2760//
2761// It is possible to lower into ILVL when the mask consists of two of the
2762// following forms interleaved:
2763// <x, x+1, x+2, ...>
2764// <n+x, n+x+1, n+x+2, ...>
2765// where n is the number of elements in the vector and x is half n.
2766// For example:
2767// <x, x, x+1, x+1, x+2, x+2, ...>
2768// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2769//
2770// When undef's appear in the mask they are treated as if they were whatever
2771// value is necessary in order to fit the above forms.
2772static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2773 SmallVector<int, 16> Indices,
2774 SelectionDAG &DAG) {
2775 assert((Indices.size() % 2) == 0);
2776
2777 unsigned HalfSize = Indices.size() / 2;
2778 SDValue Wt;
2779 SDValue Ws;
2780 const auto &Begin = Indices.begin();
2781 const auto &End = Indices.end();
2782
2783 // Check even elements are taken from the left (highest-indexed) elements of
2784 // one half or the other and pick an operand accordingly.
2785 if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
2786 Wt = Op->getOperand(0);
2787 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + HalfSize, 1))
2788 Wt = Op->getOperand(1);
2789 else
2790 return SDValue();
2791
2792 // Check odd elements are taken from the left (highest-indexed) elements of
2793 // one half or the other and pick an operand accordingly.
2794 if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
2795 Ws = Op->getOperand(0);
2796 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + HalfSize,
2797 1))
2798 Ws = Op->getOperand(1);
2799 else
2800 return SDValue();
2801
2802 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002803}
2804
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002805// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2806//
2807// PCKEV copies the even elements of each vector into the result vector.
2808//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002809// It is possible to lower into PCKEV when the mask consists of two of the
2810// following forms concatenated:
2811// <0, 2, 4, ...>
2812// <n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002813// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002814// For example:
2815// <0, 2, 4, ..., 0, 2, 4, ...>
2816// <0, 2, 4, ..., n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002817//
2818// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002819// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002820static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2821 SmallVector<int, 16> Indices,
2822 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002823 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002824
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002825 SDValue Wt;
2826 SDValue Ws;
2827 const auto &Begin = Indices.begin();
2828 const auto &Mid = Indices.begin() + Indices.size() / 2;
2829 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002830
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002831 if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
2832 Wt = Op->getOperand(0);
2833 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size(), 2))
2834 Wt = Op->getOperand(1);
2835 else
2836 return SDValue();
2837
2838 if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
2839 Ws = Op->getOperand(0);
2840 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size(), 2))
2841 Ws = Op->getOperand(1);
2842 else
2843 return SDValue();
2844
2845 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002846}
2847
2848// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2849//
2850// PCKOD copies the odd elements of each vector into the result vector.
2851//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002852// It is possible to lower into PCKOD when the mask consists of two of the
2853// following forms concatenated:
2854// <1, 3, 5, ...>
2855// <n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002856// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002857// For example:
2858// <1, 3, 5, ..., 1, 3, 5, ...>
2859// <1, 3, 5, ..., n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002860//
2861// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002862// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002863static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2864 SmallVector<int, 16> Indices,
2865 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002866 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002867
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002868 SDValue Wt;
2869 SDValue Ws;
2870 const auto &Begin = Indices.begin();
2871 const auto &Mid = Indices.begin() + Indices.size() / 2;
2872 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002873
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002874 if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
2875 Wt = Op->getOperand(0);
2876 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size() + 1, 2))
2877 Wt = Op->getOperand(1);
2878 else
2879 return SDValue();
2880
2881 if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
2882 Ws = Op->getOperand(0);
2883 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size() + 1, 2))
2884 Ws = Op->getOperand(1);
2885 else
2886 return SDValue();
2887
2888 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002889}
2890
Daniel Sanderse5087042013-09-24 14:02:15 +00002891// Lower VECTOR_SHUFFLE into VSHF.
2892//
2893// This mostly consists of converting the shuffle indices in Indices into a
2894// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2895// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2896// if the type is v8i16 and all the indices are less than 8 then the second
2897// operand is unused and can be replaced with anything. We choose to replace it
2898// with the used operand since this reduces the number of instructions overall.
2899static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2900 SmallVector<int, 16> Indices,
2901 SelectionDAG &DAG) {
2902 SmallVector<SDValue, 16> Ops;
2903 SDValue Op0;
2904 SDValue Op1;
2905 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2906 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2907 bool Using1stVec = false;
2908 bool Using2ndVec = false;
2909 SDLoc DL(Op);
2910 int ResTyNumElts = ResTy.getVectorNumElements();
2911
2912 for (int i = 0; i < ResTyNumElts; ++i) {
2913 // Idx == -1 means UNDEF
2914 int Idx = Indices[i];
2915
2916 if (0 <= Idx && Idx < ResTyNumElts)
2917 Using1stVec = true;
2918 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2919 Using2ndVec = true;
2920 }
2921
2922 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2923 ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002924 Ops.push_back(DAG.getTargetConstant(*I, DL, MaskEltTy));
Daniel Sanderse5087042013-09-24 14:02:15 +00002925
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002926 SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002927
2928 if (Using1stVec && Using2ndVec) {
2929 Op0 = Op->getOperand(0);
2930 Op1 = Op->getOperand(1);
2931 } else if (Using1stVec)
2932 Op0 = Op1 = Op->getOperand(0);
2933 else if (Using2ndVec)
2934 Op0 = Op1 = Op->getOperand(1);
2935 else
2936 llvm_unreachable("shuffle vector mask references neither vector operand?");
2937
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002938 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2939 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2940 // VSHF concatenates the vectors in a bitwise fashion:
2941 // <0b00, 0b01> + <0b10, 0b11> ->
2942 // 0b0100 + 0b1110 -> 0b01001110
2943 // <0b10, 0b11, 0b00, 0b01>
2944 // We must therefore swap the operands to get the correct result.
2945 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002946}
2947
2948// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2949// indices in the shuffle.
2950SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2951 SelectionDAG &DAG) const {
2952 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2953 EVT ResTy = Op->getValueType(0);
2954
2955 if (!ResTy.is128BitVector())
2956 return SDValue();
2957
2958 int ResTyNumElts = ResTy.getVectorNumElements();
2959 SmallVector<int, 16> Indices;
2960
2961 for (int i = 0; i < ResTyNumElts; ++i)
2962 Indices.push_back(Node->getMaskElt(i));
2963
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002964 // splati.[bhwd] is preferable to the others but is matched from
2965 // MipsISD::VSHF.
2966 if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
2967 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002968 SDValue Result;
2969 if ((Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002970 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002971 if ((Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002972 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002973 if ((Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002974 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002975 if ((Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002976 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002977 if ((Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002978 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002979 if ((Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002980 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002981 if ((Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG)))
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002982 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002983 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2984}
2985
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00002986MachineBasicBlock *
2987MipsSETargetLowering::emitBPOSGE32(MachineInstr &MI,
2988 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002989 // $bb:
2990 // bposge32_pseudo $vr0
2991 // =>
2992 // $bb:
2993 // bposge32 $tbb
2994 // $fbb:
2995 // li $vr2, 0
2996 // b $sink
2997 // $tbb:
2998 // li $vr1, 1
2999 // $sink:
3000 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
3001
3002 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003003 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003004 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003005 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003006 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003007 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003008 MachineFunction *F = BB->getParent();
3009 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3010 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3011 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3012 F->insert(It, FBB);
3013 F->insert(It, TBB);
3014 F->insert(It, Sink);
3015
3016 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003017 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003018 BB->end());
3019 Sink->transferSuccessorsAndUpdatePHIs(BB);
3020
3021 // Add successors.
3022 BB->addSuccessor(FBB);
3023 BB->addSuccessor(TBB);
3024 FBB->addSuccessor(Sink);
3025 TBB->addSuccessor(Sink);
3026
3027 // Insert the real bposge32 instruction to $BB.
3028 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +00003029 // Insert the real bposge32c instruction to $BB.
3030 BuildMI(BB, DL, TII->get(Mips::BPOSGE32C_MMR3)).addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003031
3032 // Fill $FBB.
3033 unsigned VR2 = RegInfo.createVirtualRegister(RC);
3034 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
3035 .addReg(Mips::ZERO).addImm(0);
3036 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3037
3038 // Fill $TBB.
3039 unsigned VR1 = RegInfo.createVirtualRegister(RC);
3040 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
3041 .addReg(Mips::ZERO).addImm(1);
3042
3043 // Insert phi function to $Sink.
3044 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003045 MI.getOperand(0).getReg())
3046 .addReg(VR2)
3047 .addMBB(FBB)
3048 .addReg(VR1)
3049 .addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003050
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003051 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003052 return Sink;
3053}
Daniel Sandersce09d072013-08-28 12:14:50 +00003054
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003055MachineBasicBlock *MipsSETargetLowering::emitMSACBranchPseudo(
3056 MachineInstr &MI, MachineBasicBlock *BB, unsigned BranchOp) const {
Daniel Sandersce09d072013-08-28 12:14:50 +00003057 // $bb:
3058 // vany_nonzero $rd, $ws
3059 // =>
3060 // $bb:
3061 // bnz.b $ws, $tbb
3062 // b $fbb
3063 // $fbb:
3064 // li $rd1, 0
3065 // b $sink
3066 // $tbb:
3067 // li $rd2, 1
3068 // $sink:
3069 // $rd = phi($rd1, $fbb, $rd2, $tbb)
3070
3071 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003072 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersce09d072013-08-28 12:14:50 +00003073 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003074 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersce09d072013-08-28 12:14:50 +00003075 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003076 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00003077 MachineFunction *F = BB->getParent();
3078 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3079 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3080 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3081 F->insert(It, FBB);
3082 F->insert(It, TBB);
3083 F->insert(It, Sink);
3084
3085 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003086 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00003087 BB->end());
3088 Sink->transferSuccessorsAndUpdatePHIs(BB);
3089
3090 // Add successors.
3091 BB->addSuccessor(FBB);
3092 BB->addSuccessor(TBB);
3093 FBB->addSuccessor(Sink);
3094 TBB->addSuccessor(Sink);
3095
3096 // Insert the real bnz.b instruction to $BB.
3097 BuildMI(BB, DL, TII->get(BranchOp))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003098 .addReg(MI.getOperand(1).getReg())
3099 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003100
3101 // Fill $FBB.
3102 unsigned RD1 = RegInfo.createVirtualRegister(RC);
3103 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
3104 .addReg(Mips::ZERO).addImm(0);
3105 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3106
3107 // Fill $TBB.
3108 unsigned RD2 = RegInfo.createVirtualRegister(RC);
3109 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
3110 .addReg(Mips::ZERO).addImm(1);
3111
3112 // Insert phi function to $Sink.
3113 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003114 MI.getOperand(0).getReg())
3115 .addReg(RD1)
3116 .addMBB(FBB)
3117 .addReg(RD2)
3118 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003119
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003120 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersce09d072013-08-28 12:14:50 +00003121 return Sink;
3122}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003123
3124// Emit the COPY_FW pseudo instruction.
3125//
3126// copy_fw_pseudo $fd, $ws, n
3127// =>
3128// copy_u_w $rt, $ws, $n
3129// mtc1 $rt, $fd
3130//
3131// When n is zero, the equivalent operation can be performed with (potentially)
3132// zero instructions due to register overlaps. This optimization is never valid
3133// 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 +00003134MachineBasicBlock *
3135MipsSETargetLowering::emitCOPY_FW(MachineInstr &MI,
3136 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003137 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003138 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003139 DebugLoc DL = MI.getDebugLoc();
3140 unsigned Fd = MI.getOperand(0).getReg();
3141 unsigned Ws = MI.getOperand(1).getReg();
3142 unsigned Lane = MI.getOperand(2).getImm();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003143
Daniel Sandersafe27c72015-02-23 17:22:16 +00003144 if (Lane == 0) {
3145 unsigned Wt = Ws;
3146 if (!Subtarget.useOddSPReg()) {
3147 // We must copy to an even-numbered MSA register so that the
3148 // single-precision sub-register is also guaranteed to be even-numbered.
3149 Wt = RegInfo.createVirtualRegister(&Mips::MSA128WEvensRegClass);
3150
3151 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Wt).addReg(Ws);
3152 }
3153
3154 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3155 } else {
3156 unsigned Wt = RegInfo.createVirtualRegister(
3157 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3158 &Mips::MSA128WEvensRegClass);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003159
Daniel Sandersd9207702014-03-04 13:54:30 +00003160 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003161 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3162 }
3163
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003164 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003165 return BB;
3166}
3167
3168// Emit the COPY_FD pseudo instruction.
3169//
3170// copy_fd_pseudo $fd, $ws, n
3171// =>
3172// splati.d $wt, $ws, $n
3173// copy $fd, $wt:sub_64
3174//
3175// When n is zero, the equivalent operation can be performed with (potentially)
3176// zero instructions due to register overlaps. This optimization is always
3177// valid because FR=1 mode which is the only supported mode in MSA.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003178MachineBasicBlock *
3179MipsSETargetLowering::emitCOPY_FD(MachineInstr &MI,
3180 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003181 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003182
Eric Christopher96e72c62015-01-29 23:27:36 +00003183 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003184 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003185 unsigned Fd = MI.getOperand(0).getReg();
3186 unsigned Ws = MI.getOperand(1).getReg();
3187 unsigned Lane = MI.getOperand(2).getImm() * 2;
3188 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003189
3190 if (Lane == 0)
3191 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
3192 else {
3193 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3194
3195 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
3196 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
3197 }
3198
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003199 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003200 return BB;
3201}
Daniel Sandersa5150702013-09-27 12:31:32 +00003202
3203// Emit the INSERT_FW pseudo instruction.
3204//
3205// insert_fw_pseudo $wd, $wd_in, $n, $fs
3206// =>
3207// subreg_to_reg $wt:sub_lo, $fs
3208// insve_w $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_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003211 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003212 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003213 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003214 DebugLoc DL = MI.getDebugLoc();
3215 unsigned Wd = MI.getOperand(0).getReg();
3216 unsigned Wd_in = MI.getOperand(1).getReg();
3217 unsigned Lane = MI.getOperand(2).getImm();
3218 unsigned Fs = MI.getOperand(3).getReg();
Daniel Sandersafe27c72015-02-23 17:22:16 +00003219 unsigned Wt = RegInfo.createVirtualRegister(
3220 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3221 &Mips::MSA128WEvensRegClass);
Daniel Sandersa5150702013-09-27 12:31:32 +00003222
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_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00003227 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), 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 Sandersa5150702013-09-27 12:31:32 +00003232
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003233 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003234 return BB;
3235}
3236
3237// Emit the INSERT_FD pseudo instruction.
3238//
3239// insert_fd_pseudo $wd, $fs, n
3240// =>
3241// subreg_to_reg $wt:sub_64, $fs
3242// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003243MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003244MipsSETargetLowering::emitINSERT_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003245 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003246 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00003247
Eric Christopher96e72c62015-01-29 23:27:36 +00003248 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003249 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003250 DebugLoc DL = MI.getDebugLoc();
3251 unsigned Wd = MI.getOperand(0).getReg();
3252 unsigned Wd_in = MI.getOperand(1).getReg();
3253 unsigned Lane = MI.getOperand(2).getImm();
3254 unsigned Fs = MI.getOperand(3).getReg();
Daniel Sandersa5150702013-09-27 12:31:32 +00003255 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3256
3257 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003258 .addImm(0)
3259 .addReg(Fs)
3260 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00003261 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003262 .addReg(Wd_in)
3263 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003264 .addReg(Wt)
3265 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003266
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003267 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003268 return BB;
3269}
3270
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003271// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3272//
3273// For integer:
3274// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3275// =>
3276// (SLL $lanetmp1, $lane, <log2size)
3277// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3278// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3279// (NEG $lanetmp2, $lanetmp1)
3280// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3281//
3282// For floating point:
3283// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3284// =>
3285// (SUBREG_TO_REG $wt, $fs, <subreg>)
3286// (SLL $lanetmp1, $lane, <log2size)
3287// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3288// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3289// (NEG $lanetmp2, $lanetmp1)
3290// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003291MachineBasicBlock *MipsSETargetLowering::emitINSERT_DF_VIDX(
3292 MachineInstr &MI, MachineBasicBlock *BB, unsigned EltSizeInBytes,
3293 bool IsFP) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003294 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003295 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003296 DebugLoc DL = MI.getDebugLoc();
3297 unsigned Wd = MI.getOperand(0).getReg();
3298 unsigned SrcVecReg = MI.getOperand(1).getReg();
3299 unsigned LaneReg = MI.getOperand(2).getReg();
3300 unsigned SrcValReg = MI.getOperand(3).getReg();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003301
3302 const TargetRegisterClass *VecRC = nullptr;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003303 // FIXME: This should be true for N32 too.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003304 const TargetRegisterClass *GPRRC =
Daniel Sanders4160c802015-05-05 08:48:35 +00003305 Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003306 unsigned SubRegIdx = Subtarget.isABI_N64() ? Mips::sub_32 : 0;
3307 unsigned ShiftOp = Subtarget.isABI_N64() ? Mips::DSLL : Mips::SLL;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003308 unsigned EltLog2Size;
3309 unsigned InsertOp = 0;
3310 unsigned InsveOp = 0;
3311 switch (EltSizeInBytes) {
3312 default:
3313 llvm_unreachable("Unexpected size");
3314 case 1:
3315 EltLog2Size = 0;
3316 InsertOp = Mips::INSERT_B;
3317 InsveOp = Mips::INSVE_B;
3318 VecRC = &Mips::MSA128BRegClass;
3319 break;
3320 case 2:
3321 EltLog2Size = 1;
3322 InsertOp = Mips::INSERT_H;
3323 InsveOp = Mips::INSVE_H;
3324 VecRC = &Mips::MSA128HRegClass;
3325 break;
3326 case 4:
3327 EltLog2Size = 2;
3328 InsertOp = Mips::INSERT_W;
3329 InsveOp = Mips::INSVE_W;
3330 VecRC = &Mips::MSA128WRegClass;
3331 break;
3332 case 8:
3333 EltLog2Size = 3;
3334 InsertOp = Mips::INSERT_D;
3335 InsveOp = Mips::INSVE_D;
3336 VecRC = &Mips::MSA128DRegClass;
3337 break;
3338 }
3339
3340 if (IsFP) {
3341 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3342 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3343 .addImm(0)
3344 .addReg(SrcValReg)
3345 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3346 SrcValReg = Wt;
3347 }
3348
3349 // Convert the lane index into a byte index
3350 if (EltSizeInBytes != 1) {
3351 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003352 BuildMI(*BB, MI, DL, TII->get(ShiftOp), LaneTmp1)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003353 .addReg(LaneReg)
3354 .addImm(EltLog2Size);
3355 LaneReg = LaneTmp1;
3356 }
3357
3358 // Rotate bytes around so that the desired lane is element zero
3359 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3360 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3361 .addReg(SrcVecReg)
3362 .addReg(SrcVecReg)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003363 .addReg(LaneReg, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003364
3365 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3366 if (IsFP) {
3367 // Use insve.df to insert to element zero
3368 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3369 .addReg(WdTmp1)
3370 .addImm(0)
3371 .addReg(SrcValReg)
3372 .addImm(0);
3373 } else {
3374 // Use insert.df to insert to element zero
3375 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3376 .addReg(WdTmp1)
3377 .addReg(SrcValReg)
3378 .addImm(0);
3379 }
3380
3381 // Rotate elements the rest of the way for a full rotation.
3382 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3383 // the lane index to do this.
3384 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sanders4160c802015-05-05 08:48:35 +00003385 BuildMI(*BB, MI, DL, TII->get(Subtarget.isABI_N64() ? Mips::DSUB : Mips::SUB),
3386 LaneTmp2)
3387 .addReg(Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003388 .addReg(LaneReg);
3389 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3390 .addReg(WdTmp2)
3391 .addReg(WdTmp2)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003392 .addReg(LaneTmp2, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003393
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003394 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003395 return BB;
3396}
3397
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003398// Emit the FILL_FW pseudo instruction.
3399//
3400// fill_fw_pseudo $wd, $fs
3401// =>
3402// implicit_def $wt1
3403// insert_subreg $wt2:subreg_lo, $wt1, $fs
3404// splati.w $wd, $wt2[0]
3405MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003406MipsSETargetLowering::emitFILL_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003407 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003408 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003409 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003410 DebugLoc DL = MI.getDebugLoc();
3411 unsigned Wd = MI.getOperand(0).getReg();
3412 unsigned Fs = MI.getOperand(1).getReg();
Simon Dardis0e9e2372017-01-10 15:53:10 +00003413 unsigned Wt1 = RegInfo.createVirtualRegister(
3414 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3415 : &Mips::MSA128WEvensRegClass);
3416 unsigned Wt2 = RegInfo.createVirtualRegister(
3417 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3418 : &Mips::MSA128WEvensRegClass);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003419
3420 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3421 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3422 .addReg(Wt1)
3423 .addReg(Fs)
3424 .addImm(Mips::sub_lo);
3425 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3426
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003427 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003428 return BB;
3429}
3430
3431// Emit the FILL_FD pseudo instruction.
3432//
3433// fill_fd_pseudo $wd, $fs
3434// =>
3435// implicit_def $wt1
3436// insert_subreg $wt2:subreg_64, $wt1, $fs
3437// splati.d $wd, $wt2[0]
3438MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003439MipsSETargetLowering::emitFILL_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003440 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003441 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003442
Eric Christopher96e72c62015-01-29 23:27:36 +00003443 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003444 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003445 DebugLoc DL = MI.getDebugLoc();
3446 unsigned Wd = MI.getOperand(0).getReg();
3447 unsigned Fs = MI.getOperand(1).getReg();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003448 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3449 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3450
3451 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3452 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3453 .addReg(Wt1)
3454 .addReg(Fs)
3455 .addImm(Mips::sub_64);
3456 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003457
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003458 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003459 return BB;
3460}
Daniel Sandersa9521602013-10-23 10:36:52 +00003461
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003462// Emit the ST_F16_PSEDUO instruction to store a f16 value from an MSA
3463// register.
3464//
3465// STF16 MSA128F16:$wd, mem_simm10:$addr
3466// =>
3467// copy_u.h $rtemp,$wd[0]
3468// sh $rtemp, $addr
3469//
3470// Safety: We can't use st.h & co as they would over write the memory after
3471// the destination. It would require half floats be allocated 16 bytes(!) of
3472// space.
3473MachineBasicBlock *
3474MipsSETargetLowering::emitST_F16_PSEUDO(MachineInstr &MI,
3475 MachineBasicBlock *BB) const {
3476
3477 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3478 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3479 DebugLoc DL = MI.getDebugLoc();
3480 unsigned Ws = MI.getOperand(0).getReg();
3481 unsigned Rt = MI.getOperand(1).getReg();
3482 const MachineMemOperand &MMO = **MI.memoperands_begin();
3483 unsigned Imm = MMO.getOffset();
3484
3485 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3486 // spill and reload can expand as a GPR64 operand. Examine the
3487 // operand in detail and default to ABI.
3488 const TargetRegisterClass *RC =
3489 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3490 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3491 : &Mips::GPR64RegClass);
3492 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003493 unsigned Rs = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003494
3495 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_U_H), Rs).addReg(Ws).addImm(0);
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003496 if(!UsingMips32) {
3497 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR64RegClass);
3498 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Tmp)
3499 .addImm(0)
3500 .addReg(Rs)
3501 .addImm(Mips::sub_32);
3502 Rs = Tmp;
3503 }
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003504 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::SH : Mips::SH64))
3505 .addReg(Rs)
3506 .addReg(Rt)
3507 .addImm(Imm)
3508 .addMemOperand(BB->getParent()->getMachineMemOperand(
3509 &MMO, MMO.getOffset(), MMO.getSize()));
3510
3511 MI.eraseFromParent();
3512 return BB;
3513}
3514
3515// Emit the LD_F16_PSEDUO instruction to load a f16 value into an MSA register.
3516//
3517// LD_F16 MSA128F16:$wd, mem_simm10:$addr
3518// =>
3519// lh $rtemp, $addr
3520// fill.h $wd, $rtemp
3521//
3522// Safety: We can't use ld.h & co as they over-read from the source.
3523// Additionally, if the address is not modulo 16, 2 cases can occur:
3524// a) Segmentation fault as the load instruction reads from a memory page
3525// memory it's not supposed to.
3526// b) The load crosses an implementation specific boundary, requiring OS
3527// intervention.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003528MachineBasicBlock *
3529MipsSETargetLowering::emitLD_F16_PSEUDO(MachineInstr &MI,
3530 MachineBasicBlock *BB) const {
3531
3532 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3533 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3534 DebugLoc DL = MI.getDebugLoc();
3535 unsigned Wd = MI.getOperand(0).getReg();
3536
3537 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3538 // spill and reload can expand as a GPR64 operand. Examine the
3539 // operand in detail and default to ABI.
3540 const TargetRegisterClass *RC =
3541 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3542 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3543 : &Mips::GPR64RegClass);
3544
3545 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
3546 unsigned Rt = RegInfo.createVirtualRegister(RC);
3547
3548 MachineInstrBuilder MIB =
3549 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::LH : Mips::LH64), Rt);
3550 for (unsigned i = 1; i < MI.getNumOperands(); i++)
Diana Picus116bbab2017-01-13 09:58:52 +00003551 MIB.add(MI.getOperand(i));
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003552
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003553 if(!UsingMips32) {
3554 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
3555 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Tmp).addReg(Rt, 0, Mips::sub_32);
3556 Rt = Tmp;
3557 }
3558
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003559 BuildMI(*BB, MI, DL, TII->get(Mips::FILL_H), Wd).addReg(Rt);
3560
3561 MI.eraseFromParent();
3562 return BB;
3563}
3564
3565// Emit the FPROUND_PSEUDO instruction.
3566//
3567// Round an FGR64Opnd, FGR32Opnd to an f16.
3568//
3569// Safety: Cycle the operand through the GPRs so the result always ends up
3570// the correct MSA register.
3571//
3572// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fs
3573// / FGR64Opnd:$Fs and MSA128F16:$Wd to the same physical register
3574// (which they can be, as the MSA registers are defined to alias the
3575// FPU's 64 bit and 32 bit registers) the result can be accessed using
3576// the correct register class. That requires operands be tie-able across
3577// register classes which have a sub/super register class relationship.
3578//
3579// For FPG32Opnd:
3580//
3581// FPROUND MSA128F16:$wd, FGR32Opnd:$fs
3582// =>
3583// mfc1 $rtemp, $fs
3584// fill.w $rtemp, $wtemp
3585// fexdo.w $wd, $wtemp, $wtemp
3586//
3587// For FPG64Opnd on mips32r2+:
3588//
3589// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3590// =>
3591// mfc1 $rtemp, $fs
3592// fill.w $rtemp, $wtemp
3593// mfhc1 $rtemp2, $fs
3594// insert.w $wtemp[1], $rtemp2
3595// insert.w $wtemp[3], $rtemp2
3596// fexdo.w $wtemp2, $wtemp, $wtemp
3597// fexdo.h $wd, $temp2, $temp2
3598//
3599// For FGR64Opnd on mips64r2+:
3600//
3601// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3602// =>
3603// dmfc1 $rtemp, $fs
3604// fill.d $rtemp, $wtemp
3605// fexdo.w $wtemp2, $wtemp, $wtemp
3606// fexdo.h $wd, $wtemp2, $wtemp2
3607//
3608// Safety note: As $wtemp is UNDEF, we may provoke a spurious exception if the
3609// undef bits are "just right" and the exception enable bits are
3610// set. By using fill.w to replicate $fs into all elements over
3611// insert.w for one element, we avoid that potiential case. If
3612// fexdo.[hw] causes an exception in, the exception is valid and it
3613// occurs for all elements.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003614MachineBasicBlock *
3615MipsSETargetLowering::emitFPROUND_PSEUDO(MachineInstr &MI,
3616 MachineBasicBlock *BB,
3617 bool IsFGR64) const {
3618
3619 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3620 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3621 // it.
3622 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3623
3624 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003625 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003626
3627 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3628 DebugLoc DL = MI.getDebugLoc();
3629 unsigned Wd = MI.getOperand(0).getReg();
3630 unsigned Fs = MI.getOperand(1).getReg();
3631
3632 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3633 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3634 const TargetRegisterClass *GPRRC =
3635 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003636 unsigned MFC1Opc = IsFGR64onMips64
3637 ? Mips::DMFC1
3638 : (IsFGR64onMips32 ? Mips::MFC1_D64 : Mips::MFC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003639 unsigned FILLOpc = IsFGR64onMips64 ? Mips::FILL_D : Mips::FILL_W;
3640
3641 // Perform the register class copy as mentioned above.
3642 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3643 BuildMI(*BB, MI, DL, TII->get(MFC1Opc), Rtemp).addReg(Fs);
3644 BuildMI(*BB, MI, DL, TII->get(FILLOpc), Wtemp).addReg(Rtemp);
3645 unsigned WPHI = Wtemp;
3646
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003647 if (IsFGR64onMips32) {
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003648 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3649 BuildMI(*BB, MI, DL, TII->get(Mips::MFHC1_D64), Rtemp2).addReg(Fs);
3650 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3651 unsigned Wtemp3 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3652 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp2)
3653 .addReg(Wtemp)
3654 .addReg(Rtemp2)
3655 .addImm(1);
3656 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp3)
3657 .addReg(Wtemp2)
3658 .addReg(Rtemp2)
3659 .addImm(3);
3660 WPHI = Wtemp3;
3661 }
3662
3663 if (IsFGR64) {
3664 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3665 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_W), Wtemp2)
3666 .addReg(WPHI)
3667 .addReg(WPHI);
3668 WPHI = Wtemp2;
3669 }
3670
3671 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_H), Wd).addReg(WPHI).addReg(WPHI);
3672
3673 MI.eraseFromParent();
3674 return BB;
3675}
3676
3677// Emit the FPEXTEND_PSEUDO instruction.
3678//
3679// Expand an f16 to either a FGR32Opnd or FGR64Opnd.
3680//
3681// Safety: Cycle the result through the GPRs so the result always ends up
3682// the correct floating point register.
3683//
3684// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fd
3685// / FGR64Opnd:$Fd and MSA128F16:$Ws to the same physical register
3686// (which they can be, as the MSA registers are defined to alias the
3687// FPU's 64 bit and 32 bit registers) the result can be accessed using
3688// the correct register class. That requires operands be tie-able across
3689// register classes which have a sub/super register class relationship. I
3690// haven't checked.
3691//
3692// For FGR32Opnd:
3693//
3694// FPEXTEND FGR32Opnd:$fd, MSA128F16:$ws
3695// =>
3696// fexupr.w $wtemp, $ws
3697// copy_s.w $rtemp, $ws[0]
3698// mtc1 $rtemp, $fd
3699//
3700// For FGR64Opnd on Mips64:
3701//
3702// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3703// =>
3704// fexupr.w $wtemp, $ws
3705// fexupr.d $wtemp2, $wtemp
3706// copy_s.d $rtemp, $wtemp2s[0]
3707// dmtc1 $rtemp, $fd
3708//
3709// For FGR64Opnd on Mips32:
3710//
3711// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3712// =>
3713// fexupr.w $wtemp, $ws
3714// fexupr.d $wtemp2, $wtemp
3715// copy_s.w $rtemp, $wtemp2[0]
3716// mtc1 $rtemp, $ftemp
3717// copy_s.w $rtemp2, $wtemp2[1]
3718// $fd = mthc1 $rtemp2, $ftemp
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003719MachineBasicBlock *
3720MipsSETargetLowering::emitFPEXTEND_PSEUDO(MachineInstr &MI,
3721 MachineBasicBlock *BB,
3722 bool IsFGR64) const {
3723
3724 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3725 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3726 // it.
3727 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3728
3729 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
3730 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
3731
3732 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3733 DebugLoc DL = MI.getDebugLoc();
3734 unsigned Fd = MI.getOperand(0).getReg();
3735 unsigned Ws = MI.getOperand(1).getReg();
3736
3737 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3738 const TargetRegisterClass *GPRRC =
3739 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003740 unsigned MTC1Opc = IsFGR64onMips64
3741 ? Mips::DMTC1
3742 : (IsFGR64onMips32 ? Mips::MTC1_D64 : Mips::MTC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003743 unsigned COPYOpc = IsFGR64onMips64 ? Mips::COPY_S_D : Mips::COPY_S_W;
3744
3745 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3746 unsigned WPHI = Wtemp;
3747
3748 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_W), Wtemp).addReg(Ws);
3749 if (IsFGR64) {
3750 WPHI = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3751 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_D), WPHI).addReg(Wtemp);
3752 }
3753
3754 // Perform the safety regclass copy mentioned above.
3755 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3756 unsigned FPRPHI = IsFGR64onMips32
3757 ? RegInfo.createVirtualRegister(&Mips::FGR64RegClass)
3758 : Fd;
3759 BuildMI(*BB, MI, DL, TII->get(COPYOpc), Rtemp).addReg(WPHI).addImm(0);
3760 BuildMI(*BB, MI, DL, TII->get(MTC1Opc), FPRPHI).addReg(Rtemp);
3761
3762 if (IsFGR64onMips32) {
3763 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3764 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_S_W), Rtemp2)
3765 .addReg(WPHI)
3766 .addImm(1);
3767 BuildMI(*BB, MI, DL, TII->get(Mips::MTHC1_D64), Fd)
3768 .addReg(FPRPHI)
3769 .addReg(Rtemp2);
3770 }
3771
3772 MI.eraseFromParent();
3773 return BB;
3774}
3775
Daniel Sandersa9521602013-10-23 10:36:52 +00003776// Emit the FEXP2_W_1 pseudo instructions.
3777//
3778// fexp2_w_1_pseudo $wd, $wt
3779// =>
3780// ldi.w $ws, 1
3781// fexp2.w $wd, $ws, $wt
3782MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003783MipsSETargetLowering::emitFEXP2_W_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003784 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003785 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003786 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3787 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3788 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3789 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003790 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003791
3792 // Splat 1.0 into a vector
3793 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3794 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3795
3796 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003797 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003798 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003799 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003800
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003801 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003802 return BB;
3803}
3804
3805// Emit the FEXP2_D_1 pseudo instructions.
3806//
3807// fexp2_d_1_pseudo $wd, $wt
3808// =>
3809// ldi.d $ws, 1
3810// fexp2.d $wd, $ws, $wt
3811MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003812MipsSETargetLowering::emitFEXP2_D_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003813 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003814 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003815 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3816 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3817 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3818 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003819 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003820
3821 // Splat 1.0 into a vector
3822 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3823 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3824
3825 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003826 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003827 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003828 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003829
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003830 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003831 return BB;
3832}