blob: 5bd234f955babe59c9130e4a956f005db75b7868 [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.
Graham Hunter1a9195d2019-09-17 10:19:23 +000074 for (MVT VT0 : MVT::fixedlen_vector_valuetypes()) {
75 for (MVT VT1 : MVT::fixedlen_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);
Simon Atanasyand811d912019-09-11 11:16:06 +0000330 setOperationAction(ISD::UNDEF, Ty, Legal);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000331
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000332 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000333 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000334 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000335 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000336 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000337 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000338 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000339 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000340 setOperationAction(ISD::SHL, Ty, Legal);
341 setOperationAction(ISD::SRA, Ty, Legal);
342 setOperationAction(ISD::SRL, Ty, Legal);
343 setOperationAction(ISD::SUB, Ty, Legal);
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000344 setOperationAction(ISD::SMAX, Ty, Legal);
345 setOperationAction(ISD::SMIN, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000346 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000347 setOperationAction(ISD::UREM, Ty, Legal);
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000348 setOperationAction(ISD::UMAX, Ty, Legal);
349 setOperationAction(ISD::UMIN, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000350 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000351 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000352 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000353
Daniel Sanders015972b2013-10-11 10:00:06 +0000354 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
355 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
356 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
357 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
358 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
359 }
360
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000361 setOperationAction(ISD::SETCC, Ty, Legal);
362 setCondCodeAction(ISD::SETNE, Ty, Expand);
363 setCondCodeAction(ISD::SETGE, Ty, Expand);
364 setCondCodeAction(ISD::SETGT, Ty, Expand);
365 setCondCodeAction(ISD::SETUGE, Ty, Expand);
366 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000367}
368
Daniel Sanders7a289d02013-09-23 12:02:46 +0000369// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000370void MipsSETargetLowering::
371addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000372 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000373
374 // Expand all builtin opcodes.
375 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
376 setOperationAction(Opc, Ty, Expand);
377
378 setOperationAction(ISD::LOAD, Ty, Legal);
379 setOperationAction(ISD::STORE, Ty, Legal);
380 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000381 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000382 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000383 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000384
385 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000386 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000387 setOperationAction(ISD::FADD, Ty, Legal);
388 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000389 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000390 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000391 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000392 setOperationAction(ISD::FMUL, Ty, Legal);
393 setOperationAction(ISD::FRINT, Ty, Legal);
394 setOperationAction(ISD::FSQRT, Ty, Legal);
395 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000396 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000397
398 setOperationAction(ISD::SETCC, Ty, Legal);
399 setCondCodeAction(ISD::SETOGE, Ty, Expand);
400 setCondCodeAction(ISD::SETOGT, Ty, Expand);
401 setCondCodeAction(ISD::SETUGE, Ty, Expand);
402 setCondCodeAction(ISD::SETUGT, Ty, Expand);
403 setCondCodeAction(ISD::SETGE, Ty, Expand);
404 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000405 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000406}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000407
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000408SDValue MipsSETargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000409 if(!Subtarget.hasMips32r6())
410 return MipsTargetLowering::LowerOperation(Op, DAG);
411
412 EVT ResTy = Op->getValueType(0);
413 SDLoc DL(Op);
414
415 // Although MTC1_D64 takes an i32 and writes an f64, the upper 32 bits of the
416 // floating point register are undefined. Not really an issue as sel.d, which
417 // is produced from an FSELECT node, only looks at bit 0.
418 SDValue Tmp = DAG.getNode(MipsISD::MTC1_D64, DL, MVT::f64, Op->getOperand(0));
419 return DAG.getNode(MipsISD::FSELECT, DL, ResTy, Tmp, Op->getOperand(1),
420 Op->getOperand(2));
421}
422
Simon Pilgrim4e0648a2019-06-12 17:14:03 +0000423bool MipsSETargetLowering::allowsMisalignedMemoryAccesses(
424 EVT VT, unsigned, unsigned, MachineMemOperand::Flags, bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000425 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
426
Eric Christopher1c29a652014-07-18 22:55:25 +0000427 if (Subtarget.systemSupportsUnalignedAccess()) {
Daniel Sandersac272632014-05-23 13:18:02 +0000428 // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
429 // implementation defined whether this is handled by hardware, software, or
430 // a hybrid of the two but it's expected that most implementations will
431 // handle the majority of cases in hardware.
432 if (Fast)
433 *Fast = true;
434 return true;
435 }
436
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000437 switch (SVT) {
438 case MVT::i64:
439 case MVT::i32:
440 if (Fast)
441 *Fast = true;
442 return true;
443 default:
444 return false;
445 }
446}
447
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000448SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
449 SelectionDAG &DAG) const {
450 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000451 case ISD::LOAD: return lowerLOAD(Op, DAG);
452 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000453 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
454 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
455 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
456 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
457 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
458 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000459 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
460 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000461 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
462 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000463 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000464 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000465 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000466 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000467 case ISD::SELECT: return lowerSELECT(Op, DAG);
Simon Atanasyana0291112019-04-25 07:47:28 +0000468 case ISD::BITCAST: return lowerBITCAST(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000469 }
470
471 return MipsTargetLowering::LowerOperation(Op, DAG);
472}
473
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000474// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
475//
476// Performs the following transformations:
477// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
478// sign/zero-extension is completely overwritten by the new one performed by
479// the ISD::AND.
480// - Removes redundant zero extensions performed by an ISD::AND.
481static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
482 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000483 const MipsSubtarget &Subtarget) {
484 if (!Subtarget.hasMSA())
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000485 return SDValue();
486
487 SDValue Op0 = N->getOperand(0);
488 SDValue Op1 = N->getOperand(1);
489 unsigned Op0Opcode = Op0->getOpcode();
490
491 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
492 // where $d + 1 == 2^n and n == 32
493 // or $d + 1 == 2^n and n <= 32 and ZExt
494 // -> (MipsVExtractZExt $a, $b, $c)
495 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
496 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
497 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
498
499 if (!Mask)
500 return SDValue();
501
502 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
503
504 if (Log2IfPositive <= 0)
505 return SDValue(); // Mask+1 is not a power of 2
506
507 SDValue Op0Op2 = Op0->getOperand(2);
508 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
509 unsigned ExtendTySize = ExtendTy.getSizeInBits();
510 unsigned Log2 = Log2IfPositive;
511
512 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
513 Log2 == ExtendTySize) {
514 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000515 return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
516 Op0->getVTList(),
517 makeArrayRef(Ops, Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000518 }
519 }
520
521 return SDValue();
522}
523
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000524// Determine if the specified node is a constant vector splat.
525//
526// Returns true and sets Imm if:
527// * N is a ISD::BUILD_VECTOR representing a constant splat
528//
529// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
530// differences are that it assumes the MSA has already been checked and the
531// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
532// must not be in order for binsri.d to be selectable).
533static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
534 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
535
Craig Topper062a2ba2014-04-25 05:30:21 +0000536 if (!Node)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000537 return false;
538
539 APInt SplatValue, SplatUndef;
540 unsigned SplatBitSize;
541 bool HasAnyUndefs;
542
543 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
544 8, !IsLittleEndian))
545 return false;
546
547 Imm = SplatValue;
548
549 return true;
550}
551
Daniel Sandersab94b532013-10-30 15:20:38 +0000552// Test whether the given node is an all-ones build_vector.
553static bool isVectorAllOnes(SDValue N) {
554 // Look through bitcasts. Endianness doesn't matter because we are looking
555 // for an all-ones value.
556 if (N->getOpcode() == ISD::BITCAST)
557 N = N->getOperand(0);
558
559 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
560
561 if (!BVN)
562 return false;
563
564 APInt SplatValue, SplatUndef;
565 unsigned SplatBitSize;
566 bool HasAnyUndefs;
567
568 // Endianness doesn't matter in this context because we are looking for
569 // an all-ones value.
570 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
571 return SplatValue.isAllOnesValue();
572
573 return false;
574}
575
576// Test whether N is the bitwise inverse of OfNode.
577static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
578 if (N->getOpcode() != ISD::XOR)
579 return false;
580
581 if (isVectorAllOnes(N->getOperand(0)))
582 return N->getOperand(1) == OfNode;
583
584 if (isVectorAllOnes(N->getOperand(1)))
585 return N->getOperand(0) == OfNode;
586
587 return false;
588}
589
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000590// Perform combines where ISD::OR is the root node.
591//
592// Performs the following transformations:
593// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
594// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
595// vector type.
596static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
597 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000598 const MipsSubtarget &Subtarget) {
599 if (!Subtarget.hasMSA())
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000600 return SDValue();
601
602 EVT Ty = N->getValueType(0);
603
604 if (!Ty.is128BitVector())
605 return SDValue();
606
607 SDValue Op0 = N->getOperand(0);
608 SDValue Op1 = N->getOperand(1);
609
610 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
611 SDValue Op0Op0 = Op0->getOperand(0);
612 SDValue Op0Op1 = Op0->getOperand(1);
613 SDValue Op1Op0 = Op1->getOperand(0);
614 SDValue Op1Op1 = Op1->getOperand(1);
Eric Christopher1c29a652014-07-18 22:55:25 +0000615 bool IsLittleEndian = !Subtarget.isLittle();
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000616
617 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000618 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000619 APInt Mask, InvMask;
620
621 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
622 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
623 // looking.
624 // IfClr will be set if we find a valid match.
625 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
626 Cond = Op0Op0;
627 IfSet = Op0Op1;
628
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000629 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
630 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000631 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000632 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
633 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000634 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000635
636 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000637 }
638
639 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
640 // thing again using this mask.
641 // IfClr will be set if we find a valid match.
642 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
643 Cond = Op0Op1;
644 IfSet = Op0Op0;
645
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000646 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
647 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000648 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000649 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
650 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000651 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000652
653 IsConstantMask = true;
654 }
655
656 // If IfClr is not yet set, try looking for a non-constant match.
657 // IfClr will be set if we find a valid match amongst the eight
658 // possibilities.
659 if (!IfClr.getNode()) {
660 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
661 Cond = Op1Op0;
662 IfSet = Op1Op1;
663 IfClr = Op0Op1;
664 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
665 Cond = Op1Op0;
666 IfSet = Op1Op1;
667 IfClr = Op0Op0;
668 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
669 Cond = Op1Op1;
670 IfSet = Op1Op0;
671 IfClr = Op0Op1;
672 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
673 Cond = Op1Op1;
674 IfSet = Op1Op0;
675 IfClr = Op0Op0;
676 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
677 Cond = Op0Op0;
678 IfSet = Op0Op1;
679 IfClr = Op1Op1;
680 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
681 Cond = Op0Op0;
682 IfSet = Op0Op1;
683 IfClr = Op1Op0;
684 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
685 Cond = Op0Op1;
686 IfSet = Op0Op0;
687 IfClr = Op1Op1;
688 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
689 Cond = Op0Op1;
690 IfSet = Op0Op0;
691 IfClr = Op1Op0;
692 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000693 }
694
695 // At this point, IfClr will be set if we have a valid match.
696 if (!IfClr.getNode())
697 return SDValue();
698
699 assert(Cond.getNode() && IfSet.getNode());
700
701 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000702 if (IsConstantMask) {
703 if (Mask.isAllOnesValue())
704 return IfSet;
705 else if (Mask == 0)
706 return IfClr;
707 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000708
709 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000710 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000711 }
712
713 return SDValue();
714}
715
Simon Dardis9ec9f442018-04-13 16:09:07 +0000716static bool shouldTransformMulToShiftsAddsSubs(APInt C, EVT VT,
717 SelectionDAG &DAG,
718 const MipsSubtarget &Subtarget) {
719 // Estimate the number of operations the below transform will turn a
Simon Atanasyanc1b482f2019-05-24 08:39:40 +0000720 // constant multiply into. The number is approximately equal to the minimal
721 // number of powers of two that constant can be broken down to by adding
722 // or subtracting them.
723 //
724 // If we have taken more than 12[1] / 8[2] steps to attempt the
725 // optimization for a native sized value, it is more than likely that this
726 // optimization will make things worse.
727 //
728 // [1] MIPS64 requires 6 instructions at most to materialize any constant,
729 // multiplication requires at least 4 cycles, but another cycle (or two)
730 // to retrieve the result from the HI/LO registers.
731 //
732 // [2] For MIPS32, more than 8 steps is expensive as the constant could be
733 // materialized in 2 instructions, multiplication requires at least 4
734 // cycles, but another cycle (or two) to retrieve the result from the
735 // HI/LO registers.
736 //
737 // TODO:
738 // - MaxSteps needs to consider the `VT` of the constant for the current
739 // target.
740 // - Consider to perform this optimization after type legalization.
741 // That allows to remove a workaround for types not supported natively.
742 // - Take in account `-Os, -Oz` flags because this optimization
743 // increases code size.
744 unsigned MaxSteps = Subtarget.isABI_O32() ? 8 : 12;
Simon Dardis9ec9f442018-04-13 16:09:07 +0000745
746 SmallVector<APInt, 16> WorkStack(1, C);
747 unsigned Steps = 0;
748 unsigned BitWidth = C.getBitWidth();
749
750 while (!WorkStack.empty()) {
751 APInt Val = WorkStack.pop_back_val();
752
753 if (Val == 0 || Val == 1)
754 continue;
755
Simon Atanasyanc1b482f2019-05-24 08:39:40 +0000756 if (Steps >= MaxSteps)
757 return false;
758
Simon Dardis9ec9f442018-04-13 16:09:07 +0000759 if (Val.isPowerOf2()) {
760 ++Steps;
761 continue;
762 }
763
764 APInt Floor = APInt(BitWidth, 1) << Val.logBase2();
765 APInt Ceil = Val.isNegative() ? APInt(BitWidth, 0)
766 : APInt(BitWidth, 1) << C.ceilLogBase2();
Simon Dardis9ec9f442018-04-13 16:09:07 +0000767 if ((Val - Floor).ule(Ceil - Val)) {
768 WorkStack.push_back(Floor);
769 WorkStack.push_back(Val - Floor);
Simon Atanasyanc1b482f2019-05-24 08:39:40 +0000770 } else {
771 WorkStack.push_back(Ceil);
772 WorkStack.push_back(Ceil - Val);
Simon Dardis9ec9f442018-04-13 16:09:07 +0000773 }
774
Simon Dardis9ec9f442018-04-13 16:09:07 +0000775 ++Steps;
Simon Dardis9ec9f442018-04-13 16:09:07 +0000776 }
777
778 // If the value being multiplied is not supported natively, we have to pay
779 // an additional legalization cost, conservatively assume an increase in the
780 // cost of 3 instructions per step. This values for this heuristic were
781 // determined experimentally.
782 unsigned RegisterSize = DAG.getTargetLoweringInfo()
783 .getRegisterType(*DAG.getContext(), VT)
784 .getSizeInBits();
785 Steps *= (VT.getSizeInBits() != RegisterSize) * 3;
786 if (Steps > 27)
787 return false;
788
789 return true;
790}
791
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000792static SDValue genConstMult(SDValue X, APInt C, const SDLoc &DL, EVT VT,
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000793 EVT ShiftTy, SelectionDAG &DAG) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000794 // Return 0.
795 if (C == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000796 return DAG.getConstant(0, DL, VT);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000797
798 // Return x.
799 if (C == 1)
800 return X;
801
802 // If c is power of 2, return (shl x, log2(c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000803 if (C.isPowerOf2())
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000804 return DAG.getNode(ISD::SHL, DL, VT, X,
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000805 DAG.getConstant(C.logBase2(), DL, ShiftTy));
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000806
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000807 unsigned BitWidth = C.getBitWidth();
808 APInt Floor = APInt(BitWidth, 1) << C.logBase2();
809 APInt Ceil = C.isNegative() ? APInt(BitWidth, 0) :
810 APInt(BitWidth, 1) << C.ceilLogBase2();
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000811
812 // If |c - floor_c| <= |c - ceil_c|,
813 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
814 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000815 if ((C - Floor).ule(Ceil - C)) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000816 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
817 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
818 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
819 }
820
821 // If |c - floor_c| > |c - ceil_c|,
822 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
823 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
824 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
825 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
826}
827
828static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
829 const TargetLowering::DAGCombinerInfo &DCI,
Simon Dardis9ec9f442018-04-13 16:09:07 +0000830 const MipsSETargetLowering *TL,
831 const MipsSubtarget &Subtarget) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000832 EVT VT = N->getValueType(0);
833
834 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
Simon Dardis9ec9f442018-04-13 16:09:07 +0000835 if (!VT.isVector() && shouldTransformMulToShiftsAddsSubs(
836 C->getAPIntValue(), VT, DAG, Subtarget))
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000837 return genConstMult(N->getOperand(0), C->getAPIntValue(), SDLoc(N), VT,
Mehdi Aminieaabc512015-07-09 15:12:23 +0000838 TL->getScalarShiftAmountTy(DAG.getDataLayout(), VT),
839 DAG);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000840
841 return SDValue(N, 0);
842}
843
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000844static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
845 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000846 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000847 // See if this is a vector splat immediate node.
848 APInt SplatValue, SplatUndef;
849 unsigned SplatBitSize;
850 bool HasAnyUndefs;
Sanjay Patel1ed771f2016-09-14 16:37:15 +0000851 unsigned EltSize = Ty.getScalarSizeInBits();
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000852 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
853
Eric Christopher1c29a652014-07-18 22:55:25 +0000854 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000855 return SDValue();
856
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000857 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000858 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000859 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000860 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000861 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000862 return SDValue();
863
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000864 SDLoc DL(N);
865 return DAG.getNode(Opc, DL, Ty, N->getOperand(0),
866 DAG.getConstant(SplatValue.getZExtValue(), DL, MVT::i32));
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000867}
868
869static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
870 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000871 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000872 EVT Ty = N->getValueType(0);
873
874 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
875 return SDValue();
876
877 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
878}
879
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000880// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
881// constant splats into MipsISD::SHRA_DSP for DSPr2.
882//
883// Performs the following transformations:
884// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
885// sign/zero-extension is completely overwritten by the new one performed by
886// the ISD::SRA and ISD::SHL nodes.
887// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
888// sequence.
889//
890// See performDSPShiftCombine for more information about the transformation
891// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000892static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
893 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000894 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000895 EVT Ty = N->getValueType(0);
896
Eric Christopher1c29a652014-07-18 22:55:25 +0000897 if (Subtarget.hasMSA()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000898 SDValue Op0 = N->getOperand(0);
899 SDValue Op1 = N->getOperand(1);
900
901 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
902 // where $d + sizeof($c) == 32
903 // or $d + sizeof($c) <= 32 and SExt
904 // -> (MipsVExtractSExt $a, $b, $c)
905 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
906 SDValue Op0Op0 = Op0->getOperand(0);
907 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
908
909 if (!ShAmount)
910 return SDValue();
911
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000912 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
913 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
914 return SDValue();
915
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000916 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
917 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
918
919 if (TotalBits == 32 ||
920 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
921 TotalBits <= 32)) {
922 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
923 Op0Op0->getOperand(2) };
Chandler Carruth356665a2014-08-01 22:09:43 +0000924 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
925 Op0Op0->getVTList(),
926 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000927 }
928 }
929 }
930
Eric Christopher1c29a652014-07-18 22:55:25 +0000931 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000932 return SDValue();
933
934 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
935}
936
937
938static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
939 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000940 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000941 EVT Ty = N->getValueType(0);
942
Eric Christopher1c29a652014-07-18 22:55:25 +0000943 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000944 return SDValue();
945
946 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
947}
948
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000949static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
950 bool IsV216 = (Ty == MVT::v2i16);
951
952 switch (CC) {
953 case ISD::SETEQ:
954 case ISD::SETNE: return true;
955 case ISD::SETLT:
956 case ISD::SETLE:
957 case ISD::SETGT:
958 case ISD::SETGE: return IsV216;
959 case ISD::SETULT:
960 case ISD::SETULE:
961 case ISD::SETUGT:
962 case ISD::SETUGE: return !IsV216;
963 default: return false;
964 }
965}
966
967static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
968 EVT Ty = N->getValueType(0);
969
970 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
971 return SDValue();
972
973 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
974 return SDValue();
975
Andrew Trickef9de2a2013-05-25 02:42:55 +0000976 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000977 N->getOperand(1), N->getOperand(2));
978}
979
980static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
981 EVT Ty = N->getValueType(0);
982
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000983 if (Ty == MVT::v2i16 || Ty == MVT::v4i8) {
Daniel Sanders3ce56622013-09-24 12:18:31 +0000984 SDValue SetCC = N->getOperand(0);
985
986 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
987 return SDValue();
988
989 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
990 SetCC.getOperand(0), SetCC.getOperand(1),
991 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
992 }
993
994 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000995}
996
Daniel Sandersf7456c72013-09-23 13:22:24 +0000997static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000998 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +0000999 EVT Ty = N->getValueType(0);
1000
Eric Christopher1c29a652014-07-18 22:55:25 +00001001 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001002 // Try the following combines:
1003 // (xor (or $a, $b), (build_vector allones))
1004 // (xor (or $a, $b), (bitcast (build_vector allones)))
1005 SDValue Op0 = N->getOperand(0);
1006 SDValue Op1 = N->getOperand(1);
1007 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001008
1009 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
1010 NotOp = Op1;
1011 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
1012 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001013 else
1014 return SDValue();
1015
1016 if (NotOp->getOpcode() == ISD::OR)
1017 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
1018 NotOp->getOperand(1));
1019 }
1020
1021 return SDValue();
1022}
1023
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001024SDValue
1025MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1026 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001027 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001028
1029 switch (N->getOpcode()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001030 case ISD::AND:
1031 Val = performANDCombine(N, DAG, DCI, Subtarget);
1032 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +00001033 case ISD::OR:
1034 Val = performORCombine(N, DAG, DCI, Subtarget);
1035 break;
Akira Hatanaka5832fc62013-06-26 18:48:17 +00001036 case ISD::MUL:
Simon Dardis9ec9f442018-04-13 16:09:07 +00001037 return performMULCombine(N, DAG, DCI, this, Subtarget);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001038 case ISD::SHL:
Petar Jovanovicb71386a2017-03-15 13:10:08 +00001039 Val = performSHLCombine(N, DAG, DCI, Subtarget);
1040 break;
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001041 case ISD::SRA:
1042 return performSRACombine(N, DAG, DCI, Subtarget);
1043 case ISD::SRL:
1044 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001045 case ISD::VSELECT:
1046 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +00001047 case ISD::XOR:
1048 Val = performXORCombine(N, DAG, Subtarget);
1049 break;
1050 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001051 Val = performSETCCCombine(N, DAG);
1052 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001053 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001054
Daniel Sanders62aeab82013-10-30 13:31:27 +00001055 if (Val.getNode()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001056 LLVM_DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1057 N->printrWithDepth(dbgs(), &DAG); dbgs() << "\n=> \n";
1058 Val.getNode()->printrWithDepth(dbgs(), &DAG); dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001059 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001060 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001061
1062 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001063}
1064
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001065MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001066MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001067 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001068 switch (MI.getOpcode()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001069 default:
1070 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1071 case Mips::BPOSGE32_PSEUDO:
1072 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001073 case Mips::SNZ_B_PSEUDO:
1074 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1075 case Mips::SNZ_H_PSEUDO:
1076 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1077 case Mips::SNZ_W_PSEUDO:
1078 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1079 case Mips::SNZ_D_PSEUDO:
1080 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1081 case Mips::SNZ_V_PSEUDO:
1082 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1083 case Mips::SZ_B_PSEUDO:
1084 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1085 case Mips::SZ_H_PSEUDO:
1086 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1087 case Mips::SZ_W_PSEUDO:
1088 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1089 case Mips::SZ_D_PSEUDO:
1090 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1091 case Mips::SZ_V_PSEUDO:
1092 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001093 case Mips::COPY_FW_PSEUDO:
1094 return emitCOPY_FW(MI, BB);
1095 case Mips::COPY_FD_PSEUDO:
1096 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001097 case Mips::INSERT_FW_PSEUDO:
1098 return emitINSERT_FW(MI, BB);
1099 case Mips::INSERT_FD_PSEUDO:
1100 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001101 case Mips::INSERT_B_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001102 case Mips::INSERT_B_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001103 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1104 case Mips::INSERT_H_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001105 case Mips::INSERT_H_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001106 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1107 case Mips::INSERT_W_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001108 case Mips::INSERT_W_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001109 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1110 case Mips::INSERT_D_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001111 case Mips::INSERT_D_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001112 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1113 case Mips::INSERT_FW_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001114 case Mips::INSERT_FW_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001115 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1116 case Mips::INSERT_FD_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001117 case Mips::INSERT_FD_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001118 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001119 case Mips::FILL_FW_PSEUDO:
1120 return emitFILL_FW(MI, BB);
1121 case Mips::FILL_FD_PSEUDO:
1122 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001123 case Mips::FEXP2_W_1_PSEUDO:
1124 return emitFEXP2_W_1(MI, BB);
1125 case Mips::FEXP2_D_1_PSEUDO:
1126 return emitFEXP2_D_1(MI, BB);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00001127 case Mips::ST_F16:
1128 return emitST_F16_PSEUDO(MI, BB);
1129 case Mips::LD_F16:
1130 return emitLD_F16_PSEUDO(MI, BB);
1131 case Mips::MSA_FP_EXTEND_W_PSEUDO:
1132 return emitFPEXTEND_PSEUDO(MI, BB, false);
1133 case Mips::MSA_FP_ROUND_W_PSEUDO:
1134 return emitFPROUND_PSEUDO(MI, BB, false);
1135 case Mips::MSA_FP_EXTEND_D_PSEUDO:
1136 return emitFPEXTEND_PSEUDO(MI, BB, true);
1137 case Mips::MSA_FP_ROUND_D_PSEUDO:
1138 return emitFPROUND_PSEUDO(MI, BB, true);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001139 }
1140}
1141
Daniel Sanders23e98772014-11-02 16:09:29 +00001142bool MipsSETargetLowering::isEligibleForTailCallOptimization(
1143 const CCState &CCInfo, unsigned NextStackOffset,
1144 const MipsFunctionInfo &FI) const {
Simon Dardis57f4ae42016-08-04 09:17:07 +00001145 if (!UseMipsTailCalls)
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001146 return false;
1147
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00001148 // Exception has to be cleared with eret.
1149 if (FI.isISR())
1150 return false;
1151
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001152 // Return false if either the callee or caller has a byval argument.
Daniel Sanders23e98772014-11-02 16:09:29 +00001153 if (CCInfo.getInRegsParamsCount() > 0 || FI.hasByvalArg())
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001154 return false;
1155
1156 // Return true if the callee's argument area is no larger than the
1157 // caller's.
1158 return NextStackOffset <= FI.getIncomingArgSize();
1159}
1160
1161void MipsSETargetLowering::
1162getOpndList(SmallVectorImpl<SDValue> &Ops,
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001163 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001164 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001165 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
1166 SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001167 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001168 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001169 InternalLinkage, IsCallReloc, CLI, Callee,
1170 Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001171}
1172
Akira Hatanaka63791212013-09-07 00:52:30 +00001173SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1174 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1175
1176 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1177 return MipsTargetLowering::lowerLOAD(Op, DAG);
1178
1179 // Replace a double precision load with two i32 loads and a buildpair64.
1180 SDLoc DL(Op);
1181 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1182 EVT PtrVT = Ptr.getValueType();
1183
1184 // i32 load from lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001185 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr, MachinePointerInfo(),
1186 Nd.getAlignment(), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001187
1188 // i32 load from higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001189 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001190 SDValue Hi = DAG.getLoad(
1191 MVT::i32, DL, Lo.getValue(1), Ptr, MachinePointerInfo(),
1192 std::min(Nd.getAlignment(), 4U), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001193
Eric Christopher1c29a652014-07-18 22:55:25 +00001194 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001195 std::swap(Lo, Hi);
1196
1197 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1198 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001199 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001200}
1201
1202SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1203 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1204
1205 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1206 return MipsTargetLowering::lowerSTORE(Op, DAG);
1207
1208 // Replace a double precision store with two extractelement64s and i32 stores.
1209 SDLoc DL(Op);
1210 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1211 EVT PtrVT = Ptr.getValueType();
1212 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001213 Val, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001214 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001215 Val, DAG.getConstant(1, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001216
Eric Christopher1c29a652014-07-18 22:55:25 +00001217 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001218 std::swap(Lo, Hi);
1219
1220 // i32 store to lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001221 Chain =
1222 DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(), Nd.getAlignment(),
1223 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001224
1225 // i32 store to higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001226 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Akira Hatanaka63791212013-09-07 00:52:30 +00001227 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001228 std::min(Nd.getAlignment(), 4U),
1229 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001230}
1231
Simon Atanasyana0291112019-04-25 07:47:28 +00001232SDValue MipsSETargetLowering::lowerBITCAST(SDValue Op,
1233 SelectionDAG &DAG) const {
1234 SDLoc DL(Op);
1235 MVT Src = Op.getOperand(0).getValueType().getSimpleVT();
1236 MVT Dest = Op.getValueType().getSimpleVT();
1237
1238 // Bitcast i64 to double.
1239 if (Src == MVT::i64 && Dest == MVT::f64) {
1240 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
1241 Op.getOperand(0), DAG.getIntPtrConstant(0, DL));
1242 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
1243 Op.getOperand(0), DAG.getIntPtrConstant(1, DL));
1244 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1245 }
1246
1247 // Bitcast double to i64.
1248 if (Src == MVT::f64 && Dest == MVT::i64) {
1249 SDValue Lo =
1250 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1251 DAG.getConstant(0, DL, MVT::i32));
1252 SDValue Hi =
1253 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1254 DAG.getConstant(1, DL, MVT::i32));
1255 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1256 }
1257
1258 // Skip other cases of bitcast and use default lowering.
1259 return SDValue();
1260}
1261
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001262SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1263 bool HasLo, bool HasHi,
1264 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001265 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001266 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001267
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001268 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001269 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001270 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1271 Op.getOperand(0), Op.getOperand(1));
1272 SDValue Lo, Hi;
1273
1274 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001275 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001276 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001277 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001278
1279 if (!HasLo || !HasHi)
1280 return HasLo ? Lo : Hi;
1281
1282 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001283 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001284}
1285
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001286static SDValue initAccumulator(SDValue In, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001287 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001288 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001289 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001290 DAG.getConstant(1, DL, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001291 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001292}
1293
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001294static SDValue extractLOHI(SDValue Op, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001295 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1296 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001297 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1298}
1299
1300// This function expands mips intrinsic nodes which have 64-bit input operands
1301// or output values.
1302//
1303// out64 = intrinsic-node in64
1304// =>
1305// lo = copy (extract-element (in64, 0))
1306// hi = copy (extract-element (in64, 1))
1307// mips-specific-node
1308// v0 = copy lo
1309// v1 = copy hi
1310// out64 = merge-values (v0, v1)
1311//
1312static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001313 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001314 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1315 SmallVector<SDValue, 3> Ops;
1316 unsigned OpNo = 0;
1317
1318 // See if Op has a chain input.
1319 if (HasChainIn)
1320 Ops.push_back(Op->getOperand(OpNo++));
1321
1322 // The next operand is the intrinsic opcode.
1323 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1324
1325 // See if the next operand has type i64.
1326 SDValue Opnd = Op->getOperand(++OpNo), In64;
1327
1328 if (Opnd.getValueType() == MVT::i64)
1329 In64 = initAccumulator(Opnd, DL, DAG);
1330 else
1331 Ops.push_back(Opnd);
1332
1333 // Push the remaining operands.
1334 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1335 Ops.push_back(Op->getOperand(OpNo));
1336
1337 // Add In64 to the end of the list.
1338 if (In64.getNode())
1339 Ops.push_back(In64);
1340
1341 // Scan output.
1342 SmallVector<EVT, 2> ResTys;
1343
1344 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1345 I != E; ++I)
1346 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1347
1348 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001349 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001350 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1351
1352 if (!HasChainIn)
1353 return Out;
1354
1355 assert(Val->getValueType(1) == MVT::Other);
1356 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001357 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001358}
1359
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001360// Lower an MSA copy intrinsic into the specified SelectionDAG node
1361static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1362 SDLoc DL(Op);
1363 SDValue Vec = Op->getOperand(1);
1364 SDValue Idx = Op->getOperand(2);
1365 EVT ResTy = Op->getValueType(0);
1366 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1367
1368 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1369 DAG.getValueType(EltTy));
1370
1371 return Result;
1372}
1373
Daniel Sanders50b80412013-11-15 12:56:49 +00001374static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1375 EVT ResVecTy = Op->getValueType(0);
1376 EVT ViaVecTy = ResVecTy;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001377 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
Daniel Sanders50b80412013-11-15 12:56:49 +00001378 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001379
Daniel Sanders50b80412013-11-15 12:56:49 +00001380 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1381 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1382 // lanes.
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001383 SDValue LaneA = Op->getOperand(OpNr);
1384 SDValue LaneB;
Daniel Sanders50b80412013-11-15 12:56:49 +00001385
1386 if (ResVecTy == MVT::v2i64) {
Stefan Maksimovicc7113cc2018-05-08 15:12:29 +00001387 // In case of the index being passed as an immediate value, set the upper
1388 // lane to 0 so that the splati.d instruction can be matched.
1389 if (isa<ConstantSDNode>(LaneA))
1390 LaneB = DAG.getConstant(0, DL, MVT::i32);
1391 // Having the index passed in a register, set the upper lane to the same
1392 // value as the lower - this results in the BUILD_VECTOR node not being
1393 // expanded through stack. This way we are able to pattern match the set of
1394 // nodes created here to splat.d.
1395 else
1396 LaneB = LaneA;
Daniel Sandersf49dd822013-09-24 13:33:07 +00001397 ViaVecTy = MVT::v4i32;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001398 if(BigEndian)
1399 std::swap(LaneA, LaneB);
Daniel Sanders50b80412013-11-15 12:56:49 +00001400 } else
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001401 LaneB = LaneA;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001402
Daniel Sanders50b80412013-11-15 12:56:49 +00001403 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1404 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001405
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001406 SDValue Result = DAG.getBuildVector(
1407 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001408
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001409 if (ViaVecTy != ResVecTy) {
1410 SDValue One = DAG.getConstant(1, DL, ViaVecTy);
1411 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy,
1412 DAG.getNode(ISD::AND, DL, ViaVecTy, Result, One));
1413 }
Daniel Sandersf49dd822013-09-24 13:33:07 +00001414
1415 return Result;
1416}
1417
Simon Dardis548a53f2017-01-10 16:40:57 +00001418static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG,
1419 bool IsSigned = false) {
Simon Atanasyanf26f56d2019-03-29 15:15:22 +00001420 auto *CImm = cast<ConstantSDNode>(Op->getOperand(ImmOp));
Simon Dardis548a53f2017-01-10 16:40:57 +00001421 return DAG.getConstant(
1422 APInt(Op->getValueType(0).getScalarType().getSizeInBits(),
Simon Atanasyanf26f56d2019-03-29 15:15:22 +00001423 IsSigned ? CImm->getSExtValue() : CImm->getZExtValue(), IsSigned),
Simon Dardis548a53f2017-01-10 16:40:57 +00001424 SDLoc(Op), Op->getValueType(0));
Daniel Sanders50b80412013-11-15 12:56:49 +00001425}
1426
1427static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1428 bool BigEndian, SelectionDAG &DAG) {
1429 EVT ViaVecTy = VecTy;
1430 SDValue SplatValueA = SplatValue;
1431 SDValue SplatValueB = SplatValue;
1432 SDLoc DL(SplatValue);
1433
1434 if (VecTy == MVT::v2i64) {
1435 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1436 ViaVecTy = MVT::v4i32;
1437
1438 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1439 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001440 DAG.getConstant(32, DL, MVT::i32));
Daniel Sanders50b80412013-11-15 12:56:49 +00001441 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1442 }
1443
1444 // We currently hold the parts in little endian order. Swap them if
1445 // necessary.
1446 if (BigEndian)
1447 std::swap(SplatValueA, SplatValueB);
1448
1449 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1450 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1451 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1452 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1453
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001454 SDValue Result = DAG.getBuildVector(
1455 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001456
1457 if (VecTy != ViaVecTy)
1458 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1459
1460 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001461}
1462
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001463static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1464 unsigned Opc, SDValue Imm,
1465 bool BigEndian) {
1466 EVT VecTy = Op->getValueType(0);
1467 SDValue Exp2Imm;
1468 SDLoc DL(Op);
1469
Daniel Sanders50b80412013-11-15 12:56:49 +00001470 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1471 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001472 if (VecTy == MVT::v2i64) {
1473 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1474 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1475
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001476 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), DL,
1477 MVT::i32);
1478 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), DL, MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001479
1480 if (BigEndian)
1481 std::swap(BitImmLoOp, BitImmHiOp);
1482
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001483 Exp2Imm = DAG.getNode(
1484 ISD::BITCAST, DL, MVT::v2i64,
1485 DAG.getBuildVector(MVT::v4i32, DL,
1486 {BitImmLoOp, BitImmHiOp, BitImmLoOp, BitImmHiOp}));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001487 }
1488 }
1489
Craig Topper062a2ba2014-04-25 05:30:21 +00001490 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001491 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001492
1493 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1494 // only values 0-63 are valid.
1495 if (VecTy == MVT::v2i64)
1496 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1497
1498 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1499
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001500 Exp2Imm = DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, DL, VecTy),
1501 Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001502 }
1503
1504 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1505}
1506
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001507static SDValue truncateVecElts(SDValue Op, SelectionDAG &DAG) {
1508 SDLoc DL(Op);
1509 EVT ResTy = Op->getValueType(0);
1510 SDValue Vec = Op->getOperand(2);
1511 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
1512 MVT ResEltTy = ResTy == MVT::v2i64 ? MVT::i64 : MVT::i32;
1513 SDValue ConstValue = DAG.getConstant(Vec.getScalarValueSizeInBits() - 1,
1514 DL, ResEltTy);
1515 SDValue SplatVec = getBuildVectorSplat(ResTy, ConstValue, BigEndian, DAG);
1516
1517 return DAG.getNode(ISD::AND, DL, ResTy, Vec, SplatVec);
1518}
1519
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001520static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1521 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001522 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001523 SDValue One = DAG.getConstant(1, DL, ResTy);
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001524 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, truncateVecElts(Op, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001525
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001526 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1527 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001528}
1529
1530static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1531 SDLoc DL(Op);
1532 EVT ResTy = Op->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001533 APInt BitImm = APInt(ResTy.getScalarSizeInBits(), 1)
Daniel Sanders50b80412013-11-15 12:56:49 +00001534 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001535 SDValue BitMask = DAG.getConstant(~BitImm, DL, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001536
1537 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1538}
1539
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001540SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1541 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001542 SDLoc DL(Op);
Simon Dardis548a53f2017-01-10 16:40:57 +00001543 unsigned Intrinsic = cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue();
1544 switch (Intrinsic) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001545 default:
1546 return SDValue();
1547 case Intrinsic::mips_shilo:
1548 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1549 case Intrinsic::mips_dpau_h_qbl:
1550 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1551 case Intrinsic::mips_dpau_h_qbr:
1552 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1553 case Intrinsic::mips_dpsu_h_qbl:
1554 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1555 case Intrinsic::mips_dpsu_h_qbr:
1556 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1557 case Intrinsic::mips_dpa_w_ph:
1558 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1559 case Intrinsic::mips_dps_w_ph:
1560 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1561 case Intrinsic::mips_dpax_w_ph:
1562 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1563 case Intrinsic::mips_dpsx_w_ph:
1564 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1565 case Intrinsic::mips_mulsa_w_ph:
1566 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1567 case Intrinsic::mips_mult:
1568 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1569 case Intrinsic::mips_multu:
1570 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1571 case Intrinsic::mips_madd:
1572 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1573 case Intrinsic::mips_maddu:
1574 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1575 case Intrinsic::mips_msub:
1576 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1577 case Intrinsic::mips_msubu:
1578 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001579 case Intrinsic::mips_addv_b:
1580 case Intrinsic::mips_addv_h:
1581 case Intrinsic::mips_addv_w:
1582 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001583 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1584 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001585 case Intrinsic::mips_addvi_b:
1586 case Intrinsic::mips_addvi_h:
1587 case Intrinsic::mips_addvi_w:
1588 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001589 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1590 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001591 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001592 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1593 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001594 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001595 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1596 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001597 case Intrinsic::mips_bclr_b:
1598 case Intrinsic::mips_bclr_h:
1599 case Intrinsic::mips_bclr_w:
1600 case Intrinsic::mips_bclr_d:
1601 return lowerMSABitClear(Op, DAG);
1602 case Intrinsic::mips_bclri_b:
1603 case Intrinsic::mips_bclri_h:
1604 case Intrinsic::mips_bclri_w:
1605 case Intrinsic::mips_bclri_d:
1606 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001607 case Intrinsic::mips_binsli_b:
1608 case Intrinsic::mips_binsli_h:
1609 case Intrinsic::mips_binsli_w:
1610 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001611 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001612 EVT VecTy = Op->getValueType(0);
1613 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001614 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1615 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001616 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001617 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001618 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001619 DAG.getConstant(Mask, DL, VecTy, true),
1620 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001621 }
1622 case Intrinsic::mips_binsri_b:
1623 case Intrinsic::mips_binsri_h:
1624 case Intrinsic::mips_binsri_w:
1625 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001626 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001627 EVT VecTy = Op->getValueType(0);
1628 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001629 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1630 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001631 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001632 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001633 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001634 DAG.getConstant(Mask, DL, VecTy, true),
1635 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001636 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001637 case Intrinsic::mips_bmnz_v:
1638 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1639 Op->getOperand(2), Op->getOperand(1));
1640 case Intrinsic::mips_bmnzi_b:
1641 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1642 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1643 Op->getOperand(1));
1644 case Intrinsic::mips_bmz_v:
1645 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1646 Op->getOperand(1), Op->getOperand(2));
1647 case Intrinsic::mips_bmzi_b:
1648 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1649 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1650 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001651 case Intrinsic::mips_bneg_b:
1652 case Intrinsic::mips_bneg_h:
1653 case Intrinsic::mips_bneg_w:
1654 case Intrinsic::mips_bneg_d: {
1655 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001656 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001657
1658 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1659 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001660 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001661 }
1662 case Intrinsic::mips_bnegi_b:
1663 case Intrinsic::mips_bnegi_h:
1664 case Intrinsic::mips_bnegi_w:
1665 case Intrinsic::mips_bnegi_d:
1666 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001667 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001668 case Intrinsic::mips_bnz_b:
1669 case Intrinsic::mips_bnz_h:
1670 case Intrinsic::mips_bnz_w:
1671 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001672 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1673 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001674 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001675 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1676 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001677 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001678 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001679 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001680 Op->getOperand(1), Op->getOperand(3),
1681 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001682 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001683 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001684 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001685 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1686 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001687 case Intrinsic::mips_bset_b:
1688 case Intrinsic::mips_bset_h:
1689 case Intrinsic::mips_bset_w:
1690 case Intrinsic::mips_bset_d: {
1691 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001692 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001693
1694 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1695 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001696 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001697 }
1698 case Intrinsic::mips_bseti_b:
1699 case Intrinsic::mips_bseti_h:
1700 case Intrinsic::mips_bseti_w:
1701 case Intrinsic::mips_bseti_d:
1702 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001703 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001704 case Intrinsic::mips_bz_b:
1705 case Intrinsic::mips_bz_h:
1706 case Intrinsic::mips_bz_w:
1707 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001708 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1709 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001710 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001711 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1712 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001713 case Intrinsic::mips_ceq_b:
1714 case Intrinsic::mips_ceq_h:
1715 case Intrinsic::mips_ceq_w:
1716 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001717 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001718 Op->getOperand(2), ISD::SETEQ);
1719 case Intrinsic::mips_ceqi_b:
1720 case Intrinsic::mips_ceqi_h:
1721 case Intrinsic::mips_ceqi_w:
1722 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001723 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001724 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETEQ);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001725 case Intrinsic::mips_cle_s_b:
1726 case Intrinsic::mips_cle_s_h:
1727 case Intrinsic::mips_cle_s_w:
1728 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001729 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001730 Op->getOperand(2), ISD::SETLE);
1731 case Intrinsic::mips_clei_s_b:
1732 case Intrinsic::mips_clei_s_h:
1733 case Intrinsic::mips_clei_s_w:
1734 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001735 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001736 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLE);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001737 case Intrinsic::mips_cle_u_b:
1738 case Intrinsic::mips_cle_u_h:
1739 case Intrinsic::mips_cle_u_w:
1740 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001741 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001742 Op->getOperand(2), ISD::SETULE);
1743 case Intrinsic::mips_clei_u_b:
1744 case Intrinsic::mips_clei_u_h:
1745 case Intrinsic::mips_clei_u_w:
1746 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001747 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001748 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1749 case Intrinsic::mips_clt_s_b:
1750 case Intrinsic::mips_clt_s_h:
1751 case Intrinsic::mips_clt_s_w:
1752 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001753 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001754 Op->getOperand(2), ISD::SETLT);
1755 case Intrinsic::mips_clti_s_b:
1756 case Intrinsic::mips_clti_s_h:
1757 case Intrinsic::mips_clti_s_w:
1758 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001759 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001760 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLT);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001761 case Intrinsic::mips_clt_u_b:
1762 case Intrinsic::mips_clt_u_h:
1763 case Intrinsic::mips_clt_u_w:
1764 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001765 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001766 Op->getOperand(2), ISD::SETULT);
1767 case Intrinsic::mips_clti_u_b:
1768 case Intrinsic::mips_clti_u_h:
1769 case Intrinsic::mips_clti_u_w:
1770 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001771 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001772 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001773 case Intrinsic::mips_copy_s_b:
1774 case Intrinsic::mips_copy_s_h:
1775 case Intrinsic::mips_copy_s_w:
1776 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001777 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001778 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001779 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1780 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1781 else {
1782 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1783 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1784 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1785 Op->getValueType(0), Op->getOperand(1),
1786 Op->getOperand(2));
1787 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001788 case Intrinsic::mips_copy_u_b:
1789 case Intrinsic::mips_copy_u_h:
1790 case Intrinsic::mips_copy_u_w:
1791 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001792 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001793 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001794 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1795 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1796 else {
1797 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1798 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1799 // Note: When i64 is illegal, this results in copy_s.w instructions
1800 // instead of copy_u.w instructions. This makes no difference to the
1801 // behaviour since i64 is only illegal when the register file is 32-bit.
1802 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1803 Op->getValueType(0), Op->getOperand(1),
1804 Op->getOperand(2));
1805 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001806 case Intrinsic::mips_div_s_b:
1807 case Intrinsic::mips_div_s_h:
1808 case Intrinsic::mips_div_s_w:
1809 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001810 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1811 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001812 case Intrinsic::mips_div_u_b:
1813 case Intrinsic::mips_div_u_h:
1814 case Intrinsic::mips_div_u_w:
1815 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001816 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1817 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001818 case Intrinsic::mips_fadd_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001819 case Intrinsic::mips_fadd_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001820 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001821 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1822 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001823 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1824 case Intrinsic::mips_fceq_w:
1825 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001826 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001827 Op->getOperand(2), ISD::SETOEQ);
1828 case Intrinsic::mips_fcle_w:
1829 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001830 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001831 Op->getOperand(2), ISD::SETOLE);
1832 case Intrinsic::mips_fclt_w:
1833 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001834 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001835 Op->getOperand(2), ISD::SETOLT);
1836 case Intrinsic::mips_fcne_w:
1837 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001838 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001839 Op->getOperand(2), ISD::SETONE);
1840 case Intrinsic::mips_fcor_w:
1841 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001842 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001843 Op->getOperand(2), ISD::SETO);
1844 case Intrinsic::mips_fcueq_w:
1845 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001846 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001847 Op->getOperand(2), ISD::SETUEQ);
1848 case Intrinsic::mips_fcule_w:
1849 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001850 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001851 Op->getOperand(2), ISD::SETULE);
1852 case Intrinsic::mips_fcult_w:
1853 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001854 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001855 Op->getOperand(2), ISD::SETULT);
1856 case Intrinsic::mips_fcun_w:
1857 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001858 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001859 Op->getOperand(2), ISD::SETUO);
1860 case Intrinsic::mips_fcune_w:
1861 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001862 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001863 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001864 case Intrinsic::mips_fdiv_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001865 case Intrinsic::mips_fdiv_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001866 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001867 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1868 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001869 case Intrinsic::mips_ffint_u_w:
1870 case Intrinsic::mips_ffint_u_d:
1871 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1872 Op->getOperand(1));
1873 case Intrinsic::mips_ffint_s_w:
1874 case Intrinsic::mips_ffint_s_d:
1875 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1876 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001877 case Intrinsic::mips_fill_b:
1878 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001879 case Intrinsic::mips_fill_w:
1880 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001881 EVT ResTy = Op->getValueType(0);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001882 SmallVector<SDValue, 16> Ops(ResTy.getVectorNumElements(),
1883 Op->getOperand(1));
Daniel Sandersf49dd822013-09-24 13:33:07 +00001884
Daniel Sandersc72593e2013-09-27 13:20:41 +00001885 // If ResTy is v2i64 then the type legalizer will break this node down into
1886 // an equivalent v4i32.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001887 return DAG.getBuildVector(ResTy, DL, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001888 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001889 case Intrinsic::mips_fexp2_w:
1890 case Intrinsic::mips_fexp2_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001891 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sandersa9521602013-10-23 10:36:52 +00001892 EVT ResTy = Op->getValueType(0);
1893 return DAG.getNode(
1894 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1895 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1896 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001897 case Intrinsic::mips_flog2_w:
1898 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001899 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001900 case Intrinsic::mips_fmadd_w:
1901 case Intrinsic::mips_fmadd_d:
1902 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1903 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001904 case Intrinsic::mips_fmul_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001905 case Intrinsic::mips_fmul_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001906 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001907 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1908 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001909 case Intrinsic::mips_fmsub_w:
1910 case Intrinsic::mips_fmsub_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001911 // TODO: If intrinsics have fast-math-flags, propagate them.
Aleksandar Beserminji3546c162018-04-27 13:30:27 +00001912 return DAG.getNode(MipsISD::FMS, SDLoc(Op), Op->getValueType(0),
1913 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001914 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001915 case Intrinsic::mips_frint_w:
1916 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001917 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001918 case Intrinsic::mips_fsqrt_w:
1919 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001920 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001921 case Intrinsic::mips_fsub_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001922 case Intrinsic::mips_fsub_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001923 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001924 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1925 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001926 case Intrinsic::mips_ftrunc_u_w:
1927 case Intrinsic::mips_ftrunc_u_d:
1928 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1929 Op->getOperand(1));
1930 case Intrinsic::mips_ftrunc_s_w:
1931 case Intrinsic::mips_ftrunc_s_d:
1932 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1933 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001934 case Intrinsic::mips_ilvev_b:
1935 case Intrinsic::mips_ilvev_h:
1936 case Intrinsic::mips_ilvev_w:
1937 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001938 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001939 Op->getOperand(1), Op->getOperand(2));
1940 case Intrinsic::mips_ilvl_b:
1941 case Intrinsic::mips_ilvl_h:
1942 case Intrinsic::mips_ilvl_w:
1943 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001944 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001945 Op->getOperand(1), Op->getOperand(2));
1946 case Intrinsic::mips_ilvod_b:
1947 case Intrinsic::mips_ilvod_h:
1948 case Intrinsic::mips_ilvod_w:
1949 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001950 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001951 Op->getOperand(1), Op->getOperand(2));
1952 case Intrinsic::mips_ilvr_b:
1953 case Intrinsic::mips_ilvr_h:
1954 case Intrinsic::mips_ilvr_w:
1955 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001956 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001957 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001958 case Intrinsic::mips_insert_b:
1959 case Intrinsic::mips_insert_h:
1960 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001961 case Intrinsic::mips_insert_d:
1962 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1963 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001964 case Intrinsic::mips_insve_b:
1965 case Intrinsic::mips_insve_h:
1966 case Intrinsic::mips_insve_w:
Simon Dardis548a53f2017-01-10 16:40:57 +00001967 case Intrinsic::mips_insve_d: {
1968 // Report an error for out of range values.
1969 int64_t Max;
1970 switch (Intrinsic) {
1971 case Intrinsic::mips_insve_b: Max = 15; break;
1972 case Intrinsic::mips_insve_h: Max = 7; break;
1973 case Intrinsic::mips_insve_w: Max = 3; break;
1974 case Intrinsic::mips_insve_d: Max = 1; break;
1975 default: llvm_unreachable("Unmatched intrinsic");
1976 }
1977 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
1978 if (Value < 0 || Value > Max)
1979 report_fatal_error("Immediate out of range");
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001980 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1981 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001982 DAG.getConstant(0, DL, MVT::i32));
Simon Dardis548a53f2017-01-10 16:40:57 +00001983 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00001984 case Intrinsic::mips_ldi_b:
1985 case Intrinsic::mips_ldi_h:
1986 case Intrinsic::mips_ldi_w:
1987 case Intrinsic::mips_ldi_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00001988 return lowerMSASplatImm(Op, 1, DAG, true);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001989 case Intrinsic::mips_lsa:
1990 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001991 EVT ResTy = Op->getValueType(0);
1992 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1993 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1994 Op->getOperand(2), Op->getOperand(3)));
1995 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001996 case Intrinsic::mips_maddv_b:
1997 case Intrinsic::mips_maddv_h:
1998 case Intrinsic::mips_maddv_w:
1999 case Intrinsic::mips_maddv_d: {
2000 EVT ResTy = Op->getValueType(0);
2001 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
2002 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2003 Op->getOperand(2), Op->getOperand(3)));
2004 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00002005 case Intrinsic::mips_max_s_b:
2006 case Intrinsic::mips_max_s_h:
2007 case Intrinsic::mips_max_s_w:
2008 case Intrinsic::mips_max_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002009 return DAG.getNode(ISD::SMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002010 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002011 case Intrinsic::mips_max_u_b:
2012 case Intrinsic::mips_max_u_h:
2013 case Intrinsic::mips_max_u_w:
2014 case Intrinsic::mips_max_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002015 return DAG.getNode(ISD::UMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002016 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002017 case Intrinsic::mips_maxi_s_b:
2018 case Intrinsic::mips_maxi_s_h:
2019 case Intrinsic::mips_maxi_s_w:
2020 case Intrinsic::mips_maxi_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002021 return DAG.getNode(ISD::SMAX, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00002022 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002023 case Intrinsic::mips_maxi_u_b:
2024 case Intrinsic::mips_maxi_u_h:
2025 case Intrinsic::mips_maxi_u_w:
2026 case Intrinsic::mips_maxi_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002027 return DAG.getNode(ISD::UMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002028 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002029 case Intrinsic::mips_min_s_b:
2030 case Intrinsic::mips_min_s_h:
2031 case Intrinsic::mips_min_s_w:
2032 case Intrinsic::mips_min_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002033 return DAG.getNode(ISD::SMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002034 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002035 case Intrinsic::mips_min_u_b:
2036 case Intrinsic::mips_min_u_h:
2037 case Intrinsic::mips_min_u_w:
2038 case Intrinsic::mips_min_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002039 return DAG.getNode(ISD::UMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002040 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002041 case Intrinsic::mips_mini_s_b:
2042 case Intrinsic::mips_mini_s_h:
2043 case Intrinsic::mips_mini_s_w:
2044 case Intrinsic::mips_mini_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002045 return DAG.getNode(ISD::SMIN, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00002046 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002047 case Intrinsic::mips_mini_u_b:
2048 case Intrinsic::mips_mini_u_h:
2049 case Intrinsic::mips_mini_u_w:
2050 case Intrinsic::mips_mini_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002051 return DAG.getNode(ISD::UMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002052 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00002053 case Intrinsic::mips_mod_s_b:
2054 case Intrinsic::mips_mod_s_h:
2055 case Intrinsic::mips_mod_s_w:
2056 case Intrinsic::mips_mod_s_d:
2057 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
2058 Op->getOperand(2));
2059 case Intrinsic::mips_mod_u_b:
2060 case Intrinsic::mips_mod_u_h:
2061 case Intrinsic::mips_mod_u_w:
2062 case Intrinsic::mips_mod_u_d:
2063 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
2064 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002065 case Intrinsic::mips_mulv_b:
2066 case Intrinsic::mips_mulv_h:
2067 case Intrinsic::mips_mulv_w:
2068 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002069 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
2070 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00002071 case Intrinsic::mips_msubv_b:
2072 case Intrinsic::mips_msubv_h:
2073 case Intrinsic::mips_msubv_w:
2074 case Intrinsic::mips_msubv_d: {
2075 EVT ResTy = Op->getValueType(0);
2076 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
2077 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2078 Op->getOperand(2), Op->getOperand(3)));
2079 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002080 case Intrinsic::mips_nlzc_b:
2081 case Intrinsic::mips_nlzc_h:
2082 case Intrinsic::mips_nlzc_w:
2083 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002084 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002085 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002086 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2087 Op->getOperand(1), Op->getOperand(2));
2088 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002089 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002090 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002091 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2092 Op->getOperand(1),
2093 lowerMSASplatImm(Op, 2, DAG));
2094 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002095 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002096 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002097 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2098 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002099 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002100 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2101 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002102 case Intrinsic::mips_pckev_b:
2103 case Intrinsic::mips_pckev_h:
2104 case Intrinsic::mips_pckev_w:
2105 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002106 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002107 Op->getOperand(1), Op->getOperand(2));
2108 case Intrinsic::mips_pckod_b:
2109 case Intrinsic::mips_pckod_h:
2110 case Intrinsic::mips_pckod_w:
2111 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002112 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002113 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002114 case Intrinsic::mips_pcnt_b:
2115 case Intrinsic::mips_pcnt_h:
2116 case Intrinsic::mips_pcnt_w:
2117 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002118 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002119 case Intrinsic::mips_sat_s_b:
2120 case Intrinsic::mips_sat_s_h:
2121 case Intrinsic::mips_sat_s_w:
2122 case Intrinsic::mips_sat_s_d:
2123 case Intrinsic::mips_sat_u_b:
2124 case Intrinsic::mips_sat_u_h:
2125 case Intrinsic::mips_sat_u_w:
2126 case Intrinsic::mips_sat_u_d: {
2127 // Report an error for out of range values.
2128 int64_t Max;
2129 switch (Intrinsic) {
2130 case Intrinsic::mips_sat_s_b:
2131 case Intrinsic::mips_sat_u_b: Max = 7; break;
2132 case Intrinsic::mips_sat_s_h:
2133 case Intrinsic::mips_sat_u_h: Max = 15; break;
2134 case Intrinsic::mips_sat_s_w:
2135 case Intrinsic::mips_sat_u_w: Max = 31; break;
2136 case Intrinsic::mips_sat_s_d:
2137 case Intrinsic::mips_sat_u_d: Max = 63; break;
2138 default: llvm_unreachable("Unmatched intrinsic");
2139 }
2140 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2141 if (Value < 0 || Value > Max)
2142 report_fatal_error("Immediate out of range");
2143 return SDValue();
2144 }
Daniel Sanders26307182013-09-24 14:20:00 +00002145 case Intrinsic::mips_shf_b:
2146 case Intrinsic::mips_shf_h:
Simon Dardis548a53f2017-01-10 16:40:57 +00002147 case Intrinsic::mips_shf_w: {
2148 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2149 if (Value < 0 || Value > 255)
2150 report_fatal_error("Immediate out of range");
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002151 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002152 Op->getOperand(2), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002153 }
2154 case Intrinsic::mips_sldi_b:
2155 case Intrinsic::mips_sldi_h:
2156 case Intrinsic::mips_sldi_w:
2157 case Intrinsic::mips_sldi_d: {
2158 // Report an error for out of range values.
2159 int64_t Max;
2160 switch (Intrinsic) {
2161 case Intrinsic::mips_sldi_b: Max = 15; break;
2162 case Intrinsic::mips_sldi_h: Max = 7; break;
2163 case Intrinsic::mips_sldi_w: Max = 3; break;
2164 case Intrinsic::mips_sldi_d: Max = 1; break;
2165 default: llvm_unreachable("Unmatched intrinsic");
2166 }
2167 int64_t Value = cast<ConstantSDNode>(Op->getOperand(3))->getSExtValue();
2168 if (Value < 0 || Value > Max)
2169 report_fatal_error("Immediate out of range");
2170 return SDValue();
2171 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002172 case Intrinsic::mips_sll_b:
2173 case Intrinsic::mips_sll_h:
2174 case Intrinsic::mips_sll_w:
2175 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002176 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002177 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002178 case Intrinsic::mips_slli_b:
2179 case Intrinsic::mips_slli_h:
2180 case Intrinsic::mips_slli_w:
2181 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002182 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2183 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002184 case Intrinsic::mips_splat_b:
2185 case Intrinsic::mips_splat_h:
2186 case Intrinsic::mips_splat_w:
2187 case Intrinsic::mips_splat_d:
2188 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2189 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2190 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2191 // Instead we lower to MipsISD::VSHF and match from there.
2192 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002193 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002194 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002195 case Intrinsic::mips_splati_b:
2196 case Intrinsic::mips_splati_h:
2197 case Intrinsic::mips_splati_w:
2198 case Intrinsic::mips_splati_d:
2199 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2200 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2201 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002202 case Intrinsic::mips_sra_b:
2203 case Intrinsic::mips_sra_h:
2204 case Intrinsic::mips_sra_w:
2205 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002206 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002207 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002208 case Intrinsic::mips_srai_b:
2209 case Intrinsic::mips_srai_h:
2210 case Intrinsic::mips_srai_w:
2211 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002212 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2213 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002214 case Intrinsic::mips_srari_b:
2215 case Intrinsic::mips_srari_h:
2216 case Intrinsic::mips_srari_w:
2217 case Intrinsic::mips_srari_d: {
2218 // Report an error for out of range values.
2219 int64_t Max;
2220 switch (Intrinsic) {
2221 case Intrinsic::mips_srari_b: Max = 7; break;
2222 case Intrinsic::mips_srari_h: Max = 15; break;
2223 case Intrinsic::mips_srari_w: Max = 31; break;
2224 case Intrinsic::mips_srari_d: Max = 63; break;
2225 default: llvm_unreachable("Unmatched intrinsic");
2226 }
2227 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2228 if (Value < 0 || Value > Max)
2229 report_fatal_error("Immediate out of range");
2230 return SDValue();
2231 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002232 case Intrinsic::mips_srl_b:
2233 case Intrinsic::mips_srl_h:
2234 case Intrinsic::mips_srl_w:
2235 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002236 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002237 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002238 case Intrinsic::mips_srli_b:
2239 case Intrinsic::mips_srli_h:
2240 case Intrinsic::mips_srli_w:
2241 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002242 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2243 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002244 case Intrinsic::mips_srlri_b:
2245 case Intrinsic::mips_srlri_h:
2246 case Intrinsic::mips_srlri_w:
2247 case Intrinsic::mips_srlri_d: {
2248 // Report an error for out of range values.
2249 int64_t Max;
2250 switch (Intrinsic) {
2251 case Intrinsic::mips_srlri_b: Max = 7; break;
2252 case Intrinsic::mips_srlri_h: Max = 15; break;
2253 case Intrinsic::mips_srlri_w: Max = 31; break;
2254 case Intrinsic::mips_srlri_d: Max = 63; break;
2255 default: llvm_unreachable("Unmatched intrinsic");
2256 }
2257 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2258 if (Value < 0 || Value > Max)
2259 report_fatal_error("Immediate out of range");
2260 return SDValue();
2261 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002262 case Intrinsic::mips_subv_b:
2263 case Intrinsic::mips_subv_h:
2264 case Intrinsic::mips_subv_w:
2265 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002266 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2267 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002268 case Intrinsic::mips_subvi_b:
2269 case Intrinsic::mips_subvi_h:
2270 case Intrinsic::mips_subvi_w:
2271 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002272 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2273 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002274 case Intrinsic::mips_vshf_b:
2275 case Intrinsic::mips_vshf_h:
2276 case Intrinsic::mips_vshf_w:
2277 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002278 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002279 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002280 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002281 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2282 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002283 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002284 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2285 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Marcin Koscielnicki7efdca52016-04-27 17:21:49 +00002286 case Intrinsic::thread_pointer: {
2287 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2288 return DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2289 }
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002290 }
2291}
2292
Simon Dardis548a53f2017-01-10 16:40:57 +00002293static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2294 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002295 SDLoc DL(Op);
2296 SDValue ChainIn = Op->getOperand(0);
2297 SDValue Address = Op->getOperand(2);
2298 SDValue Offset = Op->getOperand(3);
2299 EVT ResTy = Op->getValueType(0);
2300 EVT PtrTy = Address->getValueType(0);
2301
Simon Dardis548a53f2017-01-10 16:40:57 +00002302 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2303 // however takes an i32 signed constant offset. The actual type of the
2304 // intrinsic is a scaled signed i10.
2305 if (Subtarget.isABI_N64())
2306 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2307
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002308 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002309 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(),
2310 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002311}
2312
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002313SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2314 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002315 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2316 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002317 default:
2318 return SDValue();
2319 case Intrinsic::mips_extp:
2320 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2321 case Intrinsic::mips_extpdp:
2322 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2323 case Intrinsic::mips_extr_w:
2324 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2325 case Intrinsic::mips_extr_r_w:
2326 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2327 case Intrinsic::mips_extr_rs_w:
2328 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2329 case Intrinsic::mips_extr_s_h:
2330 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2331 case Intrinsic::mips_mthlip:
2332 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2333 case Intrinsic::mips_mulsaq_s_w_ph:
2334 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2335 case Intrinsic::mips_maq_s_w_phl:
2336 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2337 case Intrinsic::mips_maq_s_w_phr:
2338 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2339 case Intrinsic::mips_maq_sa_w_phl:
2340 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2341 case Intrinsic::mips_maq_sa_w_phr:
2342 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2343 case Intrinsic::mips_dpaq_s_w_ph:
2344 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2345 case Intrinsic::mips_dpsq_s_w_ph:
2346 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2347 case Intrinsic::mips_dpaq_sa_l_w:
2348 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2349 case Intrinsic::mips_dpsq_sa_l_w:
2350 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2351 case Intrinsic::mips_dpaqx_s_w_ph:
2352 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2353 case Intrinsic::mips_dpaqx_sa_w_ph:
2354 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2355 case Intrinsic::mips_dpsqx_s_w_ph:
2356 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2357 case Intrinsic::mips_dpsqx_sa_w_ph:
2358 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002359 case Intrinsic::mips_ld_b:
2360 case Intrinsic::mips_ld_h:
2361 case Intrinsic::mips_ld_w:
2362 case Intrinsic::mips_ld_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002363 return lowerMSALoadIntr(Op, DAG, Intr, Subtarget);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002364 }
2365}
2366
Simon Dardis548a53f2017-01-10 16:40:57 +00002367static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2368 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002369 SDLoc DL(Op);
2370 SDValue ChainIn = Op->getOperand(0);
2371 SDValue Value = Op->getOperand(2);
2372 SDValue Address = Op->getOperand(3);
2373 SDValue Offset = Op->getOperand(4);
2374 EVT PtrTy = Address->getValueType(0);
2375
Simon Dardis548a53f2017-01-10 16:40:57 +00002376 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2377 // however takes an i32 signed constant offset. The actual type of the
2378 // intrinsic is a scaled signed i10.
2379 if (Subtarget.isABI_N64())
2380 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2381
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002382 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2383
Justin Lebar9c375812016-07-15 18:27:10 +00002384 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(),
2385 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002386}
2387
2388SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2389 SelectionDAG &DAG) const {
2390 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2391 switch (Intr) {
2392 default:
2393 return SDValue();
2394 case Intrinsic::mips_st_b:
2395 case Intrinsic::mips_st_h:
2396 case Intrinsic::mips_st_w:
2397 case Intrinsic::mips_st_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002398 return lowerMSAStoreIntr(Op, DAG, Intr, Subtarget);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002399 }
2400}
2401
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002402// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2403//
2404// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2405// choose to sign-extend but we could have equally chosen zero-extend. The
2406// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2407// result into this node later (possibly changing it to a zero-extend in the
2408// process).
2409SDValue MipsSETargetLowering::
2410lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2411 SDLoc DL(Op);
2412 EVT ResTy = Op->getValueType(0);
2413 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002414 EVT VecTy = Op0->getValueType(0);
2415
2416 if (!VecTy.is128BitVector())
2417 return SDValue();
2418
2419 if (ResTy.isInteger()) {
2420 SDValue Op1 = Op->getOperand(1);
2421 EVT EltTy = VecTy.getVectorElementType();
2422 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2423 DAG.getValueType(EltTy));
2424 }
2425
2426 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002427}
2428
Daniel Sandersf49dd822013-09-24 13:33:07 +00002429static bool isConstantOrUndef(const SDValue Op) {
Sanjay Patel57195842016-03-14 17:28:46 +00002430 if (Op->isUndef())
Daniel Sandersf49dd822013-09-24 13:33:07 +00002431 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002432 if (isa<ConstantSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002433 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002434 if (isa<ConstantFPSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002435 return true;
2436 return false;
2437}
2438
2439static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2440 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2441 if (isConstantOrUndef(Op->getOperand(i)))
2442 return true;
2443 return false;
2444}
2445
Daniel Sanders7a289d02013-09-23 12:02:46 +00002446// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2447// backend.
2448//
2449// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002450// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2451// 2 less than or equal to 64 and the value fits into a signed 10-bit
2452// immediate
2453// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2454// is a power of 2 less than or equal to 64 and the value does not fit into a
2455// signed 10-bit immediate
2456// - Non-constant splats are legal as-is.
2457// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2458// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002459SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2460 SelectionDAG &DAG) const {
2461 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2462 EVT ResTy = Op->getValueType(0);
2463 SDLoc DL(Op);
2464 APInt SplatValue, SplatUndef;
2465 unsigned SplatBitSize;
2466 bool HasAnyUndefs;
2467
Eric Christopher1c29a652014-07-18 22:55:25 +00002468 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002469 return SDValue();
2470
2471 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2472 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002473 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002474 // We can only cope with 8, 16, 32, or 64-bit elements
2475 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2476 SplatBitSize != 64)
2477 return SDValue();
2478
Simon Dardis7090d142017-03-10 13:27:14 +00002479 // If the value isn't an integer type we will have to bitcast
2480 // from an integer type first. Also, if there are any undefs, we must
2481 // lower them to defined values first.
2482 if (ResTy.isInteger() && !HasAnyUndefs)
Daniel Sandersf49dd822013-09-24 13:33:07 +00002483 return Op;
2484
2485 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002486
2487 switch (SplatBitSize) {
2488 default:
2489 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002490 case 8:
2491 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002492 break;
2493 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002494 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002495 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002496 case 32:
2497 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002498 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002499 case 64:
2500 // There's no fill.d to fall back on for 64-bit values
2501 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002502 }
2503
Daniel Sanders50b80412013-11-15 12:56:49 +00002504 // SelectionDAG::getConstant will promote SplatValue appropriately.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002505 SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002506
Daniel Sanders50b80412013-11-15 12:56:49 +00002507 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002508 if (ViaVecTy != ResTy)
2509 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002510
2511 return Result;
Simon Pilgrimeb508f82018-12-12 18:32:29 +00002512 } else if (DAG.isSplatValue(Op, /* AllowUndefs */ false))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002513 return Op;
2514 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002515 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2516 // The resulting code is the same length as the expansion, but it doesn't
2517 // use memory operations
2518 EVT ResTy = Node->getValueType(0);
2519
2520 assert(ResTy.isVector());
2521
2522 unsigned NumElts = ResTy.getVectorNumElements();
2523 SDValue Vector = DAG.getUNDEF(ResTy);
2524 for (unsigned i = 0; i < NumElts; ++i) {
2525 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2526 Node->getOperand(i),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002527 DAG.getConstant(i, DL, MVT::i32));
Daniel Sandersf86622b2013-09-24 13:16:15 +00002528 }
2529 return Vector;
2530 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002531
2532 return SDValue();
2533}
2534
Daniel Sanders26307182013-09-24 14:20:00 +00002535// Lower VECTOR_SHUFFLE into SHF (if possible).
2536//
2537// SHF splits the vector into blocks of four elements, then shuffles these
2538// elements according to a <4 x i2> constant (encoded as an integer immediate).
2539//
2540// It is therefore possible to lower into SHF when the mask takes the form:
2541// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2542// When undef's appear they are treated as if they were whatever value is
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002543// necessary in order to fit the above forms.
Daniel Sanders26307182013-09-24 14:20:00 +00002544//
2545// For example:
2546// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2547// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2548// i32 7, i32 6, i32 5, i32 4>
2549// is lowered to:
2550// (SHF_H $w0, $w1, 27)
2551// where the 27 comes from:
2552// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2553static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2554 SmallVector<int, 16> Indices,
2555 SelectionDAG &DAG) {
2556 int SHFIndices[4] = { -1, -1, -1, -1 };
2557
2558 if (Indices.size() < 4)
2559 return SDValue();
2560
2561 for (unsigned i = 0; i < 4; ++i) {
2562 for (unsigned j = i; j < Indices.size(); j += 4) {
2563 int Idx = Indices[j];
2564
2565 // Convert from vector index to 4-element subvector index
2566 // If an index refers to an element outside of the subvector then give up
2567 if (Idx != -1) {
2568 Idx -= 4 * (j / 4);
2569 if (Idx < 0 || Idx >= 4)
2570 return SDValue();
2571 }
2572
2573 // If the mask has an undef, replace it with the current index.
2574 // Note that it might still be undef if the current index is also undef
2575 if (SHFIndices[i] == -1)
2576 SHFIndices[i] = Idx;
2577
2578 // Check that non-undef values are the same as in the mask. If they
2579 // aren't then give up
2580 if (!(Idx == -1 || Idx == SHFIndices[i]))
2581 return SDValue();
2582 }
2583 }
2584
2585 // Calculate the immediate. Replace any remaining undefs with zero
2586 APInt Imm(32, 0);
2587 for (int i = 3; i >= 0; --i) {
2588 int Idx = SHFIndices[i];
2589
2590 if (Idx == -1)
2591 Idx = 0;
2592
2593 Imm <<= 2;
2594 Imm |= Idx & 0x3;
2595 }
2596
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002597 SDLoc DL(Op);
2598 return DAG.getNode(MipsISD::SHF, DL, ResTy,
Matt Arsenault3ecab8e2019-09-19 16:26:14 +00002599 DAG.getTargetConstant(Imm, DL, MVT::i32),
2600 Op->getOperand(0));
Daniel Sanders26307182013-09-24 14:20:00 +00002601}
2602
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002603/// Determine whether a range fits a regular pattern of values.
2604/// This function accounts for the possibility of jumping over the End iterator.
2605template <typename ValType>
2606static bool
2607fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,
2608 unsigned CheckStride,
2609 typename SmallVectorImpl<ValType>::const_iterator End,
2610 ValType ExpectedIndex, unsigned ExpectedIndexStride) {
2611 auto &I = Begin;
2612
2613 while (I != End) {
2614 if (*I != -1 && *I != ExpectedIndex)
2615 return false;
2616 ExpectedIndex += ExpectedIndexStride;
2617
2618 // Incrementing past End is undefined behaviour so we must increment one
2619 // step at a time and check for End at each step.
2620 for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
2621 ; // Empty loop body.
2622 }
2623 return true;
2624}
2625
2626// Determine whether VECTOR_SHUFFLE is a SPLATI.
2627//
2628// It is a SPLATI when the mask is:
2629// <x, x, x, ...>
2630// where x is any valid index.
2631//
2632// When undef's appear in the mask they are treated as if they were whatever
2633// value is necessary in order to fit the above form.
2634static bool isVECTOR_SHUFFLE_SPLATI(SDValue Op, EVT ResTy,
2635 SmallVector<int, 16> Indices,
2636 SelectionDAG &DAG) {
2637 assert((Indices.size() % 2) == 0);
2638
2639 int SplatIndex = -1;
2640 for (const auto &V : Indices) {
2641 if (V != -1) {
2642 SplatIndex = V;
2643 break;
2644 }
2645 }
2646
2647 return fitsRegularPattern<int>(Indices.begin(), 1, Indices.end(), SplatIndex,
2648 0);
2649}
2650
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002651// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2652//
2653// ILVEV interleaves the even elements from each vector.
2654//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002655// It is possible to lower into ILVEV when the mask consists of two of the
2656// following forms interleaved:
2657// <0, 2, 4, ...>
2658// <n, n+2, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002659// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002660// For example:
2661// <0, 0, 2, 2, 4, 4, ...>
2662// <0, n, 2, n+2, 4, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002663//
2664// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002665// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002666static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2667 SmallVector<int, 16> Indices,
2668 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002669 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002670
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002671 SDValue Wt;
2672 SDValue Ws;
2673 const auto &Begin = Indices.begin();
2674 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002675
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002676 // Check even elements are taken from the even elements of one half or the
2677 // other and pick an operand accordingly.
2678 if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
2679 Wt = Op->getOperand(0);
2680 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 2))
2681 Wt = Op->getOperand(1);
2682 else
2683 return SDValue();
2684
2685 // Check odd elements are taken from the even elements of one half or the
2686 // other and pick an operand accordingly.
2687 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
2688 Ws = Op->getOperand(0);
2689 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 2))
2690 Ws = Op->getOperand(1);
2691 else
2692 return SDValue();
2693
2694 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002695}
2696
2697// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2698//
2699// ILVOD interleaves the odd elements from each vector.
2700//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002701// It is possible to lower into ILVOD when the mask consists of two of the
2702// following forms interleaved:
2703// <1, 3, 5, ...>
2704// <n+1, n+3, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002705// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002706// For example:
2707// <1, 1, 3, 3, 5, 5, ...>
2708// <1, n+1, 3, n+3, 5, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002709//
2710// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002711// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002712static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2713 SmallVector<int, 16> Indices,
2714 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002715 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002716
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002717 SDValue Wt;
2718 SDValue Ws;
2719 const auto &Begin = Indices.begin();
2720 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002721
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002722 // Check even elements are taken from the odd elements of one half or the
2723 // other and pick an operand accordingly.
2724 if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
2725 Wt = Op->getOperand(0);
2726 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + 1, 2))
2727 Wt = Op->getOperand(1);
2728 else
2729 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002730
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002731 // Check odd elements are taken from the odd elements of one half or the
2732 // other and pick an operand accordingly.
2733 if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
2734 Ws = Op->getOperand(0);
2735 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + 1, 2))
2736 Ws = Op->getOperand(1);
2737 else
2738 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002739
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002740 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Wt, Ws);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002741}
2742
2743// Lower VECTOR_SHUFFLE into ILVR (if possible).
2744//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002745// ILVR interleaves consecutive elements from the right (lowest-indexed) half of
2746// each vector.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002747//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002748// It is possible to lower into ILVR when the mask consists of two of the
2749// following forms interleaved:
2750// <0, 1, 2, ...>
2751// <n, n+1, n+2, ...>
2752// where n is the number of elements in the vector.
2753// For example:
2754// <0, 0, 1, 1, 2, 2, ...>
2755// <0, n, 1, n+1, 2, n+2, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002756//
2757// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002758// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002759static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2760 SmallVector<int, 16> Indices,
2761 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002762 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002763
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002764 SDValue Wt;
2765 SDValue Ws;
2766 const auto &Begin = Indices.begin();
2767 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002768
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002769 // Check even elements are taken from the right (lowest-indexed) elements of
2770 // one half or the other and pick an operand accordingly.
2771 if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
2772 Wt = Op->getOperand(0);
2773 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 1))
2774 Wt = Op->getOperand(1);
2775 else
2776 return SDValue();
2777
2778 // Check odd elements are taken from the right (lowest-indexed) elements of
2779 // one half or the other and pick an operand accordingly.
2780 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
2781 Ws = Op->getOperand(0);
2782 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 1))
2783 Ws = Op->getOperand(1);
2784 else
2785 return SDValue();
2786
2787 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Ws, Wt);
2788}
2789
2790// Lower VECTOR_SHUFFLE into ILVL (if possible).
2791//
2792// ILVL interleaves consecutive elements from the left (highest-indexed) half
2793// of each vector.
2794//
2795// It is possible to lower into ILVL when the mask consists of two of the
2796// following forms interleaved:
2797// <x, x+1, x+2, ...>
2798// <n+x, n+x+1, n+x+2, ...>
2799// where n is the number of elements in the vector and x is half n.
2800// For example:
2801// <x, x, x+1, x+1, x+2, x+2, ...>
2802// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2803//
2804// When undef's appear in the mask they are treated as if they were whatever
2805// value is necessary in order to fit the above forms.
2806static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2807 SmallVector<int, 16> Indices,
2808 SelectionDAG &DAG) {
2809 assert((Indices.size() % 2) == 0);
2810
2811 unsigned HalfSize = Indices.size() / 2;
2812 SDValue Wt;
2813 SDValue Ws;
2814 const auto &Begin = Indices.begin();
2815 const auto &End = Indices.end();
2816
2817 // Check even elements are taken from the left (highest-indexed) elements of
2818 // one half or the other and pick an operand accordingly.
2819 if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
2820 Wt = Op->getOperand(0);
2821 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + HalfSize, 1))
2822 Wt = Op->getOperand(1);
2823 else
2824 return SDValue();
2825
2826 // Check odd elements are taken from the left (highest-indexed) elements of
2827 // one half or the other and pick an operand accordingly.
2828 if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
2829 Ws = Op->getOperand(0);
2830 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + HalfSize,
2831 1))
2832 Ws = Op->getOperand(1);
2833 else
2834 return SDValue();
2835
2836 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002837}
2838
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002839// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2840//
2841// PCKEV copies the even elements of each vector into the result vector.
2842//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002843// It is possible to lower into PCKEV when the mask consists of two of the
2844// following forms concatenated:
2845// <0, 2, 4, ...>
2846// <n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002847// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002848// For example:
2849// <0, 2, 4, ..., 0, 2, 4, ...>
2850// <0, 2, 4, ..., n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002851//
2852// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002853// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002854static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2855 SmallVector<int, 16> Indices,
2856 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002857 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002858
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002859 SDValue Wt;
2860 SDValue Ws;
2861 const auto &Begin = Indices.begin();
2862 const auto &Mid = Indices.begin() + Indices.size() / 2;
2863 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002864
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002865 if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
2866 Wt = Op->getOperand(0);
2867 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size(), 2))
2868 Wt = Op->getOperand(1);
2869 else
2870 return SDValue();
2871
2872 if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
2873 Ws = Op->getOperand(0);
2874 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size(), 2))
2875 Ws = Op->getOperand(1);
2876 else
2877 return SDValue();
2878
2879 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002880}
2881
2882// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2883//
2884// PCKOD copies the odd elements of each vector into the result vector.
2885//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002886// It is possible to lower into PCKOD when the mask consists of two of the
2887// following forms concatenated:
2888// <1, 3, 5, ...>
2889// <n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002890// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002891// For example:
2892// <1, 3, 5, ..., 1, 3, 5, ...>
2893// <1, 3, 5, ..., n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002894//
2895// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002896// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002897static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2898 SmallVector<int, 16> Indices,
2899 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002900 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002901
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002902 SDValue Wt;
2903 SDValue Ws;
2904 const auto &Begin = Indices.begin();
2905 const auto &Mid = Indices.begin() + Indices.size() / 2;
2906 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002907
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002908 if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
2909 Wt = Op->getOperand(0);
2910 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size() + 1, 2))
2911 Wt = Op->getOperand(1);
2912 else
2913 return SDValue();
2914
2915 if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
2916 Ws = Op->getOperand(0);
2917 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size() + 1, 2))
2918 Ws = Op->getOperand(1);
2919 else
2920 return SDValue();
2921
2922 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002923}
2924
Daniel Sanderse5087042013-09-24 14:02:15 +00002925// Lower VECTOR_SHUFFLE into VSHF.
2926//
2927// This mostly consists of converting the shuffle indices in Indices into a
2928// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2929// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2930// if the type is v8i16 and all the indices are less than 8 then the second
2931// operand is unused and can be replaced with anything. We choose to replace it
2932// with the used operand since this reduces the number of instructions overall.
2933static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2934 SmallVector<int, 16> Indices,
2935 SelectionDAG &DAG) {
2936 SmallVector<SDValue, 16> Ops;
2937 SDValue Op0;
2938 SDValue Op1;
2939 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2940 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2941 bool Using1stVec = false;
2942 bool Using2ndVec = false;
2943 SDLoc DL(Op);
2944 int ResTyNumElts = ResTy.getVectorNumElements();
2945
2946 for (int i = 0; i < ResTyNumElts; ++i) {
2947 // Idx == -1 means UNDEF
2948 int Idx = Indices[i];
2949
2950 if (0 <= Idx && Idx < ResTyNumElts)
2951 Using1stVec = true;
2952 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2953 Using2ndVec = true;
2954 }
2955
2956 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2957 ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002958 Ops.push_back(DAG.getTargetConstant(*I, DL, MaskEltTy));
Daniel Sanderse5087042013-09-24 14:02:15 +00002959
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002960 SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002961
2962 if (Using1stVec && Using2ndVec) {
2963 Op0 = Op->getOperand(0);
2964 Op1 = Op->getOperand(1);
2965 } else if (Using1stVec)
2966 Op0 = Op1 = Op->getOperand(0);
2967 else if (Using2ndVec)
2968 Op0 = Op1 = Op->getOperand(1);
2969 else
2970 llvm_unreachable("shuffle vector mask references neither vector operand?");
2971
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002972 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2973 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2974 // VSHF concatenates the vectors in a bitwise fashion:
2975 // <0b00, 0b01> + <0b10, 0b11> ->
2976 // 0b0100 + 0b1110 -> 0b01001110
2977 // <0b10, 0b11, 0b00, 0b01>
2978 // We must therefore swap the operands to get the correct result.
2979 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002980}
2981
2982// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2983// indices in the shuffle.
2984SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2985 SelectionDAG &DAG) const {
2986 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2987 EVT ResTy = Op->getValueType(0);
2988
2989 if (!ResTy.is128BitVector())
2990 return SDValue();
2991
2992 int ResTyNumElts = ResTy.getVectorNumElements();
2993 SmallVector<int, 16> Indices;
2994
2995 for (int i = 0; i < ResTyNumElts; ++i)
2996 Indices.push_back(Node->getMaskElt(i));
2997
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002998 // splati.[bhwd] is preferable to the others but is matched from
2999 // MipsISD::VSHF.
3000 if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
3001 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003002 SDValue Result;
3003 if ((Result = lowerVECTOR_SHUFFLE_ILVEV(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_ILVOD(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_ILVL(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00003008 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003009 if ((Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00003010 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003011 if ((Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00003012 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003013 if ((Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00003014 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003015 if ((Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG)))
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00003016 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00003017 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
3018}
3019
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003020MachineBasicBlock *
3021MipsSETargetLowering::emitBPOSGE32(MachineInstr &MI,
3022 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003023 // $bb:
3024 // bposge32_pseudo $vr0
3025 // =>
3026 // $bb:
3027 // bposge32 $tbb
3028 // $fbb:
3029 // li $vr2, 0
3030 // b $sink
3031 // $tbb:
3032 // li $vr1, 1
3033 // $sink:
3034 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
3035
3036 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003037 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003038 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003039 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003040 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003041 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003042 MachineFunction *F = BB->getParent();
3043 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3044 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3045 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3046 F->insert(It, FBB);
3047 F->insert(It, TBB);
3048 F->insert(It, Sink);
3049
3050 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003051 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003052 BB->end());
3053 Sink->transferSuccessorsAndUpdatePHIs(BB);
3054
3055 // Add successors.
3056 BB->addSuccessor(FBB);
3057 BB->addSuccessor(TBB);
3058 FBB->addSuccessor(Sink);
3059 TBB->addSuccessor(Sink);
3060
3061 // Insert the real bposge32 instruction to $BB.
3062 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +00003063 // Insert the real bposge32c instruction to $BB.
3064 BuildMI(BB, DL, TII->get(Mips::BPOSGE32C_MMR3)).addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003065
3066 // Fill $FBB.
Daniel Sanders0c476112019-08-15 19:22:08 +00003067 Register VR2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003068 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
3069 .addReg(Mips::ZERO).addImm(0);
3070 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3071
3072 // Fill $TBB.
Daniel Sanders0c476112019-08-15 19:22:08 +00003073 Register VR1 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003074 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
3075 .addReg(Mips::ZERO).addImm(1);
3076
3077 // Insert phi function to $Sink.
3078 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003079 MI.getOperand(0).getReg())
3080 .addReg(VR2)
3081 .addMBB(FBB)
3082 .addReg(VR1)
3083 .addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003084
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003085 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003086 return Sink;
3087}
Daniel Sandersce09d072013-08-28 12:14:50 +00003088
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003089MachineBasicBlock *MipsSETargetLowering::emitMSACBranchPseudo(
3090 MachineInstr &MI, MachineBasicBlock *BB, unsigned BranchOp) const {
Daniel Sandersce09d072013-08-28 12:14:50 +00003091 // $bb:
3092 // vany_nonzero $rd, $ws
3093 // =>
3094 // $bb:
3095 // bnz.b $ws, $tbb
3096 // b $fbb
3097 // $fbb:
3098 // li $rd1, 0
3099 // b $sink
3100 // $tbb:
3101 // li $rd2, 1
3102 // $sink:
3103 // $rd = phi($rd1, $fbb, $rd2, $tbb)
3104
3105 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003106 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersce09d072013-08-28 12:14:50 +00003107 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003108 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersce09d072013-08-28 12:14:50 +00003109 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003110 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00003111 MachineFunction *F = BB->getParent();
3112 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3113 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3114 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3115 F->insert(It, FBB);
3116 F->insert(It, TBB);
3117 F->insert(It, Sink);
3118
3119 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003120 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00003121 BB->end());
3122 Sink->transferSuccessorsAndUpdatePHIs(BB);
3123
3124 // Add successors.
3125 BB->addSuccessor(FBB);
3126 BB->addSuccessor(TBB);
3127 FBB->addSuccessor(Sink);
3128 TBB->addSuccessor(Sink);
3129
3130 // Insert the real bnz.b instruction to $BB.
3131 BuildMI(BB, DL, TII->get(BranchOp))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003132 .addReg(MI.getOperand(1).getReg())
3133 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003134
3135 // Fill $FBB.
Daniel Sanders0c476112019-08-15 19:22:08 +00003136 Register RD1 = RegInfo.createVirtualRegister(RC);
Daniel Sandersce09d072013-08-28 12:14:50 +00003137 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
3138 .addReg(Mips::ZERO).addImm(0);
3139 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3140
3141 // Fill $TBB.
Daniel Sanders0c476112019-08-15 19:22:08 +00003142 Register RD2 = RegInfo.createVirtualRegister(RC);
Daniel Sandersce09d072013-08-28 12:14:50 +00003143 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
3144 .addReg(Mips::ZERO).addImm(1);
3145
3146 // Insert phi function to $Sink.
3147 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003148 MI.getOperand(0).getReg())
3149 .addReg(RD1)
3150 .addMBB(FBB)
3151 .addReg(RD2)
3152 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003153
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003154 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersce09d072013-08-28 12:14:50 +00003155 return Sink;
3156}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003157
3158// Emit the COPY_FW pseudo instruction.
3159//
3160// copy_fw_pseudo $fd, $ws, n
3161// =>
3162// copy_u_w $rt, $ws, $n
3163// mtc1 $rt, $fd
3164//
3165// When n is zero, the equivalent operation can be performed with (potentially)
3166// zero instructions due to register overlaps. This optimization is never valid
3167// 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 +00003168MachineBasicBlock *
3169MipsSETargetLowering::emitCOPY_FW(MachineInstr &MI,
3170 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003171 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003172 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003173 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0c476112019-08-15 19:22:08 +00003174 Register Fd = MI.getOperand(0).getReg();
3175 Register Ws = MI.getOperand(1).getReg();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003176 unsigned Lane = MI.getOperand(2).getImm();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003177
Daniel Sandersafe27c72015-02-23 17:22:16 +00003178 if (Lane == 0) {
3179 unsigned Wt = Ws;
3180 if (!Subtarget.useOddSPReg()) {
3181 // We must copy to an even-numbered MSA register so that the
3182 // single-precision sub-register is also guaranteed to be even-numbered.
3183 Wt = RegInfo.createVirtualRegister(&Mips::MSA128WEvensRegClass);
3184
3185 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Wt).addReg(Ws);
3186 }
3187
3188 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3189 } else {
Daniel Sanders0c476112019-08-15 19:22:08 +00003190 Register Wt = RegInfo.createVirtualRegister(
3191 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3192 : &Mips::MSA128WEvensRegClass);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003193
Daniel Sandersd9207702014-03-04 13:54:30 +00003194 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003195 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3196 }
3197
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003198 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003199 return BB;
3200}
3201
3202// Emit the COPY_FD pseudo instruction.
3203//
3204// copy_fd_pseudo $fd, $ws, n
3205// =>
3206// splati.d $wt, $ws, $n
3207// copy $fd, $wt:sub_64
3208//
3209// When n is zero, the equivalent operation can be performed with (potentially)
3210// zero instructions due to register overlaps. This optimization is always
3211// valid because FR=1 mode which is the only supported mode in MSA.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003212MachineBasicBlock *
3213MipsSETargetLowering::emitCOPY_FD(MachineInstr &MI,
3214 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003215 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003216
Eric Christopher96e72c62015-01-29 23:27:36 +00003217 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003218 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Daniel Sanders0c476112019-08-15 19:22:08 +00003219 Register Fd = MI.getOperand(0).getReg();
3220 Register Ws = MI.getOperand(1).getReg();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003221 unsigned Lane = MI.getOperand(2).getImm() * 2;
3222 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003223
3224 if (Lane == 0)
3225 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
3226 else {
Daniel Sanders0c476112019-08-15 19:22:08 +00003227 Register Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003228
3229 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
3230 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
3231 }
3232
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003233 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003234 return BB;
3235}
Daniel Sandersa5150702013-09-27 12:31:32 +00003236
3237// Emit the INSERT_FW pseudo instruction.
3238//
3239// insert_fw_pseudo $wd, $wd_in, $n, $fs
3240// =>
3241// subreg_to_reg $wt:sub_lo, $fs
3242// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003243MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003244MipsSETargetLowering::emitINSERT_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003245 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003246 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003247 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003248 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0c476112019-08-15 19:22:08 +00003249 Register Wd = MI.getOperand(0).getReg();
3250 Register Wd_in = MI.getOperand(1).getReg();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003251 unsigned Lane = MI.getOperand(2).getImm();
Daniel Sanders0c476112019-08-15 19:22:08 +00003252 Register Fs = MI.getOperand(3).getReg();
3253 Register Wt = RegInfo.createVirtualRegister(
3254 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3255 : &Mips::MSA128WEvensRegClass);
Daniel Sandersa5150702013-09-27 12:31:32 +00003256
3257 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003258 .addImm(0)
3259 .addReg(Fs)
3260 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00003261 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003262 .addReg(Wd_in)
3263 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003264 .addReg(Wt)
3265 .addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003266
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003267 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003268 return BB;
3269}
3270
3271// Emit the INSERT_FD pseudo instruction.
3272//
3273// insert_fd_pseudo $wd, $fs, n
3274// =>
3275// subreg_to_reg $wt:sub_64, $fs
3276// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003277MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003278MipsSETargetLowering::emitINSERT_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003279 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003280 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00003281
Eric Christopher96e72c62015-01-29 23:27:36 +00003282 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003283 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003284 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0c476112019-08-15 19:22:08 +00003285 Register Wd = MI.getOperand(0).getReg();
3286 Register Wd_in = MI.getOperand(1).getReg();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003287 unsigned Lane = MI.getOperand(2).getImm();
Daniel Sanders0c476112019-08-15 19:22:08 +00003288 Register Fs = MI.getOperand(3).getReg();
3289 Register Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
Daniel Sandersa5150702013-09-27 12:31:32 +00003290
3291 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003292 .addImm(0)
3293 .addReg(Fs)
3294 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00003295 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003296 .addReg(Wd_in)
3297 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003298 .addReg(Wt)
3299 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003300
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003301 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003302 return BB;
3303}
3304
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003305// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3306//
3307// For integer:
3308// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3309// =>
3310// (SLL $lanetmp1, $lane, <log2size)
3311// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3312// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3313// (NEG $lanetmp2, $lanetmp1)
3314// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3315//
3316// For floating point:
3317// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3318// =>
3319// (SUBREG_TO_REG $wt, $fs, <subreg>)
3320// (SLL $lanetmp1, $lane, <log2size)
3321// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3322// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3323// (NEG $lanetmp2, $lanetmp1)
3324// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003325MachineBasicBlock *MipsSETargetLowering::emitINSERT_DF_VIDX(
3326 MachineInstr &MI, MachineBasicBlock *BB, unsigned EltSizeInBytes,
3327 bool IsFP) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003328 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003329 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003330 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0c476112019-08-15 19:22:08 +00003331 Register Wd = MI.getOperand(0).getReg();
3332 Register SrcVecReg = MI.getOperand(1).getReg();
3333 Register LaneReg = MI.getOperand(2).getReg();
3334 Register SrcValReg = MI.getOperand(3).getReg();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003335
3336 const TargetRegisterClass *VecRC = nullptr;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003337 // FIXME: This should be true for N32 too.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003338 const TargetRegisterClass *GPRRC =
Daniel Sanders4160c802015-05-05 08:48:35 +00003339 Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003340 unsigned SubRegIdx = Subtarget.isABI_N64() ? Mips::sub_32 : 0;
3341 unsigned ShiftOp = Subtarget.isABI_N64() ? Mips::DSLL : Mips::SLL;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003342 unsigned EltLog2Size;
3343 unsigned InsertOp = 0;
3344 unsigned InsveOp = 0;
3345 switch (EltSizeInBytes) {
3346 default:
3347 llvm_unreachable("Unexpected size");
3348 case 1:
3349 EltLog2Size = 0;
3350 InsertOp = Mips::INSERT_B;
3351 InsveOp = Mips::INSVE_B;
3352 VecRC = &Mips::MSA128BRegClass;
3353 break;
3354 case 2:
3355 EltLog2Size = 1;
3356 InsertOp = Mips::INSERT_H;
3357 InsveOp = Mips::INSVE_H;
3358 VecRC = &Mips::MSA128HRegClass;
3359 break;
3360 case 4:
3361 EltLog2Size = 2;
3362 InsertOp = Mips::INSERT_W;
3363 InsveOp = Mips::INSVE_W;
3364 VecRC = &Mips::MSA128WRegClass;
3365 break;
3366 case 8:
3367 EltLog2Size = 3;
3368 InsertOp = Mips::INSERT_D;
3369 InsveOp = Mips::INSVE_D;
3370 VecRC = &Mips::MSA128DRegClass;
3371 break;
3372 }
3373
3374 if (IsFP) {
Daniel Sanders0c476112019-08-15 19:22:08 +00003375 Register Wt = RegInfo.createVirtualRegister(VecRC);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003376 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3377 .addImm(0)
3378 .addReg(SrcValReg)
3379 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3380 SrcValReg = Wt;
3381 }
3382
3383 // Convert the lane index into a byte index
3384 if (EltSizeInBytes != 1) {
Daniel Sanders0c476112019-08-15 19:22:08 +00003385 Register LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003386 BuildMI(*BB, MI, DL, TII->get(ShiftOp), LaneTmp1)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003387 .addReg(LaneReg)
3388 .addImm(EltLog2Size);
3389 LaneReg = LaneTmp1;
3390 }
3391
3392 // Rotate bytes around so that the desired lane is element zero
Daniel Sanders0c476112019-08-15 19:22:08 +00003393 Register WdTmp1 = RegInfo.createVirtualRegister(VecRC);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003394 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3395 .addReg(SrcVecReg)
3396 .addReg(SrcVecReg)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003397 .addReg(LaneReg, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003398
Daniel Sanders0c476112019-08-15 19:22:08 +00003399 Register WdTmp2 = RegInfo.createVirtualRegister(VecRC);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003400 if (IsFP) {
3401 // Use insve.df to insert to element zero
3402 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3403 .addReg(WdTmp1)
3404 .addImm(0)
3405 .addReg(SrcValReg)
3406 .addImm(0);
3407 } else {
3408 // Use insert.df to insert to element zero
3409 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3410 .addReg(WdTmp1)
3411 .addReg(SrcValReg)
3412 .addImm(0);
3413 }
3414
3415 // Rotate elements the rest of the way for a full rotation.
3416 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3417 // the lane index to do this.
Daniel Sanders0c476112019-08-15 19:22:08 +00003418 Register LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sanders4160c802015-05-05 08:48:35 +00003419 BuildMI(*BB, MI, DL, TII->get(Subtarget.isABI_N64() ? Mips::DSUB : Mips::SUB),
3420 LaneTmp2)
3421 .addReg(Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003422 .addReg(LaneReg);
3423 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3424 .addReg(WdTmp2)
3425 .addReg(WdTmp2)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003426 .addReg(LaneTmp2, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003427
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003428 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003429 return BB;
3430}
3431
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003432// Emit the FILL_FW pseudo instruction.
3433//
3434// fill_fw_pseudo $wd, $fs
3435// =>
3436// implicit_def $wt1
3437// insert_subreg $wt2:subreg_lo, $wt1, $fs
3438// splati.w $wd, $wt2[0]
3439MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003440MipsSETargetLowering::emitFILL_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003441 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003442 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003443 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003444 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0c476112019-08-15 19:22:08 +00003445 Register Wd = MI.getOperand(0).getReg();
3446 Register Fs = MI.getOperand(1).getReg();
3447 Register Wt1 = RegInfo.createVirtualRegister(
Simon Dardis0e9e2372017-01-10 15:53:10 +00003448 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3449 : &Mips::MSA128WEvensRegClass);
Daniel Sanders0c476112019-08-15 19:22:08 +00003450 Register Wt2 = RegInfo.createVirtualRegister(
Simon Dardis0e9e2372017-01-10 15:53:10 +00003451 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3452 : &Mips::MSA128WEvensRegClass);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003453
3454 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3455 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3456 .addReg(Wt1)
3457 .addReg(Fs)
3458 .addImm(Mips::sub_lo);
3459 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3460
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003461 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003462 return BB;
3463}
3464
3465// Emit the FILL_FD pseudo instruction.
3466//
3467// fill_fd_pseudo $wd, $fs
3468// =>
3469// implicit_def $wt1
3470// insert_subreg $wt2:subreg_64, $wt1, $fs
3471// splati.d $wd, $wt2[0]
3472MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003473MipsSETargetLowering::emitFILL_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003474 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003475 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003476
Eric Christopher96e72c62015-01-29 23:27:36 +00003477 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003478 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003479 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0c476112019-08-15 19:22:08 +00003480 Register Wd = MI.getOperand(0).getReg();
3481 Register Fs = MI.getOperand(1).getReg();
3482 Register Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3483 Register Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003484
3485 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3486 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3487 .addReg(Wt1)
3488 .addReg(Fs)
3489 .addImm(Mips::sub_64);
3490 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003491
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003492 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003493 return BB;
3494}
Daniel Sandersa9521602013-10-23 10:36:52 +00003495
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003496// Emit the ST_F16_PSEDUO instruction to store a f16 value from an MSA
3497// register.
3498//
3499// STF16 MSA128F16:$wd, mem_simm10:$addr
3500// =>
3501// copy_u.h $rtemp,$wd[0]
3502// sh $rtemp, $addr
3503//
3504// Safety: We can't use st.h & co as they would over write the memory after
3505// the destination. It would require half floats be allocated 16 bytes(!) of
3506// space.
3507MachineBasicBlock *
3508MipsSETargetLowering::emitST_F16_PSEUDO(MachineInstr &MI,
3509 MachineBasicBlock *BB) const {
3510
3511 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3512 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3513 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0c476112019-08-15 19:22:08 +00003514 Register Ws = MI.getOperand(0).getReg();
3515 Register Rt = MI.getOperand(1).getReg();
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003516 const MachineMemOperand &MMO = **MI.memoperands_begin();
3517 unsigned Imm = MMO.getOffset();
3518
3519 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3520 // spill and reload can expand as a GPR64 operand. Examine the
3521 // operand in detail and default to ABI.
3522 const TargetRegisterClass *RC =
3523 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3524 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3525 : &Mips::GPR64RegClass);
3526 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
Daniel Sanders0c476112019-08-15 19:22:08 +00003527 Register Rs = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003528
3529 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_U_H), Rs).addReg(Ws).addImm(0);
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003530 if(!UsingMips32) {
Daniel Sanders0c476112019-08-15 19:22:08 +00003531 Register Tmp = RegInfo.createVirtualRegister(&Mips::GPR64RegClass);
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003532 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Tmp)
3533 .addImm(0)
3534 .addReg(Rs)
3535 .addImm(Mips::sub_32);
3536 Rs = Tmp;
3537 }
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003538 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::SH : Mips::SH64))
3539 .addReg(Rs)
3540 .addReg(Rt)
3541 .addImm(Imm)
3542 .addMemOperand(BB->getParent()->getMachineMemOperand(
3543 &MMO, MMO.getOffset(), MMO.getSize()));
3544
3545 MI.eraseFromParent();
3546 return BB;
3547}
3548
3549// Emit the LD_F16_PSEDUO instruction to load a f16 value into an MSA register.
3550//
3551// LD_F16 MSA128F16:$wd, mem_simm10:$addr
3552// =>
3553// lh $rtemp, $addr
3554// fill.h $wd, $rtemp
3555//
3556// Safety: We can't use ld.h & co as they over-read from the source.
3557// Additionally, if the address is not modulo 16, 2 cases can occur:
3558// a) Segmentation fault as the load instruction reads from a memory page
3559// memory it's not supposed to.
3560// b) The load crosses an implementation specific boundary, requiring OS
3561// intervention.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003562MachineBasicBlock *
3563MipsSETargetLowering::emitLD_F16_PSEUDO(MachineInstr &MI,
3564 MachineBasicBlock *BB) const {
3565
3566 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3567 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3568 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0c476112019-08-15 19:22:08 +00003569 Register Wd = MI.getOperand(0).getReg();
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003570
3571 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3572 // spill and reload can expand as a GPR64 operand. Examine the
3573 // operand in detail and default to ABI.
3574 const TargetRegisterClass *RC =
3575 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3576 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3577 : &Mips::GPR64RegClass);
3578
3579 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
Daniel Sanders0c476112019-08-15 19:22:08 +00003580 Register Rt = RegInfo.createVirtualRegister(RC);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003581
3582 MachineInstrBuilder MIB =
3583 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::LH : Mips::LH64), Rt);
3584 for (unsigned i = 1; i < MI.getNumOperands(); i++)
Diana Picus116bbab2017-01-13 09:58:52 +00003585 MIB.add(MI.getOperand(i));
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003586
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003587 if(!UsingMips32) {
Daniel Sanders0c476112019-08-15 19:22:08 +00003588 Register Tmp = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003589 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Tmp).addReg(Rt, 0, Mips::sub_32);
3590 Rt = Tmp;
3591 }
3592
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003593 BuildMI(*BB, MI, DL, TII->get(Mips::FILL_H), Wd).addReg(Rt);
3594
3595 MI.eraseFromParent();
3596 return BB;
3597}
3598
3599// Emit the FPROUND_PSEUDO instruction.
3600//
3601// Round an FGR64Opnd, FGR32Opnd to an f16.
3602//
3603// Safety: Cycle the operand through the GPRs so the result always ends up
3604// the correct MSA register.
3605//
3606// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fs
3607// / FGR64Opnd:$Fs and MSA128F16:$Wd to the same physical register
3608// (which they can be, as the MSA registers are defined to alias the
3609// FPU's 64 bit and 32 bit registers) the result can be accessed using
3610// the correct register class. That requires operands be tie-able across
3611// register classes which have a sub/super register class relationship.
3612//
3613// For FPG32Opnd:
3614//
3615// FPROUND MSA128F16:$wd, FGR32Opnd:$fs
3616// =>
3617// mfc1 $rtemp, $fs
3618// fill.w $rtemp, $wtemp
3619// fexdo.w $wd, $wtemp, $wtemp
3620//
3621// For FPG64Opnd on mips32r2+:
3622//
3623// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3624// =>
3625// mfc1 $rtemp, $fs
3626// fill.w $rtemp, $wtemp
3627// mfhc1 $rtemp2, $fs
3628// insert.w $wtemp[1], $rtemp2
3629// insert.w $wtemp[3], $rtemp2
3630// fexdo.w $wtemp2, $wtemp, $wtemp
3631// fexdo.h $wd, $temp2, $temp2
3632//
3633// For FGR64Opnd on mips64r2+:
3634//
3635// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3636// =>
3637// dmfc1 $rtemp, $fs
3638// fill.d $rtemp, $wtemp
3639// fexdo.w $wtemp2, $wtemp, $wtemp
3640// fexdo.h $wd, $wtemp2, $wtemp2
3641//
3642// Safety note: As $wtemp is UNDEF, we may provoke a spurious exception if the
3643// undef bits are "just right" and the exception enable bits are
3644// set. By using fill.w to replicate $fs into all elements over
3645// insert.w for one element, we avoid that potiential case. If
3646// fexdo.[hw] causes an exception in, the exception is valid and it
3647// occurs for all elements.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003648MachineBasicBlock *
3649MipsSETargetLowering::emitFPROUND_PSEUDO(MachineInstr &MI,
3650 MachineBasicBlock *BB,
3651 bool IsFGR64) const {
3652
3653 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3654 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3655 // it.
3656 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3657
3658 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003659 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003660
3661 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3662 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0c476112019-08-15 19:22:08 +00003663 Register Wd = MI.getOperand(0).getReg();
3664 Register Fs = MI.getOperand(1).getReg();
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003665
3666 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Daniel Sanders0c476112019-08-15 19:22:08 +00003667 Register Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003668 const TargetRegisterClass *GPRRC =
3669 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003670 unsigned MFC1Opc = IsFGR64onMips64
3671 ? Mips::DMFC1
3672 : (IsFGR64onMips32 ? Mips::MFC1_D64 : Mips::MFC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003673 unsigned FILLOpc = IsFGR64onMips64 ? Mips::FILL_D : Mips::FILL_W;
3674
3675 // Perform the register class copy as mentioned above.
Daniel Sanders0c476112019-08-15 19:22:08 +00003676 Register Rtemp = RegInfo.createVirtualRegister(GPRRC);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003677 BuildMI(*BB, MI, DL, TII->get(MFC1Opc), Rtemp).addReg(Fs);
3678 BuildMI(*BB, MI, DL, TII->get(FILLOpc), Wtemp).addReg(Rtemp);
3679 unsigned WPHI = Wtemp;
3680
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003681 if (IsFGR64onMips32) {
Daniel Sanders0c476112019-08-15 19:22:08 +00003682 Register Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003683 BuildMI(*BB, MI, DL, TII->get(Mips::MFHC1_D64), Rtemp2).addReg(Fs);
Daniel Sanders0c476112019-08-15 19:22:08 +00003684 Register Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3685 Register Wtemp3 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003686 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp2)
3687 .addReg(Wtemp)
3688 .addReg(Rtemp2)
3689 .addImm(1);
3690 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp3)
3691 .addReg(Wtemp2)
3692 .addReg(Rtemp2)
3693 .addImm(3);
3694 WPHI = Wtemp3;
3695 }
3696
3697 if (IsFGR64) {
Daniel Sanders0c476112019-08-15 19:22:08 +00003698 Register Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003699 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_W), Wtemp2)
3700 .addReg(WPHI)
3701 .addReg(WPHI);
3702 WPHI = Wtemp2;
3703 }
3704
3705 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_H), Wd).addReg(WPHI).addReg(WPHI);
3706
3707 MI.eraseFromParent();
3708 return BB;
3709}
3710
3711// Emit the FPEXTEND_PSEUDO instruction.
3712//
3713// Expand an f16 to either a FGR32Opnd or FGR64Opnd.
3714//
3715// Safety: Cycle the result through the GPRs so the result always ends up
3716// the correct floating point register.
3717//
3718// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fd
3719// / FGR64Opnd:$Fd and MSA128F16:$Ws to the same physical register
3720// (which they can be, as the MSA registers are defined to alias the
3721// FPU's 64 bit and 32 bit registers) the result can be accessed using
3722// the correct register class. That requires operands be tie-able across
3723// register classes which have a sub/super register class relationship. I
3724// haven't checked.
3725//
3726// For FGR32Opnd:
3727//
3728// FPEXTEND FGR32Opnd:$fd, MSA128F16:$ws
3729// =>
3730// fexupr.w $wtemp, $ws
3731// copy_s.w $rtemp, $ws[0]
3732// mtc1 $rtemp, $fd
3733//
3734// For FGR64Opnd on Mips64:
3735//
3736// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3737// =>
3738// fexupr.w $wtemp, $ws
3739// fexupr.d $wtemp2, $wtemp
3740// copy_s.d $rtemp, $wtemp2s[0]
3741// dmtc1 $rtemp, $fd
3742//
3743// For FGR64Opnd on Mips32:
3744//
3745// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3746// =>
3747// fexupr.w $wtemp, $ws
3748// fexupr.d $wtemp2, $wtemp
3749// copy_s.w $rtemp, $wtemp2[0]
3750// mtc1 $rtemp, $ftemp
3751// copy_s.w $rtemp2, $wtemp2[1]
3752// $fd = mthc1 $rtemp2, $ftemp
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003753MachineBasicBlock *
3754MipsSETargetLowering::emitFPEXTEND_PSEUDO(MachineInstr &MI,
3755 MachineBasicBlock *BB,
3756 bool IsFGR64) const {
3757
3758 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3759 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3760 // it.
3761 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3762
3763 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
3764 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
3765
3766 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3767 DebugLoc DL = MI.getDebugLoc();
Matt Arsenaulte3a676e2019-06-24 15:50:29 +00003768 Register Fd = MI.getOperand(0).getReg();
3769 Register Ws = MI.getOperand(1).getReg();
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003770
3771 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3772 const TargetRegisterClass *GPRRC =
3773 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003774 unsigned MTC1Opc = IsFGR64onMips64
3775 ? Mips::DMTC1
3776 : (IsFGR64onMips32 ? Mips::MTC1_D64 : Mips::MTC1);
Matt Arsenaulte3a676e2019-06-24 15:50:29 +00003777 Register COPYOpc = IsFGR64onMips64 ? Mips::COPY_S_D : Mips::COPY_S_W;
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003778
Matt Arsenaulte3a676e2019-06-24 15:50:29 +00003779 Register Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3780 Register WPHI = Wtemp;
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003781
3782 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_W), Wtemp).addReg(Ws);
3783 if (IsFGR64) {
3784 WPHI = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3785 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_D), WPHI).addReg(Wtemp);
3786 }
3787
3788 // Perform the safety regclass copy mentioned above.
Matt Arsenaulte3a676e2019-06-24 15:50:29 +00003789 Register Rtemp = RegInfo.createVirtualRegister(GPRRC);
3790 Register FPRPHI = IsFGR64onMips32
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003791 ? RegInfo.createVirtualRegister(&Mips::FGR64RegClass)
3792 : Fd;
3793 BuildMI(*BB, MI, DL, TII->get(COPYOpc), Rtemp).addReg(WPHI).addImm(0);
3794 BuildMI(*BB, MI, DL, TII->get(MTC1Opc), FPRPHI).addReg(Rtemp);
3795
3796 if (IsFGR64onMips32) {
Matt Arsenaulte3a676e2019-06-24 15:50:29 +00003797 Register Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003798 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_S_W), Rtemp2)
3799 .addReg(WPHI)
3800 .addImm(1);
3801 BuildMI(*BB, MI, DL, TII->get(Mips::MTHC1_D64), Fd)
3802 .addReg(FPRPHI)
3803 .addReg(Rtemp2);
3804 }
3805
3806 MI.eraseFromParent();
3807 return BB;
3808}
3809
Daniel Sandersa9521602013-10-23 10:36:52 +00003810// Emit the FEXP2_W_1 pseudo instructions.
3811//
3812// fexp2_w_1_pseudo $wd, $wt
3813// =>
3814// ldi.w $ws, 1
3815// fexp2.w $wd, $ws, $wt
3816MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003817MipsSETargetLowering::emitFEXP2_W_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003818 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003819 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003820 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3821 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
Daniel Sanders0c476112019-08-15 19:22:08 +00003822 Register Ws1 = RegInfo.createVirtualRegister(RC);
3823 Register Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003824 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003825
3826 // Splat 1.0 into a vector
3827 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3828 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3829
3830 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003831 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003832 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003833 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003834
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003835 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003836 return BB;
3837}
3838
3839// Emit the FEXP2_D_1 pseudo instructions.
3840//
3841// fexp2_d_1_pseudo $wd, $wt
3842// =>
3843// ldi.d $ws, 1
3844// fexp2.d $wd, $ws, $wt
3845MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003846MipsSETargetLowering::emitFEXP2_D_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003847 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003848 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003849 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3850 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
Daniel Sanders0c476112019-08-15 19:22:08 +00003851 Register Ws1 = RegInfo.createVirtualRegister(RC);
3852 Register Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003853 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003854
3855 // Splat 1.0 into a vector
3856 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3857 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3858
3859 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003860 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003861 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003862 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003863
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003864 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003865 return BB;
3866}