blob: 6fbdba4c6919009266397d285d9f3a7a7a4c629d [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
Simon Atanasyana0291112019-04-25 07:47:28 +0000216 if (Subtarget.hasMips32r2() && !Subtarget.useSoftFloat() &&
217 !Subtarget.hasMips64()) {
218 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
219 }
220
Akira Hatanaka63791212013-09-07 00:52:30 +0000221 if (NoDPLoadStore) {
222 setOperationAction(ISD::LOAD, MVT::f64, Custom);
223 setOperationAction(ISD::STORE, MVT::f64, Custom);
224 }
225
Eric Christopher1c29a652014-07-18 22:55:25 +0000226 if (Subtarget.hasMips32r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000227 // MIPS32r6 replaces the accumulator-based multiplies with a three register
228 // instruction
Daniel Sanders826f8b32014-06-12 10:54:16 +0000229 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
230 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000231 setOperationAction(ISD::MUL, MVT::i32, Legal);
232 setOperationAction(ISD::MULHS, MVT::i32, Legal);
233 setOperationAction(ISD::MULHU, MVT::i32, Legal);
234
235 // MIPS32r6 replaces the accumulator-based division/remainder with separate
236 // three register division and remainder instructions.
237 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
238 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
239 setOperationAction(ISD::SDIV, MVT::i32, Legal);
240 setOperationAction(ISD::UDIV, MVT::i32, Legal);
241 setOperationAction(ISD::SREM, MVT::i32, Legal);
242 setOperationAction(ISD::UREM, MVT::i32, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000243
244 // MIPS32r6 replaces conditional moves with an equivalent that removes the
245 // need for three GPR read ports.
246 setOperationAction(ISD::SETCC, MVT::i32, Legal);
247 setOperationAction(ISD::SELECT, MVT::i32, Legal);
248 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
249
250 setOperationAction(ISD::SETCC, MVT::f32, Legal);
251 setOperationAction(ISD::SELECT, MVT::f32, Legal);
252 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
253
Eric Christopher1c29a652014-07-18 22:55:25 +0000254 assert(Subtarget.isFP64bit() && "FR=1 is required for MIPS32r6");
Daniel Sanders0fa60412014-06-12 13:39:06 +0000255 setOperationAction(ISD::SETCC, MVT::f64, Legal);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000256 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000257 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
258
Daniel Sanders3d3ea532014-06-12 15:00:17 +0000259 setOperationAction(ISD::BRCOND, MVT::Other, Legal);
260
Daniel Sanders0fa60412014-06-12 13:39:06 +0000261 // Floating point > and >= are supported via < and <=
262 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
263 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
264 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
265 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
266
267 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
268 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
269 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
270 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000271 }
272
Eric Christopher1c29a652014-07-18 22:55:25 +0000273 if (Subtarget.hasMips64r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000274 // MIPS64r6 replaces the accumulator-based multiplies with a three register
275 // instruction
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000276 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
277 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000278 setOperationAction(ISD::MUL, MVT::i64, Legal);
279 setOperationAction(ISD::MULHS, MVT::i64, Legal);
280 setOperationAction(ISD::MULHU, MVT::i64, Legal);
281
282 // MIPS32r6 replaces the accumulator-based division/remainder with separate
283 // three register division and remainder instructions.
284 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
285 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
286 setOperationAction(ISD::SDIV, MVT::i64, Legal);
287 setOperationAction(ISD::UDIV, MVT::i64, Legal);
288 setOperationAction(ISD::SREM, MVT::i64, Legal);
289 setOperationAction(ISD::UREM, MVT::i64, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000290
291 // MIPS64r6 replaces conditional moves with an equivalent that removes the
292 // need for three GPR read ports.
293 setOperationAction(ISD::SETCC, MVT::i64, Legal);
294 setOperationAction(ISD::SELECT, MVT::i64, Legal);
295 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000296 }
297
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000298 computeRegisterProperties(Subtarget.getRegisterInfo());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000299}
300
301const MipsTargetLowering *
Eric Christopherb1526602014-09-19 23:30:42 +0000302llvm::createMipsSETargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000303 const MipsSubtarget &STI) {
304 return new MipsSETargetLowering(TM, STI);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000305}
306
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000307const TargetRegisterClass *
308MipsSETargetLowering::getRepRegClassFor(MVT VT) const {
309 if (VT == MVT::Untyped)
Eric Christopher1c29a652014-07-18 22:55:25 +0000310 return Subtarget.hasDSP() ? &Mips::ACC64DSPRegClass : &Mips::ACC64RegClass;
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000311
312 return TargetLowering::getRepRegClassFor(VT);
313}
314
Daniel Sanders7a289d02013-09-23 12:02:46 +0000315// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000316void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000317addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
318 addRegisterClass(Ty, RC);
319
320 // Expand all builtin opcodes.
321 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
322 setOperationAction(Opc, Ty, Expand);
323
324 setOperationAction(ISD::BITCAST, Ty, Legal);
325 setOperationAction(ISD::LOAD, Ty, Legal);
326 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000327 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
328 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000329 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000330
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000331 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000332 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000333 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000334 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000335 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000336 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000337 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000338 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000339 setOperationAction(ISD::SHL, Ty, Legal);
340 setOperationAction(ISD::SRA, Ty, Legal);
341 setOperationAction(ISD::SRL, Ty, Legal);
342 setOperationAction(ISD::SUB, Ty, Legal);
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000343 setOperationAction(ISD::SMAX, Ty, Legal);
344 setOperationAction(ISD::SMIN, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000345 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000346 setOperationAction(ISD::UREM, Ty, Legal);
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000347 setOperationAction(ISD::UMAX, Ty, Legal);
348 setOperationAction(ISD::UMIN, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000349 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000350 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000351 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000352
Daniel Sanders015972b2013-10-11 10:00:06 +0000353 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
354 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
355 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
356 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
357 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
358 }
359
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000360 setOperationAction(ISD::SETCC, Ty, Legal);
361 setCondCodeAction(ISD::SETNE, Ty, Expand);
362 setCondCodeAction(ISD::SETGE, Ty, Expand);
363 setCondCodeAction(ISD::SETGT, Ty, Expand);
364 setCondCodeAction(ISD::SETUGE, Ty, Expand);
365 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000366}
367
Daniel Sanders7a289d02013-09-23 12:02:46 +0000368// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000369void MipsSETargetLowering::
370addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000371 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000372
373 // Expand all builtin opcodes.
374 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
375 setOperationAction(Opc, Ty, Expand);
376
377 setOperationAction(ISD::LOAD, Ty, Legal);
378 setOperationAction(ISD::STORE, Ty, Legal);
379 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000380 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000381 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000382 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000383
384 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000385 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000386 setOperationAction(ISD::FADD, Ty, Legal);
387 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000388 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000389 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000390 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000391 setOperationAction(ISD::FMUL, Ty, Legal);
392 setOperationAction(ISD::FRINT, Ty, Legal);
393 setOperationAction(ISD::FSQRT, Ty, Legal);
394 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000395 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000396
397 setOperationAction(ISD::SETCC, Ty, Legal);
398 setCondCodeAction(ISD::SETOGE, Ty, Expand);
399 setCondCodeAction(ISD::SETOGT, Ty, Expand);
400 setCondCodeAction(ISD::SETUGE, Ty, Expand);
401 setCondCodeAction(ISD::SETUGT, Ty, Expand);
402 setCondCodeAction(ISD::SETGE, Ty, Expand);
403 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000404 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000405}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000406
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000407SDValue MipsSETargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000408 if(!Subtarget.hasMips32r6())
409 return MipsTargetLowering::LowerOperation(Op, DAG);
410
411 EVT ResTy = Op->getValueType(0);
412 SDLoc DL(Op);
413
414 // Although MTC1_D64 takes an i32 and writes an f64, the upper 32 bits of the
415 // floating point register are undefined. Not really an issue as sel.d, which
416 // is produced from an FSELECT node, only looks at bit 0.
417 SDValue Tmp = DAG.getNode(MipsISD::MTC1_D64, DL, MVT::f64, Op->getOperand(0));
418 return DAG.getNode(MipsISD::FSELECT, DL, ResTy, Tmp, Op->getOperand(1),
419 Op->getOperand(2));
420}
421
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000422bool
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000423MipsSETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
424 unsigned,
425 unsigned,
426 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000427 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
428
Eric Christopher1c29a652014-07-18 22:55:25 +0000429 if (Subtarget.systemSupportsUnalignedAccess()) {
Daniel Sandersac272632014-05-23 13:18:02 +0000430 // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
431 // implementation defined whether this is handled by hardware, software, or
432 // a hybrid of the two but it's expected that most implementations will
433 // handle the majority of cases in hardware.
434 if (Fast)
435 *Fast = true;
436 return true;
437 }
438
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000439 switch (SVT) {
440 case MVT::i64:
441 case MVT::i32:
442 if (Fast)
443 *Fast = true;
444 return true;
445 default:
446 return false;
447 }
448}
449
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000450SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
451 SelectionDAG &DAG) const {
452 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000453 case ISD::LOAD: return lowerLOAD(Op, DAG);
454 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000455 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
456 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
457 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
458 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
459 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
460 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000461 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
462 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000463 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
464 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000465 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000466 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000467 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000468 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000469 case ISD::SELECT: return lowerSELECT(Op, DAG);
Simon Atanasyana0291112019-04-25 07:47:28 +0000470 case ISD::BITCAST: return lowerBITCAST(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000471 }
472
473 return MipsTargetLowering::LowerOperation(Op, DAG);
474}
475
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000476// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
477//
478// Performs the following transformations:
479// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
480// sign/zero-extension is completely overwritten by the new one performed by
481// the ISD::AND.
482// - Removes redundant zero extensions performed by an ISD::AND.
483static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
484 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000485 const MipsSubtarget &Subtarget) {
486 if (!Subtarget.hasMSA())
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000487 return SDValue();
488
489 SDValue Op0 = N->getOperand(0);
490 SDValue Op1 = N->getOperand(1);
491 unsigned Op0Opcode = Op0->getOpcode();
492
493 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
494 // where $d + 1 == 2^n and n == 32
495 // or $d + 1 == 2^n and n <= 32 and ZExt
496 // -> (MipsVExtractZExt $a, $b, $c)
497 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
498 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
499 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
500
501 if (!Mask)
502 return SDValue();
503
504 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
505
506 if (Log2IfPositive <= 0)
507 return SDValue(); // Mask+1 is not a power of 2
508
509 SDValue Op0Op2 = Op0->getOperand(2);
510 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
511 unsigned ExtendTySize = ExtendTy.getSizeInBits();
512 unsigned Log2 = Log2IfPositive;
513
514 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
515 Log2 == ExtendTySize) {
516 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000517 return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
518 Op0->getVTList(),
519 makeArrayRef(Ops, Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000520 }
521 }
522
523 return SDValue();
524}
525
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000526// Determine if the specified node is a constant vector splat.
527//
528// Returns true and sets Imm if:
529// * N is a ISD::BUILD_VECTOR representing a constant splat
530//
531// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
532// differences are that it assumes the MSA has already been checked and the
533// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
534// must not be in order for binsri.d to be selectable).
535static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
536 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
537
Craig Topper062a2ba2014-04-25 05:30:21 +0000538 if (!Node)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000539 return false;
540
541 APInt SplatValue, SplatUndef;
542 unsigned SplatBitSize;
543 bool HasAnyUndefs;
544
545 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
546 8, !IsLittleEndian))
547 return false;
548
549 Imm = SplatValue;
550
551 return true;
552}
553
Daniel Sandersab94b532013-10-30 15:20:38 +0000554// Test whether the given node is an all-ones build_vector.
555static bool isVectorAllOnes(SDValue N) {
556 // Look through bitcasts. Endianness doesn't matter because we are looking
557 // for an all-ones value.
558 if (N->getOpcode() == ISD::BITCAST)
559 N = N->getOperand(0);
560
561 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
562
563 if (!BVN)
564 return false;
565
566 APInt SplatValue, SplatUndef;
567 unsigned SplatBitSize;
568 bool HasAnyUndefs;
569
570 // Endianness doesn't matter in this context because we are looking for
571 // an all-ones value.
572 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
573 return SplatValue.isAllOnesValue();
574
575 return false;
576}
577
578// Test whether N is the bitwise inverse of OfNode.
579static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
580 if (N->getOpcode() != ISD::XOR)
581 return false;
582
583 if (isVectorAllOnes(N->getOperand(0)))
584 return N->getOperand(1) == OfNode;
585
586 if (isVectorAllOnes(N->getOperand(1)))
587 return N->getOperand(0) == OfNode;
588
589 return false;
590}
591
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000592// Perform combines where ISD::OR is the root node.
593//
594// Performs the following transformations:
595// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
596// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
597// vector type.
598static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
599 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000600 const MipsSubtarget &Subtarget) {
601 if (!Subtarget.hasMSA())
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000602 return SDValue();
603
604 EVT Ty = N->getValueType(0);
605
606 if (!Ty.is128BitVector())
607 return SDValue();
608
609 SDValue Op0 = N->getOperand(0);
610 SDValue Op1 = N->getOperand(1);
611
612 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
613 SDValue Op0Op0 = Op0->getOperand(0);
614 SDValue Op0Op1 = Op0->getOperand(1);
615 SDValue Op1Op0 = Op1->getOperand(0);
616 SDValue Op1Op1 = Op1->getOperand(1);
Eric Christopher1c29a652014-07-18 22:55:25 +0000617 bool IsLittleEndian = !Subtarget.isLittle();
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000618
619 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000620 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000621 APInt Mask, InvMask;
622
623 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
624 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
625 // looking.
626 // IfClr will be set if we find a valid match.
627 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
628 Cond = Op0Op0;
629 IfSet = Op0Op1;
630
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000631 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
632 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000633 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000634 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
635 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000636 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000637
638 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000639 }
640
641 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
642 // thing again using this mask.
643 // IfClr will be set if we find a valid match.
644 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
645 Cond = Op0Op1;
646 IfSet = Op0Op0;
647
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000648 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
649 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000650 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000651 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
652 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000653 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000654
655 IsConstantMask = true;
656 }
657
658 // If IfClr is not yet set, try looking for a non-constant match.
659 // IfClr will be set if we find a valid match amongst the eight
660 // possibilities.
661 if (!IfClr.getNode()) {
662 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
663 Cond = Op1Op0;
664 IfSet = Op1Op1;
665 IfClr = Op0Op1;
666 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
667 Cond = Op1Op0;
668 IfSet = Op1Op1;
669 IfClr = Op0Op0;
670 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
671 Cond = Op1Op1;
672 IfSet = Op1Op0;
673 IfClr = Op0Op1;
674 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
675 Cond = Op1Op1;
676 IfSet = Op1Op0;
677 IfClr = Op0Op0;
678 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
679 Cond = Op0Op0;
680 IfSet = Op0Op1;
681 IfClr = Op1Op1;
682 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
683 Cond = Op0Op0;
684 IfSet = Op0Op1;
685 IfClr = Op1Op0;
686 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
687 Cond = Op0Op1;
688 IfSet = Op0Op0;
689 IfClr = Op1Op1;
690 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
691 Cond = Op0Op1;
692 IfSet = Op0Op0;
693 IfClr = Op1Op0;
694 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000695 }
696
697 // At this point, IfClr will be set if we have a valid match.
698 if (!IfClr.getNode())
699 return SDValue();
700
701 assert(Cond.getNode() && IfSet.getNode());
702
703 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000704 if (IsConstantMask) {
705 if (Mask.isAllOnesValue())
706 return IfSet;
707 else if (Mask == 0)
708 return IfClr;
709 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000710
711 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000712 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000713 }
714
715 return SDValue();
716}
717
Simon Dardis9ec9f442018-04-13 16:09:07 +0000718static bool shouldTransformMulToShiftsAddsSubs(APInt C, EVT VT,
719 SelectionDAG &DAG,
720 const MipsSubtarget &Subtarget) {
721 // Estimate the number of operations the below transform will turn a
722 // constant multiply into. The number is approximately how many powers
723 // of two summed together that the constant can be broken down into.
724
725 SmallVector<APInt, 16> WorkStack(1, C);
726 unsigned Steps = 0;
727 unsigned BitWidth = C.getBitWidth();
728
729 while (!WorkStack.empty()) {
730 APInt Val = WorkStack.pop_back_val();
731
732 if (Val == 0 || Val == 1)
733 continue;
734
735 if (Val.isPowerOf2()) {
736 ++Steps;
737 continue;
738 }
739
740 APInt Floor = APInt(BitWidth, 1) << Val.logBase2();
741 APInt Ceil = Val.isNegative() ? APInt(BitWidth, 0)
742 : APInt(BitWidth, 1) << C.ceilLogBase2();
743
744 if ((Val - Floor).ule(Ceil - Val)) {
745 WorkStack.push_back(Floor);
746 WorkStack.push_back(Val - Floor);
747 ++Steps;
748 continue;
749 }
750
751 WorkStack.push_back(Ceil);
752 WorkStack.push_back(Ceil - Val);
753 ++Steps;
754
755 // If we have taken more than 12[1] / 8[2] steps to attempt the
756 // optimization for a native sized value, it is more than likely that this
757 // optimization will make things worse.
758 //
759 // [1] MIPS64 requires 6 instructions at most to materialize any constant,
760 // multiplication requires at least 4 cycles, but another cycle (or two)
761 // to retrieve the result from the HI/LO registers.
762 //
763 // [2] For MIPS32, more than 8 steps is expensive as the constant could be
764 // materialized in 2 instructions, multiplication requires at least 4
765 // cycles, but another cycle (or two) to retrieve the result from the
766 // HI/LO registers.
767
768 if (Steps > 12 && (Subtarget.isABI_N32() || Subtarget.isABI_N64()))
769 return false;
770
771 if (Steps > 8 && Subtarget.isABI_O32())
772 return false;
773 }
774
775 // If the value being multiplied is not supported natively, we have to pay
776 // an additional legalization cost, conservatively assume an increase in the
777 // cost of 3 instructions per step. This values for this heuristic were
778 // determined experimentally.
779 unsigned RegisterSize = DAG.getTargetLoweringInfo()
780 .getRegisterType(*DAG.getContext(), VT)
781 .getSizeInBits();
782 Steps *= (VT.getSizeInBits() != RegisterSize) * 3;
783 if (Steps > 27)
784 return false;
785
786 return true;
787}
788
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000789static SDValue genConstMult(SDValue X, APInt C, const SDLoc &DL, EVT VT,
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000790 EVT ShiftTy, SelectionDAG &DAG) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000791 // Return 0.
792 if (C == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000793 return DAG.getConstant(0, DL, VT);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000794
795 // Return x.
796 if (C == 1)
797 return X;
798
799 // If c is power of 2, return (shl x, log2(c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000800 if (C.isPowerOf2())
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000801 return DAG.getNode(ISD::SHL, DL, VT, X,
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000802 DAG.getConstant(C.logBase2(), DL, ShiftTy));
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000803
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000804 unsigned BitWidth = C.getBitWidth();
805 APInt Floor = APInt(BitWidth, 1) << C.logBase2();
806 APInt Ceil = C.isNegative() ? APInt(BitWidth, 0) :
807 APInt(BitWidth, 1) << C.ceilLogBase2();
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000808
809 // If |c - floor_c| <= |c - ceil_c|,
810 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
811 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000812 if ((C - Floor).ule(Ceil - C)) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000813 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
814 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
815 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
816 }
817
818 // If |c - floor_c| > |c - ceil_c|,
819 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
820 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
821 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
822 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
823}
824
825static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
826 const TargetLowering::DAGCombinerInfo &DCI,
Simon Dardis9ec9f442018-04-13 16:09:07 +0000827 const MipsSETargetLowering *TL,
828 const MipsSubtarget &Subtarget) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000829 EVT VT = N->getValueType(0);
830
831 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
Simon Dardis9ec9f442018-04-13 16:09:07 +0000832 if (!VT.isVector() && shouldTransformMulToShiftsAddsSubs(
833 C->getAPIntValue(), VT, DAG, Subtarget))
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000834 return genConstMult(N->getOperand(0), C->getAPIntValue(), SDLoc(N), VT,
Mehdi Aminieaabc512015-07-09 15:12:23 +0000835 TL->getScalarShiftAmountTy(DAG.getDataLayout(), VT),
836 DAG);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000837
838 return SDValue(N, 0);
839}
840
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000841static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
842 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000843 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000844 // See if this is a vector splat immediate node.
845 APInt SplatValue, SplatUndef;
846 unsigned SplatBitSize;
847 bool HasAnyUndefs;
Sanjay Patel1ed771f2016-09-14 16:37:15 +0000848 unsigned EltSize = Ty.getScalarSizeInBits();
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000849 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
850
Eric Christopher1c29a652014-07-18 22:55:25 +0000851 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000852 return SDValue();
853
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000854 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000855 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000856 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000857 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000858 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000859 return SDValue();
860
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000861 SDLoc DL(N);
862 return DAG.getNode(Opc, DL, Ty, N->getOperand(0),
863 DAG.getConstant(SplatValue.getZExtValue(), DL, MVT::i32));
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000864}
865
866static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
867 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000868 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000869 EVT Ty = N->getValueType(0);
870
871 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
872 return SDValue();
873
874 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
875}
876
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000877// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
878// constant splats into MipsISD::SHRA_DSP for DSPr2.
879//
880// Performs the following transformations:
881// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
882// sign/zero-extension is completely overwritten by the new one performed by
883// the ISD::SRA and ISD::SHL nodes.
884// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
885// sequence.
886//
887// See performDSPShiftCombine for more information about the transformation
888// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000889static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
890 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000891 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000892 EVT Ty = N->getValueType(0);
893
Eric Christopher1c29a652014-07-18 22:55:25 +0000894 if (Subtarget.hasMSA()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000895 SDValue Op0 = N->getOperand(0);
896 SDValue Op1 = N->getOperand(1);
897
898 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
899 // where $d + sizeof($c) == 32
900 // or $d + sizeof($c) <= 32 and SExt
901 // -> (MipsVExtractSExt $a, $b, $c)
902 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
903 SDValue Op0Op0 = Op0->getOperand(0);
904 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
905
906 if (!ShAmount)
907 return SDValue();
908
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000909 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
910 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
911 return SDValue();
912
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000913 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
914 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
915
916 if (TotalBits == 32 ||
917 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
918 TotalBits <= 32)) {
919 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
920 Op0Op0->getOperand(2) };
Chandler Carruth356665a2014-08-01 22:09:43 +0000921 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
922 Op0Op0->getVTList(),
923 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000924 }
925 }
926 }
927
Eric Christopher1c29a652014-07-18 22:55:25 +0000928 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000929 return SDValue();
930
931 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
932}
933
934
935static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
936 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000937 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000938 EVT Ty = N->getValueType(0);
939
Eric Christopher1c29a652014-07-18 22:55:25 +0000940 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000941 return SDValue();
942
943 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
944}
945
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000946static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
947 bool IsV216 = (Ty == MVT::v2i16);
948
949 switch (CC) {
950 case ISD::SETEQ:
951 case ISD::SETNE: return true;
952 case ISD::SETLT:
953 case ISD::SETLE:
954 case ISD::SETGT:
955 case ISD::SETGE: return IsV216;
956 case ISD::SETULT:
957 case ISD::SETULE:
958 case ISD::SETUGT:
959 case ISD::SETUGE: return !IsV216;
960 default: return false;
961 }
962}
963
964static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
965 EVT Ty = N->getValueType(0);
966
967 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
968 return SDValue();
969
970 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
971 return SDValue();
972
Andrew Trickef9de2a2013-05-25 02:42:55 +0000973 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000974 N->getOperand(1), N->getOperand(2));
975}
976
977static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
978 EVT Ty = N->getValueType(0);
979
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000980 if (Ty == MVT::v2i16 || Ty == MVT::v4i8) {
Daniel Sanders3ce56622013-09-24 12:18:31 +0000981 SDValue SetCC = N->getOperand(0);
982
983 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
984 return SDValue();
985
986 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
987 SetCC.getOperand(0), SetCC.getOperand(1),
988 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
989 }
990
991 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000992}
993
Daniel Sandersf7456c72013-09-23 13:22:24 +0000994static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000995 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000996 EVT Ty = N->getValueType(0);
997
Eric Christopher1c29a652014-07-18 22:55:25 +0000998 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000999 // Try the following combines:
1000 // (xor (or $a, $b), (build_vector allones))
1001 // (xor (or $a, $b), (bitcast (build_vector allones)))
1002 SDValue Op0 = N->getOperand(0);
1003 SDValue Op1 = N->getOperand(1);
1004 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001005
1006 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
1007 NotOp = Op1;
1008 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
1009 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001010 else
1011 return SDValue();
1012
1013 if (NotOp->getOpcode() == ISD::OR)
1014 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
1015 NotOp->getOperand(1));
1016 }
1017
1018 return SDValue();
1019}
1020
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001021SDValue
1022MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1023 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001024 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001025
1026 switch (N->getOpcode()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001027 case ISD::AND:
1028 Val = performANDCombine(N, DAG, DCI, Subtarget);
1029 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +00001030 case ISD::OR:
1031 Val = performORCombine(N, DAG, DCI, Subtarget);
1032 break;
Akira Hatanaka5832fc62013-06-26 18:48:17 +00001033 case ISD::MUL:
Simon Dardis9ec9f442018-04-13 16:09:07 +00001034 return performMULCombine(N, DAG, DCI, this, Subtarget);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001035 case ISD::SHL:
Petar Jovanovicb71386a2017-03-15 13:10:08 +00001036 Val = performSHLCombine(N, DAG, DCI, Subtarget);
1037 break;
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001038 case ISD::SRA:
1039 return performSRACombine(N, DAG, DCI, Subtarget);
1040 case ISD::SRL:
1041 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001042 case ISD::VSELECT:
1043 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +00001044 case ISD::XOR:
1045 Val = performXORCombine(N, DAG, Subtarget);
1046 break;
1047 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001048 Val = performSETCCCombine(N, DAG);
1049 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001050 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001051
Daniel Sanders62aeab82013-10-30 13:31:27 +00001052 if (Val.getNode()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001053 LLVM_DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1054 N->printrWithDepth(dbgs(), &DAG); dbgs() << "\n=> \n";
1055 Val.getNode()->printrWithDepth(dbgs(), &DAG); dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001056 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001057 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001058
1059 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001060}
1061
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001062MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001063MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001064 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001065 switch (MI.getOpcode()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001066 default:
1067 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1068 case Mips::BPOSGE32_PSEUDO:
1069 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001070 case Mips::SNZ_B_PSEUDO:
1071 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1072 case Mips::SNZ_H_PSEUDO:
1073 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1074 case Mips::SNZ_W_PSEUDO:
1075 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1076 case Mips::SNZ_D_PSEUDO:
1077 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1078 case Mips::SNZ_V_PSEUDO:
1079 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1080 case Mips::SZ_B_PSEUDO:
1081 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1082 case Mips::SZ_H_PSEUDO:
1083 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1084 case Mips::SZ_W_PSEUDO:
1085 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1086 case Mips::SZ_D_PSEUDO:
1087 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1088 case Mips::SZ_V_PSEUDO:
1089 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001090 case Mips::COPY_FW_PSEUDO:
1091 return emitCOPY_FW(MI, BB);
1092 case Mips::COPY_FD_PSEUDO:
1093 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001094 case Mips::INSERT_FW_PSEUDO:
1095 return emitINSERT_FW(MI, BB);
1096 case Mips::INSERT_FD_PSEUDO:
1097 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001098 case Mips::INSERT_B_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001099 case Mips::INSERT_B_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001100 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1101 case Mips::INSERT_H_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001102 case Mips::INSERT_H_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001103 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1104 case Mips::INSERT_W_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001105 case Mips::INSERT_W_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001106 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1107 case Mips::INSERT_D_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001108 case Mips::INSERT_D_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001109 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1110 case Mips::INSERT_FW_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001111 case Mips::INSERT_FW_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001112 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1113 case Mips::INSERT_FD_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001114 case Mips::INSERT_FD_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001115 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001116 case Mips::FILL_FW_PSEUDO:
1117 return emitFILL_FW(MI, BB);
1118 case Mips::FILL_FD_PSEUDO:
1119 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001120 case Mips::FEXP2_W_1_PSEUDO:
1121 return emitFEXP2_W_1(MI, BB);
1122 case Mips::FEXP2_D_1_PSEUDO:
1123 return emitFEXP2_D_1(MI, BB);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00001124 case Mips::ST_F16:
1125 return emitST_F16_PSEUDO(MI, BB);
1126 case Mips::LD_F16:
1127 return emitLD_F16_PSEUDO(MI, BB);
1128 case Mips::MSA_FP_EXTEND_W_PSEUDO:
1129 return emitFPEXTEND_PSEUDO(MI, BB, false);
1130 case Mips::MSA_FP_ROUND_W_PSEUDO:
1131 return emitFPROUND_PSEUDO(MI, BB, false);
1132 case Mips::MSA_FP_EXTEND_D_PSEUDO:
1133 return emitFPEXTEND_PSEUDO(MI, BB, true);
1134 case Mips::MSA_FP_ROUND_D_PSEUDO:
1135 return emitFPROUND_PSEUDO(MI, BB, true);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001136 }
1137}
1138
Daniel Sanders23e98772014-11-02 16:09:29 +00001139bool MipsSETargetLowering::isEligibleForTailCallOptimization(
1140 const CCState &CCInfo, unsigned NextStackOffset,
1141 const MipsFunctionInfo &FI) const {
Simon Dardis57f4ae42016-08-04 09:17:07 +00001142 if (!UseMipsTailCalls)
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001143 return false;
1144
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00001145 // Exception has to be cleared with eret.
1146 if (FI.isISR())
1147 return false;
1148
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001149 // Return false if either the callee or caller has a byval argument.
Daniel Sanders23e98772014-11-02 16:09:29 +00001150 if (CCInfo.getInRegsParamsCount() > 0 || FI.hasByvalArg())
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001151 return false;
1152
1153 // Return true if the callee's argument area is no larger than the
1154 // caller's.
1155 return NextStackOffset <= FI.getIncomingArgSize();
1156}
1157
1158void MipsSETargetLowering::
1159getOpndList(SmallVectorImpl<SDValue> &Ops,
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001160 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001161 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001162 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
1163 SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001164 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001165 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001166 InternalLinkage, IsCallReloc, CLI, Callee,
1167 Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001168}
1169
Akira Hatanaka63791212013-09-07 00:52:30 +00001170SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1171 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1172
1173 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1174 return MipsTargetLowering::lowerLOAD(Op, DAG);
1175
1176 // Replace a double precision load with two i32 loads and a buildpair64.
1177 SDLoc DL(Op);
1178 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1179 EVT PtrVT = Ptr.getValueType();
1180
1181 // i32 load from lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001182 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr, MachinePointerInfo(),
1183 Nd.getAlignment(), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001184
1185 // i32 load from higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001186 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001187 SDValue Hi = DAG.getLoad(
1188 MVT::i32, DL, Lo.getValue(1), Ptr, MachinePointerInfo(),
1189 std::min(Nd.getAlignment(), 4U), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001190
Eric Christopher1c29a652014-07-18 22:55:25 +00001191 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001192 std::swap(Lo, Hi);
1193
1194 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1195 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001196 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001197}
1198
1199SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1200 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1201
1202 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1203 return MipsTargetLowering::lowerSTORE(Op, DAG);
1204
1205 // Replace a double precision store with two extractelement64s and i32 stores.
1206 SDLoc DL(Op);
1207 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1208 EVT PtrVT = Ptr.getValueType();
1209 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001210 Val, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001211 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001212 Val, DAG.getConstant(1, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001213
Eric Christopher1c29a652014-07-18 22:55:25 +00001214 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001215 std::swap(Lo, Hi);
1216
1217 // i32 store to lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001218 Chain =
1219 DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(), Nd.getAlignment(),
1220 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001221
1222 // i32 store to higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001223 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Akira Hatanaka63791212013-09-07 00:52:30 +00001224 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001225 std::min(Nd.getAlignment(), 4U),
1226 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001227}
1228
Simon Atanasyana0291112019-04-25 07:47:28 +00001229SDValue MipsSETargetLowering::lowerBITCAST(SDValue Op,
1230 SelectionDAG &DAG) const {
1231 SDLoc DL(Op);
1232 MVT Src = Op.getOperand(0).getValueType().getSimpleVT();
1233 MVT Dest = Op.getValueType().getSimpleVT();
1234
1235 // Bitcast i64 to double.
1236 if (Src == MVT::i64 && Dest == MVT::f64) {
1237 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
1238 Op.getOperand(0), DAG.getIntPtrConstant(0, DL));
1239 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
1240 Op.getOperand(0), DAG.getIntPtrConstant(1, DL));
1241 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1242 }
1243
1244 // Bitcast double to i64.
1245 if (Src == MVT::f64 && Dest == MVT::i64) {
1246 SDValue Lo =
1247 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1248 DAG.getConstant(0, DL, MVT::i32));
1249 SDValue Hi =
1250 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1251 DAG.getConstant(1, DL, MVT::i32));
1252 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1253 }
1254
1255 // Skip other cases of bitcast and use default lowering.
1256 return SDValue();
1257}
1258
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001259SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1260 bool HasLo, bool HasHi,
1261 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001262 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001263 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001264
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001265 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001266 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001267 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1268 Op.getOperand(0), Op.getOperand(1));
1269 SDValue Lo, Hi;
1270
1271 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001272 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001273 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001274 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001275
1276 if (!HasLo || !HasHi)
1277 return HasLo ? Lo : Hi;
1278
1279 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001280 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001281}
1282
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001283static SDValue initAccumulator(SDValue In, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001284 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001285 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001286 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001287 DAG.getConstant(1, DL, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001288 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001289}
1290
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001291static SDValue extractLOHI(SDValue Op, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001292 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1293 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001294 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1295}
1296
1297// This function expands mips intrinsic nodes which have 64-bit input operands
1298// or output values.
1299//
1300// out64 = intrinsic-node in64
1301// =>
1302// lo = copy (extract-element (in64, 0))
1303// hi = copy (extract-element (in64, 1))
1304// mips-specific-node
1305// v0 = copy lo
1306// v1 = copy hi
1307// out64 = merge-values (v0, v1)
1308//
1309static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001310 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001311 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1312 SmallVector<SDValue, 3> Ops;
1313 unsigned OpNo = 0;
1314
1315 // See if Op has a chain input.
1316 if (HasChainIn)
1317 Ops.push_back(Op->getOperand(OpNo++));
1318
1319 // The next operand is the intrinsic opcode.
1320 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1321
1322 // See if the next operand has type i64.
1323 SDValue Opnd = Op->getOperand(++OpNo), In64;
1324
1325 if (Opnd.getValueType() == MVT::i64)
1326 In64 = initAccumulator(Opnd, DL, DAG);
1327 else
1328 Ops.push_back(Opnd);
1329
1330 // Push the remaining operands.
1331 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1332 Ops.push_back(Op->getOperand(OpNo));
1333
1334 // Add In64 to the end of the list.
1335 if (In64.getNode())
1336 Ops.push_back(In64);
1337
1338 // Scan output.
1339 SmallVector<EVT, 2> ResTys;
1340
1341 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1342 I != E; ++I)
1343 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1344
1345 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001346 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001347 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1348
1349 if (!HasChainIn)
1350 return Out;
1351
1352 assert(Val->getValueType(1) == MVT::Other);
1353 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001354 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001355}
1356
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001357// Lower an MSA copy intrinsic into the specified SelectionDAG node
1358static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1359 SDLoc DL(Op);
1360 SDValue Vec = Op->getOperand(1);
1361 SDValue Idx = Op->getOperand(2);
1362 EVT ResTy = Op->getValueType(0);
1363 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1364
1365 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1366 DAG.getValueType(EltTy));
1367
1368 return Result;
1369}
1370
Daniel Sanders50b80412013-11-15 12:56:49 +00001371static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1372 EVT ResVecTy = Op->getValueType(0);
1373 EVT ViaVecTy = ResVecTy;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001374 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
Daniel Sanders50b80412013-11-15 12:56:49 +00001375 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001376
Daniel Sanders50b80412013-11-15 12:56:49 +00001377 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1378 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1379 // lanes.
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001380 SDValue LaneA = Op->getOperand(OpNr);
1381 SDValue LaneB;
Daniel Sanders50b80412013-11-15 12:56:49 +00001382
1383 if (ResVecTy == MVT::v2i64) {
Stefan Maksimovicc7113cc2018-05-08 15:12:29 +00001384 // In case of the index being passed as an immediate value, set the upper
1385 // lane to 0 so that the splati.d instruction can be matched.
1386 if (isa<ConstantSDNode>(LaneA))
1387 LaneB = DAG.getConstant(0, DL, MVT::i32);
1388 // Having the index passed in a register, set the upper lane to the same
1389 // value as the lower - this results in the BUILD_VECTOR node not being
1390 // expanded through stack. This way we are able to pattern match the set of
1391 // nodes created here to splat.d.
1392 else
1393 LaneB = LaneA;
Daniel Sandersf49dd822013-09-24 13:33:07 +00001394 ViaVecTy = MVT::v4i32;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001395 if(BigEndian)
1396 std::swap(LaneA, LaneB);
Daniel Sanders50b80412013-11-15 12:56:49 +00001397 } else
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001398 LaneB = LaneA;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001399
Daniel Sanders50b80412013-11-15 12:56:49 +00001400 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1401 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001402
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001403 SDValue Result = DAG.getBuildVector(
1404 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001405
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001406 if (ViaVecTy != ResVecTy) {
1407 SDValue One = DAG.getConstant(1, DL, ViaVecTy);
1408 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy,
1409 DAG.getNode(ISD::AND, DL, ViaVecTy, Result, One));
1410 }
Daniel Sandersf49dd822013-09-24 13:33:07 +00001411
1412 return Result;
1413}
1414
Simon Dardis548a53f2017-01-10 16:40:57 +00001415static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG,
1416 bool IsSigned = false) {
Simon Atanasyanf26f56d2019-03-29 15:15:22 +00001417 auto *CImm = cast<ConstantSDNode>(Op->getOperand(ImmOp));
Simon Dardis548a53f2017-01-10 16:40:57 +00001418 return DAG.getConstant(
1419 APInt(Op->getValueType(0).getScalarType().getSizeInBits(),
Simon Atanasyanf26f56d2019-03-29 15:15:22 +00001420 IsSigned ? CImm->getSExtValue() : CImm->getZExtValue(), IsSigned),
Simon Dardis548a53f2017-01-10 16:40:57 +00001421 SDLoc(Op), Op->getValueType(0));
Daniel Sanders50b80412013-11-15 12:56:49 +00001422}
1423
1424static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1425 bool BigEndian, SelectionDAG &DAG) {
1426 EVT ViaVecTy = VecTy;
1427 SDValue SplatValueA = SplatValue;
1428 SDValue SplatValueB = SplatValue;
1429 SDLoc DL(SplatValue);
1430
1431 if (VecTy == MVT::v2i64) {
1432 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1433 ViaVecTy = MVT::v4i32;
1434
1435 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1436 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001437 DAG.getConstant(32, DL, MVT::i32));
Daniel Sanders50b80412013-11-15 12:56:49 +00001438 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1439 }
1440
1441 // We currently hold the parts in little endian order. Swap them if
1442 // necessary.
1443 if (BigEndian)
1444 std::swap(SplatValueA, SplatValueB);
1445
1446 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1447 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1448 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1449 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1450
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001451 SDValue Result = DAG.getBuildVector(
1452 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001453
1454 if (VecTy != ViaVecTy)
1455 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1456
1457 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001458}
1459
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001460static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1461 unsigned Opc, SDValue Imm,
1462 bool BigEndian) {
1463 EVT VecTy = Op->getValueType(0);
1464 SDValue Exp2Imm;
1465 SDLoc DL(Op);
1466
Daniel Sanders50b80412013-11-15 12:56:49 +00001467 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1468 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001469 if (VecTy == MVT::v2i64) {
1470 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1471 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1472
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001473 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), DL,
1474 MVT::i32);
1475 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), DL, MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001476
1477 if (BigEndian)
1478 std::swap(BitImmLoOp, BitImmHiOp);
1479
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001480 Exp2Imm = DAG.getNode(
1481 ISD::BITCAST, DL, MVT::v2i64,
1482 DAG.getBuildVector(MVT::v4i32, DL,
1483 {BitImmLoOp, BitImmHiOp, BitImmLoOp, BitImmHiOp}));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001484 }
1485 }
1486
Craig Topper062a2ba2014-04-25 05:30:21 +00001487 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001488 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001489
1490 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1491 // only values 0-63 are valid.
1492 if (VecTy == MVT::v2i64)
1493 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1494
1495 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1496
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001497 Exp2Imm = DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, DL, VecTy),
1498 Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001499 }
1500
1501 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1502}
1503
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001504static SDValue truncateVecElts(SDValue Op, SelectionDAG &DAG) {
1505 SDLoc DL(Op);
1506 EVT ResTy = Op->getValueType(0);
1507 SDValue Vec = Op->getOperand(2);
1508 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
1509 MVT ResEltTy = ResTy == MVT::v2i64 ? MVT::i64 : MVT::i32;
1510 SDValue ConstValue = DAG.getConstant(Vec.getScalarValueSizeInBits() - 1,
1511 DL, ResEltTy);
1512 SDValue SplatVec = getBuildVectorSplat(ResTy, ConstValue, BigEndian, DAG);
1513
1514 return DAG.getNode(ISD::AND, DL, ResTy, Vec, SplatVec);
1515}
1516
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001517static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1518 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001519 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001520 SDValue One = DAG.getConstant(1, DL, ResTy);
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001521 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, truncateVecElts(Op, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001522
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001523 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1524 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001525}
1526
1527static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1528 SDLoc DL(Op);
1529 EVT ResTy = Op->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001530 APInt BitImm = APInt(ResTy.getScalarSizeInBits(), 1)
Daniel Sanders50b80412013-11-15 12:56:49 +00001531 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001532 SDValue BitMask = DAG.getConstant(~BitImm, DL, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001533
1534 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1535}
1536
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001537SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1538 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001539 SDLoc DL(Op);
Simon Dardis548a53f2017-01-10 16:40:57 +00001540 unsigned Intrinsic = cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue();
1541 switch (Intrinsic) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001542 default:
1543 return SDValue();
1544 case Intrinsic::mips_shilo:
1545 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1546 case Intrinsic::mips_dpau_h_qbl:
1547 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1548 case Intrinsic::mips_dpau_h_qbr:
1549 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1550 case Intrinsic::mips_dpsu_h_qbl:
1551 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1552 case Intrinsic::mips_dpsu_h_qbr:
1553 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1554 case Intrinsic::mips_dpa_w_ph:
1555 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1556 case Intrinsic::mips_dps_w_ph:
1557 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1558 case Intrinsic::mips_dpax_w_ph:
1559 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1560 case Intrinsic::mips_dpsx_w_ph:
1561 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1562 case Intrinsic::mips_mulsa_w_ph:
1563 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1564 case Intrinsic::mips_mult:
1565 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1566 case Intrinsic::mips_multu:
1567 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1568 case Intrinsic::mips_madd:
1569 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1570 case Intrinsic::mips_maddu:
1571 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1572 case Intrinsic::mips_msub:
1573 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1574 case Intrinsic::mips_msubu:
1575 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001576 case Intrinsic::mips_addv_b:
1577 case Intrinsic::mips_addv_h:
1578 case Intrinsic::mips_addv_w:
1579 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001580 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1581 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001582 case Intrinsic::mips_addvi_b:
1583 case Intrinsic::mips_addvi_h:
1584 case Intrinsic::mips_addvi_w:
1585 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001586 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1587 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001588 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001589 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1590 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001591 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001592 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1593 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001594 case Intrinsic::mips_bclr_b:
1595 case Intrinsic::mips_bclr_h:
1596 case Intrinsic::mips_bclr_w:
1597 case Intrinsic::mips_bclr_d:
1598 return lowerMSABitClear(Op, DAG);
1599 case Intrinsic::mips_bclri_b:
1600 case Intrinsic::mips_bclri_h:
1601 case Intrinsic::mips_bclri_w:
1602 case Intrinsic::mips_bclri_d:
1603 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001604 case Intrinsic::mips_binsli_b:
1605 case Intrinsic::mips_binsli_h:
1606 case Intrinsic::mips_binsli_w:
1607 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001608 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001609 EVT VecTy = Op->getValueType(0);
1610 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001611 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1612 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001613 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001614 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001615 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001616 DAG.getConstant(Mask, DL, VecTy, true),
1617 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001618 }
1619 case Intrinsic::mips_binsri_b:
1620 case Intrinsic::mips_binsri_h:
1621 case Intrinsic::mips_binsri_w:
1622 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001623 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001624 EVT VecTy = Op->getValueType(0);
1625 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001626 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1627 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001628 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001629 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001630 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001631 DAG.getConstant(Mask, DL, VecTy, true),
1632 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001633 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001634 case Intrinsic::mips_bmnz_v:
1635 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1636 Op->getOperand(2), Op->getOperand(1));
1637 case Intrinsic::mips_bmnzi_b:
1638 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1639 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1640 Op->getOperand(1));
1641 case Intrinsic::mips_bmz_v:
1642 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1643 Op->getOperand(1), Op->getOperand(2));
1644 case Intrinsic::mips_bmzi_b:
1645 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1646 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1647 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001648 case Intrinsic::mips_bneg_b:
1649 case Intrinsic::mips_bneg_h:
1650 case Intrinsic::mips_bneg_w:
1651 case Intrinsic::mips_bneg_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::XOR, 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_bnegi_b:
1660 case Intrinsic::mips_bnegi_h:
1661 case Intrinsic::mips_bnegi_w:
1662 case Intrinsic::mips_bnegi_d:
1663 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001664 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001665 case Intrinsic::mips_bnz_b:
1666 case Intrinsic::mips_bnz_h:
1667 case Intrinsic::mips_bnz_w:
1668 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001669 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1670 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001671 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001672 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1673 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001674 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001675 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001676 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001677 Op->getOperand(1), Op->getOperand(3),
1678 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001679 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001680 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001681 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001682 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1683 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001684 case Intrinsic::mips_bset_b:
1685 case Intrinsic::mips_bset_h:
1686 case Intrinsic::mips_bset_w:
1687 case Intrinsic::mips_bset_d: {
1688 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001689 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001690
1691 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1692 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001693 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001694 }
1695 case Intrinsic::mips_bseti_b:
1696 case Intrinsic::mips_bseti_h:
1697 case Intrinsic::mips_bseti_w:
1698 case Intrinsic::mips_bseti_d:
1699 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001700 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001701 case Intrinsic::mips_bz_b:
1702 case Intrinsic::mips_bz_h:
1703 case Intrinsic::mips_bz_w:
1704 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001705 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1706 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001707 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001708 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1709 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001710 case Intrinsic::mips_ceq_b:
1711 case Intrinsic::mips_ceq_h:
1712 case Intrinsic::mips_ceq_w:
1713 case Intrinsic::mips_ceq_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::SETEQ);
1716 case Intrinsic::mips_ceqi_b:
1717 case Intrinsic::mips_ceqi_h:
1718 case Intrinsic::mips_ceqi_w:
1719 case Intrinsic::mips_ceqi_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::SETEQ);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001722 case Intrinsic::mips_cle_s_b:
1723 case Intrinsic::mips_cle_s_h:
1724 case Intrinsic::mips_cle_s_w:
1725 case Intrinsic::mips_cle_s_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::SETLE);
1728 case Intrinsic::mips_clei_s_b:
1729 case Intrinsic::mips_clei_s_h:
1730 case Intrinsic::mips_clei_s_w:
1731 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001732 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001733 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLE);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001734 case Intrinsic::mips_cle_u_b:
1735 case Intrinsic::mips_cle_u_h:
1736 case Intrinsic::mips_cle_u_w:
1737 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001738 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001739 Op->getOperand(2), ISD::SETULE);
1740 case Intrinsic::mips_clei_u_b:
1741 case Intrinsic::mips_clei_u_h:
1742 case Intrinsic::mips_clei_u_w:
1743 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001744 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001745 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1746 case Intrinsic::mips_clt_s_b:
1747 case Intrinsic::mips_clt_s_h:
1748 case Intrinsic::mips_clt_s_w:
1749 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001750 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001751 Op->getOperand(2), ISD::SETLT);
1752 case Intrinsic::mips_clti_s_b:
1753 case Intrinsic::mips_clti_s_h:
1754 case Intrinsic::mips_clti_s_w:
1755 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001756 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001757 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLT);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001758 case Intrinsic::mips_clt_u_b:
1759 case Intrinsic::mips_clt_u_h:
1760 case Intrinsic::mips_clt_u_w:
1761 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001762 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001763 Op->getOperand(2), ISD::SETULT);
1764 case Intrinsic::mips_clti_u_b:
1765 case Intrinsic::mips_clti_u_h:
1766 case Intrinsic::mips_clti_u_w:
1767 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001768 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001769 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001770 case Intrinsic::mips_copy_s_b:
1771 case Intrinsic::mips_copy_s_h:
1772 case Intrinsic::mips_copy_s_w:
1773 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001774 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001775 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001776 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1777 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1778 else {
1779 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1780 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1781 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1782 Op->getValueType(0), Op->getOperand(1),
1783 Op->getOperand(2));
1784 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001785 case Intrinsic::mips_copy_u_b:
1786 case Intrinsic::mips_copy_u_h:
1787 case Intrinsic::mips_copy_u_w:
1788 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001789 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001790 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001791 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1792 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1793 else {
1794 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1795 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1796 // Note: When i64 is illegal, this results in copy_s.w instructions
1797 // instead of copy_u.w instructions. This makes no difference to the
1798 // behaviour since i64 is only illegal when the register file is 32-bit.
1799 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1800 Op->getValueType(0), Op->getOperand(1),
1801 Op->getOperand(2));
1802 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001803 case Intrinsic::mips_div_s_b:
1804 case Intrinsic::mips_div_s_h:
1805 case Intrinsic::mips_div_s_w:
1806 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001807 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1808 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001809 case Intrinsic::mips_div_u_b:
1810 case Intrinsic::mips_div_u_h:
1811 case Intrinsic::mips_div_u_w:
1812 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001813 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1814 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001815 case Intrinsic::mips_fadd_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001816 case Intrinsic::mips_fadd_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001817 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001818 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1819 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001820 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1821 case Intrinsic::mips_fceq_w:
1822 case Intrinsic::mips_fceq_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::SETOEQ);
1825 case Intrinsic::mips_fcle_w:
1826 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001827 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001828 Op->getOperand(2), ISD::SETOLE);
1829 case Intrinsic::mips_fclt_w:
1830 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001831 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001832 Op->getOperand(2), ISD::SETOLT);
1833 case Intrinsic::mips_fcne_w:
1834 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001835 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001836 Op->getOperand(2), ISD::SETONE);
1837 case Intrinsic::mips_fcor_w:
1838 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001839 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001840 Op->getOperand(2), ISD::SETO);
1841 case Intrinsic::mips_fcueq_w:
1842 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001843 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001844 Op->getOperand(2), ISD::SETUEQ);
1845 case Intrinsic::mips_fcule_w:
1846 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001847 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001848 Op->getOperand(2), ISD::SETULE);
1849 case Intrinsic::mips_fcult_w:
1850 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001851 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001852 Op->getOperand(2), ISD::SETULT);
1853 case Intrinsic::mips_fcun_w:
1854 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001855 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001856 Op->getOperand(2), ISD::SETUO);
1857 case Intrinsic::mips_fcune_w:
1858 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001859 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001860 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001861 case Intrinsic::mips_fdiv_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001862 case Intrinsic::mips_fdiv_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001863 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001864 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1865 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001866 case Intrinsic::mips_ffint_u_w:
1867 case Intrinsic::mips_ffint_u_d:
1868 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1869 Op->getOperand(1));
1870 case Intrinsic::mips_ffint_s_w:
1871 case Intrinsic::mips_ffint_s_d:
1872 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1873 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001874 case Intrinsic::mips_fill_b:
1875 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001876 case Intrinsic::mips_fill_w:
1877 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001878 EVT ResTy = Op->getValueType(0);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001879 SmallVector<SDValue, 16> Ops(ResTy.getVectorNumElements(),
1880 Op->getOperand(1));
Daniel Sandersf49dd822013-09-24 13:33:07 +00001881
Daniel Sandersc72593e2013-09-27 13:20:41 +00001882 // If ResTy is v2i64 then the type legalizer will break this node down into
1883 // an equivalent v4i32.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001884 return DAG.getBuildVector(ResTy, DL, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001885 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001886 case Intrinsic::mips_fexp2_w:
1887 case Intrinsic::mips_fexp2_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001888 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sandersa9521602013-10-23 10:36:52 +00001889 EVT ResTy = Op->getValueType(0);
1890 return DAG.getNode(
1891 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1892 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1893 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001894 case Intrinsic::mips_flog2_w:
1895 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001896 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001897 case Intrinsic::mips_fmadd_w:
1898 case Intrinsic::mips_fmadd_d:
1899 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1900 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001901 case Intrinsic::mips_fmul_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001902 case Intrinsic::mips_fmul_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001903 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001904 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1905 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001906 case Intrinsic::mips_fmsub_w:
1907 case Intrinsic::mips_fmsub_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001908 // TODO: If intrinsics have fast-math-flags, propagate them.
Aleksandar Beserminji3546c162018-04-27 13:30:27 +00001909 return DAG.getNode(MipsISD::FMS, SDLoc(Op), Op->getValueType(0),
1910 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001911 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001912 case Intrinsic::mips_frint_w:
1913 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001914 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001915 case Intrinsic::mips_fsqrt_w:
1916 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001917 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001918 case Intrinsic::mips_fsub_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001919 case Intrinsic::mips_fsub_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001920 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001921 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1922 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001923 case Intrinsic::mips_ftrunc_u_w:
1924 case Intrinsic::mips_ftrunc_u_d:
1925 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1926 Op->getOperand(1));
1927 case Intrinsic::mips_ftrunc_s_w:
1928 case Intrinsic::mips_ftrunc_s_d:
1929 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1930 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001931 case Intrinsic::mips_ilvev_b:
1932 case Intrinsic::mips_ilvev_h:
1933 case Intrinsic::mips_ilvev_w:
1934 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001935 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001936 Op->getOperand(1), Op->getOperand(2));
1937 case Intrinsic::mips_ilvl_b:
1938 case Intrinsic::mips_ilvl_h:
1939 case Intrinsic::mips_ilvl_w:
1940 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001941 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001942 Op->getOperand(1), Op->getOperand(2));
1943 case Intrinsic::mips_ilvod_b:
1944 case Intrinsic::mips_ilvod_h:
1945 case Intrinsic::mips_ilvod_w:
1946 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001947 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001948 Op->getOperand(1), Op->getOperand(2));
1949 case Intrinsic::mips_ilvr_b:
1950 case Intrinsic::mips_ilvr_h:
1951 case Intrinsic::mips_ilvr_w:
1952 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001953 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001954 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001955 case Intrinsic::mips_insert_b:
1956 case Intrinsic::mips_insert_h:
1957 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001958 case Intrinsic::mips_insert_d:
1959 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1960 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001961 case Intrinsic::mips_insve_b:
1962 case Intrinsic::mips_insve_h:
1963 case Intrinsic::mips_insve_w:
Simon Dardis548a53f2017-01-10 16:40:57 +00001964 case Intrinsic::mips_insve_d: {
1965 // Report an error for out of range values.
1966 int64_t Max;
1967 switch (Intrinsic) {
1968 case Intrinsic::mips_insve_b: Max = 15; break;
1969 case Intrinsic::mips_insve_h: Max = 7; break;
1970 case Intrinsic::mips_insve_w: Max = 3; break;
1971 case Intrinsic::mips_insve_d: Max = 1; break;
1972 default: llvm_unreachable("Unmatched intrinsic");
1973 }
1974 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
1975 if (Value < 0 || Value > Max)
1976 report_fatal_error("Immediate out of range");
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001977 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1978 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001979 DAG.getConstant(0, DL, MVT::i32));
Simon Dardis548a53f2017-01-10 16:40:57 +00001980 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00001981 case Intrinsic::mips_ldi_b:
1982 case Intrinsic::mips_ldi_h:
1983 case Intrinsic::mips_ldi_w:
1984 case Intrinsic::mips_ldi_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00001985 return lowerMSASplatImm(Op, 1, DAG, true);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001986 case Intrinsic::mips_lsa:
1987 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001988 EVT ResTy = Op->getValueType(0);
1989 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1990 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1991 Op->getOperand(2), Op->getOperand(3)));
1992 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001993 case Intrinsic::mips_maddv_b:
1994 case Intrinsic::mips_maddv_h:
1995 case Intrinsic::mips_maddv_w:
1996 case Intrinsic::mips_maddv_d: {
1997 EVT ResTy = Op->getValueType(0);
1998 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1999 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2000 Op->getOperand(2), Op->getOperand(3)));
2001 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00002002 case Intrinsic::mips_max_s_b:
2003 case Intrinsic::mips_max_s_h:
2004 case Intrinsic::mips_max_s_w:
2005 case Intrinsic::mips_max_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002006 return DAG.getNode(ISD::SMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002007 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002008 case Intrinsic::mips_max_u_b:
2009 case Intrinsic::mips_max_u_h:
2010 case Intrinsic::mips_max_u_w:
2011 case Intrinsic::mips_max_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002012 return DAG.getNode(ISD::UMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002013 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002014 case Intrinsic::mips_maxi_s_b:
2015 case Intrinsic::mips_maxi_s_h:
2016 case Intrinsic::mips_maxi_s_w:
2017 case Intrinsic::mips_maxi_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002018 return DAG.getNode(ISD::SMAX, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00002019 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002020 case Intrinsic::mips_maxi_u_b:
2021 case Intrinsic::mips_maxi_u_h:
2022 case Intrinsic::mips_maxi_u_w:
2023 case Intrinsic::mips_maxi_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002024 return DAG.getNode(ISD::UMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002025 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002026 case Intrinsic::mips_min_s_b:
2027 case Intrinsic::mips_min_s_h:
2028 case Intrinsic::mips_min_s_w:
2029 case Intrinsic::mips_min_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002030 return DAG.getNode(ISD::SMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002031 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002032 case Intrinsic::mips_min_u_b:
2033 case Intrinsic::mips_min_u_h:
2034 case Intrinsic::mips_min_u_w:
2035 case Intrinsic::mips_min_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002036 return DAG.getNode(ISD::UMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002037 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002038 case Intrinsic::mips_mini_s_b:
2039 case Intrinsic::mips_mini_s_h:
2040 case Intrinsic::mips_mini_s_w:
2041 case Intrinsic::mips_mini_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002042 return DAG.getNode(ISD::SMIN, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00002043 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002044 case Intrinsic::mips_mini_u_b:
2045 case Intrinsic::mips_mini_u_h:
2046 case Intrinsic::mips_mini_u_w:
2047 case Intrinsic::mips_mini_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002048 return DAG.getNode(ISD::UMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002049 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00002050 case Intrinsic::mips_mod_s_b:
2051 case Intrinsic::mips_mod_s_h:
2052 case Intrinsic::mips_mod_s_w:
2053 case Intrinsic::mips_mod_s_d:
2054 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
2055 Op->getOperand(2));
2056 case Intrinsic::mips_mod_u_b:
2057 case Intrinsic::mips_mod_u_h:
2058 case Intrinsic::mips_mod_u_w:
2059 case Intrinsic::mips_mod_u_d:
2060 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
2061 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002062 case Intrinsic::mips_mulv_b:
2063 case Intrinsic::mips_mulv_h:
2064 case Intrinsic::mips_mulv_w:
2065 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002066 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
2067 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00002068 case Intrinsic::mips_msubv_b:
2069 case Intrinsic::mips_msubv_h:
2070 case Intrinsic::mips_msubv_w:
2071 case Intrinsic::mips_msubv_d: {
2072 EVT ResTy = Op->getValueType(0);
2073 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
2074 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2075 Op->getOperand(2), Op->getOperand(3)));
2076 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002077 case Intrinsic::mips_nlzc_b:
2078 case Intrinsic::mips_nlzc_h:
2079 case Intrinsic::mips_nlzc_w:
2080 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002081 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002082 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002083 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2084 Op->getOperand(1), Op->getOperand(2));
2085 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002086 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002087 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002088 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2089 Op->getOperand(1),
2090 lowerMSASplatImm(Op, 2, DAG));
2091 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002092 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002093 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002094 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2095 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002096 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002097 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2098 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002099 case Intrinsic::mips_pckev_b:
2100 case Intrinsic::mips_pckev_h:
2101 case Intrinsic::mips_pckev_w:
2102 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002103 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002104 Op->getOperand(1), Op->getOperand(2));
2105 case Intrinsic::mips_pckod_b:
2106 case Intrinsic::mips_pckod_h:
2107 case Intrinsic::mips_pckod_w:
2108 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002109 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002110 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002111 case Intrinsic::mips_pcnt_b:
2112 case Intrinsic::mips_pcnt_h:
2113 case Intrinsic::mips_pcnt_w:
2114 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002115 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002116 case Intrinsic::mips_sat_s_b:
2117 case Intrinsic::mips_sat_s_h:
2118 case Intrinsic::mips_sat_s_w:
2119 case Intrinsic::mips_sat_s_d:
2120 case Intrinsic::mips_sat_u_b:
2121 case Intrinsic::mips_sat_u_h:
2122 case Intrinsic::mips_sat_u_w:
2123 case Intrinsic::mips_sat_u_d: {
2124 // Report an error for out of range values.
2125 int64_t Max;
2126 switch (Intrinsic) {
2127 case Intrinsic::mips_sat_s_b:
2128 case Intrinsic::mips_sat_u_b: Max = 7; break;
2129 case Intrinsic::mips_sat_s_h:
2130 case Intrinsic::mips_sat_u_h: Max = 15; break;
2131 case Intrinsic::mips_sat_s_w:
2132 case Intrinsic::mips_sat_u_w: Max = 31; break;
2133 case Intrinsic::mips_sat_s_d:
2134 case Intrinsic::mips_sat_u_d: Max = 63; break;
2135 default: llvm_unreachable("Unmatched intrinsic");
2136 }
2137 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2138 if (Value < 0 || Value > Max)
2139 report_fatal_error("Immediate out of range");
2140 return SDValue();
2141 }
Daniel Sanders26307182013-09-24 14:20:00 +00002142 case Intrinsic::mips_shf_b:
2143 case Intrinsic::mips_shf_h:
Simon Dardis548a53f2017-01-10 16:40:57 +00002144 case Intrinsic::mips_shf_w: {
2145 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2146 if (Value < 0 || Value > 255)
2147 report_fatal_error("Immediate out of range");
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002148 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002149 Op->getOperand(2), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002150 }
2151 case Intrinsic::mips_sldi_b:
2152 case Intrinsic::mips_sldi_h:
2153 case Intrinsic::mips_sldi_w:
2154 case Intrinsic::mips_sldi_d: {
2155 // Report an error for out of range values.
2156 int64_t Max;
2157 switch (Intrinsic) {
2158 case Intrinsic::mips_sldi_b: Max = 15; break;
2159 case Intrinsic::mips_sldi_h: Max = 7; break;
2160 case Intrinsic::mips_sldi_w: Max = 3; break;
2161 case Intrinsic::mips_sldi_d: Max = 1; break;
2162 default: llvm_unreachable("Unmatched intrinsic");
2163 }
2164 int64_t Value = cast<ConstantSDNode>(Op->getOperand(3))->getSExtValue();
2165 if (Value < 0 || Value > Max)
2166 report_fatal_error("Immediate out of range");
2167 return SDValue();
2168 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002169 case Intrinsic::mips_sll_b:
2170 case Intrinsic::mips_sll_h:
2171 case Intrinsic::mips_sll_w:
2172 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002173 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002174 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002175 case Intrinsic::mips_slli_b:
2176 case Intrinsic::mips_slli_h:
2177 case Intrinsic::mips_slli_w:
2178 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002179 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2180 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002181 case Intrinsic::mips_splat_b:
2182 case Intrinsic::mips_splat_h:
2183 case Intrinsic::mips_splat_w:
2184 case Intrinsic::mips_splat_d:
2185 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2186 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2187 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2188 // Instead we lower to MipsISD::VSHF and match from there.
2189 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002190 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002191 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002192 case Intrinsic::mips_splati_b:
2193 case Intrinsic::mips_splati_h:
2194 case Intrinsic::mips_splati_w:
2195 case Intrinsic::mips_splati_d:
2196 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2197 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2198 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002199 case Intrinsic::mips_sra_b:
2200 case Intrinsic::mips_sra_h:
2201 case Intrinsic::mips_sra_w:
2202 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002203 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002204 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002205 case Intrinsic::mips_srai_b:
2206 case Intrinsic::mips_srai_h:
2207 case Intrinsic::mips_srai_w:
2208 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002209 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2210 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002211 case Intrinsic::mips_srari_b:
2212 case Intrinsic::mips_srari_h:
2213 case Intrinsic::mips_srari_w:
2214 case Intrinsic::mips_srari_d: {
2215 // Report an error for out of range values.
2216 int64_t Max;
2217 switch (Intrinsic) {
2218 case Intrinsic::mips_srari_b: Max = 7; break;
2219 case Intrinsic::mips_srari_h: Max = 15; break;
2220 case Intrinsic::mips_srari_w: Max = 31; break;
2221 case Intrinsic::mips_srari_d: Max = 63; break;
2222 default: llvm_unreachable("Unmatched intrinsic");
2223 }
2224 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2225 if (Value < 0 || Value > Max)
2226 report_fatal_error("Immediate out of range");
2227 return SDValue();
2228 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002229 case Intrinsic::mips_srl_b:
2230 case Intrinsic::mips_srl_h:
2231 case Intrinsic::mips_srl_w:
2232 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002233 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002234 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002235 case Intrinsic::mips_srli_b:
2236 case Intrinsic::mips_srli_h:
2237 case Intrinsic::mips_srli_w:
2238 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002239 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2240 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002241 case Intrinsic::mips_srlri_b:
2242 case Intrinsic::mips_srlri_h:
2243 case Intrinsic::mips_srlri_w:
2244 case Intrinsic::mips_srlri_d: {
2245 // Report an error for out of range values.
2246 int64_t Max;
2247 switch (Intrinsic) {
2248 case Intrinsic::mips_srlri_b: Max = 7; break;
2249 case Intrinsic::mips_srlri_h: Max = 15; break;
2250 case Intrinsic::mips_srlri_w: Max = 31; break;
2251 case Intrinsic::mips_srlri_d: Max = 63; break;
2252 default: llvm_unreachable("Unmatched intrinsic");
2253 }
2254 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2255 if (Value < 0 || Value > Max)
2256 report_fatal_error("Immediate out of range");
2257 return SDValue();
2258 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002259 case Intrinsic::mips_subv_b:
2260 case Intrinsic::mips_subv_h:
2261 case Intrinsic::mips_subv_w:
2262 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002263 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2264 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002265 case Intrinsic::mips_subvi_b:
2266 case Intrinsic::mips_subvi_h:
2267 case Intrinsic::mips_subvi_w:
2268 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002269 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2270 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002271 case Intrinsic::mips_vshf_b:
2272 case Intrinsic::mips_vshf_h:
2273 case Intrinsic::mips_vshf_w:
2274 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002275 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002276 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002277 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002278 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2279 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002280 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002281 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2282 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Marcin Koscielnicki7efdca52016-04-27 17:21:49 +00002283 case Intrinsic::thread_pointer: {
2284 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2285 return DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2286 }
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002287 }
2288}
2289
Simon Dardis548a53f2017-01-10 16:40:57 +00002290static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2291 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002292 SDLoc DL(Op);
2293 SDValue ChainIn = Op->getOperand(0);
2294 SDValue Address = Op->getOperand(2);
2295 SDValue Offset = Op->getOperand(3);
2296 EVT ResTy = Op->getValueType(0);
2297 EVT PtrTy = Address->getValueType(0);
2298
Simon Dardis548a53f2017-01-10 16:40:57 +00002299 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2300 // however takes an i32 signed constant offset. The actual type of the
2301 // intrinsic is a scaled signed i10.
2302 if (Subtarget.isABI_N64())
2303 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2304
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002305 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002306 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(),
2307 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002308}
2309
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002310SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2311 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002312 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2313 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002314 default:
2315 return SDValue();
2316 case Intrinsic::mips_extp:
2317 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2318 case Intrinsic::mips_extpdp:
2319 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2320 case Intrinsic::mips_extr_w:
2321 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2322 case Intrinsic::mips_extr_r_w:
2323 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2324 case Intrinsic::mips_extr_rs_w:
2325 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2326 case Intrinsic::mips_extr_s_h:
2327 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2328 case Intrinsic::mips_mthlip:
2329 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2330 case Intrinsic::mips_mulsaq_s_w_ph:
2331 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2332 case Intrinsic::mips_maq_s_w_phl:
2333 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2334 case Intrinsic::mips_maq_s_w_phr:
2335 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2336 case Intrinsic::mips_maq_sa_w_phl:
2337 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2338 case Intrinsic::mips_maq_sa_w_phr:
2339 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2340 case Intrinsic::mips_dpaq_s_w_ph:
2341 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2342 case Intrinsic::mips_dpsq_s_w_ph:
2343 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2344 case Intrinsic::mips_dpaq_sa_l_w:
2345 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2346 case Intrinsic::mips_dpsq_sa_l_w:
2347 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2348 case Intrinsic::mips_dpaqx_s_w_ph:
2349 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2350 case Intrinsic::mips_dpaqx_sa_w_ph:
2351 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2352 case Intrinsic::mips_dpsqx_s_w_ph:
2353 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2354 case Intrinsic::mips_dpsqx_sa_w_ph:
2355 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002356 case Intrinsic::mips_ld_b:
2357 case Intrinsic::mips_ld_h:
2358 case Intrinsic::mips_ld_w:
2359 case Intrinsic::mips_ld_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002360 return lowerMSALoadIntr(Op, DAG, Intr, Subtarget);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002361 }
2362}
2363
Simon Dardis548a53f2017-01-10 16:40:57 +00002364static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2365 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002366 SDLoc DL(Op);
2367 SDValue ChainIn = Op->getOperand(0);
2368 SDValue Value = Op->getOperand(2);
2369 SDValue Address = Op->getOperand(3);
2370 SDValue Offset = Op->getOperand(4);
2371 EVT PtrTy = Address->getValueType(0);
2372
Simon Dardis548a53f2017-01-10 16:40:57 +00002373 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2374 // however takes an i32 signed constant offset. The actual type of the
2375 // intrinsic is a scaled signed i10.
2376 if (Subtarget.isABI_N64())
2377 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2378
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002379 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2380
Justin Lebar9c375812016-07-15 18:27:10 +00002381 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(),
2382 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002383}
2384
2385SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2386 SelectionDAG &DAG) const {
2387 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2388 switch (Intr) {
2389 default:
2390 return SDValue();
2391 case Intrinsic::mips_st_b:
2392 case Intrinsic::mips_st_h:
2393 case Intrinsic::mips_st_w:
2394 case Intrinsic::mips_st_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002395 return lowerMSAStoreIntr(Op, DAG, Intr, Subtarget);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002396 }
2397}
2398
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002399// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2400//
2401// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2402// choose to sign-extend but we could have equally chosen zero-extend. The
2403// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2404// result into this node later (possibly changing it to a zero-extend in the
2405// process).
2406SDValue MipsSETargetLowering::
2407lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2408 SDLoc DL(Op);
2409 EVT ResTy = Op->getValueType(0);
2410 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002411 EVT VecTy = Op0->getValueType(0);
2412
2413 if (!VecTy.is128BitVector())
2414 return SDValue();
2415
2416 if (ResTy.isInteger()) {
2417 SDValue Op1 = Op->getOperand(1);
2418 EVT EltTy = VecTy.getVectorElementType();
2419 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2420 DAG.getValueType(EltTy));
2421 }
2422
2423 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002424}
2425
Daniel Sandersf49dd822013-09-24 13:33:07 +00002426static bool isConstantOrUndef(const SDValue Op) {
Sanjay Patel57195842016-03-14 17:28:46 +00002427 if (Op->isUndef())
Daniel Sandersf49dd822013-09-24 13:33:07 +00002428 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002429 if (isa<ConstantSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002430 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002431 if (isa<ConstantFPSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002432 return true;
2433 return false;
2434}
2435
2436static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2437 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2438 if (isConstantOrUndef(Op->getOperand(i)))
2439 return true;
2440 return false;
2441}
2442
Daniel Sanders7a289d02013-09-23 12:02:46 +00002443// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2444// backend.
2445//
2446// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002447// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2448// 2 less than or equal to 64 and the value fits into a signed 10-bit
2449// immediate
2450// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2451// is a power of 2 less than or equal to 64 and the value does not fit into a
2452// signed 10-bit immediate
2453// - Non-constant splats are legal as-is.
2454// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2455// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002456SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2457 SelectionDAG &DAG) const {
2458 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2459 EVT ResTy = Op->getValueType(0);
2460 SDLoc DL(Op);
2461 APInt SplatValue, SplatUndef;
2462 unsigned SplatBitSize;
2463 bool HasAnyUndefs;
2464
Eric Christopher1c29a652014-07-18 22:55:25 +00002465 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002466 return SDValue();
2467
2468 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2469 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002470 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002471 // We can only cope with 8, 16, 32, or 64-bit elements
2472 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2473 SplatBitSize != 64)
2474 return SDValue();
2475
Simon Dardis7090d142017-03-10 13:27:14 +00002476 // If the value isn't an integer type we will have to bitcast
2477 // from an integer type first. Also, if there are any undefs, we must
2478 // lower them to defined values first.
2479 if (ResTy.isInteger() && !HasAnyUndefs)
Daniel Sandersf49dd822013-09-24 13:33:07 +00002480 return Op;
2481
2482 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002483
2484 switch (SplatBitSize) {
2485 default:
2486 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002487 case 8:
2488 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002489 break;
2490 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002491 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002492 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002493 case 32:
2494 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002495 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002496 case 64:
2497 // There's no fill.d to fall back on for 64-bit values
2498 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002499 }
2500
Daniel Sanders50b80412013-11-15 12:56:49 +00002501 // SelectionDAG::getConstant will promote SplatValue appropriately.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002502 SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002503
Daniel Sanders50b80412013-11-15 12:56:49 +00002504 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002505 if (ViaVecTy != ResTy)
2506 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002507
2508 return Result;
Simon Pilgrimeb508f82018-12-12 18:32:29 +00002509 } else if (DAG.isSplatValue(Op, /* AllowUndefs */ false))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002510 return Op;
2511 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002512 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2513 // The resulting code is the same length as the expansion, but it doesn't
2514 // use memory operations
2515 EVT ResTy = Node->getValueType(0);
2516
2517 assert(ResTy.isVector());
2518
2519 unsigned NumElts = ResTy.getVectorNumElements();
2520 SDValue Vector = DAG.getUNDEF(ResTy);
2521 for (unsigned i = 0; i < NumElts; ++i) {
2522 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2523 Node->getOperand(i),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002524 DAG.getConstant(i, DL, MVT::i32));
Daniel Sandersf86622b2013-09-24 13:16:15 +00002525 }
2526 return Vector;
2527 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002528
2529 return SDValue();
2530}
2531
Daniel Sanders26307182013-09-24 14:20:00 +00002532// Lower VECTOR_SHUFFLE into SHF (if possible).
2533//
2534// SHF splits the vector into blocks of four elements, then shuffles these
2535// elements according to a <4 x i2> constant (encoded as an integer immediate).
2536//
2537// It is therefore possible to lower into SHF when the mask takes the form:
2538// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2539// When undef's appear they are treated as if they were whatever value is
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002540// necessary in order to fit the above forms.
Daniel Sanders26307182013-09-24 14:20:00 +00002541//
2542// For example:
2543// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2544// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2545// i32 7, i32 6, i32 5, i32 4>
2546// is lowered to:
2547// (SHF_H $w0, $w1, 27)
2548// where the 27 comes from:
2549// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2550static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2551 SmallVector<int, 16> Indices,
2552 SelectionDAG &DAG) {
2553 int SHFIndices[4] = { -1, -1, -1, -1 };
2554
2555 if (Indices.size() < 4)
2556 return SDValue();
2557
2558 for (unsigned i = 0; i < 4; ++i) {
2559 for (unsigned j = i; j < Indices.size(); j += 4) {
2560 int Idx = Indices[j];
2561
2562 // Convert from vector index to 4-element subvector index
2563 // If an index refers to an element outside of the subvector then give up
2564 if (Idx != -1) {
2565 Idx -= 4 * (j / 4);
2566 if (Idx < 0 || Idx >= 4)
2567 return SDValue();
2568 }
2569
2570 // If the mask has an undef, replace it with the current index.
2571 // Note that it might still be undef if the current index is also undef
2572 if (SHFIndices[i] == -1)
2573 SHFIndices[i] = Idx;
2574
2575 // Check that non-undef values are the same as in the mask. If they
2576 // aren't then give up
2577 if (!(Idx == -1 || Idx == SHFIndices[i]))
2578 return SDValue();
2579 }
2580 }
2581
2582 // Calculate the immediate. Replace any remaining undefs with zero
2583 APInt Imm(32, 0);
2584 for (int i = 3; i >= 0; --i) {
2585 int Idx = SHFIndices[i];
2586
2587 if (Idx == -1)
2588 Idx = 0;
2589
2590 Imm <<= 2;
2591 Imm |= Idx & 0x3;
2592 }
2593
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002594 SDLoc DL(Op);
2595 return DAG.getNode(MipsISD::SHF, DL, ResTy,
2596 DAG.getConstant(Imm, DL, MVT::i32), Op->getOperand(0));
Daniel Sanders26307182013-09-24 14:20:00 +00002597}
2598
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002599/// Determine whether a range fits a regular pattern of values.
2600/// This function accounts for the possibility of jumping over the End iterator.
2601template <typename ValType>
2602static bool
2603fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,
2604 unsigned CheckStride,
2605 typename SmallVectorImpl<ValType>::const_iterator End,
2606 ValType ExpectedIndex, unsigned ExpectedIndexStride) {
2607 auto &I = Begin;
2608
2609 while (I != End) {
2610 if (*I != -1 && *I != ExpectedIndex)
2611 return false;
2612 ExpectedIndex += ExpectedIndexStride;
2613
2614 // Incrementing past End is undefined behaviour so we must increment one
2615 // step at a time and check for End at each step.
2616 for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
2617 ; // Empty loop body.
2618 }
2619 return true;
2620}
2621
2622// Determine whether VECTOR_SHUFFLE is a SPLATI.
2623//
2624// It is a SPLATI when the mask is:
2625// <x, x, x, ...>
2626// where x is any valid index.
2627//
2628// When undef's appear in the mask they are treated as if they were whatever
2629// value is necessary in order to fit the above form.
2630static bool isVECTOR_SHUFFLE_SPLATI(SDValue Op, EVT ResTy,
2631 SmallVector<int, 16> Indices,
2632 SelectionDAG &DAG) {
2633 assert((Indices.size() % 2) == 0);
2634
2635 int SplatIndex = -1;
2636 for (const auto &V : Indices) {
2637 if (V != -1) {
2638 SplatIndex = V;
2639 break;
2640 }
2641 }
2642
2643 return fitsRegularPattern<int>(Indices.begin(), 1, Indices.end(), SplatIndex,
2644 0);
2645}
2646
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002647// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2648//
2649// ILVEV interleaves the even elements from each vector.
2650//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002651// It is possible to lower into ILVEV when the mask consists of two of the
2652// following forms interleaved:
2653// <0, 2, 4, ...>
2654// <n, n+2, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002655// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002656// For example:
2657// <0, 0, 2, 2, 4, 4, ...>
2658// <0, n, 2, n+2, 4, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002659//
2660// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002661// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002662static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2663 SmallVector<int, 16> Indices,
2664 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002665 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002666
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002667 SDValue Wt;
2668 SDValue Ws;
2669 const auto &Begin = Indices.begin();
2670 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002671
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002672 // Check even elements are taken from the even elements of one half or the
2673 // other and pick an operand accordingly.
2674 if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
2675 Wt = Op->getOperand(0);
2676 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 2))
2677 Wt = Op->getOperand(1);
2678 else
2679 return SDValue();
2680
2681 // Check odd elements are taken from the even elements of one half or the
2682 // other and pick an operand accordingly.
2683 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
2684 Ws = Op->getOperand(0);
2685 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 2))
2686 Ws = Op->getOperand(1);
2687 else
2688 return SDValue();
2689
2690 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002691}
2692
2693// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2694//
2695// ILVOD interleaves the odd elements from each vector.
2696//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002697// It is possible to lower into ILVOD when the mask consists of two of the
2698// following forms interleaved:
2699// <1, 3, 5, ...>
2700// <n+1, n+3, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002701// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002702// For example:
2703// <1, 1, 3, 3, 5, 5, ...>
2704// <1, n+1, 3, n+3, 5, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002705//
2706// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002707// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002708static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2709 SmallVector<int, 16> Indices,
2710 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002711 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002712
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002713 SDValue Wt;
2714 SDValue Ws;
2715 const auto &Begin = Indices.begin();
2716 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002717
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002718 // Check even elements are taken from the odd elements of one half or the
2719 // other and pick an operand accordingly.
2720 if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
2721 Wt = Op->getOperand(0);
2722 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + 1, 2))
2723 Wt = Op->getOperand(1);
2724 else
2725 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002726
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002727 // Check odd elements are taken from the odd elements of one half or the
2728 // other and pick an operand accordingly.
2729 if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
2730 Ws = Op->getOperand(0);
2731 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + 1, 2))
2732 Ws = Op->getOperand(1);
2733 else
2734 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002735
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002736 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Wt, Ws);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002737}
2738
2739// Lower VECTOR_SHUFFLE into ILVR (if possible).
2740//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002741// ILVR interleaves consecutive elements from the right (lowest-indexed) half of
2742// each vector.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002743//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002744// It is possible to lower into ILVR when the mask consists of two of the
2745// following forms interleaved:
2746// <0, 1, 2, ...>
2747// <n, n+1, n+2, ...>
2748// where n is the number of elements in the vector.
2749// For example:
2750// <0, 0, 1, 1, 2, 2, ...>
2751// <0, n, 1, n+1, 2, n+2, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002752//
2753// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002754// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002755static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2756 SmallVector<int, 16> Indices,
2757 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002758 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002759
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002760 SDValue Wt;
2761 SDValue Ws;
2762 const auto &Begin = Indices.begin();
2763 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002764
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002765 // Check even elements are taken from the right (lowest-indexed) elements of
2766 // one half or the other and pick an operand accordingly.
2767 if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
2768 Wt = Op->getOperand(0);
2769 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 1))
2770 Wt = Op->getOperand(1);
2771 else
2772 return SDValue();
2773
2774 // Check odd elements are taken from the right (lowest-indexed) elements of
2775 // one half or the other and pick an operand accordingly.
2776 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
2777 Ws = Op->getOperand(0);
2778 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 1))
2779 Ws = Op->getOperand(1);
2780 else
2781 return SDValue();
2782
2783 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Ws, Wt);
2784}
2785
2786// Lower VECTOR_SHUFFLE into ILVL (if possible).
2787//
2788// ILVL interleaves consecutive elements from the left (highest-indexed) half
2789// of each vector.
2790//
2791// It is possible to lower into ILVL when the mask consists of two of the
2792// following forms interleaved:
2793// <x, x+1, x+2, ...>
2794// <n+x, n+x+1, n+x+2, ...>
2795// where n is the number of elements in the vector and x is half n.
2796// For example:
2797// <x, x, x+1, x+1, x+2, x+2, ...>
2798// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2799//
2800// When undef's appear in the mask they are treated as if they were whatever
2801// value is necessary in order to fit the above forms.
2802static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2803 SmallVector<int, 16> Indices,
2804 SelectionDAG &DAG) {
2805 assert((Indices.size() % 2) == 0);
2806
2807 unsigned HalfSize = Indices.size() / 2;
2808 SDValue Wt;
2809 SDValue Ws;
2810 const auto &Begin = Indices.begin();
2811 const auto &End = Indices.end();
2812
2813 // Check even elements are taken from the left (highest-indexed) elements of
2814 // one half or the other and pick an operand accordingly.
2815 if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
2816 Wt = Op->getOperand(0);
2817 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + HalfSize, 1))
2818 Wt = Op->getOperand(1);
2819 else
2820 return SDValue();
2821
2822 // Check odd elements are taken from the left (highest-indexed) elements of
2823 // one half or the other and pick an operand accordingly.
2824 if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
2825 Ws = Op->getOperand(0);
2826 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + HalfSize,
2827 1))
2828 Ws = Op->getOperand(1);
2829 else
2830 return SDValue();
2831
2832 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002833}
2834
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002835// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2836//
2837// PCKEV copies the even elements of each vector into the result vector.
2838//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002839// It is possible to lower into PCKEV when the mask consists of two of the
2840// following forms concatenated:
2841// <0, 2, 4, ...>
2842// <n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002843// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002844// For example:
2845// <0, 2, 4, ..., 0, 2, 4, ...>
2846// <0, 2, 4, ..., n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002847//
2848// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002849// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002850static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2851 SmallVector<int, 16> Indices,
2852 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002853 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002854
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002855 SDValue Wt;
2856 SDValue Ws;
2857 const auto &Begin = Indices.begin();
2858 const auto &Mid = Indices.begin() + Indices.size() / 2;
2859 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002860
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002861 if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
2862 Wt = Op->getOperand(0);
2863 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size(), 2))
2864 Wt = Op->getOperand(1);
2865 else
2866 return SDValue();
2867
2868 if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
2869 Ws = Op->getOperand(0);
2870 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size(), 2))
2871 Ws = Op->getOperand(1);
2872 else
2873 return SDValue();
2874
2875 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002876}
2877
2878// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2879//
2880// PCKOD copies the odd elements of each vector into the result vector.
2881//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002882// It is possible to lower into PCKOD when the mask consists of two of the
2883// following forms concatenated:
2884// <1, 3, 5, ...>
2885// <n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002886// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002887// For example:
2888// <1, 3, 5, ..., 1, 3, 5, ...>
2889// <1, 3, 5, ..., n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002890//
2891// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002892// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002893static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2894 SmallVector<int, 16> Indices,
2895 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002896 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002897
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002898 SDValue Wt;
2899 SDValue Ws;
2900 const auto &Begin = Indices.begin();
2901 const auto &Mid = Indices.begin() + Indices.size() / 2;
2902 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002903
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002904 if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
2905 Wt = Op->getOperand(0);
2906 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size() + 1, 2))
2907 Wt = Op->getOperand(1);
2908 else
2909 return SDValue();
2910
2911 if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
2912 Ws = Op->getOperand(0);
2913 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size() + 1, 2))
2914 Ws = Op->getOperand(1);
2915 else
2916 return SDValue();
2917
2918 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002919}
2920
Daniel Sanderse5087042013-09-24 14:02:15 +00002921// Lower VECTOR_SHUFFLE into VSHF.
2922//
2923// This mostly consists of converting the shuffle indices in Indices into a
2924// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2925// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2926// if the type is v8i16 and all the indices are less than 8 then the second
2927// operand is unused and can be replaced with anything. We choose to replace it
2928// with the used operand since this reduces the number of instructions overall.
2929static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2930 SmallVector<int, 16> Indices,
2931 SelectionDAG &DAG) {
2932 SmallVector<SDValue, 16> Ops;
2933 SDValue Op0;
2934 SDValue Op1;
2935 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2936 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2937 bool Using1stVec = false;
2938 bool Using2ndVec = false;
2939 SDLoc DL(Op);
2940 int ResTyNumElts = ResTy.getVectorNumElements();
2941
2942 for (int i = 0; i < ResTyNumElts; ++i) {
2943 // Idx == -1 means UNDEF
2944 int Idx = Indices[i];
2945
2946 if (0 <= Idx && Idx < ResTyNumElts)
2947 Using1stVec = true;
2948 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2949 Using2ndVec = true;
2950 }
2951
2952 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2953 ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002954 Ops.push_back(DAG.getTargetConstant(*I, DL, MaskEltTy));
Daniel Sanderse5087042013-09-24 14:02:15 +00002955
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002956 SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002957
2958 if (Using1stVec && Using2ndVec) {
2959 Op0 = Op->getOperand(0);
2960 Op1 = Op->getOperand(1);
2961 } else if (Using1stVec)
2962 Op0 = Op1 = Op->getOperand(0);
2963 else if (Using2ndVec)
2964 Op0 = Op1 = Op->getOperand(1);
2965 else
2966 llvm_unreachable("shuffle vector mask references neither vector operand?");
2967
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002968 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2969 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2970 // VSHF concatenates the vectors in a bitwise fashion:
2971 // <0b00, 0b01> + <0b10, 0b11> ->
2972 // 0b0100 + 0b1110 -> 0b01001110
2973 // <0b10, 0b11, 0b00, 0b01>
2974 // We must therefore swap the operands to get the correct result.
2975 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002976}
2977
2978// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2979// indices in the shuffle.
2980SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2981 SelectionDAG &DAG) const {
2982 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2983 EVT ResTy = Op->getValueType(0);
2984
2985 if (!ResTy.is128BitVector())
2986 return SDValue();
2987
2988 int ResTyNumElts = ResTy.getVectorNumElements();
2989 SmallVector<int, 16> Indices;
2990
2991 for (int i = 0; i < ResTyNumElts; ++i)
2992 Indices.push_back(Node->getMaskElt(i));
2993
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002994 // splati.[bhwd] is preferable to the others but is matched from
2995 // MipsISD::VSHF.
2996 if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
2997 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002998 SDValue Result;
2999 if ((Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00003000 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003001 if ((Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00003002 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003003 if ((Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00003004 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003005 if ((Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00003006 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003007 if ((Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00003008 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003009 if ((Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00003010 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003011 if ((Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG)))
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00003012 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00003013 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
3014}
3015
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003016MachineBasicBlock *
3017MipsSETargetLowering::emitBPOSGE32(MachineInstr &MI,
3018 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003019 // $bb:
3020 // bposge32_pseudo $vr0
3021 // =>
3022 // $bb:
3023 // bposge32 $tbb
3024 // $fbb:
3025 // li $vr2, 0
3026 // b $sink
3027 // $tbb:
3028 // li $vr1, 1
3029 // $sink:
3030 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
3031
3032 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003033 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003034 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003035 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003036 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003037 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003038 MachineFunction *F = BB->getParent();
3039 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3040 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3041 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3042 F->insert(It, FBB);
3043 F->insert(It, TBB);
3044 F->insert(It, Sink);
3045
3046 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003047 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003048 BB->end());
3049 Sink->transferSuccessorsAndUpdatePHIs(BB);
3050
3051 // Add successors.
3052 BB->addSuccessor(FBB);
3053 BB->addSuccessor(TBB);
3054 FBB->addSuccessor(Sink);
3055 TBB->addSuccessor(Sink);
3056
3057 // Insert the real bposge32 instruction to $BB.
3058 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +00003059 // Insert the real bposge32c instruction to $BB.
3060 BuildMI(BB, DL, TII->get(Mips::BPOSGE32C_MMR3)).addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003061
3062 // Fill $FBB.
3063 unsigned VR2 = RegInfo.createVirtualRegister(RC);
3064 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
3065 .addReg(Mips::ZERO).addImm(0);
3066 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3067
3068 // Fill $TBB.
3069 unsigned VR1 = RegInfo.createVirtualRegister(RC);
3070 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
3071 .addReg(Mips::ZERO).addImm(1);
3072
3073 // Insert phi function to $Sink.
3074 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003075 MI.getOperand(0).getReg())
3076 .addReg(VR2)
3077 .addMBB(FBB)
3078 .addReg(VR1)
3079 .addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003080
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003081 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003082 return Sink;
3083}
Daniel Sandersce09d072013-08-28 12:14:50 +00003084
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003085MachineBasicBlock *MipsSETargetLowering::emitMSACBranchPseudo(
3086 MachineInstr &MI, MachineBasicBlock *BB, unsigned BranchOp) const {
Daniel Sandersce09d072013-08-28 12:14:50 +00003087 // $bb:
3088 // vany_nonzero $rd, $ws
3089 // =>
3090 // $bb:
3091 // bnz.b $ws, $tbb
3092 // b $fbb
3093 // $fbb:
3094 // li $rd1, 0
3095 // b $sink
3096 // $tbb:
3097 // li $rd2, 1
3098 // $sink:
3099 // $rd = phi($rd1, $fbb, $rd2, $tbb)
3100
3101 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003102 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersce09d072013-08-28 12:14:50 +00003103 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003104 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersce09d072013-08-28 12:14:50 +00003105 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003106 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00003107 MachineFunction *F = BB->getParent();
3108 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3109 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3110 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3111 F->insert(It, FBB);
3112 F->insert(It, TBB);
3113 F->insert(It, Sink);
3114
3115 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003116 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00003117 BB->end());
3118 Sink->transferSuccessorsAndUpdatePHIs(BB);
3119
3120 // Add successors.
3121 BB->addSuccessor(FBB);
3122 BB->addSuccessor(TBB);
3123 FBB->addSuccessor(Sink);
3124 TBB->addSuccessor(Sink);
3125
3126 // Insert the real bnz.b instruction to $BB.
3127 BuildMI(BB, DL, TII->get(BranchOp))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003128 .addReg(MI.getOperand(1).getReg())
3129 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003130
3131 // Fill $FBB.
3132 unsigned RD1 = RegInfo.createVirtualRegister(RC);
3133 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
3134 .addReg(Mips::ZERO).addImm(0);
3135 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3136
3137 // Fill $TBB.
3138 unsigned RD2 = RegInfo.createVirtualRegister(RC);
3139 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
3140 .addReg(Mips::ZERO).addImm(1);
3141
3142 // Insert phi function to $Sink.
3143 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003144 MI.getOperand(0).getReg())
3145 .addReg(RD1)
3146 .addMBB(FBB)
3147 .addReg(RD2)
3148 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003149
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003150 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersce09d072013-08-28 12:14:50 +00003151 return Sink;
3152}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003153
3154// Emit the COPY_FW pseudo instruction.
3155//
3156// copy_fw_pseudo $fd, $ws, n
3157// =>
3158// copy_u_w $rt, $ws, $n
3159// mtc1 $rt, $fd
3160//
3161// When n is zero, the equivalent operation can be performed with (potentially)
3162// zero instructions due to register overlaps. This optimization is never valid
3163// 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 +00003164MachineBasicBlock *
3165MipsSETargetLowering::emitCOPY_FW(MachineInstr &MI,
3166 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003167 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003168 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003169 DebugLoc DL = MI.getDebugLoc();
3170 unsigned Fd = MI.getOperand(0).getReg();
3171 unsigned Ws = MI.getOperand(1).getReg();
3172 unsigned Lane = MI.getOperand(2).getImm();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003173
Daniel Sandersafe27c72015-02-23 17:22:16 +00003174 if (Lane == 0) {
3175 unsigned Wt = Ws;
3176 if (!Subtarget.useOddSPReg()) {
3177 // We must copy to an even-numbered MSA register so that the
3178 // single-precision sub-register is also guaranteed to be even-numbered.
3179 Wt = RegInfo.createVirtualRegister(&Mips::MSA128WEvensRegClass);
3180
3181 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Wt).addReg(Ws);
3182 }
3183
3184 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3185 } else {
3186 unsigned Wt = RegInfo.createVirtualRegister(
3187 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3188 &Mips::MSA128WEvensRegClass);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003189
Daniel Sandersd9207702014-03-04 13:54:30 +00003190 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003191 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3192 }
3193
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003194 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003195 return BB;
3196}
3197
3198// Emit the COPY_FD pseudo instruction.
3199//
3200// copy_fd_pseudo $fd, $ws, n
3201// =>
3202// splati.d $wt, $ws, $n
3203// copy $fd, $wt:sub_64
3204//
3205// When n is zero, the equivalent operation can be performed with (potentially)
3206// zero instructions due to register overlaps. This optimization is always
3207// valid because FR=1 mode which is the only supported mode in MSA.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003208MachineBasicBlock *
3209MipsSETargetLowering::emitCOPY_FD(MachineInstr &MI,
3210 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003211 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003212
Eric Christopher96e72c62015-01-29 23:27:36 +00003213 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003214 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003215 unsigned Fd = MI.getOperand(0).getReg();
3216 unsigned Ws = MI.getOperand(1).getReg();
3217 unsigned Lane = MI.getOperand(2).getImm() * 2;
3218 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003219
3220 if (Lane == 0)
3221 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
3222 else {
3223 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3224
3225 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
3226 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
3227 }
3228
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003229 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003230 return BB;
3231}
Daniel Sandersa5150702013-09-27 12:31:32 +00003232
3233// Emit the INSERT_FW pseudo instruction.
3234//
3235// insert_fw_pseudo $wd, $wd_in, $n, $fs
3236// =>
3237// subreg_to_reg $wt:sub_lo, $fs
3238// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003239MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003240MipsSETargetLowering::emitINSERT_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003241 MachineBasicBlock *BB) const {
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 Sandersafe27c72015-02-23 17:22:16 +00003249 unsigned Wt = RegInfo.createVirtualRegister(
3250 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3251 &Mips::MSA128WEvensRegClass);
Daniel Sandersa5150702013-09-27 12:31:32 +00003252
3253 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003254 .addImm(0)
3255 .addReg(Fs)
3256 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00003257 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003258 .addReg(Wd_in)
3259 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003260 .addReg(Wt)
3261 .addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003262
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003263 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003264 return BB;
3265}
3266
3267// Emit the INSERT_FD pseudo instruction.
3268//
3269// insert_fd_pseudo $wd, $fs, n
3270// =>
3271// subreg_to_reg $wt:sub_64, $fs
3272// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003273MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003274MipsSETargetLowering::emitINSERT_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003275 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003276 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00003277
Eric Christopher96e72c62015-01-29 23:27:36 +00003278 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003279 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003280 DebugLoc DL = MI.getDebugLoc();
3281 unsigned Wd = MI.getOperand(0).getReg();
3282 unsigned Wd_in = MI.getOperand(1).getReg();
3283 unsigned Lane = MI.getOperand(2).getImm();
3284 unsigned Fs = MI.getOperand(3).getReg();
Daniel Sandersa5150702013-09-27 12:31:32 +00003285 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3286
3287 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003288 .addImm(0)
3289 .addReg(Fs)
3290 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00003291 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003292 .addReg(Wd_in)
3293 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003294 .addReg(Wt)
3295 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003296
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003297 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003298 return BB;
3299}
3300
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003301// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3302//
3303// For integer:
3304// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3305// =>
3306// (SLL $lanetmp1, $lane, <log2size)
3307// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3308// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3309// (NEG $lanetmp2, $lanetmp1)
3310// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3311//
3312// For floating point:
3313// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3314// =>
3315// (SUBREG_TO_REG $wt, $fs, <subreg>)
3316// (SLL $lanetmp1, $lane, <log2size)
3317// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3318// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3319// (NEG $lanetmp2, $lanetmp1)
3320// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003321MachineBasicBlock *MipsSETargetLowering::emitINSERT_DF_VIDX(
3322 MachineInstr &MI, MachineBasicBlock *BB, unsigned EltSizeInBytes,
3323 bool IsFP) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003324 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003325 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003326 DebugLoc DL = MI.getDebugLoc();
3327 unsigned Wd = MI.getOperand(0).getReg();
3328 unsigned SrcVecReg = MI.getOperand(1).getReg();
3329 unsigned LaneReg = MI.getOperand(2).getReg();
3330 unsigned SrcValReg = MI.getOperand(3).getReg();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003331
3332 const TargetRegisterClass *VecRC = nullptr;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003333 // FIXME: This should be true for N32 too.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003334 const TargetRegisterClass *GPRRC =
Daniel Sanders4160c802015-05-05 08:48:35 +00003335 Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003336 unsigned SubRegIdx = Subtarget.isABI_N64() ? Mips::sub_32 : 0;
3337 unsigned ShiftOp = Subtarget.isABI_N64() ? Mips::DSLL : Mips::SLL;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003338 unsigned EltLog2Size;
3339 unsigned InsertOp = 0;
3340 unsigned InsveOp = 0;
3341 switch (EltSizeInBytes) {
3342 default:
3343 llvm_unreachable("Unexpected size");
3344 case 1:
3345 EltLog2Size = 0;
3346 InsertOp = Mips::INSERT_B;
3347 InsveOp = Mips::INSVE_B;
3348 VecRC = &Mips::MSA128BRegClass;
3349 break;
3350 case 2:
3351 EltLog2Size = 1;
3352 InsertOp = Mips::INSERT_H;
3353 InsveOp = Mips::INSVE_H;
3354 VecRC = &Mips::MSA128HRegClass;
3355 break;
3356 case 4:
3357 EltLog2Size = 2;
3358 InsertOp = Mips::INSERT_W;
3359 InsveOp = Mips::INSVE_W;
3360 VecRC = &Mips::MSA128WRegClass;
3361 break;
3362 case 8:
3363 EltLog2Size = 3;
3364 InsertOp = Mips::INSERT_D;
3365 InsveOp = Mips::INSVE_D;
3366 VecRC = &Mips::MSA128DRegClass;
3367 break;
3368 }
3369
3370 if (IsFP) {
3371 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3372 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3373 .addImm(0)
3374 .addReg(SrcValReg)
3375 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3376 SrcValReg = Wt;
3377 }
3378
3379 // Convert the lane index into a byte index
3380 if (EltSizeInBytes != 1) {
3381 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003382 BuildMI(*BB, MI, DL, TII->get(ShiftOp), LaneTmp1)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003383 .addReg(LaneReg)
3384 .addImm(EltLog2Size);
3385 LaneReg = LaneTmp1;
3386 }
3387
3388 // Rotate bytes around so that the desired lane is element zero
3389 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3390 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3391 .addReg(SrcVecReg)
3392 .addReg(SrcVecReg)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003393 .addReg(LaneReg, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003394
3395 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3396 if (IsFP) {
3397 // Use insve.df to insert to element zero
3398 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3399 .addReg(WdTmp1)
3400 .addImm(0)
3401 .addReg(SrcValReg)
3402 .addImm(0);
3403 } else {
3404 // Use insert.df to insert to element zero
3405 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3406 .addReg(WdTmp1)
3407 .addReg(SrcValReg)
3408 .addImm(0);
3409 }
3410
3411 // Rotate elements the rest of the way for a full rotation.
3412 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3413 // the lane index to do this.
3414 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sanders4160c802015-05-05 08:48:35 +00003415 BuildMI(*BB, MI, DL, TII->get(Subtarget.isABI_N64() ? Mips::DSUB : Mips::SUB),
3416 LaneTmp2)
3417 .addReg(Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003418 .addReg(LaneReg);
3419 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3420 .addReg(WdTmp2)
3421 .addReg(WdTmp2)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003422 .addReg(LaneTmp2, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003423
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003424 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003425 return BB;
3426}
3427
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003428// Emit the FILL_FW pseudo instruction.
3429//
3430// fill_fw_pseudo $wd, $fs
3431// =>
3432// implicit_def $wt1
3433// insert_subreg $wt2:subreg_lo, $wt1, $fs
3434// splati.w $wd, $wt2[0]
3435MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003436MipsSETargetLowering::emitFILL_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003437 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003438 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003439 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003440 DebugLoc DL = MI.getDebugLoc();
3441 unsigned Wd = MI.getOperand(0).getReg();
3442 unsigned Fs = MI.getOperand(1).getReg();
Simon Dardis0e9e2372017-01-10 15:53:10 +00003443 unsigned Wt1 = RegInfo.createVirtualRegister(
3444 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3445 : &Mips::MSA128WEvensRegClass);
3446 unsigned Wt2 = RegInfo.createVirtualRegister(
3447 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3448 : &Mips::MSA128WEvensRegClass);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003449
3450 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3451 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3452 .addReg(Wt1)
3453 .addReg(Fs)
3454 .addImm(Mips::sub_lo);
3455 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3456
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003457 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003458 return BB;
3459}
3460
3461// Emit the FILL_FD pseudo instruction.
3462//
3463// fill_fd_pseudo $wd, $fs
3464// =>
3465// implicit_def $wt1
3466// insert_subreg $wt2:subreg_64, $wt1, $fs
3467// splati.d $wd, $wt2[0]
3468MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003469MipsSETargetLowering::emitFILL_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003470 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003471 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003472
Eric Christopher96e72c62015-01-29 23:27:36 +00003473 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003474 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003475 DebugLoc DL = MI.getDebugLoc();
3476 unsigned Wd = MI.getOperand(0).getReg();
3477 unsigned Fs = MI.getOperand(1).getReg();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003478 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3479 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3480
3481 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3482 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3483 .addReg(Wt1)
3484 .addReg(Fs)
3485 .addImm(Mips::sub_64);
3486 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003487
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003488 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003489 return BB;
3490}
Daniel Sandersa9521602013-10-23 10:36:52 +00003491
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003492// Emit the ST_F16_PSEDUO instruction to store a f16 value from an MSA
3493// register.
3494//
3495// STF16 MSA128F16:$wd, mem_simm10:$addr
3496// =>
3497// copy_u.h $rtemp,$wd[0]
3498// sh $rtemp, $addr
3499//
3500// Safety: We can't use st.h & co as they would over write the memory after
3501// the destination. It would require half floats be allocated 16 bytes(!) of
3502// space.
3503MachineBasicBlock *
3504MipsSETargetLowering::emitST_F16_PSEUDO(MachineInstr &MI,
3505 MachineBasicBlock *BB) const {
3506
3507 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3508 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3509 DebugLoc DL = MI.getDebugLoc();
3510 unsigned Ws = MI.getOperand(0).getReg();
3511 unsigned Rt = MI.getOperand(1).getReg();
3512 const MachineMemOperand &MMO = **MI.memoperands_begin();
3513 unsigned Imm = MMO.getOffset();
3514
3515 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3516 // spill and reload can expand as a GPR64 operand. Examine the
3517 // operand in detail and default to ABI.
3518 const TargetRegisterClass *RC =
3519 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3520 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3521 : &Mips::GPR64RegClass);
3522 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003523 unsigned Rs = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003524
3525 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_U_H), Rs).addReg(Ws).addImm(0);
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003526 if(!UsingMips32) {
3527 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR64RegClass);
3528 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Tmp)
3529 .addImm(0)
3530 .addReg(Rs)
3531 .addImm(Mips::sub_32);
3532 Rs = Tmp;
3533 }
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003534 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::SH : Mips::SH64))
3535 .addReg(Rs)
3536 .addReg(Rt)
3537 .addImm(Imm)
3538 .addMemOperand(BB->getParent()->getMachineMemOperand(
3539 &MMO, MMO.getOffset(), MMO.getSize()));
3540
3541 MI.eraseFromParent();
3542 return BB;
3543}
3544
3545// Emit the LD_F16_PSEDUO instruction to load a f16 value into an MSA register.
3546//
3547// LD_F16 MSA128F16:$wd, mem_simm10:$addr
3548// =>
3549// lh $rtemp, $addr
3550// fill.h $wd, $rtemp
3551//
3552// Safety: We can't use ld.h & co as they over-read from the source.
3553// Additionally, if the address is not modulo 16, 2 cases can occur:
3554// a) Segmentation fault as the load instruction reads from a memory page
3555// memory it's not supposed to.
3556// b) The load crosses an implementation specific boundary, requiring OS
3557// intervention.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003558MachineBasicBlock *
3559MipsSETargetLowering::emitLD_F16_PSEUDO(MachineInstr &MI,
3560 MachineBasicBlock *BB) const {
3561
3562 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3563 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3564 DebugLoc DL = MI.getDebugLoc();
3565 unsigned Wd = MI.getOperand(0).getReg();
3566
3567 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3568 // spill and reload can expand as a GPR64 operand. Examine the
3569 // operand in detail and default to ABI.
3570 const TargetRegisterClass *RC =
3571 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3572 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3573 : &Mips::GPR64RegClass);
3574
3575 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
3576 unsigned Rt = RegInfo.createVirtualRegister(RC);
3577
3578 MachineInstrBuilder MIB =
3579 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::LH : Mips::LH64), Rt);
3580 for (unsigned i = 1; i < MI.getNumOperands(); i++)
Diana Picus116bbab2017-01-13 09:58:52 +00003581 MIB.add(MI.getOperand(i));
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003582
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003583 if(!UsingMips32) {
3584 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
3585 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Tmp).addReg(Rt, 0, Mips::sub_32);
3586 Rt = Tmp;
3587 }
3588
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003589 BuildMI(*BB, MI, DL, TII->get(Mips::FILL_H), Wd).addReg(Rt);
3590
3591 MI.eraseFromParent();
3592 return BB;
3593}
3594
3595// Emit the FPROUND_PSEUDO instruction.
3596//
3597// Round an FGR64Opnd, FGR32Opnd to an f16.
3598//
3599// Safety: Cycle the operand through the GPRs so the result always ends up
3600// the correct MSA register.
3601//
3602// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fs
3603// / FGR64Opnd:$Fs and MSA128F16:$Wd to the same physical register
3604// (which they can be, as the MSA registers are defined to alias the
3605// FPU's 64 bit and 32 bit registers) the result can be accessed using
3606// the correct register class. That requires operands be tie-able across
3607// register classes which have a sub/super register class relationship.
3608//
3609// For FPG32Opnd:
3610//
3611// FPROUND MSA128F16:$wd, FGR32Opnd:$fs
3612// =>
3613// mfc1 $rtemp, $fs
3614// fill.w $rtemp, $wtemp
3615// fexdo.w $wd, $wtemp, $wtemp
3616//
3617// For FPG64Opnd on mips32r2+:
3618//
3619// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3620// =>
3621// mfc1 $rtemp, $fs
3622// fill.w $rtemp, $wtemp
3623// mfhc1 $rtemp2, $fs
3624// insert.w $wtemp[1], $rtemp2
3625// insert.w $wtemp[3], $rtemp2
3626// fexdo.w $wtemp2, $wtemp, $wtemp
3627// fexdo.h $wd, $temp2, $temp2
3628//
3629// For FGR64Opnd on mips64r2+:
3630//
3631// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3632// =>
3633// dmfc1 $rtemp, $fs
3634// fill.d $rtemp, $wtemp
3635// fexdo.w $wtemp2, $wtemp, $wtemp
3636// fexdo.h $wd, $wtemp2, $wtemp2
3637//
3638// Safety note: As $wtemp is UNDEF, we may provoke a spurious exception if the
3639// undef bits are "just right" and the exception enable bits are
3640// set. By using fill.w to replicate $fs into all elements over
3641// insert.w for one element, we avoid that potiential case. If
3642// fexdo.[hw] causes an exception in, the exception is valid and it
3643// occurs for all elements.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003644MachineBasicBlock *
3645MipsSETargetLowering::emitFPROUND_PSEUDO(MachineInstr &MI,
3646 MachineBasicBlock *BB,
3647 bool IsFGR64) const {
3648
3649 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3650 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3651 // it.
3652 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3653
3654 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003655 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003656
3657 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3658 DebugLoc DL = MI.getDebugLoc();
3659 unsigned Wd = MI.getOperand(0).getReg();
3660 unsigned Fs = MI.getOperand(1).getReg();
3661
3662 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3663 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3664 const TargetRegisterClass *GPRRC =
3665 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003666 unsigned MFC1Opc = IsFGR64onMips64
3667 ? Mips::DMFC1
3668 : (IsFGR64onMips32 ? Mips::MFC1_D64 : Mips::MFC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003669 unsigned FILLOpc = IsFGR64onMips64 ? Mips::FILL_D : Mips::FILL_W;
3670
3671 // Perform the register class copy as mentioned above.
3672 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3673 BuildMI(*BB, MI, DL, TII->get(MFC1Opc), Rtemp).addReg(Fs);
3674 BuildMI(*BB, MI, DL, TII->get(FILLOpc), Wtemp).addReg(Rtemp);
3675 unsigned WPHI = Wtemp;
3676
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003677 if (IsFGR64onMips32) {
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003678 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3679 BuildMI(*BB, MI, DL, TII->get(Mips::MFHC1_D64), Rtemp2).addReg(Fs);
3680 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3681 unsigned Wtemp3 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3682 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp2)
3683 .addReg(Wtemp)
3684 .addReg(Rtemp2)
3685 .addImm(1);
3686 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp3)
3687 .addReg(Wtemp2)
3688 .addReg(Rtemp2)
3689 .addImm(3);
3690 WPHI = Wtemp3;
3691 }
3692
3693 if (IsFGR64) {
3694 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3695 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_W), Wtemp2)
3696 .addReg(WPHI)
3697 .addReg(WPHI);
3698 WPHI = Wtemp2;
3699 }
3700
3701 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_H), Wd).addReg(WPHI).addReg(WPHI);
3702
3703 MI.eraseFromParent();
3704 return BB;
3705}
3706
3707// Emit the FPEXTEND_PSEUDO instruction.
3708//
3709// Expand an f16 to either a FGR32Opnd or FGR64Opnd.
3710//
3711// Safety: Cycle the result through the GPRs so the result always ends up
3712// the correct floating point register.
3713//
3714// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fd
3715// / FGR64Opnd:$Fd and MSA128F16:$Ws to the same physical register
3716// (which they can be, as the MSA registers are defined to alias the
3717// FPU's 64 bit and 32 bit registers) the result can be accessed using
3718// the correct register class. That requires operands be tie-able across
3719// register classes which have a sub/super register class relationship. I
3720// haven't checked.
3721//
3722// For FGR32Opnd:
3723//
3724// FPEXTEND FGR32Opnd:$fd, MSA128F16:$ws
3725// =>
3726// fexupr.w $wtemp, $ws
3727// copy_s.w $rtemp, $ws[0]
3728// mtc1 $rtemp, $fd
3729//
3730// For FGR64Opnd on Mips64:
3731//
3732// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3733// =>
3734// fexupr.w $wtemp, $ws
3735// fexupr.d $wtemp2, $wtemp
3736// copy_s.d $rtemp, $wtemp2s[0]
3737// dmtc1 $rtemp, $fd
3738//
3739// For FGR64Opnd on Mips32:
3740//
3741// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3742// =>
3743// fexupr.w $wtemp, $ws
3744// fexupr.d $wtemp2, $wtemp
3745// copy_s.w $rtemp, $wtemp2[0]
3746// mtc1 $rtemp, $ftemp
3747// copy_s.w $rtemp2, $wtemp2[1]
3748// $fd = mthc1 $rtemp2, $ftemp
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003749MachineBasicBlock *
3750MipsSETargetLowering::emitFPEXTEND_PSEUDO(MachineInstr &MI,
3751 MachineBasicBlock *BB,
3752 bool IsFGR64) const {
3753
3754 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3755 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3756 // it.
3757 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3758
3759 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
3760 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
3761
3762 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3763 DebugLoc DL = MI.getDebugLoc();
3764 unsigned Fd = MI.getOperand(0).getReg();
3765 unsigned Ws = MI.getOperand(1).getReg();
3766
3767 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3768 const TargetRegisterClass *GPRRC =
3769 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003770 unsigned MTC1Opc = IsFGR64onMips64
3771 ? Mips::DMTC1
3772 : (IsFGR64onMips32 ? Mips::MTC1_D64 : Mips::MTC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003773 unsigned COPYOpc = IsFGR64onMips64 ? Mips::COPY_S_D : Mips::COPY_S_W;
3774
3775 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3776 unsigned WPHI = Wtemp;
3777
3778 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_W), Wtemp).addReg(Ws);
3779 if (IsFGR64) {
3780 WPHI = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3781 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_D), WPHI).addReg(Wtemp);
3782 }
3783
3784 // Perform the safety regclass copy mentioned above.
3785 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3786 unsigned FPRPHI = IsFGR64onMips32
3787 ? RegInfo.createVirtualRegister(&Mips::FGR64RegClass)
3788 : Fd;
3789 BuildMI(*BB, MI, DL, TII->get(COPYOpc), Rtemp).addReg(WPHI).addImm(0);
3790 BuildMI(*BB, MI, DL, TII->get(MTC1Opc), FPRPHI).addReg(Rtemp);
3791
3792 if (IsFGR64onMips32) {
3793 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3794 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_S_W), Rtemp2)
3795 .addReg(WPHI)
3796 .addImm(1);
3797 BuildMI(*BB, MI, DL, TII->get(Mips::MTHC1_D64), Fd)
3798 .addReg(FPRPHI)
3799 .addReg(Rtemp2);
3800 }
3801
3802 MI.eraseFromParent();
3803 return BB;
3804}
3805
Daniel Sandersa9521602013-10-23 10:36:52 +00003806// Emit the FEXP2_W_1 pseudo instructions.
3807//
3808// fexp2_w_1_pseudo $wd, $wt
3809// =>
3810// ldi.w $ws, 1
3811// fexp2.w $wd, $ws, $wt
3812MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003813MipsSETargetLowering::emitFEXP2_W_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003814 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003815 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003816 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3817 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3818 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3819 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003820 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003821
3822 // Splat 1.0 into a vector
3823 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3824 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3825
3826 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003827 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003828 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003829 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003830
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003831 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003832 return BB;
3833}
3834
3835// Emit the FEXP2_D_1 pseudo instructions.
3836//
3837// fexp2_d_1_pseudo $wd, $wt
3838// =>
3839// ldi.d $ws, 1
3840// fexp2.d $wd, $ws, $wt
3841MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003842MipsSETargetLowering::emitFEXP2_D_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003843 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003844 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003845 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3846 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3847 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3848 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003849 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003850
3851 // Splat 1.0 into a vector
3852 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3853 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3854
3855 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003856 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003857 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003858 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003859
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003860 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003861 return BB;
3862}