blob: f7d7e2af85e476555070b6045488a799d7022d3c [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001//===- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface -------------===//
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Subclass of MipsTargetLowering specialized for mips32/64.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenko79220eae2017-08-03 22:12:30 +000013
Akira Hatanaka96ca1822013-03-13 00:54:29 +000014#include "MipsSEISelLowering.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000015#include "MipsMachineFunction.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000016#include "MipsRegisterInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000017#include "MipsSubtarget.h"
Simon Dardis548a53f2017-01-10 16:40:57 +000018#include "llvm/ADT/APInt.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000019#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/Triple.h"
23#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/ISDOpcodes.h"
25#include "llvm/CodeGen/MachineBasicBlock.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstr.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000028#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000029#include "llvm/CodeGen/MachineMemOperand.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000031#include "llvm/CodeGen/MachineValueType.h"
32#include "llvm/CodeGen/SelectionDAG.h"
33#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000034#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000035#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000036#include "llvm/CodeGen/ValueTypes.h"
37#include "llvm/IR/DebugLoc.h"
Akira Hatanakaa6bbde52013-04-13 02:13:30 +000038#include "llvm/IR/Intrinsics.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000039#include "llvm/Support/Casting.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000040#include "llvm/Support/CommandLine.h"
Daniel Sanders62aeab82013-10-30 13:31:27 +000041#include "llvm/Support/Debug.h"
Simon Dardis548a53f2017-01-10 16:40:57 +000042#include "llvm/Support/ErrorHandling.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);
Daniel Sanders607952b2013-09-11 10:38:58 +0000334 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000335 setOperationAction(ISD::UREM, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000336 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000337 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000338 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000339
Daniel Sanders015972b2013-10-11 10:00:06 +0000340 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
341 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
342 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
343 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
344 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
345 }
346
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000347 setOperationAction(ISD::SETCC, Ty, Legal);
348 setCondCodeAction(ISD::SETNE, Ty, Expand);
349 setCondCodeAction(ISD::SETGE, Ty, Expand);
350 setCondCodeAction(ISD::SETGT, Ty, Expand);
351 setCondCodeAction(ISD::SETUGE, Ty, Expand);
352 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000353}
354
Daniel Sanders7a289d02013-09-23 12:02:46 +0000355// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000356void MipsSETargetLowering::
357addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000358 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000359
360 // Expand all builtin opcodes.
361 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
362 setOperationAction(Opc, Ty, Expand);
363
364 setOperationAction(ISD::LOAD, Ty, Legal);
365 setOperationAction(ISD::STORE, Ty, Legal);
366 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000367 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000368 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000369 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000370
371 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000372 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000373 setOperationAction(ISD::FADD, Ty, Legal);
374 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000375 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000376 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000377 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000378 setOperationAction(ISD::FMUL, Ty, Legal);
379 setOperationAction(ISD::FRINT, Ty, Legal);
380 setOperationAction(ISD::FSQRT, Ty, Legal);
381 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000382 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000383
384 setOperationAction(ISD::SETCC, Ty, Legal);
385 setCondCodeAction(ISD::SETOGE, Ty, Expand);
386 setCondCodeAction(ISD::SETOGT, Ty, Expand);
387 setCondCodeAction(ISD::SETUGE, Ty, Expand);
388 setCondCodeAction(ISD::SETUGT, Ty, Expand);
389 setCondCodeAction(ISD::SETGE, Ty, Expand);
390 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000391 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000392}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000393
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000394SDValue MipsSETargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000395 if(!Subtarget.hasMips32r6())
396 return MipsTargetLowering::LowerOperation(Op, DAG);
397
398 EVT ResTy = Op->getValueType(0);
399 SDLoc DL(Op);
400
401 // Although MTC1_D64 takes an i32 and writes an f64, the upper 32 bits of the
402 // floating point register are undefined. Not really an issue as sel.d, which
403 // is produced from an FSELECT node, only looks at bit 0.
404 SDValue Tmp = DAG.getNode(MipsISD::MTC1_D64, DL, MVT::f64, Op->getOperand(0));
405 return DAG.getNode(MipsISD::FSELECT, DL, ResTy, Tmp, Op->getOperand(1),
406 Op->getOperand(2));
407}
408
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000409bool
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000410MipsSETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
411 unsigned,
412 unsigned,
413 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000414 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
415
Eric Christopher1c29a652014-07-18 22:55:25 +0000416 if (Subtarget.systemSupportsUnalignedAccess()) {
Daniel Sandersac272632014-05-23 13:18:02 +0000417 // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
418 // implementation defined whether this is handled by hardware, software, or
419 // a hybrid of the two but it's expected that most implementations will
420 // handle the majority of cases in hardware.
421 if (Fast)
422 *Fast = true;
423 return true;
424 }
425
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000426 switch (SVT) {
427 case MVT::i64:
428 case MVT::i32:
429 if (Fast)
430 *Fast = true;
431 return true;
432 default:
433 return false;
434 }
435}
436
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000437SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
438 SelectionDAG &DAG) const {
439 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000440 case ISD::LOAD: return lowerLOAD(Op, DAG);
441 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000442 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
443 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
444 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
445 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
446 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
447 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000448 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
449 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000450 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
451 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000452 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000453 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000454 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000455 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000456 case ISD::SELECT: return lowerSELECT(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000457 }
458
459 return MipsTargetLowering::LowerOperation(Op, DAG);
460}
461
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000462// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
463//
464// Performs the following transformations:
465// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
466// sign/zero-extension is completely overwritten by the new one performed by
467// the ISD::AND.
468// - Removes redundant zero extensions performed by an ISD::AND.
469static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
470 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000471 const MipsSubtarget &Subtarget) {
472 if (!Subtarget.hasMSA())
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000473 return SDValue();
474
475 SDValue Op0 = N->getOperand(0);
476 SDValue Op1 = N->getOperand(1);
477 unsigned Op0Opcode = Op0->getOpcode();
478
479 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
480 // where $d + 1 == 2^n and n == 32
481 // or $d + 1 == 2^n and n <= 32 and ZExt
482 // -> (MipsVExtractZExt $a, $b, $c)
483 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
484 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
485 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
486
487 if (!Mask)
488 return SDValue();
489
490 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
491
492 if (Log2IfPositive <= 0)
493 return SDValue(); // Mask+1 is not a power of 2
494
495 SDValue Op0Op2 = Op0->getOperand(2);
496 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
497 unsigned ExtendTySize = ExtendTy.getSizeInBits();
498 unsigned Log2 = Log2IfPositive;
499
500 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
501 Log2 == ExtendTySize) {
502 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000503 return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
504 Op0->getVTList(),
505 makeArrayRef(Ops, Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000506 }
507 }
508
509 return SDValue();
510}
511
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000512// Determine if the specified node is a constant vector splat.
513//
514// Returns true and sets Imm if:
515// * N is a ISD::BUILD_VECTOR representing a constant splat
516//
517// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
518// differences are that it assumes the MSA has already been checked and the
519// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
520// must not be in order for binsri.d to be selectable).
521static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
522 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
523
Craig Topper062a2ba2014-04-25 05:30:21 +0000524 if (!Node)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000525 return false;
526
527 APInt SplatValue, SplatUndef;
528 unsigned SplatBitSize;
529 bool HasAnyUndefs;
530
531 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
532 8, !IsLittleEndian))
533 return false;
534
535 Imm = SplatValue;
536
537 return true;
538}
539
Daniel Sandersab94b532013-10-30 15:20:38 +0000540// Test whether the given node is an all-ones build_vector.
541static bool isVectorAllOnes(SDValue N) {
542 // Look through bitcasts. Endianness doesn't matter because we are looking
543 // for an all-ones value.
544 if (N->getOpcode() == ISD::BITCAST)
545 N = N->getOperand(0);
546
547 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
548
549 if (!BVN)
550 return false;
551
552 APInt SplatValue, SplatUndef;
553 unsigned SplatBitSize;
554 bool HasAnyUndefs;
555
556 // Endianness doesn't matter in this context because we are looking for
557 // an all-ones value.
558 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
559 return SplatValue.isAllOnesValue();
560
561 return false;
562}
563
564// Test whether N is the bitwise inverse of OfNode.
565static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
566 if (N->getOpcode() != ISD::XOR)
567 return false;
568
569 if (isVectorAllOnes(N->getOperand(0)))
570 return N->getOperand(1) == OfNode;
571
572 if (isVectorAllOnes(N->getOperand(1)))
573 return N->getOperand(0) == OfNode;
574
575 return false;
576}
577
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000578// Perform combines where ISD::OR is the root node.
579//
580// Performs the following transformations:
581// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
582// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
583// vector type.
584static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
585 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000586 const MipsSubtarget &Subtarget) {
587 if (!Subtarget.hasMSA())
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000588 return SDValue();
589
590 EVT Ty = N->getValueType(0);
591
592 if (!Ty.is128BitVector())
593 return SDValue();
594
595 SDValue Op0 = N->getOperand(0);
596 SDValue Op1 = N->getOperand(1);
597
598 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
599 SDValue Op0Op0 = Op0->getOperand(0);
600 SDValue Op0Op1 = Op0->getOperand(1);
601 SDValue Op1Op0 = Op1->getOperand(0);
602 SDValue Op1Op1 = Op1->getOperand(1);
Eric Christopher1c29a652014-07-18 22:55:25 +0000603 bool IsLittleEndian = !Subtarget.isLittle();
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000604
605 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000606 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000607 APInt Mask, InvMask;
608
609 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
610 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
611 // looking.
612 // IfClr will be set if we find a valid match.
613 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
614 Cond = Op0Op0;
615 IfSet = Op0Op1;
616
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000617 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
618 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000619 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000620 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
621 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000622 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000623
624 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000625 }
626
627 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
628 // thing again using this mask.
629 // IfClr will be set if we find a valid match.
630 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
631 Cond = Op0Op1;
632 IfSet = Op0Op0;
633
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000634 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
635 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000636 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000637 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
638 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000639 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000640
641 IsConstantMask = true;
642 }
643
644 // If IfClr is not yet set, try looking for a non-constant match.
645 // IfClr will be set if we find a valid match amongst the eight
646 // possibilities.
647 if (!IfClr.getNode()) {
648 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
649 Cond = Op1Op0;
650 IfSet = Op1Op1;
651 IfClr = Op0Op1;
652 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
653 Cond = Op1Op0;
654 IfSet = Op1Op1;
655 IfClr = Op0Op0;
656 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
657 Cond = Op1Op1;
658 IfSet = Op1Op0;
659 IfClr = Op0Op1;
660 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
661 Cond = Op1Op1;
662 IfSet = Op1Op0;
663 IfClr = Op0Op0;
664 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
665 Cond = Op0Op0;
666 IfSet = Op0Op1;
667 IfClr = Op1Op1;
668 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
669 Cond = Op0Op0;
670 IfSet = Op0Op1;
671 IfClr = Op1Op0;
672 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
673 Cond = Op0Op1;
674 IfSet = Op0Op0;
675 IfClr = Op1Op1;
676 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
677 Cond = Op0Op1;
678 IfSet = Op0Op0;
679 IfClr = Op1Op0;
680 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000681 }
682
683 // At this point, IfClr will be set if we have a valid match.
684 if (!IfClr.getNode())
685 return SDValue();
686
687 assert(Cond.getNode() && IfSet.getNode());
688
689 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000690 if (IsConstantMask) {
691 if (Mask.isAllOnesValue())
692 return IfSet;
693 else if (Mask == 0)
694 return IfClr;
695 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000696
697 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000698 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000699 }
700
701 return SDValue();
702}
703
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000704static SDValue genConstMult(SDValue X, APInt C, const SDLoc &DL, EVT VT,
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000705 EVT ShiftTy, SelectionDAG &DAG) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000706 // Return 0.
707 if (C == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000708 return DAG.getConstant(0, DL, VT);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000709
710 // Return x.
711 if (C == 1)
712 return X;
713
714 // If c is power of 2, return (shl x, log2(c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000715 if (C.isPowerOf2())
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000716 return DAG.getNode(ISD::SHL, DL, VT, X,
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000717 DAG.getConstant(C.logBase2(), DL, ShiftTy));
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000718
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000719 unsigned BitWidth = C.getBitWidth();
720 APInt Floor = APInt(BitWidth, 1) << C.logBase2();
721 APInt Ceil = C.isNegative() ? APInt(BitWidth, 0) :
722 APInt(BitWidth, 1) << C.ceilLogBase2();
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000723
724 // If |c - floor_c| <= |c - ceil_c|,
725 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
726 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000727 if ((C - Floor).ule(Ceil - C)) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000728 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
729 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
730 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
731 }
732
733 // If |c - floor_c| > |c - ceil_c|,
734 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
735 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
736 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
737 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
738}
739
740static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
741 const TargetLowering::DAGCombinerInfo &DCI,
742 const MipsSETargetLowering *TL) {
743 EVT VT = N->getValueType(0);
744
745 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
746 if (!VT.isVector())
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000747 return genConstMult(N->getOperand(0), C->getAPIntValue(), SDLoc(N), VT,
Mehdi Aminieaabc512015-07-09 15:12:23 +0000748 TL->getScalarShiftAmountTy(DAG.getDataLayout(), VT),
749 DAG);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000750
751 return SDValue(N, 0);
752}
753
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000754static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
755 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000756 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000757 // See if this is a vector splat immediate node.
758 APInt SplatValue, SplatUndef;
759 unsigned SplatBitSize;
760 bool HasAnyUndefs;
Sanjay Patel1ed771f2016-09-14 16:37:15 +0000761 unsigned EltSize = Ty.getScalarSizeInBits();
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000762 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
763
Eric Christopher1c29a652014-07-18 22:55:25 +0000764 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000765 return SDValue();
766
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000767 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000768 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000769 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000770 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000771 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000772 return SDValue();
773
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000774 SDLoc DL(N);
775 return DAG.getNode(Opc, DL, Ty, N->getOperand(0),
776 DAG.getConstant(SplatValue.getZExtValue(), DL, MVT::i32));
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000777}
778
779static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
780 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000781 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000782 EVT Ty = N->getValueType(0);
783
784 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
785 return SDValue();
786
787 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
788}
789
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000790// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
791// constant splats into MipsISD::SHRA_DSP for DSPr2.
792//
793// Performs the following transformations:
794// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
795// sign/zero-extension is completely overwritten by the new one performed by
796// the ISD::SRA and ISD::SHL nodes.
797// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
798// sequence.
799//
800// See performDSPShiftCombine for more information about the transformation
801// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000802static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
803 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000804 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000805 EVT Ty = N->getValueType(0);
806
Eric Christopher1c29a652014-07-18 22:55:25 +0000807 if (Subtarget.hasMSA()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000808 SDValue Op0 = N->getOperand(0);
809 SDValue Op1 = N->getOperand(1);
810
811 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
812 // where $d + sizeof($c) == 32
813 // or $d + sizeof($c) <= 32 and SExt
814 // -> (MipsVExtractSExt $a, $b, $c)
815 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
816 SDValue Op0Op0 = Op0->getOperand(0);
817 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
818
819 if (!ShAmount)
820 return SDValue();
821
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000822 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
823 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
824 return SDValue();
825
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000826 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
827 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
828
829 if (TotalBits == 32 ||
830 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
831 TotalBits <= 32)) {
832 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
833 Op0Op0->getOperand(2) };
Chandler Carruth356665a2014-08-01 22:09:43 +0000834 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
835 Op0Op0->getVTList(),
836 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000837 }
838 }
839 }
840
Eric Christopher1c29a652014-07-18 22:55:25 +0000841 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000842 return SDValue();
843
844 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
845}
846
847
848static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
849 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000850 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000851 EVT Ty = N->getValueType(0);
852
Eric Christopher1c29a652014-07-18 22:55:25 +0000853 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000854 return SDValue();
855
856 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
857}
858
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000859static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
860 bool IsV216 = (Ty == MVT::v2i16);
861
862 switch (CC) {
863 case ISD::SETEQ:
864 case ISD::SETNE: return true;
865 case ISD::SETLT:
866 case ISD::SETLE:
867 case ISD::SETGT:
868 case ISD::SETGE: return IsV216;
869 case ISD::SETULT:
870 case ISD::SETULE:
871 case ISD::SETUGT:
872 case ISD::SETUGE: return !IsV216;
873 default: return false;
874 }
875}
876
877static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
878 EVT Ty = N->getValueType(0);
879
880 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
881 return SDValue();
882
883 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
884 return SDValue();
885
Andrew Trickef9de2a2013-05-25 02:42:55 +0000886 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000887 N->getOperand(1), N->getOperand(2));
888}
889
890static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
891 EVT Ty = N->getValueType(0);
892
Daniel Sanders3ce56622013-09-24 12:18:31 +0000893 if (Ty.is128BitVector() && Ty.isInteger()) {
894 // Try the following combines:
895 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
896 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
897 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
898 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
899 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
900 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
901 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
902 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
903 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
904 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
905 // legalizer.
906 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000907
Daniel Sanders3ce56622013-09-24 12:18:31 +0000908 if (Op0->getOpcode() != ISD::SETCC)
909 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000910
Daniel Sanders3ce56622013-09-24 12:18:31 +0000911 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
912 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000913
Daniel Sanders3ce56622013-09-24 12:18:31 +0000914 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
915 Signed = true;
916 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
917 Signed = false;
918 else
919 return SDValue();
920
921 SDValue Op1 = N->getOperand(1);
922 SDValue Op2 = N->getOperand(2);
923 SDValue Op0Op0 = Op0->getOperand(0);
924 SDValue Op0Op1 = Op0->getOperand(1);
925
926 if (Op1 == Op0Op0 && Op2 == Op0Op1)
927 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
928 Ty, Op1, Op2);
929 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
930 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
931 Ty, Op1, Op2);
932 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
933 SDValue SetCC = N->getOperand(0);
934
935 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
936 return SDValue();
937
938 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
939 SetCC.getOperand(0), SetCC.getOperand(1),
940 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
941 }
942
943 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000944}
945
Daniel Sandersf7456c72013-09-23 13:22:24 +0000946static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000947 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000948 EVT Ty = N->getValueType(0);
949
Eric Christopher1c29a652014-07-18 22:55:25 +0000950 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000951 // Try the following combines:
952 // (xor (or $a, $b), (build_vector allones))
953 // (xor (or $a, $b), (bitcast (build_vector allones)))
954 SDValue Op0 = N->getOperand(0);
955 SDValue Op1 = N->getOperand(1);
956 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000957
958 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
959 NotOp = Op1;
960 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
961 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000962 else
963 return SDValue();
964
965 if (NotOp->getOpcode() == ISD::OR)
966 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
967 NotOp->getOperand(1));
968 }
969
970 return SDValue();
971}
972
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000973SDValue
974MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
975 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000976 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000977
978 switch (N->getOpcode()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000979 case ISD::AND:
980 Val = performANDCombine(N, DAG, DCI, Subtarget);
981 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000982 case ISD::OR:
983 Val = performORCombine(N, DAG, DCI, Subtarget);
984 break;
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000985 case ISD::MUL:
986 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000987 case ISD::SHL:
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000988 Val = performSHLCombine(N, DAG, DCI, Subtarget);
989 break;
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000990 case ISD::SRA:
991 return performSRACombine(N, DAG, DCI, Subtarget);
992 case ISD::SRL:
993 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000994 case ISD::VSELECT:
995 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000996 case ISD::XOR:
997 Val = performXORCombine(N, DAG, Subtarget);
998 break;
999 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001000 Val = performSETCCCombine(N, DAG);
1001 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001002 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001003
Daniel Sanders62aeab82013-10-30 13:31:27 +00001004 if (Val.getNode()) {
1005 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1006 N->printrWithDepth(dbgs(), &DAG);
1007 dbgs() << "\n=> \n";
1008 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1009 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001010 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001011 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001012
1013 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001014}
1015
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001016MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001017MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001018 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001019 switch (MI.getOpcode()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001020 default:
1021 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1022 case Mips::BPOSGE32_PSEUDO:
1023 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001024 case Mips::SNZ_B_PSEUDO:
1025 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1026 case Mips::SNZ_H_PSEUDO:
1027 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1028 case Mips::SNZ_W_PSEUDO:
1029 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1030 case Mips::SNZ_D_PSEUDO:
1031 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1032 case Mips::SNZ_V_PSEUDO:
1033 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1034 case Mips::SZ_B_PSEUDO:
1035 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1036 case Mips::SZ_H_PSEUDO:
1037 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1038 case Mips::SZ_W_PSEUDO:
1039 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1040 case Mips::SZ_D_PSEUDO:
1041 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1042 case Mips::SZ_V_PSEUDO:
1043 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001044 case Mips::COPY_FW_PSEUDO:
1045 return emitCOPY_FW(MI, BB);
1046 case Mips::COPY_FD_PSEUDO:
1047 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001048 case Mips::INSERT_FW_PSEUDO:
1049 return emitINSERT_FW(MI, BB);
1050 case Mips::INSERT_FD_PSEUDO:
1051 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001052 case Mips::INSERT_B_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001053 case Mips::INSERT_B_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001054 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1055 case Mips::INSERT_H_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001056 case Mips::INSERT_H_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001057 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1058 case Mips::INSERT_W_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001059 case Mips::INSERT_W_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001060 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1061 case Mips::INSERT_D_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001062 case Mips::INSERT_D_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001063 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1064 case Mips::INSERT_FW_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001065 case Mips::INSERT_FW_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001066 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1067 case Mips::INSERT_FD_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001068 case Mips::INSERT_FD_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001069 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001070 case Mips::FILL_FW_PSEUDO:
1071 return emitFILL_FW(MI, BB);
1072 case Mips::FILL_FD_PSEUDO:
1073 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001074 case Mips::FEXP2_W_1_PSEUDO:
1075 return emitFEXP2_W_1(MI, BB);
1076 case Mips::FEXP2_D_1_PSEUDO:
1077 return emitFEXP2_D_1(MI, BB);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00001078 case Mips::ST_F16:
1079 return emitST_F16_PSEUDO(MI, BB);
1080 case Mips::LD_F16:
1081 return emitLD_F16_PSEUDO(MI, BB);
1082 case Mips::MSA_FP_EXTEND_W_PSEUDO:
1083 return emitFPEXTEND_PSEUDO(MI, BB, false);
1084 case Mips::MSA_FP_ROUND_W_PSEUDO:
1085 return emitFPROUND_PSEUDO(MI, BB, false);
1086 case Mips::MSA_FP_EXTEND_D_PSEUDO:
1087 return emitFPEXTEND_PSEUDO(MI, BB, true);
1088 case Mips::MSA_FP_ROUND_D_PSEUDO:
1089 return emitFPROUND_PSEUDO(MI, BB, true);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001090 }
1091}
1092
Daniel Sanders23e98772014-11-02 16:09:29 +00001093bool MipsSETargetLowering::isEligibleForTailCallOptimization(
1094 const CCState &CCInfo, unsigned NextStackOffset,
1095 const MipsFunctionInfo &FI) const {
Simon Dardis57f4ae42016-08-04 09:17:07 +00001096 if (!UseMipsTailCalls)
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001097 return false;
1098
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00001099 // Exception has to be cleared with eret.
1100 if (FI.isISR())
1101 return false;
1102
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001103 // Return false if either the callee or caller has a byval argument.
Daniel Sanders23e98772014-11-02 16:09:29 +00001104 if (CCInfo.getInRegsParamsCount() > 0 || FI.hasByvalArg())
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001105 return false;
1106
1107 // Return true if the callee's argument area is no larger than the
1108 // caller's.
1109 return NextStackOffset <= FI.getIncomingArgSize();
1110}
1111
1112void MipsSETargetLowering::
1113getOpndList(SmallVectorImpl<SDValue> &Ops,
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001114 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001115 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001116 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
1117 SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001118 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001119 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001120 InternalLinkage, IsCallReloc, CLI, Callee,
1121 Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001122}
1123
Akira Hatanaka63791212013-09-07 00:52:30 +00001124SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1125 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1126
1127 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1128 return MipsTargetLowering::lowerLOAD(Op, DAG);
1129
1130 // Replace a double precision load with two i32 loads and a buildpair64.
1131 SDLoc DL(Op);
1132 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1133 EVT PtrVT = Ptr.getValueType();
1134
1135 // i32 load from lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001136 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr, MachinePointerInfo(),
1137 Nd.getAlignment(), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001138
1139 // i32 load from higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001140 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001141 SDValue Hi = DAG.getLoad(
1142 MVT::i32, DL, Lo.getValue(1), Ptr, MachinePointerInfo(),
1143 std::min(Nd.getAlignment(), 4U), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001144
Eric Christopher1c29a652014-07-18 22:55:25 +00001145 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001146 std::swap(Lo, Hi);
1147
1148 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1149 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001150 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001151}
1152
1153SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1154 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1155
1156 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1157 return MipsTargetLowering::lowerSTORE(Op, DAG);
1158
1159 // Replace a double precision store with two extractelement64s and i32 stores.
1160 SDLoc DL(Op);
1161 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1162 EVT PtrVT = Ptr.getValueType();
1163 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001164 Val, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001165 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001166 Val, DAG.getConstant(1, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001167
Eric Christopher1c29a652014-07-18 22:55:25 +00001168 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001169 std::swap(Lo, Hi);
1170
1171 // i32 store to lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001172 Chain =
1173 DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(), Nd.getAlignment(),
1174 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001175
1176 // i32 store to higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001177 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Akira Hatanaka63791212013-09-07 00:52:30 +00001178 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001179 std::min(Nd.getAlignment(), 4U),
1180 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001181}
1182
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001183SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1184 bool HasLo, bool HasHi,
1185 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001186 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001187 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001188
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001189 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001190 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001191 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1192 Op.getOperand(0), Op.getOperand(1));
1193 SDValue Lo, Hi;
1194
1195 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001196 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001197 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001198 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001199
1200 if (!HasLo || !HasHi)
1201 return HasLo ? Lo : Hi;
1202
1203 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001204 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001205}
1206
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001207static SDValue initAccumulator(SDValue In, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001208 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001209 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001210 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001211 DAG.getConstant(1, DL, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001212 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001213}
1214
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001215static SDValue extractLOHI(SDValue Op, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001216 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1217 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001218 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1219}
1220
1221// This function expands mips intrinsic nodes which have 64-bit input operands
1222// or output values.
1223//
1224// out64 = intrinsic-node in64
1225// =>
1226// lo = copy (extract-element (in64, 0))
1227// hi = copy (extract-element (in64, 1))
1228// mips-specific-node
1229// v0 = copy lo
1230// v1 = copy hi
1231// out64 = merge-values (v0, v1)
1232//
1233static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001234 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001235 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1236 SmallVector<SDValue, 3> Ops;
1237 unsigned OpNo = 0;
1238
1239 // See if Op has a chain input.
1240 if (HasChainIn)
1241 Ops.push_back(Op->getOperand(OpNo++));
1242
1243 // The next operand is the intrinsic opcode.
1244 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1245
1246 // See if the next operand has type i64.
1247 SDValue Opnd = Op->getOperand(++OpNo), In64;
1248
1249 if (Opnd.getValueType() == MVT::i64)
1250 In64 = initAccumulator(Opnd, DL, DAG);
1251 else
1252 Ops.push_back(Opnd);
1253
1254 // Push the remaining operands.
1255 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1256 Ops.push_back(Op->getOperand(OpNo));
1257
1258 // Add In64 to the end of the list.
1259 if (In64.getNode())
1260 Ops.push_back(In64);
1261
1262 // Scan output.
1263 SmallVector<EVT, 2> ResTys;
1264
1265 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1266 I != E; ++I)
1267 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1268
1269 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001270 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001271 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1272
1273 if (!HasChainIn)
1274 return Out;
1275
1276 assert(Val->getValueType(1) == MVT::Other);
1277 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001278 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001279}
1280
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001281// Lower an MSA copy intrinsic into the specified SelectionDAG node
1282static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1283 SDLoc DL(Op);
1284 SDValue Vec = Op->getOperand(1);
1285 SDValue Idx = Op->getOperand(2);
1286 EVT ResTy = Op->getValueType(0);
1287 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1288
1289 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1290 DAG.getValueType(EltTy));
1291
1292 return Result;
1293}
1294
Daniel Sanders50b80412013-11-15 12:56:49 +00001295static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1296 EVT ResVecTy = Op->getValueType(0);
1297 EVT ViaVecTy = ResVecTy;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001298 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
Daniel Sanders50b80412013-11-15 12:56:49 +00001299 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001300
Daniel Sanders50b80412013-11-15 12:56:49 +00001301 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1302 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1303 // lanes.
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001304 SDValue LaneA = Op->getOperand(OpNr);
1305 SDValue LaneB;
Daniel Sanders50b80412013-11-15 12:56:49 +00001306
1307 if (ResVecTy == MVT::v2i64) {
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001308 LaneB = DAG.getConstant(0, DL, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001309 ViaVecTy = MVT::v4i32;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001310 if(BigEndian)
1311 std::swap(LaneA, LaneB);
Daniel Sanders50b80412013-11-15 12:56:49 +00001312 } else
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001313 LaneB = LaneA;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001314
Daniel Sanders50b80412013-11-15 12:56:49 +00001315 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1316 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001317
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001318 SDValue Result = DAG.getBuildVector(
1319 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001320
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001321 if (ViaVecTy != ResVecTy) {
1322 SDValue One = DAG.getConstant(1, DL, ViaVecTy);
1323 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy,
1324 DAG.getNode(ISD::AND, DL, ViaVecTy, Result, One));
1325 }
Daniel Sandersf49dd822013-09-24 13:33:07 +00001326
1327 return Result;
1328}
1329
Simon Dardis548a53f2017-01-10 16:40:57 +00001330static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG,
1331 bool IsSigned = false) {
1332 return DAG.getConstant(
1333 APInt(Op->getValueType(0).getScalarType().getSizeInBits(),
1334 Op->getConstantOperandVal(ImmOp), IsSigned),
1335 SDLoc(Op), Op->getValueType(0));
Daniel Sanders50b80412013-11-15 12:56:49 +00001336}
1337
1338static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1339 bool BigEndian, SelectionDAG &DAG) {
1340 EVT ViaVecTy = VecTy;
1341 SDValue SplatValueA = SplatValue;
1342 SDValue SplatValueB = SplatValue;
1343 SDLoc DL(SplatValue);
1344
1345 if (VecTy == MVT::v2i64) {
1346 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1347 ViaVecTy = MVT::v4i32;
1348
1349 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1350 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001351 DAG.getConstant(32, DL, MVT::i32));
Daniel Sanders50b80412013-11-15 12:56:49 +00001352 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1353 }
1354
1355 // We currently hold the parts in little endian order. Swap them if
1356 // necessary.
1357 if (BigEndian)
1358 std::swap(SplatValueA, SplatValueB);
1359
1360 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1361 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1362 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1363 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1364
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001365 SDValue Result = DAG.getBuildVector(
1366 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001367
1368 if (VecTy != ViaVecTy)
1369 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1370
1371 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001372}
1373
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001374static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1375 unsigned Opc, SDValue Imm,
1376 bool BigEndian) {
1377 EVT VecTy = Op->getValueType(0);
1378 SDValue Exp2Imm;
1379 SDLoc DL(Op);
1380
Daniel Sanders50b80412013-11-15 12:56:49 +00001381 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1382 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001383 if (VecTy == MVT::v2i64) {
1384 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1385 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1386
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001387 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), DL,
1388 MVT::i32);
1389 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), DL, MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001390
1391 if (BigEndian)
1392 std::swap(BitImmLoOp, BitImmHiOp);
1393
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001394 Exp2Imm = DAG.getNode(
1395 ISD::BITCAST, DL, MVT::v2i64,
1396 DAG.getBuildVector(MVT::v4i32, DL,
1397 {BitImmLoOp, BitImmHiOp, BitImmLoOp, BitImmHiOp}));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001398 }
1399 }
1400
Craig Topper062a2ba2014-04-25 05:30:21 +00001401 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001402 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001403
1404 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1405 // only values 0-63 are valid.
1406 if (VecTy == MVT::v2i64)
1407 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1408
1409 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1410
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001411 Exp2Imm = DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, DL, VecTy),
1412 Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001413 }
1414
1415 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1416}
1417
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001418static SDValue truncateVecElts(SDValue Op, SelectionDAG &DAG) {
1419 SDLoc DL(Op);
1420 EVT ResTy = Op->getValueType(0);
1421 SDValue Vec = Op->getOperand(2);
1422 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
1423 MVT ResEltTy = ResTy == MVT::v2i64 ? MVT::i64 : MVT::i32;
1424 SDValue ConstValue = DAG.getConstant(Vec.getScalarValueSizeInBits() - 1,
1425 DL, ResEltTy);
1426 SDValue SplatVec = getBuildVectorSplat(ResTy, ConstValue, BigEndian, DAG);
1427
1428 return DAG.getNode(ISD::AND, DL, ResTy, Vec, SplatVec);
1429}
1430
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001431static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1432 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001433 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001434 SDValue One = DAG.getConstant(1, DL, ResTy);
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001435 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, truncateVecElts(Op, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001436
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001437 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1438 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001439}
1440
1441static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1442 SDLoc DL(Op);
1443 EVT ResTy = Op->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001444 APInt BitImm = APInt(ResTy.getScalarSizeInBits(), 1)
Daniel Sanders50b80412013-11-15 12:56:49 +00001445 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001446 SDValue BitMask = DAG.getConstant(~BitImm, DL, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001447
1448 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1449}
1450
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001451SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1452 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001453 SDLoc DL(Op);
Simon Dardis548a53f2017-01-10 16:40:57 +00001454 unsigned Intrinsic = cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue();
1455 switch (Intrinsic) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001456 default:
1457 return SDValue();
1458 case Intrinsic::mips_shilo:
1459 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1460 case Intrinsic::mips_dpau_h_qbl:
1461 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1462 case Intrinsic::mips_dpau_h_qbr:
1463 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1464 case Intrinsic::mips_dpsu_h_qbl:
1465 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1466 case Intrinsic::mips_dpsu_h_qbr:
1467 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1468 case Intrinsic::mips_dpa_w_ph:
1469 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1470 case Intrinsic::mips_dps_w_ph:
1471 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1472 case Intrinsic::mips_dpax_w_ph:
1473 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1474 case Intrinsic::mips_dpsx_w_ph:
1475 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1476 case Intrinsic::mips_mulsa_w_ph:
1477 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1478 case Intrinsic::mips_mult:
1479 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1480 case Intrinsic::mips_multu:
1481 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1482 case Intrinsic::mips_madd:
1483 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1484 case Intrinsic::mips_maddu:
1485 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1486 case Intrinsic::mips_msub:
1487 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1488 case Intrinsic::mips_msubu:
1489 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001490 case Intrinsic::mips_addv_b:
1491 case Intrinsic::mips_addv_h:
1492 case Intrinsic::mips_addv_w:
1493 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001494 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1495 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001496 case Intrinsic::mips_addvi_b:
1497 case Intrinsic::mips_addvi_h:
1498 case Intrinsic::mips_addvi_w:
1499 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001500 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1501 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001502 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001503 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1504 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001505 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001506 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1507 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001508 case Intrinsic::mips_bclr_b:
1509 case Intrinsic::mips_bclr_h:
1510 case Intrinsic::mips_bclr_w:
1511 case Intrinsic::mips_bclr_d:
1512 return lowerMSABitClear(Op, DAG);
1513 case Intrinsic::mips_bclri_b:
1514 case Intrinsic::mips_bclri_h:
1515 case Intrinsic::mips_bclri_w:
1516 case Intrinsic::mips_bclri_d:
1517 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001518 case Intrinsic::mips_binsli_b:
1519 case Intrinsic::mips_binsli_h:
1520 case Intrinsic::mips_binsli_w:
1521 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001522 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001523 EVT VecTy = Op->getValueType(0);
1524 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001525 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1526 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001527 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001528 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001529 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001530 DAG.getConstant(Mask, DL, VecTy, true),
1531 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001532 }
1533 case Intrinsic::mips_binsri_b:
1534 case Intrinsic::mips_binsri_h:
1535 case Intrinsic::mips_binsri_w:
1536 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001537 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001538 EVT VecTy = Op->getValueType(0);
1539 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001540 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1541 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001542 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001543 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001544 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001545 DAG.getConstant(Mask, DL, VecTy, true),
1546 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001547 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001548 case Intrinsic::mips_bmnz_v:
1549 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1550 Op->getOperand(2), Op->getOperand(1));
1551 case Intrinsic::mips_bmnzi_b:
1552 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1553 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1554 Op->getOperand(1));
1555 case Intrinsic::mips_bmz_v:
1556 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1557 Op->getOperand(1), Op->getOperand(2));
1558 case Intrinsic::mips_bmzi_b:
1559 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1560 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1561 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001562 case Intrinsic::mips_bneg_b:
1563 case Intrinsic::mips_bneg_h:
1564 case Intrinsic::mips_bneg_w:
1565 case Intrinsic::mips_bneg_d: {
1566 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001567 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001568
1569 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1570 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001571 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001572 }
1573 case Intrinsic::mips_bnegi_b:
1574 case Intrinsic::mips_bnegi_h:
1575 case Intrinsic::mips_bnegi_w:
1576 case Intrinsic::mips_bnegi_d:
1577 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001578 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001579 case Intrinsic::mips_bnz_b:
1580 case Intrinsic::mips_bnz_h:
1581 case Intrinsic::mips_bnz_w:
1582 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001583 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1584 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001585 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001586 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1587 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001588 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001589 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001590 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001591 Op->getOperand(1), Op->getOperand(3),
1592 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001593 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001594 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001595 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001596 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1597 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001598 case Intrinsic::mips_bset_b:
1599 case Intrinsic::mips_bset_h:
1600 case Intrinsic::mips_bset_w:
1601 case Intrinsic::mips_bset_d: {
1602 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001603 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001604
1605 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1606 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001607 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001608 }
1609 case Intrinsic::mips_bseti_b:
1610 case Intrinsic::mips_bseti_h:
1611 case Intrinsic::mips_bseti_w:
1612 case Intrinsic::mips_bseti_d:
1613 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001614 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001615 case Intrinsic::mips_bz_b:
1616 case Intrinsic::mips_bz_h:
1617 case Intrinsic::mips_bz_w:
1618 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001619 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1620 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001621 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001622 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1623 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001624 case Intrinsic::mips_ceq_b:
1625 case Intrinsic::mips_ceq_h:
1626 case Intrinsic::mips_ceq_w:
1627 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001628 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001629 Op->getOperand(2), ISD::SETEQ);
1630 case Intrinsic::mips_ceqi_b:
1631 case Intrinsic::mips_ceqi_h:
1632 case Intrinsic::mips_ceqi_w:
1633 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001634 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001635 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETEQ);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001636 case Intrinsic::mips_cle_s_b:
1637 case Intrinsic::mips_cle_s_h:
1638 case Intrinsic::mips_cle_s_w:
1639 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001640 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001641 Op->getOperand(2), ISD::SETLE);
1642 case Intrinsic::mips_clei_s_b:
1643 case Intrinsic::mips_clei_s_h:
1644 case Intrinsic::mips_clei_s_w:
1645 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001646 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001647 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLE);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001648 case Intrinsic::mips_cle_u_b:
1649 case Intrinsic::mips_cle_u_h:
1650 case Intrinsic::mips_cle_u_w:
1651 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001652 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001653 Op->getOperand(2), ISD::SETULE);
1654 case Intrinsic::mips_clei_u_b:
1655 case Intrinsic::mips_clei_u_h:
1656 case Intrinsic::mips_clei_u_w:
1657 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001658 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001659 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1660 case Intrinsic::mips_clt_s_b:
1661 case Intrinsic::mips_clt_s_h:
1662 case Intrinsic::mips_clt_s_w:
1663 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001664 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001665 Op->getOperand(2), ISD::SETLT);
1666 case Intrinsic::mips_clti_s_b:
1667 case Intrinsic::mips_clti_s_h:
1668 case Intrinsic::mips_clti_s_w:
1669 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001670 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001671 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLT);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001672 case Intrinsic::mips_clt_u_b:
1673 case Intrinsic::mips_clt_u_h:
1674 case Intrinsic::mips_clt_u_w:
1675 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001676 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001677 Op->getOperand(2), ISD::SETULT);
1678 case Intrinsic::mips_clti_u_b:
1679 case Intrinsic::mips_clti_u_h:
1680 case Intrinsic::mips_clti_u_w:
1681 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001682 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001683 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001684 case Intrinsic::mips_copy_s_b:
1685 case Intrinsic::mips_copy_s_h:
1686 case Intrinsic::mips_copy_s_w:
1687 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001688 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001689 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001690 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1691 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1692 else {
1693 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1694 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1695 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1696 Op->getValueType(0), Op->getOperand(1),
1697 Op->getOperand(2));
1698 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001699 case Intrinsic::mips_copy_u_b:
1700 case Intrinsic::mips_copy_u_h:
1701 case Intrinsic::mips_copy_u_w:
1702 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001703 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001704 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001705 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1706 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1707 else {
1708 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1709 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1710 // Note: When i64 is illegal, this results in copy_s.w instructions
1711 // instead of copy_u.w instructions. This makes no difference to the
1712 // behaviour since i64 is only illegal when the register file is 32-bit.
1713 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1714 Op->getValueType(0), Op->getOperand(1),
1715 Op->getOperand(2));
1716 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001717 case Intrinsic::mips_div_s_b:
1718 case Intrinsic::mips_div_s_h:
1719 case Intrinsic::mips_div_s_w:
1720 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001721 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1722 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001723 case Intrinsic::mips_div_u_b:
1724 case Intrinsic::mips_div_u_h:
1725 case Intrinsic::mips_div_u_w:
1726 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001727 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1728 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001729 case Intrinsic::mips_fadd_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001730 case Intrinsic::mips_fadd_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001731 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001732 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1733 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001734 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1735 case Intrinsic::mips_fceq_w:
1736 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001737 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001738 Op->getOperand(2), ISD::SETOEQ);
1739 case Intrinsic::mips_fcle_w:
1740 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001741 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001742 Op->getOperand(2), ISD::SETOLE);
1743 case Intrinsic::mips_fclt_w:
1744 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001745 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001746 Op->getOperand(2), ISD::SETOLT);
1747 case Intrinsic::mips_fcne_w:
1748 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001749 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001750 Op->getOperand(2), ISD::SETONE);
1751 case Intrinsic::mips_fcor_w:
1752 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001753 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001754 Op->getOperand(2), ISD::SETO);
1755 case Intrinsic::mips_fcueq_w:
1756 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001757 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001758 Op->getOperand(2), ISD::SETUEQ);
1759 case Intrinsic::mips_fcule_w:
1760 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001761 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001762 Op->getOperand(2), ISD::SETULE);
1763 case Intrinsic::mips_fcult_w:
1764 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001765 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001766 Op->getOperand(2), ISD::SETULT);
1767 case Intrinsic::mips_fcun_w:
1768 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001769 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001770 Op->getOperand(2), ISD::SETUO);
1771 case Intrinsic::mips_fcune_w:
1772 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001773 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001774 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001775 case Intrinsic::mips_fdiv_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001776 case Intrinsic::mips_fdiv_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001777 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001778 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1779 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001780 case Intrinsic::mips_ffint_u_w:
1781 case Intrinsic::mips_ffint_u_d:
1782 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1783 Op->getOperand(1));
1784 case Intrinsic::mips_ffint_s_w:
1785 case Intrinsic::mips_ffint_s_d:
1786 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1787 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001788 case Intrinsic::mips_fill_b:
1789 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001790 case Intrinsic::mips_fill_w:
1791 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001792 EVT ResTy = Op->getValueType(0);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001793 SmallVector<SDValue, 16> Ops(ResTy.getVectorNumElements(),
1794 Op->getOperand(1));
Daniel Sandersf49dd822013-09-24 13:33:07 +00001795
Daniel Sandersc72593e2013-09-27 13:20:41 +00001796 // If ResTy is v2i64 then the type legalizer will break this node down into
1797 // an equivalent v4i32.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001798 return DAG.getBuildVector(ResTy, DL, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001799 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001800 case Intrinsic::mips_fexp2_w:
1801 case Intrinsic::mips_fexp2_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001802 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sandersa9521602013-10-23 10:36:52 +00001803 EVT ResTy = Op->getValueType(0);
1804 return DAG.getNode(
1805 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1806 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1807 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001808 case Intrinsic::mips_flog2_w:
1809 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001810 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001811 case Intrinsic::mips_fmadd_w:
1812 case Intrinsic::mips_fmadd_d:
1813 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1814 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001815 case Intrinsic::mips_fmul_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001816 case Intrinsic::mips_fmul_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001817 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001818 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1819 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001820 case Intrinsic::mips_fmsub_w:
1821 case Intrinsic::mips_fmsub_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001822 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanderse67bd872013-10-11 10:27:32 +00001823 EVT ResTy = Op->getValueType(0);
1824 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1825 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1826 Op->getOperand(2), Op->getOperand(3)));
1827 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001828 case Intrinsic::mips_frint_w:
1829 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001830 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001831 case Intrinsic::mips_fsqrt_w:
1832 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001833 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001834 case Intrinsic::mips_fsub_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001835 case Intrinsic::mips_fsub_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001836 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001837 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1838 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001839 case Intrinsic::mips_ftrunc_u_w:
1840 case Intrinsic::mips_ftrunc_u_d:
1841 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1842 Op->getOperand(1));
1843 case Intrinsic::mips_ftrunc_s_w:
1844 case Intrinsic::mips_ftrunc_s_d:
1845 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1846 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001847 case Intrinsic::mips_ilvev_b:
1848 case Intrinsic::mips_ilvev_h:
1849 case Intrinsic::mips_ilvev_w:
1850 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001851 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001852 Op->getOperand(1), Op->getOperand(2));
1853 case Intrinsic::mips_ilvl_b:
1854 case Intrinsic::mips_ilvl_h:
1855 case Intrinsic::mips_ilvl_w:
1856 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001857 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001858 Op->getOperand(1), Op->getOperand(2));
1859 case Intrinsic::mips_ilvod_b:
1860 case Intrinsic::mips_ilvod_h:
1861 case Intrinsic::mips_ilvod_w:
1862 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001863 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001864 Op->getOperand(1), Op->getOperand(2));
1865 case Intrinsic::mips_ilvr_b:
1866 case Intrinsic::mips_ilvr_h:
1867 case Intrinsic::mips_ilvr_w:
1868 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001869 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001870 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001871 case Intrinsic::mips_insert_b:
1872 case Intrinsic::mips_insert_h:
1873 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001874 case Intrinsic::mips_insert_d:
1875 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1876 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001877 case Intrinsic::mips_insve_b:
1878 case Intrinsic::mips_insve_h:
1879 case Intrinsic::mips_insve_w:
Simon Dardis548a53f2017-01-10 16:40:57 +00001880 case Intrinsic::mips_insve_d: {
1881 // Report an error for out of range values.
1882 int64_t Max;
1883 switch (Intrinsic) {
1884 case Intrinsic::mips_insve_b: Max = 15; break;
1885 case Intrinsic::mips_insve_h: Max = 7; break;
1886 case Intrinsic::mips_insve_w: Max = 3; break;
1887 case Intrinsic::mips_insve_d: Max = 1; break;
1888 default: llvm_unreachable("Unmatched intrinsic");
1889 }
1890 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
1891 if (Value < 0 || Value > Max)
1892 report_fatal_error("Immediate out of range");
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001893 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1894 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001895 DAG.getConstant(0, DL, MVT::i32));
Simon Dardis548a53f2017-01-10 16:40:57 +00001896 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00001897 case Intrinsic::mips_ldi_b:
1898 case Intrinsic::mips_ldi_h:
1899 case Intrinsic::mips_ldi_w:
1900 case Intrinsic::mips_ldi_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00001901 return lowerMSASplatImm(Op, 1, DAG, true);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001902 case Intrinsic::mips_lsa:
1903 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001904 EVT ResTy = Op->getValueType(0);
1905 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1906 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1907 Op->getOperand(2), Op->getOperand(3)));
1908 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001909 case Intrinsic::mips_maddv_b:
1910 case Intrinsic::mips_maddv_h:
1911 case Intrinsic::mips_maddv_w:
1912 case Intrinsic::mips_maddv_d: {
1913 EVT ResTy = Op->getValueType(0);
1914 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1915 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1916 Op->getOperand(2), Op->getOperand(3)));
1917 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001918 case Intrinsic::mips_max_s_b:
1919 case Intrinsic::mips_max_s_h:
1920 case Intrinsic::mips_max_s_w:
1921 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001922 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1923 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001924 case Intrinsic::mips_max_u_b:
1925 case Intrinsic::mips_max_u_h:
1926 case Intrinsic::mips_max_u_w:
1927 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001928 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1929 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001930 case Intrinsic::mips_maxi_s_b:
1931 case Intrinsic::mips_maxi_s_h:
1932 case Intrinsic::mips_maxi_s_w:
1933 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001934 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00001935 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001936 case Intrinsic::mips_maxi_u_b:
1937 case Intrinsic::mips_maxi_u_h:
1938 case Intrinsic::mips_maxi_u_w:
1939 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001940 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1941 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001942 case Intrinsic::mips_min_s_b:
1943 case Intrinsic::mips_min_s_h:
1944 case Intrinsic::mips_min_s_w:
1945 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001946 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1947 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001948 case Intrinsic::mips_min_u_b:
1949 case Intrinsic::mips_min_u_h:
1950 case Intrinsic::mips_min_u_w:
1951 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001952 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1953 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001954 case Intrinsic::mips_mini_s_b:
1955 case Intrinsic::mips_mini_s_h:
1956 case Intrinsic::mips_mini_s_w:
1957 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001958 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00001959 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001960 case Intrinsic::mips_mini_u_b:
1961 case Intrinsic::mips_mini_u_h:
1962 case Intrinsic::mips_mini_u_w:
1963 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001964 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1965 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001966 case Intrinsic::mips_mod_s_b:
1967 case Intrinsic::mips_mod_s_h:
1968 case Intrinsic::mips_mod_s_w:
1969 case Intrinsic::mips_mod_s_d:
1970 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1971 Op->getOperand(2));
1972 case Intrinsic::mips_mod_u_b:
1973 case Intrinsic::mips_mod_u_h:
1974 case Intrinsic::mips_mod_u_w:
1975 case Intrinsic::mips_mod_u_d:
1976 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1977 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001978 case Intrinsic::mips_mulv_b:
1979 case Intrinsic::mips_mulv_h:
1980 case Intrinsic::mips_mulv_w:
1981 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001982 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1983 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001984 case Intrinsic::mips_msubv_b:
1985 case Intrinsic::mips_msubv_h:
1986 case Intrinsic::mips_msubv_w:
1987 case Intrinsic::mips_msubv_d: {
1988 EVT ResTy = Op->getValueType(0);
1989 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
1990 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1991 Op->getOperand(2), Op->getOperand(3)));
1992 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00001993 case Intrinsic::mips_nlzc_b:
1994 case Intrinsic::mips_nlzc_h:
1995 case Intrinsic::mips_nlzc_w:
1996 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001997 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00001998 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001999 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2000 Op->getOperand(1), Op->getOperand(2));
2001 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002002 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002003 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002004 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2005 Op->getOperand(1),
2006 lowerMSASplatImm(Op, 2, DAG));
2007 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002008 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002009 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002010 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2011 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002012 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002013 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2014 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002015 case Intrinsic::mips_pckev_b:
2016 case Intrinsic::mips_pckev_h:
2017 case Intrinsic::mips_pckev_w:
2018 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002019 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002020 Op->getOperand(1), Op->getOperand(2));
2021 case Intrinsic::mips_pckod_b:
2022 case Intrinsic::mips_pckod_h:
2023 case Intrinsic::mips_pckod_w:
2024 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002025 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002026 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002027 case Intrinsic::mips_pcnt_b:
2028 case Intrinsic::mips_pcnt_h:
2029 case Intrinsic::mips_pcnt_w:
2030 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002031 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002032 case Intrinsic::mips_sat_s_b:
2033 case Intrinsic::mips_sat_s_h:
2034 case Intrinsic::mips_sat_s_w:
2035 case Intrinsic::mips_sat_s_d:
2036 case Intrinsic::mips_sat_u_b:
2037 case Intrinsic::mips_sat_u_h:
2038 case Intrinsic::mips_sat_u_w:
2039 case Intrinsic::mips_sat_u_d: {
2040 // Report an error for out of range values.
2041 int64_t Max;
2042 switch (Intrinsic) {
2043 case Intrinsic::mips_sat_s_b:
2044 case Intrinsic::mips_sat_u_b: Max = 7; break;
2045 case Intrinsic::mips_sat_s_h:
2046 case Intrinsic::mips_sat_u_h: Max = 15; break;
2047 case Intrinsic::mips_sat_s_w:
2048 case Intrinsic::mips_sat_u_w: Max = 31; break;
2049 case Intrinsic::mips_sat_s_d:
2050 case Intrinsic::mips_sat_u_d: Max = 63; break;
2051 default: llvm_unreachable("Unmatched intrinsic");
2052 }
2053 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2054 if (Value < 0 || Value > Max)
2055 report_fatal_error("Immediate out of range");
2056 return SDValue();
2057 }
Daniel Sanders26307182013-09-24 14:20:00 +00002058 case Intrinsic::mips_shf_b:
2059 case Intrinsic::mips_shf_h:
Simon Dardis548a53f2017-01-10 16:40:57 +00002060 case Intrinsic::mips_shf_w: {
2061 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2062 if (Value < 0 || Value > 255)
2063 report_fatal_error("Immediate out of range");
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002064 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002065 Op->getOperand(2), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002066 }
2067 case Intrinsic::mips_sldi_b:
2068 case Intrinsic::mips_sldi_h:
2069 case Intrinsic::mips_sldi_w:
2070 case Intrinsic::mips_sldi_d: {
2071 // Report an error for out of range values.
2072 int64_t Max;
2073 switch (Intrinsic) {
2074 case Intrinsic::mips_sldi_b: Max = 15; break;
2075 case Intrinsic::mips_sldi_h: Max = 7; break;
2076 case Intrinsic::mips_sldi_w: Max = 3; break;
2077 case Intrinsic::mips_sldi_d: Max = 1; break;
2078 default: llvm_unreachable("Unmatched intrinsic");
2079 }
2080 int64_t Value = cast<ConstantSDNode>(Op->getOperand(3))->getSExtValue();
2081 if (Value < 0 || Value > Max)
2082 report_fatal_error("Immediate out of range");
2083 return SDValue();
2084 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002085 case Intrinsic::mips_sll_b:
2086 case Intrinsic::mips_sll_h:
2087 case Intrinsic::mips_sll_w:
2088 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002089 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002090 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002091 case Intrinsic::mips_slli_b:
2092 case Intrinsic::mips_slli_h:
2093 case Intrinsic::mips_slli_w:
2094 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002095 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2096 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002097 case Intrinsic::mips_splat_b:
2098 case Intrinsic::mips_splat_h:
2099 case Intrinsic::mips_splat_w:
2100 case Intrinsic::mips_splat_d:
2101 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2102 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2103 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2104 // Instead we lower to MipsISD::VSHF and match from there.
2105 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002106 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002107 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002108 case Intrinsic::mips_splati_b:
2109 case Intrinsic::mips_splati_h:
2110 case Intrinsic::mips_splati_w:
2111 case Intrinsic::mips_splati_d:
2112 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2113 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2114 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002115 case Intrinsic::mips_sra_b:
2116 case Intrinsic::mips_sra_h:
2117 case Intrinsic::mips_sra_w:
2118 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002119 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002120 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002121 case Intrinsic::mips_srai_b:
2122 case Intrinsic::mips_srai_h:
2123 case Intrinsic::mips_srai_w:
2124 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002125 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2126 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002127 case Intrinsic::mips_srari_b:
2128 case Intrinsic::mips_srari_h:
2129 case Intrinsic::mips_srari_w:
2130 case Intrinsic::mips_srari_d: {
2131 // Report an error for out of range values.
2132 int64_t Max;
2133 switch (Intrinsic) {
2134 case Intrinsic::mips_srari_b: Max = 7; break;
2135 case Intrinsic::mips_srari_h: Max = 15; break;
2136 case Intrinsic::mips_srari_w: Max = 31; break;
2137 case Intrinsic::mips_srari_d: Max = 63; break;
2138 default: llvm_unreachable("Unmatched intrinsic");
2139 }
2140 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2141 if (Value < 0 || Value > Max)
2142 report_fatal_error("Immediate out of range");
2143 return SDValue();
2144 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002145 case Intrinsic::mips_srl_b:
2146 case Intrinsic::mips_srl_h:
2147 case Intrinsic::mips_srl_w:
2148 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002149 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002150 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002151 case Intrinsic::mips_srli_b:
2152 case Intrinsic::mips_srli_h:
2153 case Intrinsic::mips_srli_w:
2154 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002155 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2156 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002157 case Intrinsic::mips_srlri_b:
2158 case Intrinsic::mips_srlri_h:
2159 case Intrinsic::mips_srlri_w:
2160 case Intrinsic::mips_srlri_d: {
2161 // Report an error for out of range values.
2162 int64_t Max;
2163 switch (Intrinsic) {
2164 case Intrinsic::mips_srlri_b: Max = 7; break;
2165 case Intrinsic::mips_srlri_h: Max = 15; break;
2166 case Intrinsic::mips_srlri_w: Max = 31; break;
2167 case Intrinsic::mips_srlri_d: Max = 63; break;
2168 default: llvm_unreachable("Unmatched intrinsic");
2169 }
2170 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2171 if (Value < 0 || Value > Max)
2172 report_fatal_error("Immediate out of range");
2173 return SDValue();
2174 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002175 case Intrinsic::mips_subv_b:
2176 case Intrinsic::mips_subv_h:
2177 case Intrinsic::mips_subv_w:
2178 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002179 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2180 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002181 case Intrinsic::mips_subvi_b:
2182 case Intrinsic::mips_subvi_h:
2183 case Intrinsic::mips_subvi_w:
2184 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002185 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2186 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002187 case Intrinsic::mips_vshf_b:
2188 case Intrinsic::mips_vshf_h:
2189 case Intrinsic::mips_vshf_w:
2190 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002191 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002192 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002193 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002194 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2195 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002196 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002197 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2198 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Marcin Koscielnicki7efdca52016-04-27 17:21:49 +00002199 case Intrinsic::thread_pointer: {
2200 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2201 return DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2202 }
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002203 }
2204}
2205
Simon Dardis548a53f2017-01-10 16:40:57 +00002206static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2207 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002208 SDLoc DL(Op);
2209 SDValue ChainIn = Op->getOperand(0);
2210 SDValue Address = Op->getOperand(2);
2211 SDValue Offset = Op->getOperand(3);
2212 EVT ResTy = Op->getValueType(0);
2213 EVT PtrTy = Address->getValueType(0);
2214
Simon Dardis548a53f2017-01-10 16:40:57 +00002215 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2216 // however takes an i32 signed constant offset. The actual type of the
2217 // intrinsic is a scaled signed i10.
2218 if (Subtarget.isABI_N64())
2219 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2220
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002221 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002222 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(),
2223 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002224}
2225
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002226SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2227 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002228 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2229 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002230 default:
2231 return SDValue();
2232 case Intrinsic::mips_extp:
2233 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2234 case Intrinsic::mips_extpdp:
2235 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2236 case Intrinsic::mips_extr_w:
2237 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2238 case Intrinsic::mips_extr_r_w:
2239 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2240 case Intrinsic::mips_extr_rs_w:
2241 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2242 case Intrinsic::mips_extr_s_h:
2243 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2244 case Intrinsic::mips_mthlip:
2245 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2246 case Intrinsic::mips_mulsaq_s_w_ph:
2247 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2248 case Intrinsic::mips_maq_s_w_phl:
2249 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2250 case Intrinsic::mips_maq_s_w_phr:
2251 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2252 case Intrinsic::mips_maq_sa_w_phl:
2253 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2254 case Intrinsic::mips_maq_sa_w_phr:
2255 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2256 case Intrinsic::mips_dpaq_s_w_ph:
2257 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2258 case Intrinsic::mips_dpsq_s_w_ph:
2259 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2260 case Intrinsic::mips_dpaq_sa_l_w:
2261 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2262 case Intrinsic::mips_dpsq_sa_l_w:
2263 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2264 case Intrinsic::mips_dpaqx_s_w_ph:
2265 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2266 case Intrinsic::mips_dpaqx_sa_w_ph:
2267 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2268 case Intrinsic::mips_dpsqx_s_w_ph:
2269 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2270 case Intrinsic::mips_dpsqx_sa_w_ph:
2271 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002272 case Intrinsic::mips_ld_b:
2273 case Intrinsic::mips_ld_h:
2274 case Intrinsic::mips_ld_w:
2275 case Intrinsic::mips_ld_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002276 return lowerMSALoadIntr(Op, DAG, Intr, Subtarget);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002277 }
2278}
2279
Simon Dardis548a53f2017-01-10 16:40:57 +00002280static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2281 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002282 SDLoc DL(Op);
2283 SDValue ChainIn = Op->getOperand(0);
2284 SDValue Value = Op->getOperand(2);
2285 SDValue Address = Op->getOperand(3);
2286 SDValue Offset = Op->getOperand(4);
2287 EVT PtrTy = Address->getValueType(0);
2288
Simon Dardis548a53f2017-01-10 16:40:57 +00002289 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2290 // however takes an i32 signed constant offset. The actual type of the
2291 // intrinsic is a scaled signed i10.
2292 if (Subtarget.isABI_N64())
2293 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2294
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002295 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2296
Justin Lebar9c375812016-07-15 18:27:10 +00002297 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(),
2298 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002299}
2300
2301SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2302 SelectionDAG &DAG) const {
2303 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2304 switch (Intr) {
2305 default:
2306 return SDValue();
2307 case Intrinsic::mips_st_b:
2308 case Intrinsic::mips_st_h:
2309 case Intrinsic::mips_st_w:
2310 case Intrinsic::mips_st_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002311 return lowerMSAStoreIntr(Op, DAG, Intr, Subtarget);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002312 }
2313}
2314
Daniel Sanders7a289d02013-09-23 12:02:46 +00002315/// \brief Check if the given BuildVectorSDNode is a splat.
2316/// This method currently relies on DAG nodes being reused when equivalent,
2317/// so it's possible for this to return false even when isConstantSplat returns
2318/// true.
2319static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002320 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002321 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002322
2323 SDValue Operand0 = N->getOperand(0);
2324
2325 for (unsigned int i = 1; i < nOps; ++i) {
2326 if (N->getOperand(i) != Operand0)
2327 return false;
2328 }
2329
2330 return true;
2331}
2332
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002333// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2334//
2335// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2336// choose to sign-extend but we could have equally chosen zero-extend. The
2337// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2338// result into this node later (possibly changing it to a zero-extend in the
2339// process).
2340SDValue MipsSETargetLowering::
2341lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2342 SDLoc DL(Op);
2343 EVT ResTy = Op->getValueType(0);
2344 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002345 EVT VecTy = Op0->getValueType(0);
2346
2347 if (!VecTy.is128BitVector())
2348 return SDValue();
2349
2350 if (ResTy.isInteger()) {
2351 SDValue Op1 = Op->getOperand(1);
2352 EVT EltTy = VecTy.getVectorElementType();
2353 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2354 DAG.getValueType(EltTy));
2355 }
2356
2357 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002358}
2359
Daniel Sandersf49dd822013-09-24 13:33:07 +00002360static bool isConstantOrUndef(const SDValue Op) {
Sanjay Patel57195842016-03-14 17:28:46 +00002361 if (Op->isUndef())
Daniel Sandersf49dd822013-09-24 13:33:07 +00002362 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002363 if (isa<ConstantSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002364 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002365 if (isa<ConstantFPSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002366 return true;
2367 return false;
2368}
2369
2370static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2371 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2372 if (isConstantOrUndef(Op->getOperand(i)))
2373 return true;
2374 return false;
2375}
2376
Daniel Sanders7a289d02013-09-23 12:02:46 +00002377// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2378// backend.
2379//
2380// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002381// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2382// 2 less than or equal to 64 and the value fits into a signed 10-bit
2383// immediate
2384// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2385// is a power of 2 less than or equal to 64 and the value does not fit into a
2386// signed 10-bit immediate
2387// - Non-constant splats are legal as-is.
2388// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2389// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002390SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2391 SelectionDAG &DAG) const {
2392 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2393 EVT ResTy = Op->getValueType(0);
2394 SDLoc DL(Op);
2395 APInt SplatValue, SplatUndef;
2396 unsigned SplatBitSize;
2397 bool HasAnyUndefs;
2398
Eric Christopher1c29a652014-07-18 22:55:25 +00002399 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002400 return SDValue();
2401
2402 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2403 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002404 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002405 // We can only cope with 8, 16, 32, or 64-bit elements
2406 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2407 SplatBitSize != 64)
2408 return SDValue();
2409
Simon Dardis7090d142017-03-10 13:27:14 +00002410 // If the value isn't an integer type we will have to bitcast
2411 // from an integer type first. Also, if there are any undefs, we must
2412 // lower them to defined values first.
2413 if (ResTy.isInteger() && !HasAnyUndefs)
Daniel Sandersf49dd822013-09-24 13:33:07 +00002414 return Op;
2415
2416 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002417
2418 switch (SplatBitSize) {
2419 default:
2420 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002421 case 8:
2422 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002423 break;
2424 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002425 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002426 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002427 case 32:
2428 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002429 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002430 case 64:
2431 // There's no fill.d to fall back on for 64-bit values
2432 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002433 }
2434
Daniel Sanders50b80412013-11-15 12:56:49 +00002435 // SelectionDAG::getConstant will promote SplatValue appropriately.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002436 SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002437
Daniel Sanders50b80412013-11-15 12:56:49 +00002438 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002439 if (ViaVecTy != ResTy)
2440 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002441
2442 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002443 } else if (isSplatVector(Node))
2444 return Op;
2445 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002446 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2447 // The resulting code is the same length as the expansion, but it doesn't
2448 // use memory operations
2449 EVT ResTy = Node->getValueType(0);
2450
2451 assert(ResTy.isVector());
2452
2453 unsigned NumElts = ResTy.getVectorNumElements();
2454 SDValue Vector = DAG.getUNDEF(ResTy);
2455 for (unsigned i = 0; i < NumElts; ++i) {
2456 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2457 Node->getOperand(i),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002458 DAG.getConstant(i, DL, MVT::i32));
Daniel Sandersf86622b2013-09-24 13:16:15 +00002459 }
2460 return Vector;
2461 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002462
2463 return SDValue();
2464}
2465
Daniel Sanders26307182013-09-24 14:20:00 +00002466// Lower VECTOR_SHUFFLE into SHF (if possible).
2467//
2468// SHF splits the vector into blocks of four elements, then shuffles these
2469// elements according to a <4 x i2> constant (encoded as an integer immediate).
2470//
2471// It is therefore possible to lower into SHF when the mask takes the form:
2472// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2473// When undef's appear they are treated as if they were whatever value is
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002474// necessary in order to fit the above forms.
Daniel Sanders26307182013-09-24 14:20:00 +00002475//
2476// For example:
2477// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2478// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2479// i32 7, i32 6, i32 5, i32 4>
2480// is lowered to:
2481// (SHF_H $w0, $w1, 27)
2482// where the 27 comes from:
2483// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2484static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2485 SmallVector<int, 16> Indices,
2486 SelectionDAG &DAG) {
2487 int SHFIndices[4] = { -1, -1, -1, -1 };
2488
2489 if (Indices.size() < 4)
2490 return SDValue();
2491
2492 for (unsigned i = 0; i < 4; ++i) {
2493 for (unsigned j = i; j < Indices.size(); j += 4) {
2494 int Idx = Indices[j];
2495
2496 // Convert from vector index to 4-element subvector index
2497 // If an index refers to an element outside of the subvector then give up
2498 if (Idx != -1) {
2499 Idx -= 4 * (j / 4);
2500 if (Idx < 0 || Idx >= 4)
2501 return SDValue();
2502 }
2503
2504 // If the mask has an undef, replace it with the current index.
2505 // Note that it might still be undef if the current index is also undef
2506 if (SHFIndices[i] == -1)
2507 SHFIndices[i] = Idx;
2508
2509 // Check that non-undef values are the same as in the mask. If they
2510 // aren't then give up
2511 if (!(Idx == -1 || Idx == SHFIndices[i]))
2512 return SDValue();
2513 }
2514 }
2515
2516 // Calculate the immediate. Replace any remaining undefs with zero
2517 APInt Imm(32, 0);
2518 for (int i = 3; i >= 0; --i) {
2519 int Idx = SHFIndices[i];
2520
2521 if (Idx == -1)
2522 Idx = 0;
2523
2524 Imm <<= 2;
2525 Imm |= Idx & 0x3;
2526 }
2527
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002528 SDLoc DL(Op);
2529 return DAG.getNode(MipsISD::SHF, DL, ResTy,
2530 DAG.getConstant(Imm, DL, MVT::i32), Op->getOperand(0));
Daniel Sanders26307182013-09-24 14:20:00 +00002531}
2532
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002533/// Determine whether a range fits a regular pattern of values.
2534/// This function accounts for the possibility of jumping over the End iterator.
2535template <typename ValType>
2536static bool
2537fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,
2538 unsigned CheckStride,
2539 typename SmallVectorImpl<ValType>::const_iterator End,
2540 ValType ExpectedIndex, unsigned ExpectedIndexStride) {
2541 auto &I = Begin;
2542
2543 while (I != End) {
2544 if (*I != -1 && *I != ExpectedIndex)
2545 return false;
2546 ExpectedIndex += ExpectedIndexStride;
2547
2548 // Incrementing past End is undefined behaviour so we must increment one
2549 // step at a time and check for End at each step.
2550 for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
2551 ; // Empty loop body.
2552 }
2553 return true;
2554}
2555
2556// Determine whether VECTOR_SHUFFLE is a SPLATI.
2557//
2558// It is a SPLATI when the mask is:
2559// <x, x, x, ...>
2560// where x is any valid index.
2561//
2562// When undef's appear in the mask they are treated as if they were whatever
2563// value is necessary in order to fit the above form.
2564static bool isVECTOR_SHUFFLE_SPLATI(SDValue Op, EVT ResTy,
2565 SmallVector<int, 16> Indices,
2566 SelectionDAG &DAG) {
2567 assert((Indices.size() % 2) == 0);
2568
2569 int SplatIndex = -1;
2570 for (const auto &V : Indices) {
2571 if (V != -1) {
2572 SplatIndex = V;
2573 break;
2574 }
2575 }
2576
2577 return fitsRegularPattern<int>(Indices.begin(), 1, Indices.end(), SplatIndex,
2578 0);
2579}
2580
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002581// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2582//
2583// ILVEV interleaves the even elements from each vector.
2584//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002585// It is possible to lower into ILVEV when the mask consists of two of the
2586// following forms interleaved:
2587// <0, 2, 4, ...>
2588// <n, n+2, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002589// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002590// For example:
2591// <0, 0, 2, 2, 4, 4, ...>
2592// <0, n, 2, n+2, 4, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002593//
2594// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002595// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002596static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2597 SmallVector<int, 16> Indices,
2598 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002599 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002600
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002601 SDValue Wt;
2602 SDValue Ws;
2603 const auto &Begin = Indices.begin();
2604 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002605
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002606 // Check even elements are taken from the even elements of one half or the
2607 // other and pick an operand accordingly.
2608 if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
2609 Wt = Op->getOperand(0);
2610 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 2))
2611 Wt = Op->getOperand(1);
2612 else
2613 return SDValue();
2614
2615 // Check odd elements are taken from the even elements of one half or the
2616 // other and pick an operand accordingly.
2617 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
2618 Ws = Op->getOperand(0);
2619 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 2))
2620 Ws = Op->getOperand(1);
2621 else
2622 return SDValue();
2623
2624 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002625}
2626
2627// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2628//
2629// ILVOD interleaves the odd elements from each vector.
2630//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002631// It is possible to lower into ILVOD when the mask consists of two of the
2632// following forms interleaved:
2633// <1, 3, 5, ...>
2634// <n+1, n+3, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002635// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002636// For example:
2637// <1, 1, 3, 3, 5, 5, ...>
2638// <1, n+1, 3, n+3, 5, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002639//
2640// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002641// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002642static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2643 SmallVector<int, 16> Indices,
2644 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002645 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002646
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002647 SDValue Wt;
2648 SDValue Ws;
2649 const auto &Begin = Indices.begin();
2650 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002651
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002652 // Check even elements are taken from the odd elements of one half or the
2653 // other and pick an operand accordingly.
2654 if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
2655 Wt = Op->getOperand(0);
2656 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + 1, 2))
2657 Wt = Op->getOperand(1);
2658 else
2659 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002660
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002661 // Check odd elements are taken from the odd elements of one half or the
2662 // other and pick an operand accordingly.
2663 if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
2664 Ws = Op->getOperand(0);
2665 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + 1, 2))
2666 Ws = Op->getOperand(1);
2667 else
2668 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002669
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002670 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Wt, Ws);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002671}
2672
2673// Lower VECTOR_SHUFFLE into ILVR (if possible).
2674//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002675// ILVR interleaves consecutive elements from the right (lowest-indexed) half of
2676// each vector.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002677//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002678// It is possible to lower into ILVR when the mask consists of two of the
2679// following forms interleaved:
2680// <0, 1, 2, ...>
2681// <n, n+1, n+2, ...>
2682// where n is the number of elements in the vector.
2683// For example:
2684// <0, 0, 1, 1, 2, 2, ...>
2685// <0, n, 1, n+1, 2, n+2, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002686//
2687// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002688// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002689static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2690 SmallVector<int, 16> Indices,
2691 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002692 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002693
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002694 SDValue Wt;
2695 SDValue Ws;
2696 const auto &Begin = Indices.begin();
2697 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002698
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002699 // Check even elements are taken from the right (lowest-indexed) elements of
2700 // one half or the other and pick an operand accordingly.
2701 if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
2702 Wt = Op->getOperand(0);
2703 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 1))
2704 Wt = Op->getOperand(1);
2705 else
2706 return SDValue();
2707
2708 // Check odd elements are taken from the right (lowest-indexed) elements of
2709 // one half or the other and pick an operand accordingly.
2710 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
2711 Ws = Op->getOperand(0);
2712 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 1))
2713 Ws = Op->getOperand(1);
2714 else
2715 return SDValue();
2716
2717 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Ws, Wt);
2718}
2719
2720// Lower VECTOR_SHUFFLE into ILVL (if possible).
2721//
2722// ILVL interleaves consecutive elements from the left (highest-indexed) half
2723// of each vector.
2724//
2725// It is possible to lower into ILVL when the mask consists of two of the
2726// following forms interleaved:
2727// <x, x+1, x+2, ...>
2728// <n+x, n+x+1, n+x+2, ...>
2729// where n is the number of elements in the vector and x is half n.
2730// For example:
2731// <x, x, x+1, x+1, x+2, x+2, ...>
2732// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2733//
2734// When undef's appear in the mask they are treated as if they were whatever
2735// value is necessary in order to fit the above forms.
2736static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2737 SmallVector<int, 16> Indices,
2738 SelectionDAG &DAG) {
2739 assert((Indices.size() % 2) == 0);
2740
2741 unsigned HalfSize = Indices.size() / 2;
2742 SDValue Wt;
2743 SDValue Ws;
2744 const auto &Begin = Indices.begin();
2745 const auto &End = Indices.end();
2746
2747 // Check even elements are taken from the left (highest-indexed) elements of
2748 // one half or the other and pick an operand accordingly.
2749 if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
2750 Wt = Op->getOperand(0);
2751 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + HalfSize, 1))
2752 Wt = Op->getOperand(1);
2753 else
2754 return SDValue();
2755
2756 // Check odd elements are taken from the left (highest-indexed) elements of
2757 // one half or the other and pick an operand accordingly.
2758 if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
2759 Ws = Op->getOperand(0);
2760 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + HalfSize,
2761 1))
2762 Ws = Op->getOperand(1);
2763 else
2764 return SDValue();
2765
2766 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002767}
2768
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002769// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2770//
2771// PCKEV copies the even elements of each vector into the result vector.
2772//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002773// It is possible to lower into PCKEV when the mask consists of two of the
2774// following forms concatenated:
2775// <0, 2, 4, ...>
2776// <n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002777// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002778// For example:
2779// <0, 2, 4, ..., 0, 2, 4, ...>
2780// <0, 2, 4, ..., n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002781//
2782// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002783// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002784static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2785 SmallVector<int, 16> Indices,
2786 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002787 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002788
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002789 SDValue Wt;
2790 SDValue Ws;
2791 const auto &Begin = Indices.begin();
2792 const auto &Mid = Indices.begin() + Indices.size() / 2;
2793 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002794
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002795 if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
2796 Wt = Op->getOperand(0);
2797 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size(), 2))
2798 Wt = Op->getOperand(1);
2799 else
2800 return SDValue();
2801
2802 if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
2803 Ws = Op->getOperand(0);
2804 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size(), 2))
2805 Ws = Op->getOperand(1);
2806 else
2807 return SDValue();
2808
2809 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002810}
2811
2812// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2813//
2814// PCKOD copies the odd elements of each vector into the result vector.
2815//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002816// It is possible to lower into PCKOD when the mask consists of two of the
2817// following forms concatenated:
2818// <1, 3, 5, ...>
2819// <n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002820// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002821// For example:
2822// <1, 3, 5, ..., 1, 3, 5, ...>
2823// <1, 3, 5, ..., n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002824//
2825// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002826// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002827static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2828 SmallVector<int, 16> Indices,
2829 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002830 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002831
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002832 SDValue Wt;
2833 SDValue Ws;
2834 const auto &Begin = Indices.begin();
2835 const auto &Mid = Indices.begin() + Indices.size() / 2;
2836 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002837
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002838 if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
2839 Wt = Op->getOperand(0);
2840 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size() + 1, 2))
2841 Wt = Op->getOperand(1);
2842 else
2843 return SDValue();
2844
2845 if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
2846 Ws = Op->getOperand(0);
2847 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size() + 1, 2))
2848 Ws = Op->getOperand(1);
2849 else
2850 return SDValue();
2851
2852 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002853}
2854
Daniel Sanderse5087042013-09-24 14:02:15 +00002855// Lower VECTOR_SHUFFLE into VSHF.
2856//
2857// This mostly consists of converting the shuffle indices in Indices into a
2858// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2859// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2860// if the type is v8i16 and all the indices are less than 8 then the second
2861// operand is unused and can be replaced with anything. We choose to replace it
2862// with the used operand since this reduces the number of instructions overall.
2863static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2864 SmallVector<int, 16> Indices,
2865 SelectionDAG &DAG) {
2866 SmallVector<SDValue, 16> Ops;
2867 SDValue Op0;
2868 SDValue Op1;
2869 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2870 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2871 bool Using1stVec = false;
2872 bool Using2ndVec = false;
2873 SDLoc DL(Op);
2874 int ResTyNumElts = ResTy.getVectorNumElements();
2875
2876 for (int i = 0; i < ResTyNumElts; ++i) {
2877 // Idx == -1 means UNDEF
2878 int Idx = Indices[i];
2879
2880 if (0 <= Idx && Idx < ResTyNumElts)
2881 Using1stVec = true;
2882 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2883 Using2ndVec = true;
2884 }
2885
2886 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2887 ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002888 Ops.push_back(DAG.getTargetConstant(*I, DL, MaskEltTy));
Daniel Sanderse5087042013-09-24 14:02:15 +00002889
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002890 SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002891
2892 if (Using1stVec && Using2ndVec) {
2893 Op0 = Op->getOperand(0);
2894 Op1 = Op->getOperand(1);
2895 } else if (Using1stVec)
2896 Op0 = Op1 = Op->getOperand(0);
2897 else if (Using2ndVec)
2898 Op0 = Op1 = Op->getOperand(1);
2899 else
2900 llvm_unreachable("shuffle vector mask references neither vector operand?");
2901
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002902 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2903 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2904 // VSHF concatenates the vectors in a bitwise fashion:
2905 // <0b00, 0b01> + <0b10, 0b11> ->
2906 // 0b0100 + 0b1110 -> 0b01001110
2907 // <0b10, 0b11, 0b00, 0b01>
2908 // We must therefore swap the operands to get the correct result.
2909 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002910}
2911
2912// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2913// indices in the shuffle.
2914SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2915 SelectionDAG &DAG) const {
2916 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2917 EVT ResTy = Op->getValueType(0);
2918
2919 if (!ResTy.is128BitVector())
2920 return SDValue();
2921
2922 int ResTyNumElts = ResTy.getVectorNumElements();
2923 SmallVector<int, 16> Indices;
2924
2925 for (int i = 0; i < ResTyNumElts; ++i)
2926 Indices.push_back(Node->getMaskElt(i));
2927
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002928 // splati.[bhwd] is preferable to the others but is matched from
2929 // MipsISD::VSHF.
2930 if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
2931 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002932 SDValue Result;
2933 if ((Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002934 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002935 if ((Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002936 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002937 if ((Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002938 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002939 if ((Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002940 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002941 if ((Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002942 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002943 if ((Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002944 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002945 if ((Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG)))
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002946 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002947 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2948}
2949
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00002950MachineBasicBlock *
2951MipsSETargetLowering::emitBPOSGE32(MachineInstr &MI,
2952 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002953 // $bb:
2954 // bposge32_pseudo $vr0
2955 // =>
2956 // $bb:
2957 // bposge32 $tbb
2958 // $fbb:
2959 // li $vr2, 0
2960 // b $sink
2961 // $tbb:
2962 // li $vr1, 1
2963 // $sink:
2964 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2965
2966 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002967 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002968 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00002969 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002970 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002971 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002972 MachineFunction *F = BB->getParent();
2973 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2974 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2975 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2976 F->insert(It, FBB);
2977 F->insert(It, TBB);
2978 F->insert(It, Sink);
2979
2980 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002981 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002982 BB->end());
2983 Sink->transferSuccessorsAndUpdatePHIs(BB);
2984
2985 // Add successors.
2986 BB->addSuccessor(FBB);
2987 BB->addSuccessor(TBB);
2988 FBB->addSuccessor(Sink);
2989 TBB->addSuccessor(Sink);
2990
2991 // Insert the real bposge32 instruction to $BB.
2992 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +00002993 // Insert the real bposge32c instruction to $BB.
2994 BuildMI(BB, DL, TII->get(Mips::BPOSGE32C_MMR3)).addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002995
2996 // Fill $FBB.
2997 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2998 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2999 .addReg(Mips::ZERO).addImm(0);
3000 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3001
3002 // Fill $TBB.
3003 unsigned VR1 = RegInfo.createVirtualRegister(RC);
3004 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
3005 .addReg(Mips::ZERO).addImm(1);
3006
3007 // Insert phi function to $Sink.
3008 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003009 MI.getOperand(0).getReg())
3010 .addReg(VR2)
3011 .addMBB(FBB)
3012 .addReg(VR1)
3013 .addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003014
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003015 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003016 return Sink;
3017}
Daniel Sandersce09d072013-08-28 12:14:50 +00003018
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003019MachineBasicBlock *MipsSETargetLowering::emitMSACBranchPseudo(
3020 MachineInstr &MI, MachineBasicBlock *BB, unsigned BranchOp) const {
Daniel Sandersce09d072013-08-28 12:14:50 +00003021 // $bb:
3022 // vany_nonzero $rd, $ws
3023 // =>
3024 // $bb:
3025 // bnz.b $ws, $tbb
3026 // b $fbb
3027 // $fbb:
3028 // li $rd1, 0
3029 // b $sink
3030 // $tbb:
3031 // li $rd2, 1
3032 // $sink:
3033 // $rd = phi($rd1, $fbb, $rd2, $tbb)
3034
3035 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003036 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersce09d072013-08-28 12:14:50 +00003037 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003038 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersce09d072013-08-28 12:14:50 +00003039 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003040 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00003041 MachineFunction *F = BB->getParent();
3042 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3043 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3044 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3045 F->insert(It, FBB);
3046 F->insert(It, TBB);
3047 F->insert(It, Sink);
3048
3049 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003050 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00003051 BB->end());
3052 Sink->transferSuccessorsAndUpdatePHIs(BB);
3053
3054 // Add successors.
3055 BB->addSuccessor(FBB);
3056 BB->addSuccessor(TBB);
3057 FBB->addSuccessor(Sink);
3058 TBB->addSuccessor(Sink);
3059
3060 // Insert the real bnz.b instruction to $BB.
3061 BuildMI(BB, DL, TII->get(BranchOp))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003062 .addReg(MI.getOperand(1).getReg())
3063 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003064
3065 // Fill $FBB.
3066 unsigned RD1 = RegInfo.createVirtualRegister(RC);
3067 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
3068 .addReg(Mips::ZERO).addImm(0);
3069 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3070
3071 // Fill $TBB.
3072 unsigned RD2 = RegInfo.createVirtualRegister(RC);
3073 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
3074 .addReg(Mips::ZERO).addImm(1);
3075
3076 // Insert phi function to $Sink.
3077 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003078 MI.getOperand(0).getReg())
3079 .addReg(RD1)
3080 .addMBB(FBB)
3081 .addReg(RD2)
3082 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003083
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003084 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersce09d072013-08-28 12:14:50 +00003085 return Sink;
3086}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003087
3088// Emit the COPY_FW pseudo instruction.
3089//
3090// copy_fw_pseudo $fd, $ws, n
3091// =>
3092// copy_u_w $rt, $ws, $n
3093// mtc1 $rt, $fd
3094//
3095// When n is zero, the equivalent operation can be performed with (potentially)
3096// zero instructions due to register overlaps. This optimization is never valid
3097// 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 +00003098MachineBasicBlock *
3099MipsSETargetLowering::emitCOPY_FW(MachineInstr &MI,
3100 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003101 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003102 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003103 DebugLoc DL = MI.getDebugLoc();
3104 unsigned Fd = MI.getOperand(0).getReg();
3105 unsigned Ws = MI.getOperand(1).getReg();
3106 unsigned Lane = MI.getOperand(2).getImm();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003107
Daniel Sandersafe27c72015-02-23 17:22:16 +00003108 if (Lane == 0) {
3109 unsigned Wt = Ws;
3110 if (!Subtarget.useOddSPReg()) {
3111 // We must copy to an even-numbered MSA register so that the
3112 // single-precision sub-register is also guaranteed to be even-numbered.
3113 Wt = RegInfo.createVirtualRegister(&Mips::MSA128WEvensRegClass);
3114
3115 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Wt).addReg(Ws);
3116 }
3117
3118 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3119 } else {
3120 unsigned Wt = RegInfo.createVirtualRegister(
3121 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3122 &Mips::MSA128WEvensRegClass);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003123
Daniel Sandersd9207702014-03-04 13:54:30 +00003124 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003125 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3126 }
3127
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003128 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003129 return BB;
3130}
3131
3132// Emit the COPY_FD pseudo instruction.
3133//
3134// copy_fd_pseudo $fd, $ws, n
3135// =>
3136// splati.d $wt, $ws, $n
3137// copy $fd, $wt:sub_64
3138//
3139// When n is zero, the equivalent operation can be performed with (potentially)
3140// zero instructions due to register overlaps. This optimization is always
3141// valid because FR=1 mode which is the only supported mode in MSA.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003142MachineBasicBlock *
3143MipsSETargetLowering::emitCOPY_FD(MachineInstr &MI,
3144 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003145 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003146
Eric Christopher96e72c62015-01-29 23:27:36 +00003147 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003148 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003149 unsigned Fd = MI.getOperand(0).getReg();
3150 unsigned Ws = MI.getOperand(1).getReg();
3151 unsigned Lane = MI.getOperand(2).getImm() * 2;
3152 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003153
3154 if (Lane == 0)
3155 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
3156 else {
3157 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3158
3159 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
3160 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
3161 }
3162
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003163 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003164 return BB;
3165}
Daniel Sandersa5150702013-09-27 12:31:32 +00003166
3167// Emit the INSERT_FW pseudo instruction.
3168//
3169// insert_fw_pseudo $wd, $wd_in, $n, $fs
3170// =>
3171// subreg_to_reg $wt:sub_lo, $fs
3172// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003173MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003174MipsSETargetLowering::emitINSERT_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003175 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003176 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003177 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003178 DebugLoc DL = MI.getDebugLoc();
3179 unsigned Wd = MI.getOperand(0).getReg();
3180 unsigned Wd_in = MI.getOperand(1).getReg();
3181 unsigned Lane = MI.getOperand(2).getImm();
3182 unsigned Fs = MI.getOperand(3).getReg();
Daniel Sandersafe27c72015-02-23 17:22:16 +00003183 unsigned Wt = RegInfo.createVirtualRegister(
3184 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3185 &Mips::MSA128WEvensRegClass);
Daniel Sandersa5150702013-09-27 12:31:32 +00003186
3187 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003188 .addImm(0)
3189 .addReg(Fs)
3190 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00003191 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003192 .addReg(Wd_in)
3193 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003194 .addReg(Wt)
3195 .addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003196
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003197 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003198 return BB;
3199}
3200
3201// Emit the INSERT_FD pseudo instruction.
3202//
3203// insert_fd_pseudo $wd, $fs, n
3204// =>
3205// subreg_to_reg $wt:sub_64, $fs
3206// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003207MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003208MipsSETargetLowering::emitINSERT_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003209 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003210 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00003211
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 Sandersa5150702013-09-27 12:31:32 +00003219 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3220
3221 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003222 .addImm(0)
3223 .addReg(Fs)
3224 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00003225 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003226 .addReg(Wd_in)
3227 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003228 .addReg(Wt)
3229 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003230
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003231 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003232 return BB;
3233}
3234
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003235// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3236//
3237// For integer:
3238// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3239// =>
3240// (SLL $lanetmp1, $lane, <log2size)
3241// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3242// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3243// (NEG $lanetmp2, $lanetmp1)
3244// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3245//
3246// For floating point:
3247// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3248// =>
3249// (SUBREG_TO_REG $wt, $fs, <subreg>)
3250// (SLL $lanetmp1, $lane, <log2size)
3251// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3252// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3253// (NEG $lanetmp2, $lanetmp1)
3254// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003255MachineBasicBlock *MipsSETargetLowering::emitINSERT_DF_VIDX(
3256 MachineInstr &MI, MachineBasicBlock *BB, unsigned EltSizeInBytes,
3257 bool IsFP) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003258 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003259 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003260 DebugLoc DL = MI.getDebugLoc();
3261 unsigned Wd = MI.getOperand(0).getReg();
3262 unsigned SrcVecReg = MI.getOperand(1).getReg();
3263 unsigned LaneReg = MI.getOperand(2).getReg();
3264 unsigned SrcValReg = MI.getOperand(3).getReg();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003265
3266 const TargetRegisterClass *VecRC = nullptr;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003267 // FIXME: This should be true for N32 too.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003268 const TargetRegisterClass *GPRRC =
Daniel Sanders4160c802015-05-05 08:48:35 +00003269 Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003270 unsigned SubRegIdx = Subtarget.isABI_N64() ? Mips::sub_32 : 0;
3271 unsigned ShiftOp = Subtarget.isABI_N64() ? Mips::DSLL : Mips::SLL;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003272 unsigned EltLog2Size;
3273 unsigned InsertOp = 0;
3274 unsigned InsveOp = 0;
3275 switch (EltSizeInBytes) {
3276 default:
3277 llvm_unreachable("Unexpected size");
3278 case 1:
3279 EltLog2Size = 0;
3280 InsertOp = Mips::INSERT_B;
3281 InsveOp = Mips::INSVE_B;
3282 VecRC = &Mips::MSA128BRegClass;
3283 break;
3284 case 2:
3285 EltLog2Size = 1;
3286 InsertOp = Mips::INSERT_H;
3287 InsveOp = Mips::INSVE_H;
3288 VecRC = &Mips::MSA128HRegClass;
3289 break;
3290 case 4:
3291 EltLog2Size = 2;
3292 InsertOp = Mips::INSERT_W;
3293 InsveOp = Mips::INSVE_W;
3294 VecRC = &Mips::MSA128WRegClass;
3295 break;
3296 case 8:
3297 EltLog2Size = 3;
3298 InsertOp = Mips::INSERT_D;
3299 InsveOp = Mips::INSVE_D;
3300 VecRC = &Mips::MSA128DRegClass;
3301 break;
3302 }
3303
3304 if (IsFP) {
3305 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3306 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3307 .addImm(0)
3308 .addReg(SrcValReg)
3309 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3310 SrcValReg = Wt;
3311 }
3312
3313 // Convert the lane index into a byte index
3314 if (EltSizeInBytes != 1) {
3315 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003316 BuildMI(*BB, MI, DL, TII->get(ShiftOp), LaneTmp1)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003317 .addReg(LaneReg)
3318 .addImm(EltLog2Size);
3319 LaneReg = LaneTmp1;
3320 }
3321
3322 // Rotate bytes around so that the desired lane is element zero
3323 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3324 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3325 .addReg(SrcVecReg)
3326 .addReg(SrcVecReg)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003327 .addReg(LaneReg, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003328
3329 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3330 if (IsFP) {
3331 // Use insve.df to insert to element zero
3332 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3333 .addReg(WdTmp1)
3334 .addImm(0)
3335 .addReg(SrcValReg)
3336 .addImm(0);
3337 } else {
3338 // Use insert.df to insert to element zero
3339 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3340 .addReg(WdTmp1)
3341 .addReg(SrcValReg)
3342 .addImm(0);
3343 }
3344
3345 // Rotate elements the rest of the way for a full rotation.
3346 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3347 // the lane index to do this.
3348 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sanders4160c802015-05-05 08:48:35 +00003349 BuildMI(*BB, MI, DL, TII->get(Subtarget.isABI_N64() ? Mips::DSUB : Mips::SUB),
3350 LaneTmp2)
3351 .addReg(Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003352 .addReg(LaneReg);
3353 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3354 .addReg(WdTmp2)
3355 .addReg(WdTmp2)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003356 .addReg(LaneTmp2, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003357
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003358 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003359 return BB;
3360}
3361
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003362// Emit the FILL_FW pseudo instruction.
3363//
3364// fill_fw_pseudo $wd, $fs
3365// =>
3366// implicit_def $wt1
3367// insert_subreg $wt2:subreg_lo, $wt1, $fs
3368// splati.w $wd, $wt2[0]
3369MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003370MipsSETargetLowering::emitFILL_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003371 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003372 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003373 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003374 DebugLoc DL = MI.getDebugLoc();
3375 unsigned Wd = MI.getOperand(0).getReg();
3376 unsigned Fs = MI.getOperand(1).getReg();
Simon Dardis0e9e2372017-01-10 15:53:10 +00003377 unsigned Wt1 = RegInfo.createVirtualRegister(
3378 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3379 : &Mips::MSA128WEvensRegClass);
3380 unsigned Wt2 = RegInfo.createVirtualRegister(
3381 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3382 : &Mips::MSA128WEvensRegClass);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003383
3384 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3385 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3386 .addReg(Wt1)
3387 .addReg(Fs)
3388 .addImm(Mips::sub_lo);
3389 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3390
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003391 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003392 return BB;
3393}
3394
3395// Emit the FILL_FD pseudo instruction.
3396//
3397// fill_fd_pseudo $wd, $fs
3398// =>
3399// implicit_def $wt1
3400// insert_subreg $wt2:subreg_64, $wt1, $fs
3401// splati.d $wd, $wt2[0]
3402MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003403MipsSETargetLowering::emitFILL_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003404 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003405 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003406
Eric Christopher96e72c62015-01-29 23:27:36 +00003407 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003408 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003409 DebugLoc DL = MI.getDebugLoc();
3410 unsigned Wd = MI.getOperand(0).getReg();
3411 unsigned Fs = MI.getOperand(1).getReg();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003412 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3413 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3414
3415 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3416 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3417 .addReg(Wt1)
3418 .addReg(Fs)
3419 .addImm(Mips::sub_64);
3420 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003421
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003422 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003423 return BB;
3424}
Daniel Sandersa9521602013-10-23 10:36:52 +00003425
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003426// Emit the ST_F16_PSEDUO instruction to store a f16 value from an MSA
3427// register.
3428//
3429// STF16 MSA128F16:$wd, mem_simm10:$addr
3430// =>
3431// copy_u.h $rtemp,$wd[0]
3432// sh $rtemp, $addr
3433//
3434// Safety: We can't use st.h & co as they would over write the memory after
3435// the destination. It would require half floats be allocated 16 bytes(!) of
3436// space.
3437MachineBasicBlock *
3438MipsSETargetLowering::emitST_F16_PSEUDO(MachineInstr &MI,
3439 MachineBasicBlock *BB) const {
3440
3441 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3442 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3443 DebugLoc DL = MI.getDebugLoc();
3444 unsigned Ws = MI.getOperand(0).getReg();
3445 unsigned Rt = MI.getOperand(1).getReg();
3446 const MachineMemOperand &MMO = **MI.memoperands_begin();
3447 unsigned Imm = MMO.getOffset();
3448
3449 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3450 // spill and reload can expand as a GPR64 operand. Examine the
3451 // operand in detail and default to ABI.
3452 const TargetRegisterClass *RC =
3453 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3454 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3455 : &Mips::GPR64RegClass);
3456 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003457 unsigned Rs = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003458
3459 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_U_H), Rs).addReg(Ws).addImm(0);
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003460 if(!UsingMips32) {
3461 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR64RegClass);
3462 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Tmp)
3463 .addImm(0)
3464 .addReg(Rs)
3465 .addImm(Mips::sub_32);
3466 Rs = Tmp;
3467 }
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003468 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::SH : Mips::SH64))
3469 .addReg(Rs)
3470 .addReg(Rt)
3471 .addImm(Imm)
3472 .addMemOperand(BB->getParent()->getMachineMemOperand(
3473 &MMO, MMO.getOffset(), MMO.getSize()));
3474
3475 MI.eraseFromParent();
3476 return BB;
3477}
3478
3479// Emit the LD_F16_PSEDUO instruction to load a f16 value into an MSA register.
3480//
3481// LD_F16 MSA128F16:$wd, mem_simm10:$addr
3482// =>
3483// lh $rtemp, $addr
3484// fill.h $wd, $rtemp
3485//
3486// Safety: We can't use ld.h & co as they over-read from the source.
3487// Additionally, if the address is not modulo 16, 2 cases can occur:
3488// a) Segmentation fault as the load instruction reads from a memory page
3489// memory it's not supposed to.
3490// b) The load crosses an implementation specific boundary, requiring OS
3491// intervention.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003492MachineBasicBlock *
3493MipsSETargetLowering::emitLD_F16_PSEUDO(MachineInstr &MI,
3494 MachineBasicBlock *BB) const {
3495
3496 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3497 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3498 DebugLoc DL = MI.getDebugLoc();
3499 unsigned Wd = MI.getOperand(0).getReg();
3500
3501 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3502 // spill and reload can expand as a GPR64 operand. Examine the
3503 // operand in detail and default to ABI.
3504 const TargetRegisterClass *RC =
3505 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3506 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3507 : &Mips::GPR64RegClass);
3508
3509 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
3510 unsigned Rt = RegInfo.createVirtualRegister(RC);
3511
3512 MachineInstrBuilder MIB =
3513 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::LH : Mips::LH64), Rt);
3514 for (unsigned i = 1; i < MI.getNumOperands(); i++)
Diana Picus116bbab2017-01-13 09:58:52 +00003515 MIB.add(MI.getOperand(i));
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003516
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003517 if(!UsingMips32) {
3518 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
3519 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Tmp).addReg(Rt, 0, Mips::sub_32);
3520 Rt = Tmp;
3521 }
3522
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003523 BuildMI(*BB, MI, DL, TII->get(Mips::FILL_H), Wd).addReg(Rt);
3524
3525 MI.eraseFromParent();
3526 return BB;
3527}
3528
3529// Emit the FPROUND_PSEUDO instruction.
3530//
3531// Round an FGR64Opnd, FGR32Opnd to an f16.
3532//
3533// Safety: Cycle the operand through the GPRs so the result always ends up
3534// the correct MSA register.
3535//
3536// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fs
3537// / FGR64Opnd:$Fs and MSA128F16:$Wd to the same physical register
3538// (which they can be, as the MSA registers are defined to alias the
3539// FPU's 64 bit and 32 bit registers) the result can be accessed using
3540// the correct register class. That requires operands be tie-able across
3541// register classes which have a sub/super register class relationship.
3542//
3543// For FPG32Opnd:
3544//
3545// FPROUND MSA128F16:$wd, FGR32Opnd:$fs
3546// =>
3547// mfc1 $rtemp, $fs
3548// fill.w $rtemp, $wtemp
3549// fexdo.w $wd, $wtemp, $wtemp
3550//
3551// For FPG64Opnd on mips32r2+:
3552//
3553// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3554// =>
3555// mfc1 $rtemp, $fs
3556// fill.w $rtemp, $wtemp
3557// mfhc1 $rtemp2, $fs
3558// insert.w $wtemp[1], $rtemp2
3559// insert.w $wtemp[3], $rtemp2
3560// fexdo.w $wtemp2, $wtemp, $wtemp
3561// fexdo.h $wd, $temp2, $temp2
3562//
3563// For FGR64Opnd on mips64r2+:
3564//
3565// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3566// =>
3567// dmfc1 $rtemp, $fs
3568// fill.d $rtemp, $wtemp
3569// fexdo.w $wtemp2, $wtemp, $wtemp
3570// fexdo.h $wd, $wtemp2, $wtemp2
3571//
3572// Safety note: As $wtemp is UNDEF, we may provoke a spurious exception if the
3573// undef bits are "just right" and the exception enable bits are
3574// set. By using fill.w to replicate $fs into all elements over
3575// insert.w for one element, we avoid that potiential case. If
3576// fexdo.[hw] causes an exception in, the exception is valid and it
3577// occurs for all elements.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003578MachineBasicBlock *
3579MipsSETargetLowering::emitFPROUND_PSEUDO(MachineInstr &MI,
3580 MachineBasicBlock *BB,
3581 bool IsFGR64) const {
3582
3583 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3584 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3585 // it.
3586 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3587
3588 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003589 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003590
3591 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3592 DebugLoc DL = MI.getDebugLoc();
3593 unsigned Wd = MI.getOperand(0).getReg();
3594 unsigned Fs = MI.getOperand(1).getReg();
3595
3596 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3597 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3598 const TargetRegisterClass *GPRRC =
3599 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003600 unsigned MFC1Opc = IsFGR64onMips64
3601 ? Mips::DMFC1
3602 : (IsFGR64onMips32 ? Mips::MFC1_D64 : Mips::MFC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003603 unsigned FILLOpc = IsFGR64onMips64 ? Mips::FILL_D : Mips::FILL_W;
3604
3605 // Perform the register class copy as mentioned above.
3606 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3607 BuildMI(*BB, MI, DL, TII->get(MFC1Opc), Rtemp).addReg(Fs);
3608 BuildMI(*BB, MI, DL, TII->get(FILLOpc), Wtemp).addReg(Rtemp);
3609 unsigned WPHI = Wtemp;
3610
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003611 if (IsFGR64onMips32) {
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003612 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3613 BuildMI(*BB, MI, DL, TII->get(Mips::MFHC1_D64), Rtemp2).addReg(Fs);
3614 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3615 unsigned Wtemp3 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3616 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp2)
3617 .addReg(Wtemp)
3618 .addReg(Rtemp2)
3619 .addImm(1);
3620 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp3)
3621 .addReg(Wtemp2)
3622 .addReg(Rtemp2)
3623 .addImm(3);
3624 WPHI = Wtemp3;
3625 }
3626
3627 if (IsFGR64) {
3628 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3629 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_W), Wtemp2)
3630 .addReg(WPHI)
3631 .addReg(WPHI);
3632 WPHI = Wtemp2;
3633 }
3634
3635 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_H), Wd).addReg(WPHI).addReg(WPHI);
3636
3637 MI.eraseFromParent();
3638 return BB;
3639}
3640
3641// Emit the FPEXTEND_PSEUDO instruction.
3642//
3643// Expand an f16 to either a FGR32Opnd or FGR64Opnd.
3644//
3645// Safety: Cycle the result through the GPRs so the result always ends up
3646// the correct floating point register.
3647//
3648// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fd
3649// / FGR64Opnd:$Fd and MSA128F16:$Ws to the same physical register
3650// (which they can be, as the MSA registers are defined to alias the
3651// FPU's 64 bit and 32 bit registers) the result can be accessed using
3652// the correct register class. That requires operands be tie-able across
3653// register classes which have a sub/super register class relationship. I
3654// haven't checked.
3655//
3656// For FGR32Opnd:
3657//
3658// FPEXTEND FGR32Opnd:$fd, MSA128F16:$ws
3659// =>
3660// fexupr.w $wtemp, $ws
3661// copy_s.w $rtemp, $ws[0]
3662// mtc1 $rtemp, $fd
3663//
3664// For FGR64Opnd on Mips64:
3665//
3666// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3667// =>
3668// fexupr.w $wtemp, $ws
3669// fexupr.d $wtemp2, $wtemp
3670// copy_s.d $rtemp, $wtemp2s[0]
3671// dmtc1 $rtemp, $fd
3672//
3673// For FGR64Opnd on Mips32:
3674//
3675// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3676// =>
3677// fexupr.w $wtemp, $ws
3678// fexupr.d $wtemp2, $wtemp
3679// copy_s.w $rtemp, $wtemp2[0]
3680// mtc1 $rtemp, $ftemp
3681// copy_s.w $rtemp2, $wtemp2[1]
3682// $fd = mthc1 $rtemp2, $ftemp
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003683MachineBasicBlock *
3684MipsSETargetLowering::emitFPEXTEND_PSEUDO(MachineInstr &MI,
3685 MachineBasicBlock *BB,
3686 bool IsFGR64) const {
3687
3688 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3689 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3690 // it.
3691 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3692
3693 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
3694 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
3695
3696 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3697 DebugLoc DL = MI.getDebugLoc();
3698 unsigned Fd = MI.getOperand(0).getReg();
3699 unsigned Ws = MI.getOperand(1).getReg();
3700
3701 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3702 const TargetRegisterClass *GPRRC =
3703 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003704 unsigned MTC1Opc = IsFGR64onMips64
3705 ? Mips::DMTC1
3706 : (IsFGR64onMips32 ? Mips::MTC1_D64 : Mips::MTC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003707 unsigned COPYOpc = IsFGR64onMips64 ? Mips::COPY_S_D : Mips::COPY_S_W;
3708
3709 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3710 unsigned WPHI = Wtemp;
3711
3712 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_W), Wtemp).addReg(Ws);
3713 if (IsFGR64) {
3714 WPHI = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3715 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_D), WPHI).addReg(Wtemp);
3716 }
3717
3718 // Perform the safety regclass copy mentioned above.
3719 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3720 unsigned FPRPHI = IsFGR64onMips32
3721 ? RegInfo.createVirtualRegister(&Mips::FGR64RegClass)
3722 : Fd;
3723 BuildMI(*BB, MI, DL, TII->get(COPYOpc), Rtemp).addReg(WPHI).addImm(0);
3724 BuildMI(*BB, MI, DL, TII->get(MTC1Opc), FPRPHI).addReg(Rtemp);
3725
3726 if (IsFGR64onMips32) {
3727 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3728 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_S_W), Rtemp2)
3729 .addReg(WPHI)
3730 .addImm(1);
3731 BuildMI(*BB, MI, DL, TII->get(Mips::MTHC1_D64), Fd)
3732 .addReg(FPRPHI)
3733 .addReg(Rtemp2);
3734 }
3735
3736 MI.eraseFromParent();
3737 return BB;
3738}
3739
Daniel Sandersa9521602013-10-23 10:36:52 +00003740// Emit the FEXP2_W_1 pseudo instructions.
3741//
3742// fexp2_w_1_pseudo $wd, $wt
3743// =>
3744// ldi.w $ws, 1
3745// fexp2.w $wd, $ws, $wt
3746MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003747MipsSETargetLowering::emitFEXP2_W_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003748 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003749 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003750 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3751 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3752 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3753 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003754 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003755
3756 // Splat 1.0 into a vector
3757 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3758 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3759
3760 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003761 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003762 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003763 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003764
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003765 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003766 return BB;
3767}
3768
3769// Emit the FEXP2_D_1 pseudo instructions.
3770//
3771// fexp2_d_1_pseudo $wd, $wt
3772// =>
3773// ldi.d $ws, 1
3774// fexp2.d $wd, $ws, $wt
3775MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003776MipsSETargetLowering::emitFEXP2_D_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003777 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003778 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003779 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3780 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3781 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3782 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003783 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003784
3785 // Splat 1.0 into a vector
3786 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3787 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3788
3789 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003790 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003791 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003792 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003793
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003794 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003795 return BB;
3796}