blob: 9698a4979e58fa05032e53f302f358386759bf9c [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Akira Hatanaka96ca1822013-03-13 00:54:29 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Subclass of MipsTargetLowering specialized for mips32/64.
10//
11//===----------------------------------------------------------------------===//
Eugene Zelenko79220eae2017-08-03 22:12:30 +000012
Akira Hatanaka96ca1822013-03-13 00:54:29 +000013#include "MipsSEISelLowering.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000014#include "MipsMachineFunction.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000015#include "MipsRegisterInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000016#include "MipsSubtarget.h"
Simon Dardis548a53f2017-01-10 16:40:57 +000017#include "llvm/ADT/APInt.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000018#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/Triple.h"
22#include "llvm/CodeGen/CallingConvLower.h"
23#include "llvm/CodeGen/ISDOpcodes.h"
24#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstr.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000027#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000030#include "llvm/CodeGen/SelectionDAG.h"
31#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000032#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000033#include "llvm/CodeGen/TargetSubtargetInfo.h"
Craig Topper2fa14362018-03-29 17:21:10 +000034#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000035#include "llvm/IR/DebugLoc.h"
Akira Hatanakaa6bbde52013-04-13 02:13:30 +000036#include "llvm/IR/Intrinsics.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000037#include "llvm/Support/Casting.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000038#include "llvm/Support/CommandLine.h"
Daniel Sanders62aeab82013-10-30 13:31:27 +000039#include "llvm/Support/Debug.h"
Simon Dardis548a53f2017-01-10 16:40:57 +000040#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000041#include "llvm/Support/MachineValueType.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000042#include "llvm/Support/MathExtras.h"
Hans Wennborg3e9b1c12013-10-30 16:10:10 +000043#include "llvm/Support/raw_ostream.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000044#include <algorithm>
45#include <cassert>
46#include <cstdint>
47#include <iterator>
48#include <utility>
Akira Hatanaka96ca1822013-03-13 00:54:29 +000049
50using namespace llvm;
51
Chandler Carruth84e68b22014-04-22 02:41:26 +000052#define DEBUG_TYPE "mips-isel"
53
Akira Hatanaka96ca1822013-03-13 00:54:29 +000054static cl::opt<bool>
Simon Dardis57f4ae42016-08-04 09:17:07 +000055UseMipsTailCalls("mips-tail-calls", cl::Hidden,
Simon Dardisd2ed8ab2016-09-27 13:15:54 +000056 cl::desc("MIPS: permit tail calls."), cl::init(false));
Akira Hatanaka96ca1822013-03-13 00:54:29 +000057
Akira Hatanaka63791212013-09-07 00:52:30 +000058static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
59 cl::desc("Expand double precision loads and "
60 "stores to their single precision "
61 "counterparts"));
62
Eric Christopherb1526602014-09-19 23:30:42 +000063MipsSETargetLowering::MipsSETargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +000064 const MipsSubtarget &STI)
65 : MipsTargetLowering(TM, STI) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000066 // Set up the register classes
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000067 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000068
Eric Christopher1c29a652014-07-18 22:55:25 +000069 if (Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000070 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000071
Eric Christopher1c29a652014-07-18 22:55:25 +000072 if (Subtarget.hasDSP() || Subtarget.hasMSA()) {
Daniel Sanders36c671e2013-09-27 09:44:59 +000073 // Expand all truncating stores and extending loads.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000074 for (MVT VT0 : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000075 for (MVT VT1 : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000076 setTruncStoreAction(VT0, VT1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000077 setLoadExtAction(ISD::SEXTLOAD, VT0, VT1, Expand);
78 setLoadExtAction(ISD::ZEXTLOAD, VT0, VT1, Expand);
79 setLoadExtAction(ISD::EXTLOAD, VT0, VT1, Expand);
80 }
Daniel Sanders36c671e2013-09-27 09:44:59 +000081 }
82 }
83
Eric Christopher1c29a652014-07-18 22:55:25 +000084 if (Subtarget.hasDSP()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000085 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
86
87 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
Akira Hatanaka654655f2013-08-14 00:53:38 +000088 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000089
90 // Expand all builtin opcodes.
91 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
92 setOperationAction(Opc, VecTys[i], Expand);
93
Akira Hatanaka2f088222013-04-13 00:55:41 +000094 setOperationAction(ISD::ADD, VecTys[i], Legal);
95 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000096 setOperationAction(ISD::LOAD, VecTys[i], Legal);
97 setOperationAction(ISD::STORE, VecTys[i], Legal);
98 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
99 }
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000100
101 setTargetDAGCombine(ISD::SHL);
102 setTargetDAGCombine(ISD::SRA);
103 setTargetDAGCombine(ISD::SRL);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000104 setTargetDAGCombine(ISD::SETCC);
105 setTargetDAGCombine(ISD::VSELECT);
Amaury Secheta79b6b32018-06-05 22:13:56 +0000106
107 if (Subtarget.hasMips32r2()) {
108 setOperationAction(ISD::ADDC, MVT::i32, Legal);
109 setOperationAction(ISD::ADDE, MVT::i32, Legal);
110 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000111 }
112
Eric Christopher1c29a652014-07-18 22:55:25 +0000113 if (Subtarget.hasDSPR2())
Akira Hatanaka2f088222013-04-13 00:55:41 +0000114 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
115
Eric Christopher1c29a652014-07-18 22:55:25 +0000116 if (Subtarget.hasMSA()) {
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000117 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
118 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
119 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
120 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
121 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
122 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
123 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000124
Simon Dardis0e2ee3b2016-11-18 16:17:44 +0000125 // f16 is a storage-only type, always promote it to f32.
126 addRegisterClass(MVT::f16, &Mips::MSA128HRegClass);
127 setOperationAction(ISD::SETCC, MVT::f16, Promote);
128 setOperationAction(ISD::BR_CC, MVT::f16, Promote);
129 setOperationAction(ISD::SELECT_CC, MVT::f16, Promote);
130 setOperationAction(ISD::SELECT, MVT::f16, Promote);
131 setOperationAction(ISD::FADD, MVT::f16, Promote);
132 setOperationAction(ISD::FSUB, MVT::f16, Promote);
133 setOperationAction(ISD::FMUL, MVT::f16, Promote);
134 setOperationAction(ISD::FDIV, MVT::f16, Promote);
135 setOperationAction(ISD::FREM, MVT::f16, Promote);
136 setOperationAction(ISD::FMA, MVT::f16, Promote);
137 setOperationAction(ISD::FNEG, MVT::f16, Promote);
138 setOperationAction(ISD::FABS, MVT::f16, Promote);
139 setOperationAction(ISD::FCEIL, MVT::f16, Promote);
140 setOperationAction(ISD::FCOPYSIGN, MVT::f16, Promote);
141 setOperationAction(ISD::FCOS, MVT::f16, Promote);
142 setOperationAction(ISD::FP_EXTEND, MVT::f16, Promote);
143 setOperationAction(ISD::FFLOOR, MVT::f16, Promote);
144 setOperationAction(ISD::FNEARBYINT, MVT::f16, Promote);
145 setOperationAction(ISD::FPOW, MVT::f16, Promote);
146 setOperationAction(ISD::FPOWI, MVT::f16, Promote);
147 setOperationAction(ISD::FRINT, MVT::f16, Promote);
148 setOperationAction(ISD::FSIN, MVT::f16, Promote);
149 setOperationAction(ISD::FSINCOS, MVT::f16, Promote);
150 setOperationAction(ISD::FSQRT, MVT::f16, Promote);
151 setOperationAction(ISD::FEXP, MVT::f16, Promote);
152 setOperationAction(ISD::FEXP2, MVT::f16, Promote);
153 setOperationAction(ISD::FLOG, MVT::f16, Promote);
154 setOperationAction(ISD::FLOG2, MVT::f16, Promote);
155 setOperationAction(ISD::FLOG10, MVT::f16, Promote);
156 setOperationAction(ISD::FROUND, MVT::f16, Promote);
157 setOperationAction(ISD::FTRUNC, MVT::f16, Promote);
158 setOperationAction(ISD::FMINNUM, MVT::f16, Promote);
159 setOperationAction(ISD::FMAXNUM, MVT::f16, Promote);
Thomas Lively30f1d692018-10-24 22:49:55 +0000160 setOperationAction(ISD::FMINIMUM, MVT::f16, Promote);
161 setOperationAction(ISD::FMAXIMUM, MVT::f16, Promote);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +0000162
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000163 setTargetDAGCombine(ISD::AND);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000164 setTargetDAGCombine(ISD::OR);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000165 setTargetDAGCombine(ISD::SRA);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000166 setTargetDAGCombine(ISD::VSELECT);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000167 setTargetDAGCombine(ISD::XOR);
Jack Carter3a2c2d42013-08-13 20:54:07 +0000168 }
169
Eric Christophere8ae3e32015-05-07 23:10:21 +0000170 if (!Subtarget.useSoftFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000171 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
172
173 // When dealing with single precision only, use libcalls
Eric Christopher1c29a652014-07-18 22:55:25 +0000174 if (!Subtarget.isSingleFloat()) {
175 if (Subtarget.isFP64bit())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000176 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
177 else
178 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
179 }
180 }
181
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000182 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
183 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
184 setOperationAction(ISD::MULHS, MVT::i32, Custom);
185 setOperationAction(ISD::MULHU, MVT::i32, Custom);
186
Eric Christopher1c29a652014-07-18 22:55:25 +0000187 if (Subtarget.hasCnMips())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000188 setOperationAction(ISD::MUL, MVT::i64, Legal);
Eric Christopher1c29a652014-07-18 22:55:25 +0000189 else if (Subtarget.isGP64bit())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000190 setOperationAction(ISD::MUL, MVT::i64, Custom);
191
Eric Christopher1c29a652014-07-18 22:55:25 +0000192 if (Subtarget.isGP64bit()) {
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000193 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Custom);
194 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000195 setOperationAction(ISD::MULHS, MVT::i64, Custom);
196 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Jan Vesely54468a5a2014-10-17 14:45:28 +0000197 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
198 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000199 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000200
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000201 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
202 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
203
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000204 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
205 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000206 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
207 setOperationAction(ISD::LOAD, MVT::i32, Custom);
208 setOperationAction(ISD::STORE, MVT::i32, Custom);
209
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000210 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000211
Daniel Sandersce09d072013-08-28 12:14:50 +0000212 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000213 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
214 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
215
Akira Hatanaka63791212013-09-07 00:52:30 +0000216 if (NoDPLoadStore) {
217 setOperationAction(ISD::LOAD, MVT::f64, Custom);
218 setOperationAction(ISD::STORE, MVT::f64, Custom);
219 }
220
Eric Christopher1c29a652014-07-18 22:55:25 +0000221 if (Subtarget.hasMips32r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000222 // MIPS32r6 replaces the accumulator-based multiplies with a three register
223 // instruction
Daniel Sanders826f8b32014-06-12 10:54:16 +0000224 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
225 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000226 setOperationAction(ISD::MUL, MVT::i32, Legal);
227 setOperationAction(ISD::MULHS, MVT::i32, Legal);
228 setOperationAction(ISD::MULHU, MVT::i32, Legal);
229
230 // MIPS32r6 replaces the accumulator-based division/remainder with separate
231 // three register division and remainder instructions.
232 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
233 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
234 setOperationAction(ISD::SDIV, MVT::i32, Legal);
235 setOperationAction(ISD::UDIV, MVT::i32, Legal);
236 setOperationAction(ISD::SREM, MVT::i32, Legal);
237 setOperationAction(ISD::UREM, MVT::i32, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000238
239 // MIPS32r6 replaces conditional moves with an equivalent that removes the
240 // need for three GPR read ports.
241 setOperationAction(ISD::SETCC, MVT::i32, Legal);
242 setOperationAction(ISD::SELECT, MVT::i32, Legal);
243 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
244
245 setOperationAction(ISD::SETCC, MVT::f32, Legal);
246 setOperationAction(ISD::SELECT, MVT::f32, Legal);
247 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
248
Eric Christopher1c29a652014-07-18 22:55:25 +0000249 assert(Subtarget.isFP64bit() && "FR=1 is required for MIPS32r6");
Daniel Sanders0fa60412014-06-12 13:39:06 +0000250 setOperationAction(ISD::SETCC, MVT::f64, Legal);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000251 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000252 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
253
Daniel Sanders3d3ea532014-06-12 15:00:17 +0000254 setOperationAction(ISD::BRCOND, MVT::Other, Legal);
255
Daniel Sanders0fa60412014-06-12 13:39:06 +0000256 // Floating point > and >= are supported via < and <=
257 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
258 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
259 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
260 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
261
262 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
263 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
264 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
265 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000266 }
267
Eric Christopher1c29a652014-07-18 22:55:25 +0000268 if (Subtarget.hasMips64r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000269 // MIPS64r6 replaces the accumulator-based multiplies with a three register
270 // instruction
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000271 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
272 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000273 setOperationAction(ISD::MUL, MVT::i64, Legal);
274 setOperationAction(ISD::MULHS, MVT::i64, Legal);
275 setOperationAction(ISD::MULHU, MVT::i64, Legal);
276
277 // MIPS32r6 replaces the accumulator-based division/remainder with separate
278 // three register division and remainder instructions.
279 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
280 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
281 setOperationAction(ISD::SDIV, MVT::i64, Legal);
282 setOperationAction(ISD::UDIV, MVT::i64, Legal);
283 setOperationAction(ISD::SREM, MVT::i64, Legal);
284 setOperationAction(ISD::UREM, MVT::i64, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000285
286 // MIPS64r6 replaces conditional moves with an equivalent that removes the
287 // need for three GPR read ports.
288 setOperationAction(ISD::SETCC, MVT::i64, Legal);
289 setOperationAction(ISD::SELECT, MVT::i64, Legal);
290 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000291 }
292
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000293 computeRegisterProperties(Subtarget.getRegisterInfo());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000294}
295
296const MipsTargetLowering *
Eric Christopherb1526602014-09-19 23:30:42 +0000297llvm::createMipsSETargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000298 const MipsSubtarget &STI) {
299 return new MipsSETargetLowering(TM, STI);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000300}
301
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000302const TargetRegisterClass *
303MipsSETargetLowering::getRepRegClassFor(MVT VT) const {
304 if (VT == MVT::Untyped)
Eric Christopher1c29a652014-07-18 22:55:25 +0000305 return Subtarget.hasDSP() ? &Mips::ACC64DSPRegClass : &Mips::ACC64RegClass;
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000306
307 return TargetLowering::getRepRegClassFor(VT);
308}
309
Daniel Sanders7a289d02013-09-23 12:02:46 +0000310// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000311void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000312addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
313 addRegisterClass(Ty, RC);
314
315 // Expand all builtin opcodes.
316 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
317 setOperationAction(Opc, Ty, Expand);
318
319 setOperationAction(ISD::BITCAST, Ty, Legal);
320 setOperationAction(ISD::LOAD, Ty, Legal);
321 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000322 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
323 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000324 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000325
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000326 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000327 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000328 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000329 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000330 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000331 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000332 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000333 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000334 setOperationAction(ISD::SHL, Ty, Legal);
335 setOperationAction(ISD::SRA, Ty, Legal);
336 setOperationAction(ISD::SRL, Ty, Legal);
337 setOperationAction(ISD::SUB, Ty, Legal);
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000338 setOperationAction(ISD::SMAX, Ty, Legal);
339 setOperationAction(ISD::SMIN, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000340 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000341 setOperationAction(ISD::UREM, Ty, Legal);
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000342 setOperationAction(ISD::UMAX, Ty, Legal);
343 setOperationAction(ISD::UMIN, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000344 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000345 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000346 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000347
Daniel Sanders015972b2013-10-11 10:00:06 +0000348 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
349 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
350 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
351 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
352 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
353 }
354
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000355 setOperationAction(ISD::SETCC, Ty, Legal);
356 setCondCodeAction(ISD::SETNE, Ty, Expand);
357 setCondCodeAction(ISD::SETGE, Ty, Expand);
358 setCondCodeAction(ISD::SETGT, Ty, Expand);
359 setCondCodeAction(ISD::SETUGE, Ty, Expand);
360 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000361}
362
Daniel Sanders7a289d02013-09-23 12:02:46 +0000363// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000364void MipsSETargetLowering::
365addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000366 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000367
368 // Expand all builtin opcodes.
369 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
370 setOperationAction(Opc, Ty, Expand);
371
372 setOperationAction(ISD::LOAD, Ty, Legal);
373 setOperationAction(ISD::STORE, Ty, Legal);
374 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000375 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000376 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000377 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000378
379 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000380 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000381 setOperationAction(ISD::FADD, Ty, Legal);
382 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000383 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000384 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000385 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000386 setOperationAction(ISD::FMUL, Ty, Legal);
387 setOperationAction(ISD::FRINT, Ty, Legal);
388 setOperationAction(ISD::FSQRT, Ty, Legal);
389 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000390 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000391
392 setOperationAction(ISD::SETCC, Ty, Legal);
393 setCondCodeAction(ISD::SETOGE, Ty, Expand);
394 setCondCodeAction(ISD::SETOGT, Ty, Expand);
395 setCondCodeAction(ISD::SETUGE, Ty, Expand);
396 setCondCodeAction(ISD::SETUGT, Ty, Expand);
397 setCondCodeAction(ISD::SETGE, Ty, Expand);
398 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000399 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000400}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000401
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000402SDValue MipsSETargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000403 if(!Subtarget.hasMips32r6())
404 return MipsTargetLowering::LowerOperation(Op, DAG);
405
406 EVT ResTy = Op->getValueType(0);
407 SDLoc DL(Op);
408
409 // Although MTC1_D64 takes an i32 and writes an f64, the upper 32 bits of the
410 // floating point register are undefined. Not really an issue as sel.d, which
411 // is produced from an FSELECT node, only looks at bit 0.
412 SDValue Tmp = DAG.getNode(MipsISD::MTC1_D64, DL, MVT::f64, Op->getOperand(0));
413 return DAG.getNode(MipsISD::FSELECT, DL, ResTy, Tmp, Op->getOperand(1),
414 Op->getOperand(2));
415}
416
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000417bool
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000418MipsSETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
419 unsigned,
420 unsigned,
421 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000422 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
423
Eric Christopher1c29a652014-07-18 22:55:25 +0000424 if (Subtarget.systemSupportsUnalignedAccess()) {
Daniel Sandersac272632014-05-23 13:18:02 +0000425 // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
426 // implementation defined whether this is handled by hardware, software, or
427 // a hybrid of the two but it's expected that most implementations will
428 // handle the majority of cases in hardware.
429 if (Fast)
430 *Fast = true;
431 return true;
432 }
433
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000434 switch (SVT) {
435 case MVT::i64:
436 case MVT::i32:
437 if (Fast)
438 *Fast = true;
439 return true;
440 default:
441 return false;
442 }
443}
444
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000445SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
446 SelectionDAG &DAG) const {
447 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000448 case ISD::LOAD: return lowerLOAD(Op, DAG);
449 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000450 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
451 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
452 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
453 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
454 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
455 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000456 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
457 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000458 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
459 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000460 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000461 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000462 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000463 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000464 case ISD::SELECT: return lowerSELECT(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000465 }
466
467 return MipsTargetLowering::LowerOperation(Op, DAG);
468}
469
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000470// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
471//
472// Performs the following transformations:
473// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
474// sign/zero-extension is completely overwritten by the new one performed by
475// the ISD::AND.
476// - Removes redundant zero extensions performed by an ISD::AND.
477static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
478 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000479 const MipsSubtarget &Subtarget) {
480 if (!Subtarget.hasMSA())
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000481 return SDValue();
482
483 SDValue Op0 = N->getOperand(0);
484 SDValue Op1 = N->getOperand(1);
485 unsigned Op0Opcode = Op0->getOpcode();
486
487 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
488 // where $d + 1 == 2^n and n == 32
489 // or $d + 1 == 2^n and n <= 32 and ZExt
490 // -> (MipsVExtractZExt $a, $b, $c)
491 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
492 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
493 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
494
495 if (!Mask)
496 return SDValue();
497
498 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
499
500 if (Log2IfPositive <= 0)
501 return SDValue(); // Mask+1 is not a power of 2
502
503 SDValue Op0Op2 = Op0->getOperand(2);
504 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
505 unsigned ExtendTySize = ExtendTy.getSizeInBits();
506 unsigned Log2 = Log2IfPositive;
507
508 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
509 Log2 == ExtendTySize) {
510 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000511 return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
512 Op0->getVTList(),
513 makeArrayRef(Ops, Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000514 }
515 }
516
517 return SDValue();
518}
519
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000520// Determine if the specified node is a constant vector splat.
521//
522// Returns true and sets Imm if:
523// * N is a ISD::BUILD_VECTOR representing a constant splat
524//
525// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
526// differences are that it assumes the MSA has already been checked and the
527// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
528// must not be in order for binsri.d to be selectable).
529static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
530 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
531
Craig Topper062a2ba2014-04-25 05:30:21 +0000532 if (!Node)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000533 return false;
534
535 APInt SplatValue, SplatUndef;
536 unsigned SplatBitSize;
537 bool HasAnyUndefs;
538
539 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
540 8, !IsLittleEndian))
541 return false;
542
543 Imm = SplatValue;
544
545 return true;
546}
547
Daniel Sandersab94b532013-10-30 15:20:38 +0000548// Test whether the given node is an all-ones build_vector.
549static bool isVectorAllOnes(SDValue N) {
550 // Look through bitcasts. Endianness doesn't matter because we are looking
551 // for an all-ones value.
552 if (N->getOpcode() == ISD::BITCAST)
553 N = N->getOperand(0);
554
555 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
556
557 if (!BVN)
558 return false;
559
560 APInt SplatValue, SplatUndef;
561 unsigned SplatBitSize;
562 bool HasAnyUndefs;
563
564 // Endianness doesn't matter in this context because we are looking for
565 // an all-ones value.
566 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
567 return SplatValue.isAllOnesValue();
568
569 return false;
570}
571
572// Test whether N is the bitwise inverse of OfNode.
573static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
574 if (N->getOpcode() != ISD::XOR)
575 return false;
576
577 if (isVectorAllOnes(N->getOperand(0)))
578 return N->getOperand(1) == OfNode;
579
580 if (isVectorAllOnes(N->getOperand(1)))
581 return N->getOperand(0) == OfNode;
582
583 return false;
584}
585
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000586// Perform combines where ISD::OR is the root node.
587//
588// Performs the following transformations:
589// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
590// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
591// vector type.
592static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
593 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000594 const MipsSubtarget &Subtarget) {
595 if (!Subtarget.hasMSA())
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000596 return SDValue();
597
598 EVT Ty = N->getValueType(0);
599
600 if (!Ty.is128BitVector())
601 return SDValue();
602
603 SDValue Op0 = N->getOperand(0);
604 SDValue Op1 = N->getOperand(1);
605
606 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
607 SDValue Op0Op0 = Op0->getOperand(0);
608 SDValue Op0Op1 = Op0->getOperand(1);
609 SDValue Op1Op0 = Op1->getOperand(0);
610 SDValue Op1Op1 = Op1->getOperand(1);
Eric Christopher1c29a652014-07-18 22:55:25 +0000611 bool IsLittleEndian = !Subtarget.isLittle();
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000612
613 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000614 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000615 APInt Mask, InvMask;
616
617 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
618 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
619 // looking.
620 // IfClr will be set if we find a valid match.
621 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
622 Cond = Op0Op0;
623 IfSet = Op0Op1;
624
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000625 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
626 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000627 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000628 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
629 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000630 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000631
632 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000633 }
634
635 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
636 // thing again using this mask.
637 // IfClr will be set if we find a valid match.
638 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
639 Cond = Op0Op1;
640 IfSet = Op0Op0;
641
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000642 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
643 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000644 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000645 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
646 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000647 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000648
649 IsConstantMask = true;
650 }
651
652 // If IfClr is not yet set, try looking for a non-constant match.
653 // IfClr will be set if we find a valid match amongst the eight
654 // possibilities.
655 if (!IfClr.getNode()) {
656 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
657 Cond = Op1Op0;
658 IfSet = Op1Op1;
659 IfClr = Op0Op1;
660 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
661 Cond = Op1Op0;
662 IfSet = Op1Op1;
663 IfClr = Op0Op0;
664 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
665 Cond = Op1Op1;
666 IfSet = Op1Op0;
667 IfClr = Op0Op1;
668 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
669 Cond = Op1Op1;
670 IfSet = Op1Op0;
671 IfClr = Op0Op0;
672 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
673 Cond = Op0Op0;
674 IfSet = Op0Op1;
675 IfClr = Op1Op1;
676 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
677 Cond = Op0Op0;
678 IfSet = Op0Op1;
679 IfClr = Op1Op0;
680 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
681 Cond = Op0Op1;
682 IfSet = Op0Op0;
683 IfClr = Op1Op1;
684 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
685 Cond = Op0Op1;
686 IfSet = Op0Op0;
687 IfClr = Op1Op0;
688 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000689 }
690
691 // At this point, IfClr will be set if we have a valid match.
692 if (!IfClr.getNode())
693 return SDValue();
694
695 assert(Cond.getNode() && IfSet.getNode());
696
697 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000698 if (IsConstantMask) {
699 if (Mask.isAllOnesValue())
700 return IfSet;
701 else if (Mask == 0)
702 return IfClr;
703 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000704
705 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000706 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000707 }
708
709 return SDValue();
710}
711
Simon Dardis9ec9f442018-04-13 16:09:07 +0000712static bool shouldTransformMulToShiftsAddsSubs(APInt C, EVT VT,
713 SelectionDAG &DAG,
714 const MipsSubtarget &Subtarget) {
715 // Estimate the number of operations the below transform will turn a
716 // constant multiply into. The number is approximately how many powers
717 // of two summed together that the constant can be broken down into.
718
719 SmallVector<APInt, 16> WorkStack(1, C);
720 unsigned Steps = 0;
721 unsigned BitWidth = C.getBitWidth();
722
723 while (!WorkStack.empty()) {
724 APInt Val = WorkStack.pop_back_val();
725
726 if (Val == 0 || Val == 1)
727 continue;
728
729 if (Val.isPowerOf2()) {
730 ++Steps;
731 continue;
732 }
733
734 APInt Floor = APInt(BitWidth, 1) << Val.logBase2();
735 APInt Ceil = Val.isNegative() ? APInt(BitWidth, 0)
736 : APInt(BitWidth, 1) << C.ceilLogBase2();
737
738 if ((Val - Floor).ule(Ceil - Val)) {
739 WorkStack.push_back(Floor);
740 WorkStack.push_back(Val - Floor);
741 ++Steps;
742 continue;
743 }
744
745 WorkStack.push_back(Ceil);
746 WorkStack.push_back(Ceil - Val);
747 ++Steps;
748
749 // If we have taken more than 12[1] / 8[2] steps to attempt the
750 // optimization for a native sized value, it is more than likely that this
751 // optimization will make things worse.
752 //
753 // [1] MIPS64 requires 6 instructions at most to materialize any constant,
754 // multiplication requires at least 4 cycles, but another cycle (or two)
755 // to retrieve the result from the HI/LO registers.
756 //
757 // [2] For MIPS32, more than 8 steps is expensive as the constant could be
758 // materialized in 2 instructions, multiplication requires at least 4
759 // cycles, but another cycle (or two) to retrieve the result from the
760 // HI/LO registers.
761
762 if (Steps > 12 && (Subtarget.isABI_N32() || Subtarget.isABI_N64()))
763 return false;
764
765 if (Steps > 8 && Subtarget.isABI_O32())
766 return false;
767 }
768
769 // If the value being multiplied is not supported natively, we have to pay
770 // an additional legalization cost, conservatively assume an increase in the
771 // cost of 3 instructions per step. This values for this heuristic were
772 // determined experimentally.
773 unsigned RegisterSize = DAG.getTargetLoweringInfo()
774 .getRegisterType(*DAG.getContext(), VT)
775 .getSizeInBits();
776 Steps *= (VT.getSizeInBits() != RegisterSize) * 3;
777 if (Steps > 27)
778 return false;
779
780 return true;
781}
782
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000783static SDValue genConstMult(SDValue X, APInt C, const SDLoc &DL, EVT VT,
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000784 EVT ShiftTy, SelectionDAG &DAG) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000785 // Return 0.
786 if (C == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000787 return DAG.getConstant(0, DL, VT);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000788
789 // Return x.
790 if (C == 1)
791 return X;
792
793 // If c is power of 2, return (shl x, log2(c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000794 if (C.isPowerOf2())
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000795 return DAG.getNode(ISD::SHL, DL, VT, X,
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000796 DAG.getConstant(C.logBase2(), DL, ShiftTy));
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000797
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000798 unsigned BitWidth = C.getBitWidth();
799 APInt Floor = APInt(BitWidth, 1) << C.logBase2();
800 APInt Ceil = C.isNegative() ? APInt(BitWidth, 0) :
801 APInt(BitWidth, 1) << C.ceilLogBase2();
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000802
803 // If |c - floor_c| <= |c - ceil_c|,
804 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
805 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000806 if ((C - Floor).ule(Ceil - C)) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000807 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
808 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
809 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
810 }
811
812 // If |c - floor_c| > |c - ceil_c|,
813 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
814 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
815 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
816 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
817}
818
819static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
820 const TargetLowering::DAGCombinerInfo &DCI,
Simon Dardis9ec9f442018-04-13 16:09:07 +0000821 const MipsSETargetLowering *TL,
822 const MipsSubtarget &Subtarget) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000823 EVT VT = N->getValueType(0);
824
825 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
Simon Dardis9ec9f442018-04-13 16:09:07 +0000826 if (!VT.isVector() && shouldTransformMulToShiftsAddsSubs(
827 C->getAPIntValue(), VT, DAG, Subtarget))
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000828 return genConstMult(N->getOperand(0), C->getAPIntValue(), SDLoc(N), VT,
Mehdi Aminieaabc512015-07-09 15:12:23 +0000829 TL->getScalarShiftAmountTy(DAG.getDataLayout(), VT),
830 DAG);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000831
832 return SDValue(N, 0);
833}
834
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000835static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
836 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000837 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000838 // See if this is a vector splat immediate node.
839 APInt SplatValue, SplatUndef;
840 unsigned SplatBitSize;
841 bool HasAnyUndefs;
Sanjay Patel1ed771f2016-09-14 16:37:15 +0000842 unsigned EltSize = Ty.getScalarSizeInBits();
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000843 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
844
Eric Christopher1c29a652014-07-18 22:55:25 +0000845 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000846 return SDValue();
847
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000848 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000849 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000850 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000851 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000852 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000853 return SDValue();
854
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000855 SDLoc DL(N);
856 return DAG.getNode(Opc, DL, Ty, N->getOperand(0),
857 DAG.getConstant(SplatValue.getZExtValue(), DL, MVT::i32));
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000858}
859
860static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
861 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000862 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000863 EVT Ty = N->getValueType(0);
864
865 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
866 return SDValue();
867
868 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
869}
870
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000871// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
872// constant splats into MipsISD::SHRA_DSP for DSPr2.
873//
874// Performs the following transformations:
875// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
876// sign/zero-extension is completely overwritten by the new one performed by
877// the ISD::SRA and ISD::SHL nodes.
878// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
879// sequence.
880//
881// See performDSPShiftCombine for more information about the transformation
882// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000883static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
884 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000885 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000886 EVT Ty = N->getValueType(0);
887
Eric Christopher1c29a652014-07-18 22:55:25 +0000888 if (Subtarget.hasMSA()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000889 SDValue Op0 = N->getOperand(0);
890 SDValue Op1 = N->getOperand(1);
891
892 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
893 // where $d + sizeof($c) == 32
894 // or $d + sizeof($c) <= 32 and SExt
895 // -> (MipsVExtractSExt $a, $b, $c)
896 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
897 SDValue Op0Op0 = Op0->getOperand(0);
898 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
899
900 if (!ShAmount)
901 return SDValue();
902
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000903 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
904 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
905 return SDValue();
906
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000907 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
908 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
909
910 if (TotalBits == 32 ||
911 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
912 TotalBits <= 32)) {
913 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
914 Op0Op0->getOperand(2) };
Chandler Carruth356665a2014-08-01 22:09:43 +0000915 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
916 Op0Op0->getVTList(),
917 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000918 }
919 }
920 }
921
Eric Christopher1c29a652014-07-18 22:55:25 +0000922 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000923 return SDValue();
924
925 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
926}
927
928
929static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
930 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000931 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000932 EVT Ty = N->getValueType(0);
933
Eric Christopher1c29a652014-07-18 22:55:25 +0000934 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000935 return SDValue();
936
937 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
938}
939
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000940static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
941 bool IsV216 = (Ty == MVT::v2i16);
942
943 switch (CC) {
944 case ISD::SETEQ:
945 case ISD::SETNE: return true;
946 case ISD::SETLT:
947 case ISD::SETLE:
948 case ISD::SETGT:
949 case ISD::SETGE: return IsV216;
950 case ISD::SETULT:
951 case ISD::SETULE:
952 case ISD::SETUGT:
953 case ISD::SETUGE: return !IsV216;
954 default: return false;
955 }
956}
957
958static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
959 EVT Ty = N->getValueType(0);
960
961 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
962 return SDValue();
963
964 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
965 return SDValue();
966
Andrew Trickef9de2a2013-05-25 02:42:55 +0000967 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000968 N->getOperand(1), N->getOperand(2));
969}
970
971static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
972 EVT Ty = N->getValueType(0);
973
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000974 if (Ty == MVT::v2i16 || Ty == MVT::v4i8) {
Daniel Sanders3ce56622013-09-24 12:18:31 +0000975 SDValue SetCC = N->getOperand(0);
976
977 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
978 return SDValue();
979
980 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
981 SetCC.getOperand(0), SetCC.getOperand(1),
982 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
983 }
984
985 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000986}
987
Daniel Sandersf7456c72013-09-23 13:22:24 +0000988static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000989 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000990 EVT Ty = N->getValueType(0);
991
Eric Christopher1c29a652014-07-18 22:55:25 +0000992 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000993 // Try the following combines:
994 // (xor (or $a, $b), (build_vector allones))
995 // (xor (or $a, $b), (bitcast (build_vector allones)))
996 SDValue Op0 = N->getOperand(0);
997 SDValue Op1 = N->getOperand(1);
998 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +0000999
1000 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
1001 NotOp = Op1;
1002 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
1003 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001004 else
1005 return SDValue();
1006
1007 if (NotOp->getOpcode() == ISD::OR)
1008 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
1009 NotOp->getOperand(1));
1010 }
1011
1012 return SDValue();
1013}
1014
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001015SDValue
1016MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1017 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001018 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001019
1020 switch (N->getOpcode()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001021 case ISD::AND:
1022 Val = performANDCombine(N, DAG, DCI, Subtarget);
1023 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +00001024 case ISD::OR:
1025 Val = performORCombine(N, DAG, DCI, Subtarget);
1026 break;
Akira Hatanaka5832fc62013-06-26 18:48:17 +00001027 case ISD::MUL:
Simon Dardis9ec9f442018-04-13 16:09:07 +00001028 return performMULCombine(N, DAG, DCI, this, Subtarget);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001029 case ISD::SHL:
Petar Jovanovicb71386a2017-03-15 13:10:08 +00001030 Val = performSHLCombine(N, DAG, DCI, Subtarget);
1031 break;
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001032 case ISD::SRA:
1033 return performSRACombine(N, DAG, DCI, Subtarget);
1034 case ISD::SRL:
1035 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001036 case ISD::VSELECT:
1037 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +00001038 case ISD::XOR:
1039 Val = performXORCombine(N, DAG, Subtarget);
1040 break;
1041 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001042 Val = performSETCCCombine(N, DAG);
1043 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001044 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001045
Daniel Sanders62aeab82013-10-30 13:31:27 +00001046 if (Val.getNode()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001047 LLVM_DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1048 N->printrWithDepth(dbgs(), &DAG); dbgs() << "\n=> \n";
1049 Val.getNode()->printrWithDepth(dbgs(), &DAG); dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001050 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001051 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001052
1053 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001054}
1055
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001056MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001057MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001058 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001059 switch (MI.getOpcode()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001060 default:
1061 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1062 case Mips::BPOSGE32_PSEUDO:
1063 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001064 case Mips::SNZ_B_PSEUDO:
1065 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1066 case Mips::SNZ_H_PSEUDO:
1067 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1068 case Mips::SNZ_W_PSEUDO:
1069 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1070 case Mips::SNZ_D_PSEUDO:
1071 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1072 case Mips::SNZ_V_PSEUDO:
1073 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1074 case Mips::SZ_B_PSEUDO:
1075 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1076 case Mips::SZ_H_PSEUDO:
1077 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1078 case Mips::SZ_W_PSEUDO:
1079 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1080 case Mips::SZ_D_PSEUDO:
1081 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1082 case Mips::SZ_V_PSEUDO:
1083 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001084 case Mips::COPY_FW_PSEUDO:
1085 return emitCOPY_FW(MI, BB);
1086 case Mips::COPY_FD_PSEUDO:
1087 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001088 case Mips::INSERT_FW_PSEUDO:
1089 return emitINSERT_FW(MI, BB);
1090 case Mips::INSERT_FD_PSEUDO:
1091 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001092 case Mips::INSERT_B_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001093 case Mips::INSERT_B_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001094 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1095 case Mips::INSERT_H_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001096 case Mips::INSERT_H_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001097 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1098 case Mips::INSERT_W_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001099 case Mips::INSERT_W_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001100 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1101 case Mips::INSERT_D_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001102 case Mips::INSERT_D_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001103 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1104 case Mips::INSERT_FW_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001105 case Mips::INSERT_FW_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001106 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1107 case Mips::INSERT_FD_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001108 case Mips::INSERT_FD_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001109 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001110 case Mips::FILL_FW_PSEUDO:
1111 return emitFILL_FW(MI, BB);
1112 case Mips::FILL_FD_PSEUDO:
1113 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001114 case Mips::FEXP2_W_1_PSEUDO:
1115 return emitFEXP2_W_1(MI, BB);
1116 case Mips::FEXP2_D_1_PSEUDO:
1117 return emitFEXP2_D_1(MI, BB);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00001118 case Mips::ST_F16:
1119 return emitST_F16_PSEUDO(MI, BB);
1120 case Mips::LD_F16:
1121 return emitLD_F16_PSEUDO(MI, BB);
1122 case Mips::MSA_FP_EXTEND_W_PSEUDO:
1123 return emitFPEXTEND_PSEUDO(MI, BB, false);
1124 case Mips::MSA_FP_ROUND_W_PSEUDO:
1125 return emitFPROUND_PSEUDO(MI, BB, false);
1126 case Mips::MSA_FP_EXTEND_D_PSEUDO:
1127 return emitFPEXTEND_PSEUDO(MI, BB, true);
1128 case Mips::MSA_FP_ROUND_D_PSEUDO:
1129 return emitFPROUND_PSEUDO(MI, BB, true);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001130 }
1131}
1132
Daniel Sanders23e98772014-11-02 16:09:29 +00001133bool MipsSETargetLowering::isEligibleForTailCallOptimization(
1134 const CCState &CCInfo, unsigned NextStackOffset,
1135 const MipsFunctionInfo &FI) const {
Simon Dardis57f4ae42016-08-04 09:17:07 +00001136 if (!UseMipsTailCalls)
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001137 return false;
1138
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00001139 // Exception has to be cleared with eret.
1140 if (FI.isISR())
1141 return false;
1142
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001143 // Return false if either the callee or caller has a byval argument.
Daniel Sanders23e98772014-11-02 16:09:29 +00001144 if (CCInfo.getInRegsParamsCount() > 0 || FI.hasByvalArg())
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001145 return false;
1146
1147 // Return true if the callee's argument area is no larger than the
1148 // caller's.
1149 return NextStackOffset <= FI.getIncomingArgSize();
1150}
1151
1152void MipsSETargetLowering::
1153getOpndList(SmallVectorImpl<SDValue> &Ops,
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001154 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001155 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001156 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
1157 SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001158 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001159 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001160 InternalLinkage, IsCallReloc, CLI, Callee,
1161 Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001162}
1163
Akira Hatanaka63791212013-09-07 00:52:30 +00001164SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1165 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1166
1167 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1168 return MipsTargetLowering::lowerLOAD(Op, DAG);
1169
1170 // Replace a double precision load with two i32 loads and a buildpair64.
1171 SDLoc DL(Op);
1172 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1173 EVT PtrVT = Ptr.getValueType();
1174
1175 // i32 load from lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001176 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr, MachinePointerInfo(),
1177 Nd.getAlignment(), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001178
1179 // i32 load from higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001180 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001181 SDValue Hi = DAG.getLoad(
1182 MVT::i32, DL, Lo.getValue(1), Ptr, MachinePointerInfo(),
1183 std::min(Nd.getAlignment(), 4U), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001184
Eric Christopher1c29a652014-07-18 22:55:25 +00001185 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001186 std::swap(Lo, Hi);
1187
1188 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1189 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001190 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001191}
1192
1193SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1194 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1195
1196 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1197 return MipsTargetLowering::lowerSTORE(Op, DAG);
1198
1199 // Replace a double precision store with two extractelement64s and i32 stores.
1200 SDLoc DL(Op);
1201 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1202 EVT PtrVT = Ptr.getValueType();
1203 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001204 Val, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001205 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001206 Val, DAG.getConstant(1, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001207
Eric Christopher1c29a652014-07-18 22:55:25 +00001208 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001209 std::swap(Lo, Hi);
1210
1211 // i32 store to lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001212 Chain =
1213 DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(), Nd.getAlignment(),
1214 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001215
1216 // i32 store to higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001217 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Akira Hatanaka63791212013-09-07 00:52:30 +00001218 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001219 std::min(Nd.getAlignment(), 4U),
1220 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001221}
1222
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001223SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1224 bool HasLo, bool HasHi,
1225 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001226 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001227 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001228
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001229 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001230 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001231 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1232 Op.getOperand(0), Op.getOperand(1));
1233 SDValue Lo, Hi;
1234
1235 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001236 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001237 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001238 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001239
1240 if (!HasLo || !HasHi)
1241 return HasLo ? Lo : Hi;
1242
1243 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001244 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001245}
1246
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001247static SDValue initAccumulator(SDValue In, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001248 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001249 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001250 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001251 DAG.getConstant(1, DL, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001252 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001253}
1254
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001255static SDValue extractLOHI(SDValue Op, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001256 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1257 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001258 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1259}
1260
1261// This function expands mips intrinsic nodes which have 64-bit input operands
1262// or output values.
1263//
1264// out64 = intrinsic-node in64
1265// =>
1266// lo = copy (extract-element (in64, 0))
1267// hi = copy (extract-element (in64, 1))
1268// mips-specific-node
1269// v0 = copy lo
1270// v1 = copy hi
1271// out64 = merge-values (v0, v1)
1272//
1273static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001274 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001275 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1276 SmallVector<SDValue, 3> Ops;
1277 unsigned OpNo = 0;
1278
1279 // See if Op has a chain input.
1280 if (HasChainIn)
1281 Ops.push_back(Op->getOperand(OpNo++));
1282
1283 // The next operand is the intrinsic opcode.
1284 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1285
1286 // See if the next operand has type i64.
1287 SDValue Opnd = Op->getOperand(++OpNo), In64;
1288
1289 if (Opnd.getValueType() == MVT::i64)
1290 In64 = initAccumulator(Opnd, DL, DAG);
1291 else
1292 Ops.push_back(Opnd);
1293
1294 // Push the remaining operands.
1295 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1296 Ops.push_back(Op->getOperand(OpNo));
1297
1298 // Add In64 to the end of the list.
1299 if (In64.getNode())
1300 Ops.push_back(In64);
1301
1302 // Scan output.
1303 SmallVector<EVT, 2> ResTys;
1304
1305 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1306 I != E; ++I)
1307 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1308
1309 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001310 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001311 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1312
1313 if (!HasChainIn)
1314 return Out;
1315
1316 assert(Val->getValueType(1) == MVT::Other);
1317 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001318 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001319}
1320
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001321// Lower an MSA copy intrinsic into the specified SelectionDAG node
1322static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1323 SDLoc DL(Op);
1324 SDValue Vec = Op->getOperand(1);
1325 SDValue Idx = Op->getOperand(2);
1326 EVT ResTy = Op->getValueType(0);
1327 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1328
1329 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1330 DAG.getValueType(EltTy));
1331
1332 return Result;
1333}
1334
Daniel Sanders50b80412013-11-15 12:56:49 +00001335static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1336 EVT ResVecTy = Op->getValueType(0);
1337 EVT ViaVecTy = ResVecTy;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001338 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
Daniel Sanders50b80412013-11-15 12:56:49 +00001339 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001340
Daniel Sanders50b80412013-11-15 12:56:49 +00001341 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1342 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1343 // lanes.
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001344 SDValue LaneA = Op->getOperand(OpNr);
1345 SDValue LaneB;
Daniel Sanders50b80412013-11-15 12:56:49 +00001346
1347 if (ResVecTy == MVT::v2i64) {
Stefan Maksimovicc7113cc2018-05-08 15:12:29 +00001348 // In case of the index being passed as an immediate value, set the upper
1349 // lane to 0 so that the splati.d instruction can be matched.
1350 if (isa<ConstantSDNode>(LaneA))
1351 LaneB = DAG.getConstant(0, DL, MVT::i32);
1352 // Having the index passed in a register, set the upper lane to the same
1353 // value as the lower - this results in the BUILD_VECTOR node not being
1354 // expanded through stack. This way we are able to pattern match the set of
1355 // nodes created here to splat.d.
1356 else
1357 LaneB = LaneA;
Daniel Sandersf49dd822013-09-24 13:33:07 +00001358 ViaVecTy = MVT::v4i32;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001359 if(BigEndian)
1360 std::swap(LaneA, LaneB);
Daniel Sanders50b80412013-11-15 12:56:49 +00001361 } else
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001362 LaneB = LaneA;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001363
Daniel Sanders50b80412013-11-15 12:56:49 +00001364 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1365 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001366
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001367 SDValue Result = DAG.getBuildVector(
1368 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001369
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001370 if (ViaVecTy != ResVecTy) {
1371 SDValue One = DAG.getConstant(1, DL, ViaVecTy);
1372 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy,
1373 DAG.getNode(ISD::AND, DL, ViaVecTy, Result, One));
1374 }
Daniel Sandersf49dd822013-09-24 13:33:07 +00001375
1376 return Result;
1377}
1378
Simon Dardis548a53f2017-01-10 16:40:57 +00001379static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG,
1380 bool IsSigned = false) {
Simon Atanasyanf26f56d2019-03-29 15:15:22 +00001381 auto *CImm = cast<ConstantSDNode>(Op->getOperand(ImmOp));
Simon Dardis548a53f2017-01-10 16:40:57 +00001382 return DAG.getConstant(
1383 APInt(Op->getValueType(0).getScalarType().getSizeInBits(),
Simon Atanasyanf26f56d2019-03-29 15:15:22 +00001384 IsSigned ? CImm->getSExtValue() : CImm->getZExtValue(), IsSigned),
Simon Dardis548a53f2017-01-10 16:40:57 +00001385 SDLoc(Op), Op->getValueType(0));
Daniel Sanders50b80412013-11-15 12:56:49 +00001386}
1387
1388static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1389 bool BigEndian, SelectionDAG &DAG) {
1390 EVT ViaVecTy = VecTy;
1391 SDValue SplatValueA = SplatValue;
1392 SDValue SplatValueB = SplatValue;
1393 SDLoc DL(SplatValue);
1394
1395 if (VecTy == MVT::v2i64) {
1396 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1397 ViaVecTy = MVT::v4i32;
1398
1399 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1400 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001401 DAG.getConstant(32, DL, MVT::i32));
Daniel Sanders50b80412013-11-15 12:56:49 +00001402 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1403 }
1404
1405 // We currently hold the parts in little endian order. Swap them if
1406 // necessary.
1407 if (BigEndian)
1408 std::swap(SplatValueA, SplatValueB);
1409
1410 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1411 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1412 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1413 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1414
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001415 SDValue Result = DAG.getBuildVector(
1416 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001417
1418 if (VecTy != ViaVecTy)
1419 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1420
1421 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001422}
1423
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001424static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1425 unsigned Opc, SDValue Imm,
1426 bool BigEndian) {
1427 EVT VecTy = Op->getValueType(0);
1428 SDValue Exp2Imm;
1429 SDLoc DL(Op);
1430
Daniel Sanders50b80412013-11-15 12:56:49 +00001431 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1432 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001433 if (VecTy == MVT::v2i64) {
1434 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1435 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1436
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001437 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), DL,
1438 MVT::i32);
1439 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), DL, MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001440
1441 if (BigEndian)
1442 std::swap(BitImmLoOp, BitImmHiOp);
1443
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001444 Exp2Imm = DAG.getNode(
1445 ISD::BITCAST, DL, MVT::v2i64,
1446 DAG.getBuildVector(MVT::v4i32, DL,
1447 {BitImmLoOp, BitImmHiOp, BitImmLoOp, BitImmHiOp}));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001448 }
1449 }
1450
Craig Topper062a2ba2014-04-25 05:30:21 +00001451 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001452 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001453
1454 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1455 // only values 0-63 are valid.
1456 if (VecTy == MVT::v2i64)
1457 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1458
1459 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1460
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001461 Exp2Imm = DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, DL, VecTy),
1462 Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001463 }
1464
1465 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1466}
1467
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001468static SDValue truncateVecElts(SDValue Op, SelectionDAG &DAG) {
1469 SDLoc DL(Op);
1470 EVT ResTy = Op->getValueType(0);
1471 SDValue Vec = Op->getOperand(2);
1472 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
1473 MVT ResEltTy = ResTy == MVT::v2i64 ? MVT::i64 : MVT::i32;
1474 SDValue ConstValue = DAG.getConstant(Vec.getScalarValueSizeInBits() - 1,
1475 DL, ResEltTy);
1476 SDValue SplatVec = getBuildVectorSplat(ResTy, ConstValue, BigEndian, DAG);
1477
1478 return DAG.getNode(ISD::AND, DL, ResTy, Vec, SplatVec);
1479}
1480
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001481static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1482 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001483 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001484 SDValue One = DAG.getConstant(1, DL, ResTy);
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001485 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, truncateVecElts(Op, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001486
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001487 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1488 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001489}
1490
1491static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1492 SDLoc DL(Op);
1493 EVT ResTy = Op->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001494 APInt BitImm = APInt(ResTy.getScalarSizeInBits(), 1)
Daniel Sanders50b80412013-11-15 12:56:49 +00001495 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001496 SDValue BitMask = DAG.getConstant(~BitImm, DL, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001497
1498 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1499}
1500
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001501SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1502 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001503 SDLoc DL(Op);
Simon Dardis548a53f2017-01-10 16:40:57 +00001504 unsigned Intrinsic = cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue();
1505 switch (Intrinsic) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001506 default:
1507 return SDValue();
1508 case Intrinsic::mips_shilo:
1509 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1510 case Intrinsic::mips_dpau_h_qbl:
1511 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1512 case Intrinsic::mips_dpau_h_qbr:
1513 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1514 case Intrinsic::mips_dpsu_h_qbl:
1515 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1516 case Intrinsic::mips_dpsu_h_qbr:
1517 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1518 case Intrinsic::mips_dpa_w_ph:
1519 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1520 case Intrinsic::mips_dps_w_ph:
1521 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1522 case Intrinsic::mips_dpax_w_ph:
1523 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1524 case Intrinsic::mips_dpsx_w_ph:
1525 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1526 case Intrinsic::mips_mulsa_w_ph:
1527 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1528 case Intrinsic::mips_mult:
1529 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1530 case Intrinsic::mips_multu:
1531 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1532 case Intrinsic::mips_madd:
1533 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1534 case Intrinsic::mips_maddu:
1535 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1536 case Intrinsic::mips_msub:
1537 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1538 case Intrinsic::mips_msubu:
1539 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001540 case Intrinsic::mips_addv_b:
1541 case Intrinsic::mips_addv_h:
1542 case Intrinsic::mips_addv_w:
1543 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001544 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1545 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001546 case Intrinsic::mips_addvi_b:
1547 case Intrinsic::mips_addvi_h:
1548 case Intrinsic::mips_addvi_w:
1549 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001550 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1551 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001552 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001553 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1554 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001555 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001556 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1557 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001558 case Intrinsic::mips_bclr_b:
1559 case Intrinsic::mips_bclr_h:
1560 case Intrinsic::mips_bclr_w:
1561 case Intrinsic::mips_bclr_d:
1562 return lowerMSABitClear(Op, DAG);
1563 case Intrinsic::mips_bclri_b:
1564 case Intrinsic::mips_bclri_h:
1565 case Intrinsic::mips_bclri_w:
1566 case Intrinsic::mips_bclri_d:
1567 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001568 case Intrinsic::mips_binsli_b:
1569 case Intrinsic::mips_binsli_h:
1570 case Intrinsic::mips_binsli_w:
1571 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001572 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001573 EVT VecTy = Op->getValueType(0);
1574 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001575 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1576 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001577 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001578 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001579 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001580 DAG.getConstant(Mask, DL, VecTy, true),
1581 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001582 }
1583 case Intrinsic::mips_binsri_b:
1584 case Intrinsic::mips_binsri_h:
1585 case Intrinsic::mips_binsri_w:
1586 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001587 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001588 EVT VecTy = Op->getValueType(0);
1589 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001590 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1591 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001592 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001593 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001594 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001595 DAG.getConstant(Mask, DL, VecTy, true),
1596 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001597 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001598 case Intrinsic::mips_bmnz_v:
1599 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1600 Op->getOperand(2), Op->getOperand(1));
1601 case Intrinsic::mips_bmnzi_b:
1602 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1603 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1604 Op->getOperand(1));
1605 case Intrinsic::mips_bmz_v:
1606 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1607 Op->getOperand(1), Op->getOperand(2));
1608 case Intrinsic::mips_bmzi_b:
1609 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1610 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1611 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001612 case Intrinsic::mips_bneg_b:
1613 case Intrinsic::mips_bneg_h:
1614 case Intrinsic::mips_bneg_w:
1615 case Intrinsic::mips_bneg_d: {
1616 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001617 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001618
1619 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1620 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001621 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001622 }
1623 case Intrinsic::mips_bnegi_b:
1624 case Intrinsic::mips_bnegi_h:
1625 case Intrinsic::mips_bnegi_w:
1626 case Intrinsic::mips_bnegi_d:
1627 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001628 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001629 case Intrinsic::mips_bnz_b:
1630 case Intrinsic::mips_bnz_h:
1631 case Intrinsic::mips_bnz_w:
1632 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001633 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1634 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001635 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001636 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1637 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001638 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001639 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001640 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001641 Op->getOperand(1), Op->getOperand(3),
1642 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001643 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001644 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001645 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001646 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1647 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001648 case Intrinsic::mips_bset_b:
1649 case Intrinsic::mips_bset_h:
1650 case Intrinsic::mips_bset_w:
1651 case Intrinsic::mips_bset_d: {
1652 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001653 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001654
1655 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1656 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001657 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001658 }
1659 case Intrinsic::mips_bseti_b:
1660 case Intrinsic::mips_bseti_h:
1661 case Intrinsic::mips_bseti_w:
1662 case Intrinsic::mips_bseti_d:
1663 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001664 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001665 case Intrinsic::mips_bz_b:
1666 case Intrinsic::mips_bz_h:
1667 case Intrinsic::mips_bz_w:
1668 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001669 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1670 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001671 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001672 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1673 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001674 case Intrinsic::mips_ceq_b:
1675 case Intrinsic::mips_ceq_h:
1676 case Intrinsic::mips_ceq_w:
1677 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001678 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001679 Op->getOperand(2), ISD::SETEQ);
1680 case Intrinsic::mips_ceqi_b:
1681 case Intrinsic::mips_ceqi_h:
1682 case Intrinsic::mips_ceqi_w:
1683 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001684 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001685 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETEQ);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001686 case Intrinsic::mips_cle_s_b:
1687 case Intrinsic::mips_cle_s_h:
1688 case Intrinsic::mips_cle_s_w:
1689 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001690 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001691 Op->getOperand(2), ISD::SETLE);
1692 case Intrinsic::mips_clei_s_b:
1693 case Intrinsic::mips_clei_s_h:
1694 case Intrinsic::mips_clei_s_w:
1695 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001696 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001697 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLE);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001698 case Intrinsic::mips_cle_u_b:
1699 case Intrinsic::mips_cle_u_h:
1700 case Intrinsic::mips_cle_u_w:
1701 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001702 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001703 Op->getOperand(2), ISD::SETULE);
1704 case Intrinsic::mips_clei_u_b:
1705 case Intrinsic::mips_clei_u_h:
1706 case Intrinsic::mips_clei_u_w:
1707 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001708 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001709 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1710 case Intrinsic::mips_clt_s_b:
1711 case Intrinsic::mips_clt_s_h:
1712 case Intrinsic::mips_clt_s_w:
1713 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001714 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001715 Op->getOperand(2), ISD::SETLT);
1716 case Intrinsic::mips_clti_s_b:
1717 case Intrinsic::mips_clti_s_h:
1718 case Intrinsic::mips_clti_s_w:
1719 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001720 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001721 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLT);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001722 case Intrinsic::mips_clt_u_b:
1723 case Intrinsic::mips_clt_u_h:
1724 case Intrinsic::mips_clt_u_w:
1725 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001726 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001727 Op->getOperand(2), ISD::SETULT);
1728 case Intrinsic::mips_clti_u_b:
1729 case Intrinsic::mips_clti_u_h:
1730 case Intrinsic::mips_clti_u_w:
1731 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001732 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001733 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001734 case Intrinsic::mips_copy_s_b:
1735 case Intrinsic::mips_copy_s_h:
1736 case Intrinsic::mips_copy_s_w:
1737 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001738 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001739 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001740 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1741 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1742 else {
1743 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1744 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1745 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1746 Op->getValueType(0), Op->getOperand(1),
1747 Op->getOperand(2));
1748 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001749 case Intrinsic::mips_copy_u_b:
1750 case Intrinsic::mips_copy_u_h:
1751 case Intrinsic::mips_copy_u_w:
1752 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001753 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001754 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001755 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1756 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1757 else {
1758 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1759 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1760 // Note: When i64 is illegal, this results in copy_s.w instructions
1761 // instead of copy_u.w instructions. This makes no difference to the
1762 // behaviour since i64 is only illegal when the register file is 32-bit.
1763 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1764 Op->getValueType(0), Op->getOperand(1),
1765 Op->getOperand(2));
1766 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001767 case Intrinsic::mips_div_s_b:
1768 case Intrinsic::mips_div_s_h:
1769 case Intrinsic::mips_div_s_w:
1770 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001771 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1772 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001773 case Intrinsic::mips_div_u_b:
1774 case Intrinsic::mips_div_u_h:
1775 case Intrinsic::mips_div_u_w:
1776 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001777 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1778 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001779 case Intrinsic::mips_fadd_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001780 case Intrinsic::mips_fadd_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001781 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001782 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1783 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001784 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1785 case Intrinsic::mips_fceq_w:
1786 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001787 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001788 Op->getOperand(2), ISD::SETOEQ);
1789 case Intrinsic::mips_fcle_w:
1790 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001791 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001792 Op->getOperand(2), ISD::SETOLE);
1793 case Intrinsic::mips_fclt_w:
1794 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001795 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001796 Op->getOperand(2), ISD::SETOLT);
1797 case Intrinsic::mips_fcne_w:
1798 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001799 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001800 Op->getOperand(2), ISD::SETONE);
1801 case Intrinsic::mips_fcor_w:
1802 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001803 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001804 Op->getOperand(2), ISD::SETO);
1805 case Intrinsic::mips_fcueq_w:
1806 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001807 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001808 Op->getOperand(2), ISD::SETUEQ);
1809 case Intrinsic::mips_fcule_w:
1810 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001811 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001812 Op->getOperand(2), ISD::SETULE);
1813 case Intrinsic::mips_fcult_w:
1814 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001815 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001816 Op->getOperand(2), ISD::SETULT);
1817 case Intrinsic::mips_fcun_w:
1818 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001819 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001820 Op->getOperand(2), ISD::SETUO);
1821 case Intrinsic::mips_fcune_w:
1822 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001823 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001824 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001825 case Intrinsic::mips_fdiv_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001826 case Intrinsic::mips_fdiv_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001827 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001828 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1829 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001830 case Intrinsic::mips_ffint_u_w:
1831 case Intrinsic::mips_ffint_u_d:
1832 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1833 Op->getOperand(1));
1834 case Intrinsic::mips_ffint_s_w:
1835 case Intrinsic::mips_ffint_s_d:
1836 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1837 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001838 case Intrinsic::mips_fill_b:
1839 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001840 case Intrinsic::mips_fill_w:
1841 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001842 EVT ResTy = Op->getValueType(0);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001843 SmallVector<SDValue, 16> Ops(ResTy.getVectorNumElements(),
1844 Op->getOperand(1));
Daniel Sandersf49dd822013-09-24 13:33:07 +00001845
Daniel Sandersc72593e2013-09-27 13:20:41 +00001846 // If ResTy is v2i64 then the type legalizer will break this node down into
1847 // an equivalent v4i32.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001848 return DAG.getBuildVector(ResTy, DL, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001849 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001850 case Intrinsic::mips_fexp2_w:
1851 case Intrinsic::mips_fexp2_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001852 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sandersa9521602013-10-23 10:36:52 +00001853 EVT ResTy = Op->getValueType(0);
1854 return DAG.getNode(
1855 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1856 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1857 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001858 case Intrinsic::mips_flog2_w:
1859 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001860 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001861 case Intrinsic::mips_fmadd_w:
1862 case Intrinsic::mips_fmadd_d:
1863 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1864 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001865 case Intrinsic::mips_fmul_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001866 case Intrinsic::mips_fmul_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001867 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001868 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1869 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001870 case Intrinsic::mips_fmsub_w:
1871 case Intrinsic::mips_fmsub_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001872 // TODO: If intrinsics have fast-math-flags, propagate them.
Aleksandar Beserminji3546c162018-04-27 13:30:27 +00001873 return DAG.getNode(MipsISD::FMS, SDLoc(Op), Op->getValueType(0),
1874 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001875 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001876 case Intrinsic::mips_frint_w:
1877 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001878 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001879 case Intrinsic::mips_fsqrt_w:
1880 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001881 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001882 case Intrinsic::mips_fsub_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001883 case Intrinsic::mips_fsub_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001884 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001885 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1886 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001887 case Intrinsic::mips_ftrunc_u_w:
1888 case Intrinsic::mips_ftrunc_u_d:
1889 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1890 Op->getOperand(1));
1891 case Intrinsic::mips_ftrunc_s_w:
1892 case Intrinsic::mips_ftrunc_s_d:
1893 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1894 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001895 case Intrinsic::mips_ilvev_b:
1896 case Intrinsic::mips_ilvev_h:
1897 case Intrinsic::mips_ilvev_w:
1898 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001899 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001900 Op->getOperand(1), Op->getOperand(2));
1901 case Intrinsic::mips_ilvl_b:
1902 case Intrinsic::mips_ilvl_h:
1903 case Intrinsic::mips_ilvl_w:
1904 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001905 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001906 Op->getOperand(1), Op->getOperand(2));
1907 case Intrinsic::mips_ilvod_b:
1908 case Intrinsic::mips_ilvod_h:
1909 case Intrinsic::mips_ilvod_w:
1910 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001911 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001912 Op->getOperand(1), Op->getOperand(2));
1913 case Intrinsic::mips_ilvr_b:
1914 case Intrinsic::mips_ilvr_h:
1915 case Intrinsic::mips_ilvr_w:
1916 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001917 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001918 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001919 case Intrinsic::mips_insert_b:
1920 case Intrinsic::mips_insert_h:
1921 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001922 case Intrinsic::mips_insert_d:
1923 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1924 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001925 case Intrinsic::mips_insve_b:
1926 case Intrinsic::mips_insve_h:
1927 case Intrinsic::mips_insve_w:
Simon Dardis548a53f2017-01-10 16:40:57 +00001928 case Intrinsic::mips_insve_d: {
1929 // Report an error for out of range values.
1930 int64_t Max;
1931 switch (Intrinsic) {
1932 case Intrinsic::mips_insve_b: Max = 15; break;
1933 case Intrinsic::mips_insve_h: Max = 7; break;
1934 case Intrinsic::mips_insve_w: Max = 3; break;
1935 case Intrinsic::mips_insve_d: Max = 1; break;
1936 default: llvm_unreachable("Unmatched intrinsic");
1937 }
1938 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
1939 if (Value < 0 || Value > Max)
1940 report_fatal_error("Immediate out of range");
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001941 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1942 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001943 DAG.getConstant(0, DL, MVT::i32));
Simon Dardis548a53f2017-01-10 16:40:57 +00001944 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00001945 case Intrinsic::mips_ldi_b:
1946 case Intrinsic::mips_ldi_h:
1947 case Intrinsic::mips_ldi_w:
1948 case Intrinsic::mips_ldi_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00001949 return lowerMSASplatImm(Op, 1, DAG, true);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001950 case Intrinsic::mips_lsa:
1951 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001952 EVT ResTy = Op->getValueType(0);
1953 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1954 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1955 Op->getOperand(2), Op->getOperand(3)));
1956 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001957 case Intrinsic::mips_maddv_b:
1958 case Intrinsic::mips_maddv_h:
1959 case Intrinsic::mips_maddv_w:
1960 case Intrinsic::mips_maddv_d: {
1961 EVT ResTy = Op->getValueType(0);
1962 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1963 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1964 Op->getOperand(2), Op->getOperand(3)));
1965 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001966 case Intrinsic::mips_max_s_b:
1967 case Intrinsic::mips_max_s_h:
1968 case Intrinsic::mips_max_s_w:
1969 case Intrinsic::mips_max_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001970 return DAG.getNode(ISD::SMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001971 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001972 case Intrinsic::mips_max_u_b:
1973 case Intrinsic::mips_max_u_h:
1974 case Intrinsic::mips_max_u_w:
1975 case Intrinsic::mips_max_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001976 return DAG.getNode(ISD::UMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001977 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001978 case Intrinsic::mips_maxi_s_b:
1979 case Intrinsic::mips_maxi_s_h:
1980 case Intrinsic::mips_maxi_s_w:
1981 case Intrinsic::mips_maxi_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001982 return DAG.getNode(ISD::SMAX, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00001983 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001984 case Intrinsic::mips_maxi_u_b:
1985 case Intrinsic::mips_maxi_u_h:
1986 case Intrinsic::mips_maxi_u_w:
1987 case Intrinsic::mips_maxi_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001988 return DAG.getNode(ISD::UMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001989 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001990 case Intrinsic::mips_min_s_b:
1991 case Intrinsic::mips_min_s_h:
1992 case Intrinsic::mips_min_s_w:
1993 case Intrinsic::mips_min_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00001994 return DAG.getNode(ISD::SMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001995 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001996 case Intrinsic::mips_min_u_b:
1997 case Intrinsic::mips_min_u_h:
1998 case Intrinsic::mips_min_u_w:
1999 case Intrinsic::mips_min_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002000 return DAG.getNode(ISD::UMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002001 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002002 case Intrinsic::mips_mini_s_b:
2003 case Intrinsic::mips_mini_s_h:
2004 case Intrinsic::mips_mini_s_w:
2005 case Intrinsic::mips_mini_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002006 return DAG.getNode(ISD::SMIN, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00002007 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002008 case Intrinsic::mips_mini_u_b:
2009 case Intrinsic::mips_mini_u_h:
2010 case Intrinsic::mips_mini_u_w:
2011 case Intrinsic::mips_mini_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002012 return DAG.getNode(ISD::UMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002013 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00002014 case Intrinsic::mips_mod_s_b:
2015 case Intrinsic::mips_mod_s_h:
2016 case Intrinsic::mips_mod_s_w:
2017 case Intrinsic::mips_mod_s_d:
2018 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
2019 Op->getOperand(2));
2020 case Intrinsic::mips_mod_u_b:
2021 case Intrinsic::mips_mod_u_h:
2022 case Intrinsic::mips_mod_u_w:
2023 case Intrinsic::mips_mod_u_d:
2024 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
2025 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002026 case Intrinsic::mips_mulv_b:
2027 case Intrinsic::mips_mulv_h:
2028 case Intrinsic::mips_mulv_w:
2029 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002030 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
2031 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00002032 case Intrinsic::mips_msubv_b:
2033 case Intrinsic::mips_msubv_h:
2034 case Intrinsic::mips_msubv_w:
2035 case Intrinsic::mips_msubv_d: {
2036 EVT ResTy = Op->getValueType(0);
2037 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
2038 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2039 Op->getOperand(2), Op->getOperand(3)));
2040 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002041 case Intrinsic::mips_nlzc_b:
2042 case Intrinsic::mips_nlzc_h:
2043 case Intrinsic::mips_nlzc_w:
2044 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002045 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002046 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002047 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2048 Op->getOperand(1), Op->getOperand(2));
2049 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002050 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002051 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002052 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2053 Op->getOperand(1),
2054 lowerMSASplatImm(Op, 2, DAG));
2055 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002056 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002057 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002058 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2059 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002060 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002061 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2062 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002063 case Intrinsic::mips_pckev_b:
2064 case Intrinsic::mips_pckev_h:
2065 case Intrinsic::mips_pckev_w:
2066 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002067 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002068 Op->getOperand(1), Op->getOperand(2));
2069 case Intrinsic::mips_pckod_b:
2070 case Intrinsic::mips_pckod_h:
2071 case Intrinsic::mips_pckod_w:
2072 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002073 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002074 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002075 case Intrinsic::mips_pcnt_b:
2076 case Intrinsic::mips_pcnt_h:
2077 case Intrinsic::mips_pcnt_w:
2078 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002079 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002080 case Intrinsic::mips_sat_s_b:
2081 case Intrinsic::mips_sat_s_h:
2082 case Intrinsic::mips_sat_s_w:
2083 case Intrinsic::mips_sat_s_d:
2084 case Intrinsic::mips_sat_u_b:
2085 case Intrinsic::mips_sat_u_h:
2086 case Intrinsic::mips_sat_u_w:
2087 case Intrinsic::mips_sat_u_d: {
2088 // Report an error for out of range values.
2089 int64_t Max;
2090 switch (Intrinsic) {
2091 case Intrinsic::mips_sat_s_b:
2092 case Intrinsic::mips_sat_u_b: Max = 7; break;
2093 case Intrinsic::mips_sat_s_h:
2094 case Intrinsic::mips_sat_u_h: Max = 15; break;
2095 case Intrinsic::mips_sat_s_w:
2096 case Intrinsic::mips_sat_u_w: Max = 31; break;
2097 case Intrinsic::mips_sat_s_d:
2098 case Intrinsic::mips_sat_u_d: Max = 63; break;
2099 default: llvm_unreachable("Unmatched intrinsic");
2100 }
2101 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2102 if (Value < 0 || Value > Max)
2103 report_fatal_error("Immediate out of range");
2104 return SDValue();
2105 }
Daniel Sanders26307182013-09-24 14:20:00 +00002106 case Intrinsic::mips_shf_b:
2107 case Intrinsic::mips_shf_h:
Simon Dardis548a53f2017-01-10 16:40:57 +00002108 case Intrinsic::mips_shf_w: {
2109 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2110 if (Value < 0 || Value > 255)
2111 report_fatal_error("Immediate out of range");
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002112 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002113 Op->getOperand(2), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002114 }
2115 case Intrinsic::mips_sldi_b:
2116 case Intrinsic::mips_sldi_h:
2117 case Intrinsic::mips_sldi_w:
2118 case Intrinsic::mips_sldi_d: {
2119 // Report an error for out of range values.
2120 int64_t Max;
2121 switch (Intrinsic) {
2122 case Intrinsic::mips_sldi_b: Max = 15; break;
2123 case Intrinsic::mips_sldi_h: Max = 7; break;
2124 case Intrinsic::mips_sldi_w: Max = 3; break;
2125 case Intrinsic::mips_sldi_d: Max = 1; break;
2126 default: llvm_unreachable("Unmatched intrinsic");
2127 }
2128 int64_t Value = cast<ConstantSDNode>(Op->getOperand(3))->getSExtValue();
2129 if (Value < 0 || Value > Max)
2130 report_fatal_error("Immediate out of range");
2131 return SDValue();
2132 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002133 case Intrinsic::mips_sll_b:
2134 case Intrinsic::mips_sll_h:
2135 case Intrinsic::mips_sll_w:
2136 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002137 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002138 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002139 case Intrinsic::mips_slli_b:
2140 case Intrinsic::mips_slli_h:
2141 case Intrinsic::mips_slli_w:
2142 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002143 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2144 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002145 case Intrinsic::mips_splat_b:
2146 case Intrinsic::mips_splat_h:
2147 case Intrinsic::mips_splat_w:
2148 case Intrinsic::mips_splat_d:
2149 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2150 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2151 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2152 // Instead we lower to MipsISD::VSHF and match from there.
2153 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002154 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002155 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002156 case Intrinsic::mips_splati_b:
2157 case Intrinsic::mips_splati_h:
2158 case Intrinsic::mips_splati_w:
2159 case Intrinsic::mips_splati_d:
2160 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2161 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2162 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002163 case Intrinsic::mips_sra_b:
2164 case Intrinsic::mips_sra_h:
2165 case Intrinsic::mips_sra_w:
2166 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002167 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002168 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002169 case Intrinsic::mips_srai_b:
2170 case Intrinsic::mips_srai_h:
2171 case Intrinsic::mips_srai_w:
2172 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002173 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2174 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002175 case Intrinsic::mips_srari_b:
2176 case Intrinsic::mips_srari_h:
2177 case Intrinsic::mips_srari_w:
2178 case Intrinsic::mips_srari_d: {
2179 // Report an error for out of range values.
2180 int64_t Max;
2181 switch (Intrinsic) {
2182 case Intrinsic::mips_srari_b: Max = 7; break;
2183 case Intrinsic::mips_srari_h: Max = 15; break;
2184 case Intrinsic::mips_srari_w: Max = 31; break;
2185 case Intrinsic::mips_srari_d: Max = 63; break;
2186 default: llvm_unreachable("Unmatched intrinsic");
2187 }
2188 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2189 if (Value < 0 || Value > Max)
2190 report_fatal_error("Immediate out of range");
2191 return SDValue();
2192 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002193 case Intrinsic::mips_srl_b:
2194 case Intrinsic::mips_srl_h:
2195 case Intrinsic::mips_srl_w:
2196 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002197 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002198 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002199 case Intrinsic::mips_srli_b:
2200 case Intrinsic::mips_srli_h:
2201 case Intrinsic::mips_srli_w:
2202 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002203 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2204 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002205 case Intrinsic::mips_srlri_b:
2206 case Intrinsic::mips_srlri_h:
2207 case Intrinsic::mips_srlri_w:
2208 case Intrinsic::mips_srlri_d: {
2209 // Report an error for out of range values.
2210 int64_t Max;
2211 switch (Intrinsic) {
2212 case Intrinsic::mips_srlri_b: Max = 7; break;
2213 case Intrinsic::mips_srlri_h: Max = 15; break;
2214 case Intrinsic::mips_srlri_w: Max = 31; break;
2215 case Intrinsic::mips_srlri_d: Max = 63; break;
2216 default: llvm_unreachable("Unmatched intrinsic");
2217 }
2218 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2219 if (Value < 0 || Value > Max)
2220 report_fatal_error("Immediate out of range");
2221 return SDValue();
2222 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002223 case Intrinsic::mips_subv_b:
2224 case Intrinsic::mips_subv_h:
2225 case Intrinsic::mips_subv_w:
2226 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002227 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2228 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002229 case Intrinsic::mips_subvi_b:
2230 case Intrinsic::mips_subvi_h:
2231 case Intrinsic::mips_subvi_w:
2232 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002233 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2234 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002235 case Intrinsic::mips_vshf_b:
2236 case Intrinsic::mips_vshf_h:
2237 case Intrinsic::mips_vshf_w:
2238 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002239 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002240 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002241 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002242 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2243 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002244 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002245 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2246 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Marcin Koscielnicki7efdca52016-04-27 17:21:49 +00002247 case Intrinsic::thread_pointer: {
2248 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2249 return DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2250 }
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002251 }
2252}
2253
Simon Dardis548a53f2017-01-10 16:40:57 +00002254static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2255 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002256 SDLoc DL(Op);
2257 SDValue ChainIn = Op->getOperand(0);
2258 SDValue Address = Op->getOperand(2);
2259 SDValue Offset = Op->getOperand(3);
2260 EVT ResTy = Op->getValueType(0);
2261 EVT PtrTy = Address->getValueType(0);
2262
Simon Dardis548a53f2017-01-10 16:40:57 +00002263 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2264 // however takes an i32 signed constant offset. The actual type of the
2265 // intrinsic is a scaled signed i10.
2266 if (Subtarget.isABI_N64())
2267 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2268
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002269 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002270 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(),
2271 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002272}
2273
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002274SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2275 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002276 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2277 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002278 default:
2279 return SDValue();
2280 case Intrinsic::mips_extp:
2281 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2282 case Intrinsic::mips_extpdp:
2283 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2284 case Intrinsic::mips_extr_w:
2285 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2286 case Intrinsic::mips_extr_r_w:
2287 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2288 case Intrinsic::mips_extr_rs_w:
2289 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2290 case Intrinsic::mips_extr_s_h:
2291 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2292 case Intrinsic::mips_mthlip:
2293 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2294 case Intrinsic::mips_mulsaq_s_w_ph:
2295 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2296 case Intrinsic::mips_maq_s_w_phl:
2297 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2298 case Intrinsic::mips_maq_s_w_phr:
2299 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2300 case Intrinsic::mips_maq_sa_w_phl:
2301 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2302 case Intrinsic::mips_maq_sa_w_phr:
2303 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2304 case Intrinsic::mips_dpaq_s_w_ph:
2305 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2306 case Intrinsic::mips_dpsq_s_w_ph:
2307 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2308 case Intrinsic::mips_dpaq_sa_l_w:
2309 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2310 case Intrinsic::mips_dpsq_sa_l_w:
2311 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2312 case Intrinsic::mips_dpaqx_s_w_ph:
2313 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2314 case Intrinsic::mips_dpaqx_sa_w_ph:
2315 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2316 case Intrinsic::mips_dpsqx_s_w_ph:
2317 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2318 case Intrinsic::mips_dpsqx_sa_w_ph:
2319 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002320 case Intrinsic::mips_ld_b:
2321 case Intrinsic::mips_ld_h:
2322 case Intrinsic::mips_ld_w:
2323 case Intrinsic::mips_ld_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002324 return lowerMSALoadIntr(Op, DAG, Intr, Subtarget);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002325 }
2326}
2327
Simon Dardis548a53f2017-01-10 16:40:57 +00002328static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2329 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002330 SDLoc DL(Op);
2331 SDValue ChainIn = Op->getOperand(0);
2332 SDValue Value = Op->getOperand(2);
2333 SDValue Address = Op->getOperand(3);
2334 SDValue Offset = Op->getOperand(4);
2335 EVT PtrTy = Address->getValueType(0);
2336
Simon Dardis548a53f2017-01-10 16:40:57 +00002337 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2338 // however takes an i32 signed constant offset. The actual type of the
2339 // intrinsic is a scaled signed i10.
2340 if (Subtarget.isABI_N64())
2341 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2342
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002343 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2344
Justin Lebar9c375812016-07-15 18:27:10 +00002345 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(),
2346 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002347}
2348
2349SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2350 SelectionDAG &DAG) const {
2351 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2352 switch (Intr) {
2353 default:
2354 return SDValue();
2355 case Intrinsic::mips_st_b:
2356 case Intrinsic::mips_st_h:
2357 case Intrinsic::mips_st_w:
2358 case Intrinsic::mips_st_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002359 return lowerMSAStoreIntr(Op, DAG, Intr, Subtarget);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002360 }
2361}
2362
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002363// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2364//
2365// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2366// choose to sign-extend but we could have equally chosen zero-extend. The
2367// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2368// result into this node later (possibly changing it to a zero-extend in the
2369// process).
2370SDValue MipsSETargetLowering::
2371lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2372 SDLoc DL(Op);
2373 EVT ResTy = Op->getValueType(0);
2374 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002375 EVT VecTy = Op0->getValueType(0);
2376
2377 if (!VecTy.is128BitVector())
2378 return SDValue();
2379
2380 if (ResTy.isInteger()) {
2381 SDValue Op1 = Op->getOperand(1);
2382 EVT EltTy = VecTy.getVectorElementType();
2383 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2384 DAG.getValueType(EltTy));
2385 }
2386
2387 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002388}
2389
Daniel Sandersf49dd822013-09-24 13:33:07 +00002390static bool isConstantOrUndef(const SDValue Op) {
Sanjay Patel57195842016-03-14 17:28:46 +00002391 if (Op->isUndef())
Daniel Sandersf49dd822013-09-24 13:33:07 +00002392 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002393 if (isa<ConstantSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002394 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002395 if (isa<ConstantFPSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002396 return true;
2397 return false;
2398}
2399
2400static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2401 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2402 if (isConstantOrUndef(Op->getOperand(i)))
2403 return true;
2404 return false;
2405}
2406
Daniel Sanders7a289d02013-09-23 12:02:46 +00002407// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2408// backend.
2409//
2410// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002411// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2412// 2 less than or equal to 64 and the value fits into a signed 10-bit
2413// immediate
2414// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2415// is a power of 2 less than or equal to 64 and the value does not fit into a
2416// signed 10-bit immediate
2417// - Non-constant splats are legal as-is.
2418// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2419// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002420SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2421 SelectionDAG &DAG) const {
2422 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2423 EVT ResTy = Op->getValueType(0);
2424 SDLoc DL(Op);
2425 APInt SplatValue, SplatUndef;
2426 unsigned SplatBitSize;
2427 bool HasAnyUndefs;
2428
Eric Christopher1c29a652014-07-18 22:55:25 +00002429 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002430 return SDValue();
2431
2432 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2433 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002434 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002435 // We can only cope with 8, 16, 32, or 64-bit elements
2436 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2437 SplatBitSize != 64)
2438 return SDValue();
2439
Simon Dardis7090d142017-03-10 13:27:14 +00002440 // If the value isn't an integer type we will have to bitcast
2441 // from an integer type first. Also, if there are any undefs, we must
2442 // lower them to defined values first.
2443 if (ResTy.isInteger() && !HasAnyUndefs)
Daniel Sandersf49dd822013-09-24 13:33:07 +00002444 return Op;
2445
2446 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002447
2448 switch (SplatBitSize) {
2449 default:
2450 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002451 case 8:
2452 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002453 break;
2454 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002455 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002456 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002457 case 32:
2458 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002459 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002460 case 64:
2461 // There's no fill.d to fall back on for 64-bit values
2462 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002463 }
2464
Daniel Sanders50b80412013-11-15 12:56:49 +00002465 // SelectionDAG::getConstant will promote SplatValue appropriately.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002466 SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002467
Daniel Sanders50b80412013-11-15 12:56:49 +00002468 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002469 if (ViaVecTy != ResTy)
2470 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002471
2472 return Result;
Simon Pilgrimeb508f82018-12-12 18:32:29 +00002473 } else if (DAG.isSplatValue(Op, /* AllowUndefs */ false))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002474 return Op;
2475 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002476 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2477 // The resulting code is the same length as the expansion, but it doesn't
2478 // use memory operations
2479 EVT ResTy = Node->getValueType(0);
2480
2481 assert(ResTy.isVector());
2482
2483 unsigned NumElts = ResTy.getVectorNumElements();
2484 SDValue Vector = DAG.getUNDEF(ResTy);
2485 for (unsigned i = 0; i < NumElts; ++i) {
2486 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2487 Node->getOperand(i),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002488 DAG.getConstant(i, DL, MVT::i32));
Daniel Sandersf86622b2013-09-24 13:16:15 +00002489 }
2490 return Vector;
2491 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002492
2493 return SDValue();
2494}
2495
Daniel Sanders26307182013-09-24 14:20:00 +00002496// Lower VECTOR_SHUFFLE into SHF (if possible).
2497//
2498// SHF splits the vector into blocks of four elements, then shuffles these
2499// elements according to a <4 x i2> constant (encoded as an integer immediate).
2500//
2501// It is therefore possible to lower into SHF when the mask takes the form:
2502// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2503// When undef's appear they are treated as if they were whatever value is
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002504// necessary in order to fit the above forms.
Daniel Sanders26307182013-09-24 14:20:00 +00002505//
2506// For example:
2507// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2508// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2509// i32 7, i32 6, i32 5, i32 4>
2510// is lowered to:
2511// (SHF_H $w0, $w1, 27)
2512// where the 27 comes from:
2513// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2514static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2515 SmallVector<int, 16> Indices,
2516 SelectionDAG &DAG) {
2517 int SHFIndices[4] = { -1, -1, -1, -1 };
2518
2519 if (Indices.size() < 4)
2520 return SDValue();
2521
2522 for (unsigned i = 0; i < 4; ++i) {
2523 for (unsigned j = i; j < Indices.size(); j += 4) {
2524 int Idx = Indices[j];
2525
2526 // Convert from vector index to 4-element subvector index
2527 // If an index refers to an element outside of the subvector then give up
2528 if (Idx != -1) {
2529 Idx -= 4 * (j / 4);
2530 if (Idx < 0 || Idx >= 4)
2531 return SDValue();
2532 }
2533
2534 // If the mask has an undef, replace it with the current index.
2535 // Note that it might still be undef if the current index is also undef
2536 if (SHFIndices[i] == -1)
2537 SHFIndices[i] = Idx;
2538
2539 // Check that non-undef values are the same as in the mask. If they
2540 // aren't then give up
2541 if (!(Idx == -1 || Idx == SHFIndices[i]))
2542 return SDValue();
2543 }
2544 }
2545
2546 // Calculate the immediate. Replace any remaining undefs with zero
2547 APInt Imm(32, 0);
2548 for (int i = 3; i >= 0; --i) {
2549 int Idx = SHFIndices[i];
2550
2551 if (Idx == -1)
2552 Idx = 0;
2553
2554 Imm <<= 2;
2555 Imm |= Idx & 0x3;
2556 }
2557
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002558 SDLoc DL(Op);
2559 return DAG.getNode(MipsISD::SHF, DL, ResTy,
2560 DAG.getConstant(Imm, DL, MVT::i32), Op->getOperand(0));
Daniel Sanders26307182013-09-24 14:20:00 +00002561}
2562
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002563/// Determine whether a range fits a regular pattern of values.
2564/// This function accounts for the possibility of jumping over the End iterator.
2565template <typename ValType>
2566static bool
2567fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,
2568 unsigned CheckStride,
2569 typename SmallVectorImpl<ValType>::const_iterator End,
2570 ValType ExpectedIndex, unsigned ExpectedIndexStride) {
2571 auto &I = Begin;
2572
2573 while (I != End) {
2574 if (*I != -1 && *I != ExpectedIndex)
2575 return false;
2576 ExpectedIndex += ExpectedIndexStride;
2577
2578 // Incrementing past End is undefined behaviour so we must increment one
2579 // step at a time and check for End at each step.
2580 for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
2581 ; // Empty loop body.
2582 }
2583 return true;
2584}
2585
2586// Determine whether VECTOR_SHUFFLE is a SPLATI.
2587//
2588// It is a SPLATI when the mask is:
2589// <x, x, x, ...>
2590// where x is any valid index.
2591//
2592// When undef's appear in the mask they are treated as if they were whatever
2593// value is necessary in order to fit the above form.
2594static bool isVECTOR_SHUFFLE_SPLATI(SDValue Op, EVT ResTy,
2595 SmallVector<int, 16> Indices,
2596 SelectionDAG &DAG) {
2597 assert((Indices.size() % 2) == 0);
2598
2599 int SplatIndex = -1;
2600 for (const auto &V : Indices) {
2601 if (V != -1) {
2602 SplatIndex = V;
2603 break;
2604 }
2605 }
2606
2607 return fitsRegularPattern<int>(Indices.begin(), 1, Indices.end(), SplatIndex,
2608 0);
2609}
2610
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002611// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2612//
2613// ILVEV interleaves the even elements from each vector.
2614//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002615// It is possible to lower into ILVEV when the mask consists of two of the
2616// following forms interleaved:
2617// <0, 2, 4, ...>
2618// <n, n+2, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002619// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002620// For example:
2621// <0, 0, 2, 2, 4, 4, ...>
2622// <0, n, 2, n+2, 4, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002623//
2624// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002625// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002626static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2627 SmallVector<int, 16> Indices,
2628 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002629 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002630
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002631 SDValue Wt;
2632 SDValue Ws;
2633 const auto &Begin = Indices.begin();
2634 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002635
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002636 // Check even elements are taken from the even elements of one half or the
2637 // other and pick an operand accordingly.
2638 if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
2639 Wt = Op->getOperand(0);
2640 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 2))
2641 Wt = Op->getOperand(1);
2642 else
2643 return SDValue();
2644
2645 // Check odd elements are taken from the even elements of one half or the
2646 // other and pick an operand accordingly.
2647 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
2648 Ws = Op->getOperand(0);
2649 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 2))
2650 Ws = Op->getOperand(1);
2651 else
2652 return SDValue();
2653
2654 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002655}
2656
2657// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2658//
2659// ILVOD interleaves the odd elements from each vector.
2660//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002661// It is possible to lower into ILVOD when the mask consists of two of the
2662// following forms interleaved:
2663// <1, 3, 5, ...>
2664// <n+1, n+3, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002665// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002666// For example:
2667// <1, 1, 3, 3, 5, 5, ...>
2668// <1, n+1, 3, n+3, 5, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002669//
2670// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002671// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002672static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2673 SmallVector<int, 16> Indices,
2674 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002675 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002676
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002677 SDValue Wt;
2678 SDValue Ws;
2679 const auto &Begin = Indices.begin();
2680 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002681
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002682 // Check even elements are taken from the odd elements of one half or the
2683 // other and pick an operand accordingly.
2684 if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
2685 Wt = Op->getOperand(0);
2686 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + 1, 2))
2687 Wt = Op->getOperand(1);
2688 else
2689 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002690
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002691 // Check odd elements are taken from the odd elements of one half or the
2692 // other and pick an operand accordingly.
2693 if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
2694 Ws = Op->getOperand(0);
2695 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + 1, 2))
2696 Ws = Op->getOperand(1);
2697 else
2698 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002699
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002700 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Wt, Ws);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002701}
2702
2703// Lower VECTOR_SHUFFLE into ILVR (if possible).
2704//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002705// ILVR interleaves consecutive elements from the right (lowest-indexed) half of
2706// each vector.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002707//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002708// It is possible to lower into ILVR when the mask consists of two of the
2709// following forms interleaved:
2710// <0, 1, 2, ...>
2711// <n, n+1, n+2, ...>
2712// where n is the number of elements in the vector.
2713// For example:
2714// <0, 0, 1, 1, 2, 2, ...>
2715// <0, n, 1, n+1, 2, n+2, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002716//
2717// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002718// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002719static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2720 SmallVector<int, 16> Indices,
2721 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002722 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002723
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002724 SDValue Wt;
2725 SDValue Ws;
2726 const auto &Begin = Indices.begin();
2727 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002728
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002729 // Check even elements are taken from the right (lowest-indexed) elements of
2730 // one half or the other and pick an operand accordingly.
2731 if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
2732 Wt = Op->getOperand(0);
2733 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 1))
2734 Wt = Op->getOperand(1);
2735 else
2736 return SDValue();
2737
2738 // Check odd elements are taken from the right (lowest-indexed) elements of
2739 // one half or the other and pick an operand accordingly.
2740 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
2741 Ws = Op->getOperand(0);
2742 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 1))
2743 Ws = Op->getOperand(1);
2744 else
2745 return SDValue();
2746
2747 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Ws, Wt);
2748}
2749
2750// Lower VECTOR_SHUFFLE into ILVL (if possible).
2751//
2752// ILVL interleaves consecutive elements from the left (highest-indexed) half
2753// of each vector.
2754//
2755// It is possible to lower into ILVL when the mask consists of two of the
2756// following forms interleaved:
2757// <x, x+1, x+2, ...>
2758// <n+x, n+x+1, n+x+2, ...>
2759// where n is the number of elements in the vector and x is half n.
2760// For example:
2761// <x, x, x+1, x+1, x+2, x+2, ...>
2762// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2763//
2764// When undef's appear in the mask they are treated as if they were whatever
2765// value is necessary in order to fit the above forms.
2766static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2767 SmallVector<int, 16> Indices,
2768 SelectionDAG &DAG) {
2769 assert((Indices.size() % 2) == 0);
2770
2771 unsigned HalfSize = Indices.size() / 2;
2772 SDValue Wt;
2773 SDValue Ws;
2774 const auto &Begin = Indices.begin();
2775 const auto &End = Indices.end();
2776
2777 // Check even elements are taken from the left (highest-indexed) elements of
2778 // one half or the other and pick an operand accordingly.
2779 if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
2780 Wt = Op->getOperand(0);
2781 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + HalfSize, 1))
2782 Wt = Op->getOperand(1);
2783 else
2784 return SDValue();
2785
2786 // Check odd elements are taken from the left (highest-indexed) elements of
2787 // one half or the other and pick an operand accordingly.
2788 if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
2789 Ws = Op->getOperand(0);
2790 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + HalfSize,
2791 1))
2792 Ws = Op->getOperand(1);
2793 else
2794 return SDValue();
2795
2796 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002797}
2798
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002799// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2800//
2801// PCKEV copies the even elements of each vector into the result vector.
2802//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002803// It is possible to lower into PCKEV when the mask consists of two of the
2804// following forms concatenated:
2805// <0, 2, 4, ...>
2806// <n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002807// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002808// For example:
2809// <0, 2, 4, ..., 0, 2, 4, ...>
2810// <0, 2, 4, ..., n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002811//
2812// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002813// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002814static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2815 SmallVector<int, 16> Indices,
2816 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002817 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002818
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002819 SDValue Wt;
2820 SDValue Ws;
2821 const auto &Begin = Indices.begin();
2822 const auto &Mid = Indices.begin() + Indices.size() / 2;
2823 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002824
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002825 if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
2826 Wt = Op->getOperand(0);
2827 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size(), 2))
2828 Wt = Op->getOperand(1);
2829 else
2830 return SDValue();
2831
2832 if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
2833 Ws = Op->getOperand(0);
2834 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size(), 2))
2835 Ws = Op->getOperand(1);
2836 else
2837 return SDValue();
2838
2839 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002840}
2841
2842// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2843//
2844// PCKOD copies the odd elements of each vector into the result vector.
2845//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002846// It is possible to lower into PCKOD when the mask consists of two of the
2847// following forms concatenated:
2848// <1, 3, 5, ...>
2849// <n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002850// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002851// For example:
2852// <1, 3, 5, ..., 1, 3, 5, ...>
2853// <1, 3, 5, ..., n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002854//
2855// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002856// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002857static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2858 SmallVector<int, 16> Indices,
2859 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002860 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002861
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002862 SDValue Wt;
2863 SDValue Ws;
2864 const auto &Begin = Indices.begin();
2865 const auto &Mid = Indices.begin() + Indices.size() / 2;
2866 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002867
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002868 if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
2869 Wt = Op->getOperand(0);
2870 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size() + 1, 2))
2871 Wt = Op->getOperand(1);
2872 else
2873 return SDValue();
2874
2875 if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
2876 Ws = Op->getOperand(0);
2877 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size() + 1, 2))
2878 Ws = Op->getOperand(1);
2879 else
2880 return SDValue();
2881
2882 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002883}
2884
Daniel Sanderse5087042013-09-24 14:02:15 +00002885// Lower VECTOR_SHUFFLE into VSHF.
2886//
2887// This mostly consists of converting the shuffle indices in Indices into a
2888// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2889// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2890// if the type is v8i16 and all the indices are less than 8 then the second
2891// operand is unused and can be replaced with anything. We choose to replace it
2892// with the used operand since this reduces the number of instructions overall.
2893static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2894 SmallVector<int, 16> Indices,
2895 SelectionDAG &DAG) {
2896 SmallVector<SDValue, 16> Ops;
2897 SDValue Op0;
2898 SDValue Op1;
2899 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2900 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2901 bool Using1stVec = false;
2902 bool Using2ndVec = false;
2903 SDLoc DL(Op);
2904 int ResTyNumElts = ResTy.getVectorNumElements();
2905
2906 for (int i = 0; i < ResTyNumElts; ++i) {
2907 // Idx == -1 means UNDEF
2908 int Idx = Indices[i];
2909
2910 if (0 <= Idx && Idx < ResTyNumElts)
2911 Using1stVec = true;
2912 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2913 Using2ndVec = true;
2914 }
2915
2916 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2917 ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002918 Ops.push_back(DAG.getTargetConstant(*I, DL, MaskEltTy));
Daniel Sanderse5087042013-09-24 14:02:15 +00002919
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002920 SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002921
2922 if (Using1stVec && Using2ndVec) {
2923 Op0 = Op->getOperand(0);
2924 Op1 = Op->getOperand(1);
2925 } else if (Using1stVec)
2926 Op0 = Op1 = Op->getOperand(0);
2927 else if (Using2ndVec)
2928 Op0 = Op1 = Op->getOperand(1);
2929 else
2930 llvm_unreachable("shuffle vector mask references neither vector operand?");
2931
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002932 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2933 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2934 // VSHF concatenates the vectors in a bitwise fashion:
2935 // <0b00, 0b01> + <0b10, 0b11> ->
2936 // 0b0100 + 0b1110 -> 0b01001110
2937 // <0b10, 0b11, 0b00, 0b01>
2938 // We must therefore swap the operands to get the correct result.
2939 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002940}
2941
2942// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2943// indices in the shuffle.
2944SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2945 SelectionDAG &DAG) const {
2946 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2947 EVT ResTy = Op->getValueType(0);
2948
2949 if (!ResTy.is128BitVector())
2950 return SDValue();
2951
2952 int ResTyNumElts = ResTy.getVectorNumElements();
2953 SmallVector<int, 16> Indices;
2954
2955 for (int i = 0; i < ResTyNumElts; ++i)
2956 Indices.push_back(Node->getMaskElt(i));
2957
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002958 // splati.[bhwd] is preferable to the others but is matched from
2959 // MipsISD::VSHF.
2960 if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
2961 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002962 SDValue Result;
2963 if ((Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002964 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002965 if ((Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002966 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002967 if ((Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002968 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002969 if ((Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002970 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002971 if ((Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002972 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002973 if ((Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002974 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002975 if ((Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG)))
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002976 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002977 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2978}
2979
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00002980MachineBasicBlock *
2981MipsSETargetLowering::emitBPOSGE32(MachineInstr &MI,
2982 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002983 // $bb:
2984 // bposge32_pseudo $vr0
2985 // =>
2986 // $bb:
2987 // bposge32 $tbb
2988 // $fbb:
2989 // li $vr2, 0
2990 // b $sink
2991 // $tbb:
2992 // li $vr1, 1
2993 // $sink:
2994 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2995
2996 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002997 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002998 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00002999 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003000 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003001 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003002 MachineFunction *F = BB->getParent();
3003 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3004 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3005 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3006 F->insert(It, FBB);
3007 F->insert(It, TBB);
3008 F->insert(It, Sink);
3009
3010 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003011 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003012 BB->end());
3013 Sink->transferSuccessorsAndUpdatePHIs(BB);
3014
3015 // Add successors.
3016 BB->addSuccessor(FBB);
3017 BB->addSuccessor(TBB);
3018 FBB->addSuccessor(Sink);
3019 TBB->addSuccessor(Sink);
3020
3021 // Insert the real bposge32 instruction to $BB.
3022 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +00003023 // Insert the real bposge32c instruction to $BB.
3024 BuildMI(BB, DL, TII->get(Mips::BPOSGE32C_MMR3)).addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003025
3026 // Fill $FBB.
3027 unsigned VR2 = RegInfo.createVirtualRegister(RC);
3028 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
3029 .addReg(Mips::ZERO).addImm(0);
3030 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3031
3032 // Fill $TBB.
3033 unsigned VR1 = RegInfo.createVirtualRegister(RC);
3034 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
3035 .addReg(Mips::ZERO).addImm(1);
3036
3037 // Insert phi function to $Sink.
3038 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003039 MI.getOperand(0).getReg())
3040 .addReg(VR2)
3041 .addMBB(FBB)
3042 .addReg(VR1)
3043 .addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003044
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003045 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003046 return Sink;
3047}
Daniel Sandersce09d072013-08-28 12:14:50 +00003048
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003049MachineBasicBlock *MipsSETargetLowering::emitMSACBranchPseudo(
3050 MachineInstr &MI, MachineBasicBlock *BB, unsigned BranchOp) const {
Daniel Sandersce09d072013-08-28 12:14:50 +00003051 // $bb:
3052 // vany_nonzero $rd, $ws
3053 // =>
3054 // $bb:
3055 // bnz.b $ws, $tbb
3056 // b $fbb
3057 // $fbb:
3058 // li $rd1, 0
3059 // b $sink
3060 // $tbb:
3061 // li $rd2, 1
3062 // $sink:
3063 // $rd = phi($rd1, $fbb, $rd2, $tbb)
3064
3065 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003066 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersce09d072013-08-28 12:14:50 +00003067 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003068 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersce09d072013-08-28 12:14:50 +00003069 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003070 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00003071 MachineFunction *F = BB->getParent();
3072 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3073 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3074 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3075 F->insert(It, FBB);
3076 F->insert(It, TBB);
3077 F->insert(It, Sink);
3078
3079 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003080 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00003081 BB->end());
3082 Sink->transferSuccessorsAndUpdatePHIs(BB);
3083
3084 // Add successors.
3085 BB->addSuccessor(FBB);
3086 BB->addSuccessor(TBB);
3087 FBB->addSuccessor(Sink);
3088 TBB->addSuccessor(Sink);
3089
3090 // Insert the real bnz.b instruction to $BB.
3091 BuildMI(BB, DL, TII->get(BranchOp))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003092 .addReg(MI.getOperand(1).getReg())
3093 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003094
3095 // Fill $FBB.
3096 unsigned RD1 = RegInfo.createVirtualRegister(RC);
3097 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
3098 .addReg(Mips::ZERO).addImm(0);
3099 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3100
3101 // Fill $TBB.
3102 unsigned RD2 = RegInfo.createVirtualRegister(RC);
3103 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
3104 .addReg(Mips::ZERO).addImm(1);
3105
3106 // Insert phi function to $Sink.
3107 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003108 MI.getOperand(0).getReg())
3109 .addReg(RD1)
3110 .addMBB(FBB)
3111 .addReg(RD2)
3112 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003113
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003114 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersce09d072013-08-28 12:14:50 +00003115 return Sink;
3116}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003117
3118// Emit the COPY_FW pseudo instruction.
3119//
3120// copy_fw_pseudo $fd, $ws, n
3121// =>
3122// copy_u_w $rt, $ws, $n
3123// mtc1 $rt, $fd
3124//
3125// When n is zero, the equivalent operation can be performed with (potentially)
3126// zero instructions due to register overlaps. This optimization is never valid
3127// 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 +00003128MachineBasicBlock *
3129MipsSETargetLowering::emitCOPY_FW(MachineInstr &MI,
3130 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003131 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003132 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003133 DebugLoc DL = MI.getDebugLoc();
3134 unsigned Fd = MI.getOperand(0).getReg();
3135 unsigned Ws = MI.getOperand(1).getReg();
3136 unsigned Lane = MI.getOperand(2).getImm();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003137
Daniel Sandersafe27c72015-02-23 17:22:16 +00003138 if (Lane == 0) {
3139 unsigned Wt = Ws;
3140 if (!Subtarget.useOddSPReg()) {
3141 // We must copy to an even-numbered MSA register so that the
3142 // single-precision sub-register is also guaranteed to be even-numbered.
3143 Wt = RegInfo.createVirtualRegister(&Mips::MSA128WEvensRegClass);
3144
3145 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Wt).addReg(Ws);
3146 }
3147
3148 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3149 } else {
3150 unsigned Wt = RegInfo.createVirtualRegister(
3151 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3152 &Mips::MSA128WEvensRegClass);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003153
Daniel Sandersd9207702014-03-04 13:54:30 +00003154 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003155 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3156 }
3157
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003158 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003159 return BB;
3160}
3161
3162// Emit the COPY_FD pseudo instruction.
3163//
3164// copy_fd_pseudo $fd, $ws, n
3165// =>
3166// splati.d $wt, $ws, $n
3167// copy $fd, $wt:sub_64
3168//
3169// When n is zero, the equivalent operation can be performed with (potentially)
3170// zero instructions due to register overlaps. This optimization is always
3171// valid because FR=1 mode which is the only supported mode in MSA.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003172MachineBasicBlock *
3173MipsSETargetLowering::emitCOPY_FD(MachineInstr &MI,
3174 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003175 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003176
Eric Christopher96e72c62015-01-29 23:27:36 +00003177 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003178 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003179 unsigned Fd = MI.getOperand(0).getReg();
3180 unsigned Ws = MI.getOperand(1).getReg();
3181 unsigned Lane = MI.getOperand(2).getImm() * 2;
3182 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003183
3184 if (Lane == 0)
3185 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
3186 else {
3187 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3188
3189 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
3190 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
3191 }
3192
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003193 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003194 return BB;
3195}
Daniel Sandersa5150702013-09-27 12:31:32 +00003196
3197// Emit the INSERT_FW pseudo instruction.
3198//
3199// insert_fw_pseudo $wd, $wd_in, $n, $fs
3200// =>
3201// subreg_to_reg $wt:sub_lo, $fs
3202// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003203MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003204MipsSETargetLowering::emitINSERT_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003205 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003206 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003207 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003208 DebugLoc DL = MI.getDebugLoc();
3209 unsigned Wd = MI.getOperand(0).getReg();
3210 unsigned Wd_in = MI.getOperand(1).getReg();
3211 unsigned Lane = MI.getOperand(2).getImm();
3212 unsigned Fs = MI.getOperand(3).getReg();
Daniel Sandersafe27c72015-02-23 17:22:16 +00003213 unsigned Wt = RegInfo.createVirtualRegister(
3214 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3215 &Mips::MSA128WEvensRegClass);
Daniel Sandersa5150702013-09-27 12:31:32 +00003216
3217 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003218 .addImm(0)
3219 .addReg(Fs)
3220 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00003221 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003222 .addReg(Wd_in)
3223 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003224 .addReg(Wt)
3225 .addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003226
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003227 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003228 return BB;
3229}
3230
3231// Emit the INSERT_FD pseudo instruction.
3232//
3233// insert_fd_pseudo $wd, $fs, n
3234// =>
3235// subreg_to_reg $wt:sub_64, $fs
3236// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003237MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003238MipsSETargetLowering::emitINSERT_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003239 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003240 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00003241
Eric Christopher96e72c62015-01-29 23:27:36 +00003242 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003243 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003244 DebugLoc DL = MI.getDebugLoc();
3245 unsigned Wd = MI.getOperand(0).getReg();
3246 unsigned Wd_in = MI.getOperand(1).getReg();
3247 unsigned Lane = MI.getOperand(2).getImm();
3248 unsigned Fs = MI.getOperand(3).getReg();
Daniel Sandersa5150702013-09-27 12:31:32 +00003249 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3250
3251 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003252 .addImm(0)
3253 .addReg(Fs)
3254 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00003255 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003256 .addReg(Wd_in)
3257 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003258 .addReg(Wt)
3259 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003260
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003261 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003262 return BB;
3263}
3264
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003265// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3266//
3267// For integer:
3268// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3269// =>
3270// (SLL $lanetmp1, $lane, <log2size)
3271// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3272// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3273// (NEG $lanetmp2, $lanetmp1)
3274// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3275//
3276// For floating point:
3277// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3278// =>
3279// (SUBREG_TO_REG $wt, $fs, <subreg>)
3280// (SLL $lanetmp1, $lane, <log2size)
3281// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3282// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3283// (NEG $lanetmp2, $lanetmp1)
3284// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003285MachineBasicBlock *MipsSETargetLowering::emitINSERT_DF_VIDX(
3286 MachineInstr &MI, MachineBasicBlock *BB, unsigned EltSizeInBytes,
3287 bool IsFP) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003288 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003289 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003290 DebugLoc DL = MI.getDebugLoc();
3291 unsigned Wd = MI.getOperand(0).getReg();
3292 unsigned SrcVecReg = MI.getOperand(1).getReg();
3293 unsigned LaneReg = MI.getOperand(2).getReg();
3294 unsigned SrcValReg = MI.getOperand(3).getReg();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003295
3296 const TargetRegisterClass *VecRC = nullptr;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003297 // FIXME: This should be true for N32 too.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003298 const TargetRegisterClass *GPRRC =
Daniel Sanders4160c802015-05-05 08:48:35 +00003299 Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003300 unsigned SubRegIdx = Subtarget.isABI_N64() ? Mips::sub_32 : 0;
3301 unsigned ShiftOp = Subtarget.isABI_N64() ? Mips::DSLL : Mips::SLL;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003302 unsigned EltLog2Size;
3303 unsigned InsertOp = 0;
3304 unsigned InsveOp = 0;
3305 switch (EltSizeInBytes) {
3306 default:
3307 llvm_unreachable("Unexpected size");
3308 case 1:
3309 EltLog2Size = 0;
3310 InsertOp = Mips::INSERT_B;
3311 InsveOp = Mips::INSVE_B;
3312 VecRC = &Mips::MSA128BRegClass;
3313 break;
3314 case 2:
3315 EltLog2Size = 1;
3316 InsertOp = Mips::INSERT_H;
3317 InsveOp = Mips::INSVE_H;
3318 VecRC = &Mips::MSA128HRegClass;
3319 break;
3320 case 4:
3321 EltLog2Size = 2;
3322 InsertOp = Mips::INSERT_W;
3323 InsveOp = Mips::INSVE_W;
3324 VecRC = &Mips::MSA128WRegClass;
3325 break;
3326 case 8:
3327 EltLog2Size = 3;
3328 InsertOp = Mips::INSERT_D;
3329 InsveOp = Mips::INSVE_D;
3330 VecRC = &Mips::MSA128DRegClass;
3331 break;
3332 }
3333
3334 if (IsFP) {
3335 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3336 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3337 .addImm(0)
3338 .addReg(SrcValReg)
3339 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3340 SrcValReg = Wt;
3341 }
3342
3343 // Convert the lane index into a byte index
3344 if (EltSizeInBytes != 1) {
3345 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003346 BuildMI(*BB, MI, DL, TII->get(ShiftOp), LaneTmp1)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003347 .addReg(LaneReg)
3348 .addImm(EltLog2Size);
3349 LaneReg = LaneTmp1;
3350 }
3351
3352 // Rotate bytes around so that the desired lane is element zero
3353 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3354 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3355 .addReg(SrcVecReg)
3356 .addReg(SrcVecReg)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003357 .addReg(LaneReg, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003358
3359 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3360 if (IsFP) {
3361 // Use insve.df to insert to element zero
3362 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3363 .addReg(WdTmp1)
3364 .addImm(0)
3365 .addReg(SrcValReg)
3366 .addImm(0);
3367 } else {
3368 // Use insert.df to insert to element zero
3369 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3370 .addReg(WdTmp1)
3371 .addReg(SrcValReg)
3372 .addImm(0);
3373 }
3374
3375 // Rotate elements the rest of the way for a full rotation.
3376 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3377 // the lane index to do this.
3378 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sanders4160c802015-05-05 08:48:35 +00003379 BuildMI(*BB, MI, DL, TII->get(Subtarget.isABI_N64() ? Mips::DSUB : Mips::SUB),
3380 LaneTmp2)
3381 .addReg(Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003382 .addReg(LaneReg);
3383 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3384 .addReg(WdTmp2)
3385 .addReg(WdTmp2)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003386 .addReg(LaneTmp2, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003387
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003388 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003389 return BB;
3390}
3391
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003392// Emit the FILL_FW pseudo instruction.
3393//
3394// fill_fw_pseudo $wd, $fs
3395// =>
3396// implicit_def $wt1
3397// insert_subreg $wt2:subreg_lo, $wt1, $fs
3398// splati.w $wd, $wt2[0]
3399MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003400MipsSETargetLowering::emitFILL_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003401 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003402 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003403 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003404 DebugLoc DL = MI.getDebugLoc();
3405 unsigned Wd = MI.getOperand(0).getReg();
3406 unsigned Fs = MI.getOperand(1).getReg();
Simon Dardis0e9e2372017-01-10 15:53:10 +00003407 unsigned Wt1 = RegInfo.createVirtualRegister(
3408 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3409 : &Mips::MSA128WEvensRegClass);
3410 unsigned Wt2 = RegInfo.createVirtualRegister(
3411 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3412 : &Mips::MSA128WEvensRegClass);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003413
3414 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3415 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3416 .addReg(Wt1)
3417 .addReg(Fs)
3418 .addImm(Mips::sub_lo);
3419 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3420
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003421 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003422 return BB;
3423}
3424
3425// Emit the FILL_FD pseudo instruction.
3426//
3427// fill_fd_pseudo $wd, $fs
3428// =>
3429// implicit_def $wt1
3430// insert_subreg $wt2:subreg_64, $wt1, $fs
3431// splati.d $wd, $wt2[0]
3432MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003433MipsSETargetLowering::emitFILL_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003434 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003435 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003436
Eric Christopher96e72c62015-01-29 23:27:36 +00003437 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003438 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003439 DebugLoc DL = MI.getDebugLoc();
3440 unsigned Wd = MI.getOperand(0).getReg();
3441 unsigned Fs = MI.getOperand(1).getReg();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003442 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3443 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3444
3445 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3446 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3447 .addReg(Wt1)
3448 .addReg(Fs)
3449 .addImm(Mips::sub_64);
3450 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003451
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003452 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003453 return BB;
3454}
Daniel Sandersa9521602013-10-23 10:36:52 +00003455
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003456// Emit the ST_F16_PSEDUO instruction to store a f16 value from an MSA
3457// register.
3458//
3459// STF16 MSA128F16:$wd, mem_simm10:$addr
3460// =>
3461// copy_u.h $rtemp,$wd[0]
3462// sh $rtemp, $addr
3463//
3464// Safety: We can't use st.h & co as they would over write the memory after
3465// the destination. It would require half floats be allocated 16 bytes(!) of
3466// space.
3467MachineBasicBlock *
3468MipsSETargetLowering::emitST_F16_PSEUDO(MachineInstr &MI,
3469 MachineBasicBlock *BB) const {
3470
3471 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3472 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3473 DebugLoc DL = MI.getDebugLoc();
3474 unsigned Ws = MI.getOperand(0).getReg();
3475 unsigned Rt = MI.getOperand(1).getReg();
3476 const MachineMemOperand &MMO = **MI.memoperands_begin();
3477 unsigned Imm = MMO.getOffset();
3478
3479 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3480 // spill and reload can expand as a GPR64 operand. Examine the
3481 // operand in detail and default to ABI.
3482 const TargetRegisterClass *RC =
3483 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3484 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3485 : &Mips::GPR64RegClass);
3486 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003487 unsigned Rs = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003488
3489 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_U_H), Rs).addReg(Ws).addImm(0);
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003490 if(!UsingMips32) {
3491 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR64RegClass);
3492 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Tmp)
3493 .addImm(0)
3494 .addReg(Rs)
3495 .addImm(Mips::sub_32);
3496 Rs = Tmp;
3497 }
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003498 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::SH : Mips::SH64))
3499 .addReg(Rs)
3500 .addReg(Rt)
3501 .addImm(Imm)
3502 .addMemOperand(BB->getParent()->getMachineMemOperand(
3503 &MMO, MMO.getOffset(), MMO.getSize()));
3504
3505 MI.eraseFromParent();
3506 return BB;
3507}
3508
3509// Emit the LD_F16_PSEDUO instruction to load a f16 value into an MSA register.
3510//
3511// LD_F16 MSA128F16:$wd, mem_simm10:$addr
3512// =>
3513// lh $rtemp, $addr
3514// fill.h $wd, $rtemp
3515//
3516// Safety: We can't use ld.h & co as they over-read from the source.
3517// Additionally, if the address is not modulo 16, 2 cases can occur:
3518// a) Segmentation fault as the load instruction reads from a memory page
3519// memory it's not supposed to.
3520// b) The load crosses an implementation specific boundary, requiring OS
3521// intervention.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003522MachineBasicBlock *
3523MipsSETargetLowering::emitLD_F16_PSEUDO(MachineInstr &MI,
3524 MachineBasicBlock *BB) const {
3525
3526 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3527 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3528 DebugLoc DL = MI.getDebugLoc();
3529 unsigned Wd = MI.getOperand(0).getReg();
3530
3531 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3532 // spill and reload can expand as a GPR64 operand. Examine the
3533 // operand in detail and default to ABI.
3534 const TargetRegisterClass *RC =
3535 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3536 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3537 : &Mips::GPR64RegClass);
3538
3539 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
3540 unsigned Rt = RegInfo.createVirtualRegister(RC);
3541
3542 MachineInstrBuilder MIB =
3543 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::LH : Mips::LH64), Rt);
3544 for (unsigned i = 1; i < MI.getNumOperands(); i++)
Diana Picus116bbab2017-01-13 09:58:52 +00003545 MIB.add(MI.getOperand(i));
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003546
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003547 if(!UsingMips32) {
3548 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
3549 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Tmp).addReg(Rt, 0, Mips::sub_32);
3550 Rt = Tmp;
3551 }
3552
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003553 BuildMI(*BB, MI, DL, TII->get(Mips::FILL_H), Wd).addReg(Rt);
3554
3555 MI.eraseFromParent();
3556 return BB;
3557}
3558
3559// Emit the FPROUND_PSEUDO instruction.
3560//
3561// Round an FGR64Opnd, FGR32Opnd to an f16.
3562//
3563// Safety: Cycle the operand through the GPRs so the result always ends up
3564// the correct MSA register.
3565//
3566// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fs
3567// / FGR64Opnd:$Fs and MSA128F16:$Wd to the same physical register
3568// (which they can be, as the MSA registers are defined to alias the
3569// FPU's 64 bit and 32 bit registers) the result can be accessed using
3570// the correct register class. That requires operands be tie-able across
3571// register classes which have a sub/super register class relationship.
3572//
3573// For FPG32Opnd:
3574//
3575// FPROUND MSA128F16:$wd, FGR32Opnd:$fs
3576// =>
3577// mfc1 $rtemp, $fs
3578// fill.w $rtemp, $wtemp
3579// fexdo.w $wd, $wtemp, $wtemp
3580//
3581// For FPG64Opnd on mips32r2+:
3582//
3583// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3584// =>
3585// mfc1 $rtemp, $fs
3586// fill.w $rtemp, $wtemp
3587// mfhc1 $rtemp2, $fs
3588// insert.w $wtemp[1], $rtemp2
3589// insert.w $wtemp[3], $rtemp2
3590// fexdo.w $wtemp2, $wtemp, $wtemp
3591// fexdo.h $wd, $temp2, $temp2
3592//
3593// For FGR64Opnd on mips64r2+:
3594//
3595// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3596// =>
3597// dmfc1 $rtemp, $fs
3598// fill.d $rtemp, $wtemp
3599// fexdo.w $wtemp2, $wtemp, $wtemp
3600// fexdo.h $wd, $wtemp2, $wtemp2
3601//
3602// Safety note: As $wtemp is UNDEF, we may provoke a spurious exception if the
3603// undef bits are "just right" and the exception enable bits are
3604// set. By using fill.w to replicate $fs into all elements over
3605// insert.w for one element, we avoid that potiential case. If
3606// fexdo.[hw] causes an exception in, the exception is valid and it
3607// occurs for all elements.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003608MachineBasicBlock *
3609MipsSETargetLowering::emitFPROUND_PSEUDO(MachineInstr &MI,
3610 MachineBasicBlock *BB,
3611 bool IsFGR64) const {
3612
3613 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3614 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3615 // it.
3616 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3617
3618 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003619 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003620
3621 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3622 DebugLoc DL = MI.getDebugLoc();
3623 unsigned Wd = MI.getOperand(0).getReg();
3624 unsigned Fs = MI.getOperand(1).getReg();
3625
3626 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3627 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3628 const TargetRegisterClass *GPRRC =
3629 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003630 unsigned MFC1Opc = IsFGR64onMips64
3631 ? Mips::DMFC1
3632 : (IsFGR64onMips32 ? Mips::MFC1_D64 : Mips::MFC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003633 unsigned FILLOpc = IsFGR64onMips64 ? Mips::FILL_D : Mips::FILL_W;
3634
3635 // Perform the register class copy as mentioned above.
3636 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3637 BuildMI(*BB, MI, DL, TII->get(MFC1Opc), Rtemp).addReg(Fs);
3638 BuildMI(*BB, MI, DL, TII->get(FILLOpc), Wtemp).addReg(Rtemp);
3639 unsigned WPHI = Wtemp;
3640
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003641 if (IsFGR64onMips32) {
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003642 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3643 BuildMI(*BB, MI, DL, TII->get(Mips::MFHC1_D64), Rtemp2).addReg(Fs);
3644 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3645 unsigned Wtemp3 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3646 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp2)
3647 .addReg(Wtemp)
3648 .addReg(Rtemp2)
3649 .addImm(1);
3650 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp3)
3651 .addReg(Wtemp2)
3652 .addReg(Rtemp2)
3653 .addImm(3);
3654 WPHI = Wtemp3;
3655 }
3656
3657 if (IsFGR64) {
3658 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3659 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_W), Wtemp2)
3660 .addReg(WPHI)
3661 .addReg(WPHI);
3662 WPHI = Wtemp2;
3663 }
3664
3665 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_H), Wd).addReg(WPHI).addReg(WPHI);
3666
3667 MI.eraseFromParent();
3668 return BB;
3669}
3670
3671// Emit the FPEXTEND_PSEUDO instruction.
3672//
3673// Expand an f16 to either a FGR32Opnd or FGR64Opnd.
3674//
3675// Safety: Cycle the result through the GPRs so the result always ends up
3676// the correct floating point register.
3677//
3678// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fd
3679// / FGR64Opnd:$Fd and MSA128F16:$Ws to the same physical register
3680// (which they can be, as the MSA registers are defined to alias the
3681// FPU's 64 bit and 32 bit registers) the result can be accessed using
3682// the correct register class. That requires operands be tie-able across
3683// register classes which have a sub/super register class relationship. I
3684// haven't checked.
3685//
3686// For FGR32Opnd:
3687//
3688// FPEXTEND FGR32Opnd:$fd, MSA128F16:$ws
3689// =>
3690// fexupr.w $wtemp, $ws
3691// copy_s.w $rtemp, $ws[0]
3692// mtc1 $rtemp, $fd
3693//
3694// For FGR64Opnd on Mips64:
3695//
3696// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3697// =>
3698// fexupr.w $wtemp, $ws
3699// fexupr.d $wtemp2, $wtemp
3700// copy_s.d $rtemp, $wtemp2s[0]
3701// dmtc1 $rtemp, $fd
3702//
3703// For FGR64Opnd on Mips32:
3704//
3705// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3706// =>
3707// fexupr.w $wtemp, $ws
3708// fexupr.d $wtemp2, $wtemp
3709// copy_s.w $rtemp, $wtemp2[0]
3710// mtc1 $rtemp, $ftemp
3711// copy_s.w $rtemp2, $wtemp2[1]
3712// $fd = mthc1 $rtemp2, $ftemp
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003713MachineBasicBlock *
3714MipsSETargetLowering::emitFPEXTEND_PSEUDO(MachineInstr &MI,
3715 MachineBasicBlock *BB,
3716 bool IsFGR64) const {
3717
3718 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3719 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3720 // it.
3721 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3722
3723 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
3724 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
3725
3726 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3727 DebugLoc DL = MI.getDebugLoc();
3728 unsigned Fd = MI.getOperand(0).getReg();
3729 unsigned Ws = MI.getOperand(1).getReg();
3730
3731 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3732 const TargetRegisterClass *GPRRC =
3733 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003734 unsigned MTC1Opc = IsFGR64onMips64
3735 ? Mips::DMTC1
3736 : (IsFGR64onMips32 ? Mips::MTC1_D64 : Mips::MTC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003737 unsigned COPYOpc = IsFGR64onMips64 ? Mips::COPY_S_D : Mips::COPY_S_W;
3738
3739 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3740 unsigned WPHI = Wtemp;
3741
3742 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_W), Wtemp).addReg(Ws);
3743 if (IsFGR64) {
3744 WPHI = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3745 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_D), WPHI).addReg(Wtemp);
3746 }
3747
3748 // Perform the safety regclass copy mentioned above.
3749 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3750 unsigned FPRPHI = IsFGR64onMips32
3751 ? RegInfo.createVirtualRegister(&Mips::FGR64RegClass)
3752 : Fd;
3753 BuildMI(*BB, MI, DL, TII->get(COPYOpc), Rtemp).addReg(WPHI).addImm(0);
3754 BuildMI(*BB, MI, DL, TII->get(MTC1Opc), FPRPHI).addReg(Rtemp);
3755
3756 if (IsFGR64onMips32) {
3757 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3758 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_S_W), Rtemp2)
3759 .addReg(WPHI)
3760 .addImm(1);
3761 BuildMI(*BB, MI, DL, TII->get(Mips::MTHC1_D64), Fd)
3762 .addReg(FPRPHI)
3763 .addReg(Rtemp2);
3764 }
3765
3766 MI.eraseFromParent();
3767 return BB;
3768}
3769
Daniel Sandersa9521602013-10-23 10:36:52 +00003770// Emit the FEXP2_W_1 pseudo instructions.
3771//
3772// fexp2_w_1_pseudo $wd, $wt
3773// =>
3774// ldi.w $ws, 1
3775// fexp2.w $wd, $ws, $wt
3776MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003777MipsSETargetLowering::emitFEXP2_W_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003778 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003779 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003780 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3781 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3782 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3783 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003784 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003785
3786 // Splat 1.0 into a vector
3787 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3788 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3789
3790 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003791 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003792 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003793 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003794
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003795 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003796 return BB;
3797}
3798
3799// Emit the FEXP2_D_1 pseudo instructions.
3800//
3801// fexp2_d_1_pseudo $wd, $wt
3802// =>
3803// ldi.d $ws, 1
3804// fexp2.d $wd, $ws, $wt
3805MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003806MipsSETargetLowering::emitFEXP2_D_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003807 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003808 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003809 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3810 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3811 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3812 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003813 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003814
3815 // Splat 1.0 into a vector
3816 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3817 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3818
3819 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003820 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003821 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003822 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003823
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003824 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003825 return BB;
3826}