blob: 2fe2f1694f109084aba9a552b1c7cdf2709f1d78 [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001//===- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface -------------===//
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Akira Hatanaka96ca1822013-03-13 00:54:29 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Subclass of MipsTargetLowering specialized for mips32/64.
10//
11//===----------------------------------------------------------------------===//
Eugene Zelenko79220eae2017-08-03 22:12:30 +000012
Akira Hatanaka96ca1822013-03-13 00:54:29 +000013#include "MipsSEISelLowering.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000014#include "MipsMachineFunction.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000015#include "MipsRegisterInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000016#include "MipsSubtarget.h"
Simon Dardis548a53f2017-01-10 16:40:57 +000017#include "llvm/ADT/APInt.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000018#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/Triple.h"
22#include "llvm/CodeGen/CallingConvLower.h"
23#include "llvm/CodeGen/ISDOpcodes.h"
24#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstr.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000027#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000030#include "llvm/CodeGen/SelectionDAG.h"
31#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000032#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000033#include "llvm/CodeGen/TargetSubtargetInfo.h"
Craig Topper2fa14362018-03-29 17:21:10 +000034#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000035#include "llvm/IR/DebugLoc.h"
Akira Hatanakaa6bbde52013-04-13 02:13:30 +000036#include "llvm/IR/Intrinsics.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000037#include "llvm/Support/Casting.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000038#include "llvm/Support/CommandLine.h"
Daniel Sanders62aeab82013-10-30 13:31:27 +000039#include "llvm/Support/Debug.h"
Simon Dardis548a53f2017-01-10 16:40:57 +000040#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000041#include "llvm/Support/MachineValueType.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000042#include "llvm/Support/MathExtras.h"
Hans Wennborg3e9b1c12013-10-30 16:10:10 +000043#include "llvm/Support/raw_ostream.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000044#include <algorithm>
45#include <cassert>
46#include <cstdint>
47#include <iterator>
48#include <utility>
Akira Hatanaka96ca1822013-03-13 00:54:29 +000049
50using namespace llvm;
51
Chandler Carruth84e68b22014-04-22 02:41:26 +000052#define DEBUG_TYPE "mips-isel"
53
Akira Hatanaka96ca1822013-03-13 00:54:29 +000054static cl::opt<bool>
Simon Dardis57f4ae42016-08-04 09:17:07 +000055UseMipsTailCalls("mips-tail-calls", cl::Hidden,
Simon Dardisd2ed8ab2016-09-27 13:15:54 +000056 cl::desc("MIPS: permit tail calls."), cl::init(false));
Akira Hatanaka96ca1822013-03-13 00:54:29 +000057
Akira Hatanaka63791212013-09-07 00:52:30 +000058static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
59 cl::desc("Expand double precision loads and "
60 "stores to their single precision "
61 "counterparts"));
62
Eric Christopherb1526602014-09-19 23:30:42 +000063MipsSETargetLowering::MipsSETargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +000064 const MipsSubtarget &STI)
65 : MipsTargetLowering(TM, STI) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000066 // Set up the register classes
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000067 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000068
Eric Christopher1c29a652014-07-18 22:55:25 +000069 if (Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000070 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000071
Eric Christopher1c29a652014-07-18 22:55:25 +000072 if (Subtarget.hasDSP() || Subtarget.hasMSA()) {
Daniel Sanders36c671e2013-09-27 09:44:59 +000073 // Expand all truncating stores and extending loads.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000074 for (MVT VT0 : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000075 for (MVT VT1 : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000076 setTruncStoreAction(VT0, VT1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000077 setLoadExtAction(ISD::SEXTLOAD, VT0, VT1, Expand);
78 setLoadExtAction(ISD::ZEXTLOAD, VT0, VT1, Expand);
79 setLoadExtAction(ISD::EXTLOAD, VT0, VT1, Expand);
80 }
Daniel Sanders36c671e2013-09-27 09:44:59 +000081 }
82 }
83
Eric Christopher1c29a652014-07-18 22:55:25 +000084 if (Subtarget.hasDSP()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000085 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
86
87 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
Akira Hatanaka654655f2013-08-14 00:53:38 +000088 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000089
90 // Expand all builtin opcodes.
91 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
92 setOperationAction(Opc, VecTys[i], Expand);
93
Akira Hatanaka2f088222013-04-13 00:55:41 +000094 setOperationAction(ISD::ADD, VecTys[i], Legal);
95 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000096 setOperationAction(ISD::LOAD, VecTys[i], Legal);
97 setOperationAction(ISD::STORE, VecTys[i], Legal);
98 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
99 }
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000100
101 setTargetDAGCombine(ISD::SHL);
102 setTargetDAGCombine(ISD::SRA);
103 setTargetDAGCombine(ISD::SRL);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000104 setTargetDAGCombine(ISD::SETCC);
105 setTargetDAGCombine(ISD::VSELECT);
Amaury Secheta79b6b32018-06-05 22:13:56 +0000106
107 if (Subtarget.hasMips32r2()) {
108 setOperationAction(ISD::ADDC, MVT::i32, Legal);
109 setOperationAction(ISD::ADDE, MVT::i32, Legal);
110 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000111 }
112
Eric Christopher1c29a652014-07-18 22:55:25 +0000113 if (Subtarget.hasDSPR2())
Akira Hatanaka2f088222013-04-13 00:55:41 +0000114 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
115
Eric Christopher1c29a652014-07-18 22:55:25 +0000116 if (Subtarget.hasMSA()) {
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000117 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
118 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
119 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
120 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
121 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
122 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
123 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000124
Simon Dardis0e2ee3b2016-11-18 16:17:44 +0000125 // f16 is a storage-only type, always promote it to f32.
126 addRegisterClass(MVT::f16, &Mips::MSA128HRegClass);
127 setOperationAction(ISD::SETCC, MVT::f16, Promote);
128 setOperationAction(ISD::BR_CC, MVT::f16, Promote);
129 setOperationAction(ISD::SELECT_CC, MVT::f16, Promote);
130 setOperationAction(ISD::SELECT, MVT::f16, Promote);
131 setOperationAction(ISD::FADD, MVT::f16, Promote);
132 setOperationAction(ISD::FSUB, MVT::f16, Promote);
133 setOperationAction(ISD::FMUL, MVT::f16, Promote);
134 setOperationAction(ISD::FDIV, MVT::f16, Promote);
135 setOperationAction(ISD::FREM, MVT::f16, Promote);
136 setOperationAction(ISD::FMA, MVT::f16, Promote);
137 setOperationAction(ISD::FNEG, MVT::f16, Promote);
138 setOperationAction(ISD::FABS, MVT::f16, Promote);
139 setOperationAction(ISD::FCEIL, MVT::f16, Promote);
140 setOperationAction(ISD::FCOPYSIGN, MVT::f16, Promote);
141 setOperationAction(ISD::FCOS, MVT::f16, Promote);
142 setOperationAction(ISD::FP_EXTEND, MVT::f16, Promote);
143 setOperationAction(ISD::FFLOOR, MVT::f16, Promote);
144 setOperationAction(ISD::FNEARBYINT, MVT::f16, Promote);
145 setOperationAction(ISD::FPOW, MVT::f16, Promote);
146 setOperationAction(ISD::FPOWI, MVT::f16, Promote);
147 setOperationAction(ISD::FRINT, MVT::f16, Promote);
148 setOperationAction(ISD::FSIN, MVT::f16, Promote);
149 setOperationAction(ISD::FSINCOS, MVT::f16, Promote);
150 setOperationAction(ISD::FSQRT, MVT::f16, Promote);
151 setOperationAction(ISD::FEXP, MVT::f16, Promote);
152 setOperationAction(ISD::FEXP2, MVT::f16, Promote);
153 setOperationAction(ISD::FLOG, MVT::f16, Promote);
154 setOperationAction(ISD::FLOG2, MVT::f16, Promote);
155 setOperationAction(ISD::FLOG10, MVT::f16, Promote);
156 setOperationAction(ISD::FROUND, MVT::f16, Promote);
157 setOperationAction(ISD::FTRUNC, MVT::f16, Promote);
158 setOperationAction(ISD::FMINNUM, MVT::f16, Promote);
159 setOperationAction(ISD::FMAXNUM, MVT::f16, Promote);
Thomas Lively30f1d692018-10-24 22:49:55 +0000160 setOperationAction(ISD::FMINIMUM, MVT::f16, Promote);
161 setOperationAction(ISD::FMAXIMUM, MVT::f16, Promote);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +0000162
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000163 setTargetDAGCombine(ISD::AND);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000164 setTargetDAGCombine(ISD::OR);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000165 setTargetDAGCombine(ISD::SRA);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000166 setTargetDAGCombine(ISD::VSELECT);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000167 setTargetDAGCombine(ISD::XOR);
Jack Carter3a2c2d42013-08-13 20:54:07 +0000168 }
169
Eric Christophere8ae3e32015-05-07 23:10:21 +0000170 if (!Subtarget.useSoftFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000171 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
172
173 // When dealing with single precision only, use libcalls
Eric Christopher1c29a652014-07-18 22:55:25 +0000174 if (!Subtarget.isSingleFloat()) {
175 if (Subtarget.isFP64bit())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000176 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
177 else
178 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
179 }
180 }
181
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000182 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
183 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
184 setOperationAction(ISD::MULHS, MVT::i32, Custom);
185 setOperationAction(ISD::MULHU, MVT::i32, Custom);
186
Eric Christopher1c29a652014-07-18 22:55:25 +0000187 if (Subtarget.hasCnMips())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000188 setOperationAction(ISD::MUL, MVT::i64, Legal);
Eric Christopher1c29a652014-07-18 22:55:25 +0000189 else if (Subtarget.isGP64bit())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000190 setOperationAction(ISD::MUL, MVT::i64, Custom);
191
Eric Christopher1c29a652014-07-18 22:55:25 +0000192 if (Subtarget.isGP64bit()) {
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000193 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Custom);
194 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000195 setOperationAction(ISD::MULHS, MVT::i64, Custom);
196 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Jan Vesely54468a5a2014-10-17 14:45:28 +0000197 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
198 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000199 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000200
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000201 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
202 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
203
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000204 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
205 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000206 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
207 setOperationAction(ISD::LOAD, MVT::i32, Custom);
208 setOperationAction(ISD::STORE, MVT::i32, Custom);
209
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000210 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000211
Daniel Sandersce09d072013-08-28 12:14:50 +0000212 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000213 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
214 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
215
Simon Atanasyana0291112019-04-25 07:47:28 +0000216 if (Subtarget.hasMips32r2() && !Subtarget.useSoftFloat() &&
217 !Subtarget.hasMips64()) {
218 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
219 }
220
Akira Hatanaka63791212013-09-07 00:52:30 +0000221 if (NoDPLoadStore) {
222 setOperationAction(ISD::LOAD, MVT::f64, Custom);
223 setOperationAction(ISD::STORE, MVT::f64, Custom);
224 }
225
Eric Christopher1c29a652014-07-18 22:55:25 +0000226 if (Subtarget.hasMips32r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000227 // MIPS32r6 replaces the accumulator-based multiplies with a three register
228 // instruction
Daniel Sanders826f8b32014-06-12 10:54:16 +0000229 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
230 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000231 setOperationAction(ISD::MUL, MVT::i32, Legal);
232 setOperationAction(ISD::MULHS, MVT::i32, Legal);
233 setOperationAction(ISD::MULHU, MVT::i32, Legal);
234
235 // MIPS32r6 replaces the accumulator-based division/remainder with separate
236 // three register division and remainder instructions.
237 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
238 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
239 setOperationAction(ISD::SDIV, MVT::i32, Legal);
240 setOperationAction(ISD::UDIV, MVT::i32, Legal);
241 setOperationAction(ISD::SREM, MVT::i32, Legal);
242 setOperationAction(ISD::UREM, MVT::i32, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000243
244 // MIPS32r6 replaces conditional moves with an equivalent that removes the
245 // need for three GPR read ports.
246 setOperationAction(ISD::SETCC, MVT::i32, Legal);
247 setOperationAction(ISD::SELECT, MVT::i32, Legal);
248 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
249
250 setOperationAction(ISD::SETCC, MVT::f32, Legal);
251 setOperationAction(ISD::SELECT, MVT::f32, Legal);
252 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
253
Eric Christopher1c29a652014-07-18 22:55:25 +0000254 assert(Subtarget.isFP64bit() && "FR=1 is required for MIPS32r6");
Daniel Sanders0fa60412014-06-12 13:39:06 +0000255 setOperationAction(ISD::SETCC, MVT::f64, Legal);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000256 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000257 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
258
Daniel Sanders3d3ea532014-06-12 15:00:17 +0000259 setOperationAction(ISD::BRCOND, MVT::Other, Legal);
260
Daniel Sanders0fa60412014-06-12 13:39:06 +0000261 // Floating point > and >= are supported via < and <=
262 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
263 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
264 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
265 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
266
267 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
268 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
269 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
270 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000271 }
272
Eric Christopher1c29a652014-07-18 22:55:25 +0000273 if (Subtarget.hasMips64r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000274 // MIPS64r6 replaces the accumulator-based multiplies with a three register
275 // instruction
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000276 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
277 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000278 setOperationAction(ISD::MUL, MVT::i64, Legal);
279 setOperationAction(ISD::MULHS, MVT::i64, Legal);
280 setOperationAction(ISD::MULHU, MVT::i64, Legal);
281
282 // MIPS32r6 replaces the accumulator-based division/remainder with separate
283 // three register division and remainder instructions.
284 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
285 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
286 setOperationAction(ISD::SDIV, MVT::i64, Legal);
287 setOperationAction(ISD::UDIV, MVT::i64, Legal);
288 setOperationAction(ISD::SREM, MVT::i64, Legal);
289 setOperationAction(ISD::UREM, MVT::i64, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000290
291 // MIPS64r6 replaces conditional moves with an equivalent that removes the
292 // need for three GPR read ports.
293 setOperationAction(ISD::SETCC, MVT::i64, Legal);
294 setOperationAction(ISD::SELECT, MVT::i64, Legal);
295 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000296 }
297
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000298 computeRegisterProperties(Subtarget.getRegisterInfo());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000299}
300
301const MipsTargetLowering *
Eric Christopherb1526602014-09-19 23:30:42 +0000302llvm::createMipsSETargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000303 const MipsSubtarget &STI) {
304 return new MipsSETargetLowering(TM, STI);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000305}
306
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000307const TargetRegisterClass *
308MipsSETargetLowering::getRepRegClassFor(MVT VT) const {
309 if (VT == MVT::Untyped)
Eric Christopher1c29a652014-07-18 22:55:25 +0000310 return Subtarget.hasDSP() ? &Mips::ACC64DSPRegClass : &Mips::ACC64RegClass;
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000311
312 return TargetLowering::getRepRegClassFor(VT);
313}
314
Daniel Sanders7a289d02013-09-23 12:02:46 +0000315// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000316void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000317addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
318 addRegisterClass(Ty, RC);
319
320 // Expand all builtin opcodes.
321 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
322 setOperationAction(Opc, Ty, Expand);
323
324 setOperationAction(ISD::BITCAST, Ty, Legal);
325 setOperationAction(ISD::LOAD, Ty, Legal);
326 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000327 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
328 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000329 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000330
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000331 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000332 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000333 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000334 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000335 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000336 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000337 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000338 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000339 setOperationAction(ISD::SHL, Ty, Legal);
340 setOperationAction(ISD::SRA, Ty, Legal);
341 setOperationAction(ISD::SRL, Ty, Legal);
342 setOperationAction(ISD::SUB, Ty, Legal);
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000343 setOperationAction(ISD::SMAX, Ty, Legal);
344 setOperationAction(ISD::SMIN, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000345 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000346 setOperationAction(ISD::UREM, Ty, Legal);
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000347 setOperationAction(ISD::UMAX, Ty, Legal);
348 setOperationAction(ISD::UMIN, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000349 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000350 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000351 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000352
Daniel Sanders015972b2013-10-11 10:00:06 +0000353 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
354 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
355 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
356 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
357 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
358 }
359
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000360 setOperationAction(ISD::SETCC, Ty, Legal);
361 setCondCodeAction(ISD::SETNE, Ty, Expand);
362 setCondCodeAction(ISD::SETGE, Ty, Expand);
363 setCondCodeAction(ISD::SETGT, Ty, Expand);
364 setCondCodeAction(ISD::SETUGE, Ty, Expand);
365 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000366}
367
Daniel Sanders7a289d02013-09-23 12:02:46 +0000368// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000369void MipsSETargetLowering::
370addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000371 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000372
373 // Expand all builtin opcodes.
374 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
375 setOperationAction(Opc, Ty, Expand);
376
377 setOperationAction(ISD::LOAD, Ty, Legal);
378 setOperationAction(ISD::STORE, Ty, Legal);
379 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000380 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000381 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000382 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000383
384 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000385 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000386 setOperationAction(ISD::FADD, Ty, Legal);
387 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000388 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000389 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000390 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000391 setOperationAction(ISD::FMUL, Ty, Legal);
392 setOperationAction(ISD::FRINT, Ty, Legal);
393 setOperationAction(ISD::FSQRT, Ty, Legal);
394 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000395 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000396
397 setOperationAction(ISD::SETCC, Ty, Legal);
398 setCondCodeAction(ISD::SETOGE, Ty, Expand);
399 setCondCodeAction(ISD::SETOGT, Ty, Expand);
400 setCondCodeAction(ISD::SETUGE, Ty, Expand);
401 setCondCodeAction(ISD::SETUGT, Ty, Expand);
402 setCondCodeAction(ISD::SETGE, Ty, Expand);
403 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000404 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000405}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000406
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000407SDValue MipsSETargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000408 if(!Subtarget.hasMips32r6())
409 return MipsTargetLowering::LowerOperation(Op, DAG);
410
411 EVT ResTy = Op->getValueType(0);
412 SDLoc DL(Op);
413
414 // Although MTC1_D64 takes an i32 and writes an f64, the upper 32 bits of the
415 // floating point register are undefined. Not really an issue as sel.d, which
416 // is produced from an FSELECT node, only looks at bit 0.
417 SDValue Tmp = DAG.getNode(MipsISD::MTC1_D64, DL, MVT::f64, Op->getOperand(0));
418 return DAG.getNode(MipsISD::FSELECT, DL, ResTy, Tmp, Op->getOperand(1),
419 Op->getOperand(2));
420}
421
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000422bool
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000423MipsSETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
424 unsigned,
425 unsigned,
426 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000427 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
428
Eric Christopher1c29a652014-07-18 22:55:25 +0000429 if (Subtarget.systemSupportsUnalignedAccess()) {
Daniel Sandersac272632014-05-23 13:18:02 +0000430 // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
431 // implementation defined whether this is handled by hardware, software, or
432 // a hybrid of the two but it's expected that most implementations will
433 // handle the majority of cases in hardware.
434 if (Fast)
435 *Fast = true;
436 return true;
437 }
438
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000439 switch (SVT) {
440 case MVT::i64:
441 case MVT::i32:
442 if (Fast)
443 *Fast = true;
444 return true;
445 default:
446 return false;
447 }
448}
449
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000450SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
451 SelectionDAG &DAG) const {
452 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000453 case ISD::LOAD: return lowerLOAD(Op, DAG);
454 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000455 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
456 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
457 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
458 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
459 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
460 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000461 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
462 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000463 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
464 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000465 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000466 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000467 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000468 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000469 case ISD::SELECT: return lowerSELECT(Op, DAG);
Simon Atanasyana0291112019-04-25 07:47:28 +0000470 case ISD::BITCAST: return lowerBITCAST(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000471 }
472
473 return MipsTargetLowering::LowerOperation(Op, DAG);
474}
475
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000476// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
477//
478// Performs the following transformations:
479// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
480// sign/zero-extension is completely overwritten by the new one performed by
481// the ISD::AND.
482// - Removes redundant zero extensions performed by an ISD::AND.
483static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
484 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000485 const MipsSubtarget &Subtarget) {
486 if (!Subtarget.hasMSA())
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000487 return SDValue();
488
489 SDValue Op0 = N->getOperand(0);
490 SDValue Op1 = N->getOperand(1);
491 unsigned Op0Opcode = Op0->getOpcode();
492
493 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
494 // where $d + 1 == 2^n and n == 32
495 // or $d + 1 == 2^n and n <= 32 and ZExt
496 // -> (MipsVExtractZExt $a, $b, $c)
497 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
498 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
499 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
500
501 if (!Mask)
502 return SDValue();
503
504 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
505
506 if (Log2IfPositive <= 0)
507 return SDValue(); // Mask+1 is not a power of 2
508
509 SDValue Op0Op2 = Op0->getOperand(2);
510 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
511 unsigned ExtendTySize = ExtendTy.getSizeInBits();
512 unsigned Log2 = Log2IfPositive;
513
514 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
515 Log2 == ExtendTySize) {
516 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000517 return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
518 Op0->getVTList(),
519 makeArrayRef(Ops, Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000520 }
521 }
522
523 return SDValue();
524}
525
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000526// Determine if the specified node is a constant vector splat.
527//
528// Returns true and sets Imm if:
529// * N is a ISD::BUILD_VECTOR representing a constant splat
530//
531// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
532// differences are that it assumes the MSA has already been checked and the
533// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
534// must not be in order for binsri.d to be selectable).
535static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
536 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
537
Craig Topper062a2ba2014-04-25 05:30:21 +0000538 if (!Node)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000539 return false;
540
541 APInt SplatValue, SplatUndef;
542 unsigned SplatBitSize;
543 bool HasAnyUndefs;
544
545 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
546 8, !IsLittleEndian))
547 return false;
548
549 Imm = SplatValue;
550
551 return true;
552}
553
Daniel Sandersab94b532013-10-30 15:20:38 +0000554// Test whether the given node is an all-ones build_vector.
555static bool isVectorAllOnes(SDValue N) {
556 // Look through bitcasts. Endianness doesn't matter because we are looking
557 // for an all-ones value.
558 if (N->getOpcode() == ISD::BITCAST)
559 N = N->getOperand(0);
560
561 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
562
563 if (!BVN)
564 return false;
565
566 APInt SplatValue, SplatUndef;
567 unsigned SplatBitSize;
568 bool HasAnyUndefs;
569
570 // Endianness doesn't matter in this context because we are looking for
571 // an all-ones value.
572 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
573 return SplatValue.isAllOnesValue();
574
575 return false;
576}
577
578// Test whether N is the bitwise inverse of OfNode.
579static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
580 if (N->getOpcode() != ISD::XOR)
581 return false;
582
583 if (isVectorAllOnes(N->getOperand(0)))
584 return N->getOperand(1) == OfNode;
585
586 if (isVectorAllOnes(N->getOperand(1)))
587 return N->getOperand(0) == OfNode;
588
589 return false;
590}
591
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000592// Perform combines where ISD::OR is the root node.
593//
594// Performs the following transformations:
595// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
596// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
597// vector type.
598static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
599 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000600 const MipsSubtarget &Subtarget) {
601 if (!Subtarget.hasMSA())
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000602 return SDValue();
603
604 EVT Ty = N->getValueType(0);
605
606 if (!Ty.is128BitVector())
607 return SDValue();
608
609 SDValue Op0 = N->getOperand(0);
610 SDValue Op1 = N->getOperand(1);
611
612 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
613 SDValue Op0Op0 = Op0->getOperand(0);
614 SDValue Op0Op1 = Op0->getOperand(1);
615 SDValue Op1Op0 = Op1->getOperand(0);
616 SDValue Op1Op1 = Op1->getOperand(1);
Eric Christopher1c29a652014-07-18 22:55:25 +0000617 bool IsLittleEndian = !Subtarget.isLittle();
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000618
619 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000620 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000621 APInt Mask, InvMask;
622
623 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
624 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
625 // looking.
626 // IfClr will be set if we find a valid match.
627 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
628 Cond = Op0Op0;
629 IfSet = Op0Op1;
630
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000631 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
632 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000633 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000634 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
635 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000636 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000637
638 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000639 }
640
641 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
642 // thing again using this mask.
643 // IfClr will be set if we find a valid match.
644 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
645 Cond = Op0Op1;
646 IfSet = Op0Op0;
647
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000648 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
649 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000650 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000651 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
652 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000653 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000654
655 IsConstantMask = true;
656 }
657
658 // If IfClr is not yet set, try looking for a non-constant match.
659 // IfClr will be set if we find a valid match amongst the eight
660 // possibilities.
661 if (!IfClr.getNode()) {
662 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
663 Cond = Op1Op0;
664 IfSet = Op1Op1;
665 IfClr = Op0Op1;
666 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
667 Cond = Op1Op0;
668 IfSet = Op1Op1;
669 IfClr = Op0Op0;
670 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
671 Cond = Op1Op1;
672 IfSet = Op1Op0;
673 IfClr = Op0Op1;
674 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
675 Cond = Op1Op1;
676 IfSet = Op1Op0;
677 IfClr = Op0Op0;
678 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
679 Cond = Op0Op0;
680 IfSet = Op0Op1;
681 IfClr = Op1Op1;
682 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
683 Cond = Op0Op0;
684 IfSet = Op0Op1;
685 IfClr = Op1Op0;
686 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
687 Cond = Op0Op1;
688 IfSet = Op0Op0;
689 IfClr = Op1Op1;
690 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
691 Cond = Op0Op1;
692 IfSet = Op0Op0;
693 IfClr = Op1Op0;
694 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000695 }
696
697 // At this point, IfClr will be set if we have a valid match.
698 if (!IfClr.getNode())
699 return SDValue();
700
701 assert(Cond.getNode() && IfSet.getNode());
702
703 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000704 if (IsConstantMask) {
705 if (Mask.isAllOnesValue())
706 return IfSet;
707 else if (Mask == 0)
708 return IfClr;
709 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000710
711 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000712 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000713 }
714
715 return SDValue();
716}
717
Simon Dardis9ec9f442018-04-13 16:09:07 +0000718static bool shouldTransformMulToShiftsAddsSubs(APInt C, EVT VT,
719 SelectionDAG &DAG,
720 const MipsSubtarget &Subtarget) {
721 // Estimate the number of operations the below transform will turn a
Simon Atanasyanc1b482f2019-05-24 08:39:40 +0000722 // constant multiply into. The number is approximately equal to the minimal
723 // number of powers of two that constant can be broken down to by adding
724 // or subtracting them.
725 //
726 // If we have taken more than 12[1] / 8[2] steps to attempt the
727 // optimization for a native sized value, it is more than likely that this
728 // optimization will make things worse.
729 //
730 // [1] MIPS64 requires 6 instructions at most to materialize any constant,
731 // multiplication requires at least 4 cycles, but another cycle (or two)
732 // to retrieve the result from the HI/LO registers.
733 //
734 // [2] For MIPS32, more than 8 steps is expensive as the constant could be
735 // materialized in 2 instructions, multiplication requires at least 4
736 // cycles, but another cycle (or two) to retrieve the result from the
737 // HI/LO registers.
738 //
739 // TODO:
740 // - MaxSteps needs to consider the `VT` of the constant for the current
741 // target.
742 // - Consider to perform this optimization after type legalization.
743 // That allows to remove a workaround for types not supported natively.
744 // - Take in account `-Os, -Oz` flags because this optimization
745 // increases code size.
746 unsigned MaxSteps = Subtarget.isABI_O32() ? 8 : 12;
Simon Dardis9ec9f442018-04-13 16:09:07 +0000747
748 SmallVector<APInt, 16> WorkStack(1, C);
749 unsigned Steps = 0;
750 unsigned BitWidth = C.getBitWidth();
751
752 while (!WorkStack.empty()) {
753 APInt Val = WorkStack.pop_back_val();
754
755 if (Val == 0 || Val == 1)
756 continue;
757
Simon Atanasyanc1b482f2019-05-24 08:39:40 +0000758 if (Steps >= MaxSteps)
759 return false;
760
Simon Dardis9ec9f442018-04-13 16:09:07 +0000761 if (Val.isPowerOf2()) {
762 ++Steps;
763 continue;
764 }
765
766 APInt Floor = APInt(BitWidth, 1) << Val.logBase2();
767 APInt Ceil = Val.isNegative() ? APInt(BitWidth, 0)
768 : APInt(BitWidth, 1) << C.ceilLogBase2();
Simon Dardis9ec9f442018-04-13 16:09:07 +0000769 if ((Val - Floor).ule(Ceil - Val)) {
770 WorkStack.push_back(Floor);
771 WorkStack.push_back(Val - Floor);
Simon Atanasyanc1b482f2019-05-24 08:39:40 +0000772 } else {
773 WorkStack.push_back(Ceil);
774 WorkStack.push_back(Ceil - Val);
Simon Dardis9ec9f442018-04-13 16:09:07 +0000775 }
776
Simon Dardis9ec9f442018-04-13 16:09:07 +0000777 ++Steps;
Simon Dardis9ec9f442018-04-13 16:09:07 +0000778 }
779
780 // If the value being multiplied is not supported natively, we have to pay
781 // an additional legalization cost, conservatively assume an increase in the
782 // cost of 3 instructions per step. This values for this heuristic were
783 // determined experimentally.
784 unsigned RegisterSize = DAG.getTargetLoweringInfo()
785 .getRegisterType(*DAG.getContext(), VT)
786 .getSizeInBits();
787 Steps *= (VT.getSizeInBits() != RegisterSize) * 3;
788 if (Steps > 27)
789 return false;
790
791 return true;
792}
793
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000794static SDValue genConstMult(SDValue X, APInt C, const SDLoc &DL, EVT VT,
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000795 EVT ShiftTy, SelectionDAG &DAG) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000796 // Return 0.
797 if (C == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000798 return DAG.getConstant(0, DL, VT);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000799
800 // Return x.
801 if (C == 1)
802 return X;
803
804 // If c is power of 2, return (shl x, log2(c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000805 if (C.isPowerOf2())
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000806 return DAG.getNode(ISD::SHL, DL, VT, X,
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000807 DAG.getConstant(C.logBase2(), DL, ShiftTy));
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000808
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000809 unsigned BitWidth = C.getBitWidth();
810 APInt Floor = APInt(BitWidth, 1) << C.logBase2();
811 APInt Ceil = C.isNegative() ? APInt(BitWidth, 0) :
812 APInt(BitWidth, 1) << C.ceilLogBase2();
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000813
814 // If |c - floor_c| <= |c - ceil_c|,
815 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
816 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000817 if ((C - Floor).ule(Ceil - C)) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000818 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
819 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
820 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
821 }
822
823 // If |c - floor_c| > |c - ceil_c|,
824 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
825 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
826 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
827 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
828}
829
830static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
831 const TargetLowering::DAGCombinerInfo &DCI,
Simon Dardis9ec9f442018-04-13 16:09:07 +0000832 const MipsSETargetLowering *TL,
833 const MipsSubtarget &Subtarget) {
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000834 EVT VT = N->getValueType(0);
835
836 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
Simon Dardis9ec9f442018-04-13 16:09:07 +0000837 if (!VT.isVector() && shouldTransformMulToShiftsAddsSubs(
838 C->getAPIntValue(), VT, DAG, Subtarget))
Petar Jovanoviccd729ea2017-11-15 15:24:04 +0000839 return genConstMult(N->getOperand(0), C->getAPIntValue(), SDLoc(N), VT,
Mehdi Aminieaabc512015-07-09 15:12:23 +0000840 TL->getScalarShiftAmountTy(DAG.getDataLayout(), VT),
841 DAG);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000842
843 return SDValue(N, 0);
844}
845
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000846static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
847 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000848 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000849 // See if this is a vector splat immediate node.
850 APInt SplatValue, SplatUndef;
851 unsigned SplatBitSize;
852 bool HasAnyUndefs;
Sanjay Patel1ed771f2016-09-14 16:37:15 +0000853 unsigned EltSize = Ty.getScalarSizeInBits();
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000854 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
855
Eric Christopher1c29a652014-07-18 22:55:25 +0000856 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000857 return SDValue();
858
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000859 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000860 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000861 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000862 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000863 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000864 return SDValue();
865
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000866 SDLoc DL(N);
867 return DAG.getNode(Opc, DL, Ty, N->getOperand(0),
868 DAG.getConstant(SplatValue.getZExtValue(), DL, MVT::i32));
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000869}
870
871static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
872 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000873 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000874 EVT Ty = N->getValueType(0);
875
876 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
877 return SDValue();
878
879 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
880}
881
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000882// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
883// constant splats into MipsISD::SHRA_DSP for DSPr2.
884//
885// Performs the following transformations:
886// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
887// sign/zero-extension is completely overwritten by the new one performed by
888// the ISD::SRA and ISD::SHL nodes.
889// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
890// sequence.
891//
892// See performDSPShiftCombine for more information about the transformation
893// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000894static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
895 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000896 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000897 EVT Ty = N->getValueType(0);
898
Eric Christopher1c29a652014-07-18 22:55:25 +0000899 if (Subtarget.hasMSA()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000900 SDValue Op0 = N->getOperand(0);
901 SDValue Op1 = N->getOperand(1);
902
903 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
904 // where $d + sizeof($c) == 32
905 // or $d + sizeof($c) <= 32 and SExt
906 // -> (MipsVExtractSExt $a, $b, $c)
907 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
908 SDValue Op0Op0 = Op0->getOperand(0);
909 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
910
911 if (!ShAmount)
912 return SDValue();
913
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000914 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
915 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
916 return SDValue();
917
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000918 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
919 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
920
921 if (TotalBits == 32 ||
922 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
923 TotalBits <= 32)) {
924 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
925 Op0Op0->getOperand(2) };
Chandler Carruth356665a2014-08-01 22:09:43 +0000926 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
927 Op0Op0->getVTList(),
928 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000929 }
930 }
931 }
932
Eric Christopher1c29a652014-07-18 22:55:25 +0000933 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000934 return SDValue();
935
936 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
937}
938
939
940static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
941 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000942 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000943 EVT Ty = N->getValueType(0);
944
Eric Christopher1c29a652014-07-18 22:55:25 +0000945 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000946 return SDValue();
947
948 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
949}
950
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000951static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
952 bool IsV216 = (Ty == MVT::v2i16);
953
954 switch (CC) {
955 case ISD::SETEQ:
956 case ISD::SETNE: return true;
957 case ISD::SETLT:
958 case ISD::SETLE:
959 case ISD::SETGT:
960 case ISD::SETGE: return IsV216;
961 case ISD::SETULT:
962 case ISD::SETULE:
963 case ISD::SETUGT:
964 case ISD::SETUGE: return !IsV216;
965 default: return false;
966 }
967}
968
969static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
970 EVT Ty = N->getValueType(0);
971
972 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
973 return SDValue();
974
975 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
976 return SDValue();
977
Andrew Trickef9de2a2013-05-25 02:42:55 +0000978 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000979 N->getOperand(1), N->getOperand(2));
980}
981
982static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
983 EVT Ty = N->getValueType(0);
984
Simon Pilgrim386b8dd2018-02-17 21:29:45 +0000985 if (Ty == MVT::v2i16 || Ty == MVT::v4i8) {
Daniel Sanders3ce56622013-09-24 12:18:31 +0000986 SDValue SetCC = N->getOperand(0);
987
988 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
989 return SDValue();
990
991 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
992 SetCC.getOperand(0), SetCC.getOperand(1),
993 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
994 }
995
996 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000997}
998
Daniel Sandersf7456c72013-09-23 13:22:24 +0000999static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001000 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001001 EVT Ty = N->getValueType(0);
1002
Eric Christopher1c29a652014-07-18 22:55:25 +00001003 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001004 // Try the following combines:
1005 // (xor (or $a, $b), (build_vector allones))
1006 // (xor (or $a, $b), (bitcast (build_vector allones)))
1007 SDValue Op0 = N->getOperand(0);
1008 SDValue Op1 = N->getOperand(1);
1009 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001010
1011 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
1012 NotOp = Op1;
1013 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
1014 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001015 else
1016 return SDValue();
1017
1018 if (NotOp->getOpcode() == ISD::OR)
1019 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
1020 NotOp->getOperand(1));
1021 }
1022
1023 return SDValue();
1024}
1025
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001026SDValue
1027MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1028 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001029 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001030
1031 switch (N->getOpcode()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001032 case ISD::AND:
1033 Val = performANDCombine(N, DAG, DCI, Subtarget);
1034 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +00001035 case ISD::OR:
1036 Val = performORCombine(N, DAG, DCI, Subtarget);
1037 break;
Akira Hatanaka5832fc62013-06-26 18:48:17 +00001038 case ISD::MUL:
Simon Dardis9ec9f442018-04-13 16:09:07 +00001039 return performMULCombine(N, DAG, DCI, this, Subtarget);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001040 case ISD::SHL:
Petar Jovanovicb71386a2017-03-15 13:10:08 +00001041 Val = performSHLCombine(N, DAG, DCI, Subtarget);
1042 break;
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001043 case ISD::SRA:
1044 return performSRACombine(N, DAG, DCI, Subtarget);
1045 case ISD::SRL:
1046 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001047 case ISD::VSELECT:
1048 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +00001049 case ISD::XOR:
1050 Val = performXORCombine(N, DAG, Subtarget);
1051 break;
1052 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001053 Val = performSETCCCombine(N, DAG);
1054 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001055 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001056
Daniel Sanders62aeab82013-10-30 13:31:27 +00001057 if (Val.getNode()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001058 LLVM_DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1059 N->printrWithDepth(dbgs(), &DAG); dbgs() << "\n=> \n";
1060 Val.getNode()->printrWithDepth(dbgs(), &DAG); dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001061 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001062 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001063
1064 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001065}
1066
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001067MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001068MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001069 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001070 switch (MI.getOpcode()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001071 default:
1072 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1073 case Mips::BPOSGE32_PSEUDO:
1074 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001075 case Mips::SNZ_B_PSEUDO:
1076 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1077 case Mips::SNZ_H_PSEUDO:
1078 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1079 case Mips::SNZ_W_PSEUDO:
1080 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1081 case Mips::SNZ_D_PSEUDO:
1082 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1083 case Mips::SNZ_V_PSEUDO:
1084 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1085 case Mips::SZ_B_PSEUDO:
1086 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1087 case Mips::SZ_H_PSEUDO:
1088 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1089 case Mips::SZ_W_PSEUDO:
1090 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1091 case Mips::SZ_D_PSEUDO:
1092 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1093 case Mips::SZ_V_PSEUDO:
1094 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001095 case Mips::COPY_FW_PSEUDO:
1096 return emitCOPY_FW(MI, BB);
1097 case Mips::COPY_FD_PSEUDO:
1098 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001099 case Mips::INSERT_FW_PSEUDO:
1100 return emitINSERT_FW(MI, BB);
1101 case Mips::INSERT_FD_PSEUDO:
1102 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001103 case Mips::INSERT_B_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001104 case Mips::INSERT_B_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001105 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1106 case Mips::INSERT_H_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001107 case Mips::INSERT_H_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001108 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1109 case Mips::INSERT_W_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001110 case Mips::INSERT_W_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001111 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1112 case Mips::INSERT_D_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001113 case Mips::INSERT_D_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001114 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1115 case Mips::INSERT_FW_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001116 case Mips::INSERT_FW_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001117 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1118 case Mips::INSERT_FD_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001119 case Mips::INSERT_FD_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001120 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001121 case Mips::FILL_FW_PSEUDO:
1122 return emitFILL_FW(MI, BB);
1123 case Mips::FILL_FD_PSEUDO:
1124 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001125 case Mips::FEXP2_W_1_PSEUDO:
1126 return emitFEXP2_W_1(MI, BB);
1127 case Mips::FEXP2_D_1_PSEUDO:
1128 return emitFEXP2_D_1(MI, BB);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00001129 case Mips::ST_F16:
1130 return emitST_F16_PSEUDO(MI, BB);
1131 case Mips::LD_F16:
1132 return emitLD_F16_PSEUDO(MI, BB);
1133 case Mips::MSA_FP_EXTEND_W_PSEUDO:
1134 return emitFPEXTEND_PSEUDO(MI, BB, false);
1135 case Mips::MSA_FP_ROUND_W_PSEUDO:
1136 return emitFPROUND_PSEUDO(MI, BB, false);
1137 case Mips::MSA_FP_EXTEND_D_PSEUDO:
1138 return emitFPEXTEND_PSEUDO(MI, BB, true);
1139 case Mips::MSA_FP_ROUND_D_PSEUDO:
1140 return emitFPROUND_PSEUDO(MI, BB, true);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001141 }
1142}
1143
Daniel Sanders23e98772014-11-02 16:09:29 +00001144bool MipsSETargetLowering::isEligibleForTailCallOptimization(
1145 const CCState &CCInfo, unsigned NextStackOffset,
1146 const MipsFunctionInfo &FI) const {
Simon Dardis57f4ae42016-08-04 09:17:07 +00001147 if (!UseMipsTailCalls)
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001148 return false;
1149
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00001150 // Exception has to be cleared with eret.
1151 if (FI.isISR())
1152 return false;
1153
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001154 // Return false if either the callee or caller has a byval argument.
Daniel Sanders23e98772014-11-02 16:09:29 +00001155 if (CCInfo.getInRegsParamsCount() > 0 || FI.hasByvalArg())
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001156 return false;
1157
1158 // Return true if the callee's argument area is no larger than the
1159 // caller's.
1160 return NextStackOffset <= FI.getIncomingArgSize();
1161}
1162
1163void MipsSETargetLowering::
1164getOpndList(SmallVectorImpl<SDValue> &Ops,
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001165 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001166 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001167 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
1168 SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001169 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001170 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001171 InternalLinkage, IsCallReloc, CLI, Callee,
1172 Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001173}
1174
Akira Hatanaka63791212013-09-07 00:52:30 +00001175SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1176 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1177
1178 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1179 return MipsTargetLowering::lowerLOAD(Op, DAG);
1180
1181 // Replace a double precision load with two i32 loads and a buildpair64.
1182 SDLoc DL(Op);
1183 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1184 EVT PtrVT = Ptr.getValueType();
1185
1186 // i32 load from lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001187 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr, MachinePointerInfo(),
1188 Nd.getAlignment(), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001189
1190 // i32 load from higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001191 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001192 SDValue Hi = DAG.getLoad(
1193 MVT::i32, DL, Lo.getValue(1), Ptr, MachinePointerInfo(),
1194 std::min(Nd.getAlignment(), 4U), Nd.getMemOperand()->getFlags());
Akira Hatanaka63791212013-09-07 00:52:30 +00001195
Eric Christopher1c29a652014-07-18 22:55:25 +00001196 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001197 std::swap(Lo, Hi);
1198
1199 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1200 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001201 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001202}
1203
1204SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1205 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1206
1207 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1208 return MipsTargetLowering::lowerSTORE(Op, DAG);
1209
1210 // Replace a double precision store with two extractelement64s and i32 stores.
1211 SDLoc DL(Op);
1212 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1213 EVT PtrVT = Ptr.getValueType();
1214 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001215 Val, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001216 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001217 Val, DAG.getConstant(1, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001218
Eric Christopher1c29a652014-07-18 22:55:25 +00001219 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001220 std::swap(Lo, Hi);
1221
1222 // i32 store to lower address.
Justin Lebar9c375812016-07-15 18:27:10 +00001223 Chain =
1224 DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(), Nd.getAlignment(),
1225 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001226
1227 // i32 store to higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001228 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Akira Hatanaka63791212013-09-07 00:52:30 +00001229 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00001230 std::min(Nd.getAlignment(), 4U),
1231 Nd.getMemOperand()->getFlags(), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001232}
1233
Simon Atanasyana0291112019-04-25 07:47:28 +00001234SDValue MipsSETargetLowering::lowerBITCAST(SDValue Op,
1235 SelectionDAG &DAG) const {
1236 SDLoc DL(Op);
1237 MVT Src = Op.getOperand(0).getValueType().getSimpleVT();
1238 MVT Dest = Op.getValueType().getSimpleVT();
1239
1240 // Bitcast i64 to double.
1241 if (Src == MVT::i64 && Dest == MVT::f64) {
1242 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
1243 Op.getOperand(0), DAG.getIntPtrConstant(0, DL));
1244 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
1245 Op.getOperand(0), DAG.getIntPtrConstant(1, DL));
1246 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1247 }
1248
1249 // Bitcast double to i64.
1250 if (Src == MVT::f64 && Dest == MVT::i64) {
1251 SDValue Lo =
1252 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1253 DAG.getConstant(0, DL, MVT::i32));
1254 SDValue Hi =
1255 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1256 DAG.getConstant(1, DL, MVT::i32));
1257 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1258 }
1259
1260 // Skip other cases of bitcast and use default lowering.
1261 return SDValue();
1262}
1263
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001264SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1265 bool HasLo, bool HasHi,
1266 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001267 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001268 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001269
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001270 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001271 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001272 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1273 Op.getOperand(0), Op.getOperand(1));
1274 SDValue Lo, Hi;
1275
1276 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001277 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001278 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001279 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001280
1281 if (!HasLo || !HasHi)
1282 return HasLo ? Lo : Hi;
1283
1284 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001285 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001286}
1287
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001288static SDValue initAccumulator(SDValue In, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001289 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001290 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001291 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001292 DAG.getConstant(1, DL, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001293 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001294}
1295
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001296static SDValue extractLOHI(SDValue Op, const SDLoc &DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001297 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1298 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001299 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1300}
1301
1302// This function expands mips intrinsic nodes which have 64-bit input operands
1303// or output values.
1304//
1305// out64 = intrinsic-node in64
1306// =>
1307// lo = copy (extract-element (in64, 0))
1308// hi = copy (extract-element (in64, 1))
1309// mips-specific-node
1310// v0 = copy lo
1311// v1 = copy hi
1312// out64 = merge-values (v0, v1)
1313//
1314static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001315 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001316 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1317 SmallVector<SDValue, 3> Ops;
1318 unsigned OpNo = 0;
1319
1320 // See if Op has a chain input.
1321 if (HasChainIn)
1322 Ops.push_back(Op->getOperand(OpNo++));
1323
1324 // The next operand is the intrinsic opcode.
1325 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1326
1327 // See if the next operand has type i64.
1328 SDValue Opnd = Op->getOperand(++OpNo), In64;
1329
1330 if (Opnd.getValueType() == MVT::i64)
1331 In64 = initAccumulator(Opnd, DL, DAG);
1332 else
1333 Ops.push_back(Opnd);
1334
1335 // Push the remaining operands.
1336 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1337 Ops.push_back(Op->getOperand(OpNo));
1338
1339 // Add In64 to the end of the list.
1340 if (In64.getNode())
1341 Ops.push_back(In64);
1342
1343 // Scan output.
1344 SmallVector<EVT, 2> ResTys;
1345
1346 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1347 I != E; ++I)
1348 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1349
1350 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001351 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001352 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1353
1354 if (!HasChainIn)
1355 return Out;
1356
1357 assert(Val->getValueType(1) == MVT::Other);
1358 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001359 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001360}
1361
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001362// Lower an MSA copy intrinsic into the specified SelectionDAG node
1363static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1364 SDLoc DL(Op);
1365 SDValue Vec = Op->getOperand(1);
1366 SDValue Idx = Op->getOperand(2);
1367 EVT ResTy = Op->getValueType(0);
1368 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1369
1370 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1371 DAG.getValueType(EltTy));
1372
1373 return Result;
1374}
1375
Daniel Sanders50b80412013-11-15 12:56:49 +00001376static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1377 EVT ResVecTy = Op->getValueType(0);
1378 EVT ViaVecTy = ResVecTy;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001379 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
Daniel Sanders50b80412013-11-15 12:56:49 +00001380 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001381
Daniel Sanders50b80412013-11-15 12:56:49 +00001382 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1383 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1384 // lanes.
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001385 SDValue LaneA = Op->getOperand(OpNr);
1386 SDValue LaneB;
Daniel Sanders50b80412013-11-15 12:56:49 +00001387
1388 if (ResVecTy == MVT::v2i64) {
Stefan Maksimovicc7113cc2018-05-08 15:12:29 +00001389 // In case of the index being passed as an immediate value, set the upper
1390 // lane to 0 so that the splati.d instruction can be matched.
1391 if (isa<ConstantSDNode>(LaneA))
1392 LaneB = DAG.getConstant(0, DL, MVT::i32);
1393 // Having the index passed in a register, set the upper lane to the same
1394 // value as the lower - this results in the BUILD_VECTOR node not being
1395 // expanded through stack. This way we are able to pattern match the set of
1396 // nodes created here to splat.d.
1397 else
1398 LaneB = LaneA;
Daniel Sandersf49dd822013-09-24 13:33:07 +00001399 ViaVecTy = MVT::v4i32;
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001400 if(BigEndian)
1401 std::swap(LaneA, LaneB);
Daniel Sanders50b80412013-11-15 12:56:49 +00001402 } else
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001403 LaneB = LaneA;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001404
Daniel Sanders50b80412013-11-15 12:56:49 +00001405 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1406 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001407
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001408 SDValue Result = DAG.getBuildVector(
1409 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001410
Stefan Maksimovicb794c0a2017-06-23 09:09:31 +00001411 if (ViaVecTy != ResVecTy) {
1412 SDValue One = DAG.getConstant(1, DL, ViaVecTy);
1413 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy,
1414 DAG.getNode(ISD::AND, DL, ViaVecTy, Result, One));
1415 }
Daniel Sandersf49dd822013-09-24 13:33:07 +00001416
1417 return Result;
1418}
1419
Simon Dardis548a53f2017-01-10 16:40:57 +00001420static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG,
1421 bool IsSigned = false) {
Simon Atanasyanf26f56d2019-03-29 15:15:22 +00001422 auto *CImm = cast<ConstantSDNode>(Op->getOperand(ImmOp));
Simon Dardis548a53f2017-01-10 16:40:57 +00001423 return DAG.getConstant(
1424 APInt(Op->getValueType(0).getScalarType().getSizeInBits(),
Simon Atanasyanf26f56d2019-03-29 15:15:22 +00001425 IsSigned ? CImm->getSExtValue() : CImm->getZExtValue(), IsSigned),
Simon Dardis548a53f2017-01-10 16:40:57 +00001426 SDLoc(Op), Op->getValueType(0));
Daniel Sanders50b80412013-11-15 12:56:49 +00001427}
1428
1429static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1430 bool BigEndian, SelectionDAG &DAG) {
1431 EVT ViaVecTy = VecTy;
1432 SDValue SplatValueA = SplatValue;
1433 SDValue SplatValueB = SplatValue;
1434 SDLoc DL(SplatValue);
1435
1436 if (VecTy == MVT::v2i64) {
1437 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1438 ViaVecTy = MVT::v4i32;
1439
1440 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1441 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001442 DAG.getConstant(32, DL, MVT::i32));
Daniel Sanders50b80412013-11-15 12:56:49 +00001443 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1444 }
1445
1446 // We currently hold the parts in little endian order. Swap them if
1447 // necessary.
1448 if (BigEndian)
1449 std::swap(SplatValueA, SplatValueB);
1450
1451 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1452 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1453 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1454 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1455
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001456 SDValue Result = DAG.getBuildVector(
1457 ViaVecTy, DL, makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001458
1459 if (VecTy != ViaVecTy)
1460 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1461
1462 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001463}
1464
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001465static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1466 unsigned Opc, SDValue Imm,
1467 bool BigEndian) {
1468 EVT VecTy = Op->getValueType(0);
1469 SDValue Exp2Imm;
1470 SDLoc DL(Op);
1471
Daniel Sanders50b80412013-11-15 12:56:49 +00001472 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1473 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001474 if (VecTy == MVT::v2i64) {
1475 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1476 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1477
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001478 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), DL,
1479 MVT::i32);
1480 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), DL, MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001481
1482 if (BigEndian)
1483 std::swap(BitImmLoOp, BitImmHiOp);
1484
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001485 Exp2Imm = DAG.getNode(
1486 ISD::BITCAST, DL, MVT::v2i64,
1487 DAG.getBuildVector(MVT::v4i32, DL,
1488 {BitImmLoOp, BitImmHiOp, BitImmLoOp, BitImmHiOp}));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001489 }
1490 }
1491
Craig Topper062a2ba2014-04-25 05:30:21 +00001492 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001493 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001494
1495 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1496 // only values 0-63 are valid.
1497 if (VecTy == MVT::v2i64)
1498 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1499
1500 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1501
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001502 Exp2Imm = DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, DL, VecTy),
1503 Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001504 }
1505
1506 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1507}
1508
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001509static SDValue truncateVecElts(SDValue Op, SelectionDAG &DAG) {
1510 SDLoc DL(Op);
1511 EVT ResTy = Op->getValueType(0);
1512 SDValue Vec = Op->getOperand(2);
1513 bool BigEndian = !DAG.getSubtarget().getTargetTriple().isLittleEndian();
1514 MVT ResEltTy = ResTy == MVT::v2i64 ? MVT::i64 : MVT::i32;
1515 SDValue ConstValue = DAG.getConstant(Vec.getScalarValueSizeInBits() - 1,
1516 DL, ResEltTy);
1517 SDValue SplatVec = getBuildVectorSplat(ResTy, ConstValue, BigEndian, DAG);
1518
1519 return DAG.getNode(ISD::AND, DL, ResTy, Vec, SplatVec);
1520}
1521
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001522static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1523 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001524 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001525 SDValue One = DAG.getConstant(1, DL, ResTy);
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001526 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, truncateVecElts(Op, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001527
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001528 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1529 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001530}
1531
1532static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1533 SDLoc DL(Op);
1534 EVT ResTy = Op->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001535 APInt BitImm = APInt(ResTy.getScalarSizeInBits(), 1)
Daniel Sanders50b80412013-11-15 12:56:49 +00001536 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001537 SDValue BitMask = DAG.getConstant(~BitImm, DL, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001538
1539 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1540}
1541
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001542SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1543 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001544 SDLoc DL(Op);
Simon Dardis548a53f2017-01-10 16:40:57 +00001545 unsigned Intrinsic = cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue();
1546 switch (Intrinsic) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001547 default:
1548 return SDValue();
1549 case Intrinsic::mips_shilo:
1550 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1551 case Intrinsic::mips_dpau_h_qbl:
1552 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1553 case Intrinsic::mips_dpau_h_qbr:
1554 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1555 case Intrinsic::mips_dpsu_h_qbl:
1556 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1557 case Intrinsic::mips_dpsu_h_qbr:
1558 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1559 case Intrinsic::mips_dpa_w_ph:
1560 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1561 case Intrinsic::mips_dps_w_ph:
1562 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1563 case Intrinsic::mips_dpax_w_ph:
1564 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1565 case Intrinsic::mips_dpsx_w_ph:
1566 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1567 case Intrinsic::mips_mulsa_w_ph:
1568 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1569 case Intrinsic::mips_mult:
1570 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1571 case Intrinsic::mips_multu:
1572 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1573 case Intrinsic::mips_madd:
1574 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1575 case Intrinsic::mips_maddu:
1576 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1577 case Intrinsic::mips_msub:
1578 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1579 case Intrinsic::mips_msubu:
1580 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001581 case Intrinsic::mips_addv_b:
1582 case Intrinsic::mips_addv_h:
1583 case Intrinsic::mips_addv_w:
1584 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001585 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1586 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001587 case Intrinsic::mips_addvi_b:
1588 case Intrinsic::mips_addvi_h:
1589 case Intrinsic::mips_addvi_w:
1590 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001591 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1592 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001593 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001594 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1595 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001596 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001597 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1598 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001599 case Intrinsic::mips_bclr_b:
1600 case Intrinsic::mips_bclr_h:
1601 case Intrinsic::mips_bclr_w:
1602 case Intrinsic::mips_bclr_d:
1603 return lowerMSABitClear(Op, DAG);
1604 case Intrinsic::mips_bclri_b:
1605 case Intrinsic::mips_bclri_h:
1606 case Intrinsic::mips_bclri_w:
1607 case Intrinsic::mips_bclri_d:
1608 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001609 case Intrinsic::mips_binsli_b:
1610 case Intrinsic::mips_binsli_h:
1611 case Intrinsic::mips_binsli_w:
1612 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001613 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001614 EVT VecTy = Op->getValueType(0);
1615 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001616 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1617 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001618 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001619 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001620 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001621 DAG.getConstant(Mask, DL, VecTy, true),
1622 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001623 }
1624 case Intrinsic::mips_binsri_b:
1625 case Intrinsic::mips_binsri_h:
1626 case Intrinsic::mips_binsri_w:
1627 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001628 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001629 EVT VecTy = Op->getValueType(0);
1630 EVT EltTy = VecTy.getVectorElementType();
Simon Dardis548a53f2017-01-10 16:40:57 +00001631 if (Op->getConstantOperandVal(3) >= EltTy.getSizeInBits())
1632 report_fatal_error("Immediate out of range");
Daniel Sandersd74b1302013-10-30 14:45:14 +00001633 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
Petar Jovanovicbc54eb82017-04-07 13:31:36 +00001634 Op->getConstantOperandVal(3) + 1);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001635 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001636 DAG.getConstant(Mask, DL, VecTy, true),
1637 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001638 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001639 case Intrinsic::mips_bmnz_v:
1640 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1641 Op->getOperand(2), Op->getOperand(1));
1642 case Intrinsic::mips_bmnzi_b:
1643 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1644 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1645 Op->getOperand(1));
1646 case Intrinsic::mips_bmz_v:
1647 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1648 Op->getOperand(1), Op->getOperand(2));
1649 case Intrinsic::mips_bmzi_b:
1650 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1651 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1652 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001653 case Intrinsic::mips_bneg_b:
1654 case Intrinsic::mips_bneg_h:
1655 case Intrinsic::mips_bneg_w:
1656 case Intrinsic::mips_bneg_d: {
1657 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001658 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001659
1660 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1661 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001662 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001663 }
1664 case Intrinsic::mips_bnegi_b:
1665 case Intrinsic::mips_bnegi_h:
1666 case Intrinsic::mips_bnegi_w:
1667 case Intrinsic::mips_bnegi_d:
1668 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001669 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001670 case Intrinsic::mips_bnz_b:
1671 case Intrinsic::mips_bnz_h:
1672 case Intrinsic::mips_bnz_w:
1673 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001674 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1675 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001676 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001677 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1678 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001679 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001680 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001681 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001682 Op->getOperand(1), Op->getOperand(3),
1683 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001684 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001685 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001686 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001687 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1688 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001689 case Intrinsic::mips_bset_b:
1690 case Intrinsic::mips_bset_h:
1691 case Intrinsic::mips_bset_w:
1692 case Intrinsic::mips_bset_d: {
1693 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001694 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001695
1696 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1697 DAG.getNode(ISD::SHL, DL, VecTy, One,
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00001698 truncateVecElts(Op, DAG)));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001699 }
1700 case Intrinsic::mips_bseti_b:
1701 case Intrinsic::mips_bseti_h:
1702 case Intrinsic::mips_bseti_w:
1703 case Intrinsic::mips_bseti_d:
1704 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001705 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001706 case Intrinsic::mips_bz_b:
1707 case Intrinsic::mips_bz_h:
1708 case Intrinsic::mips_bz_w:
1709 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001710 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1711 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001712 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001713 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1714 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001715 case Intrinsic::mips_ceq_b:
1716 case Intrinsic::mips_ceq_h:
1717 case Intrinsic::mips_ceq_w:
1718 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001719 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001720 Op->getOperand(2), ISD::SETEQ);
1721 case Intrinsic::mips_ceqi_b:
1722 case Intrinsic::mips_ceqi_h:
1723 case Intrinsic::mips_ceqi_w:
1724 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001725 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001726 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETEQ);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001727 case Intrinsic::mips_cle_s_b:
1728 case Intrinsic::mips_cle_s_h:
1729 case Intrinsic::mips_cle_s_w:
1730 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001731 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001732 Op->getOperand(2), ISD::SETLE);
1733 case Intrinsic::mips_clei_s_b:
1734 case Intrinsic::mips_clei_s_h:
1735 case Intrinsic::mips_clei_s_w:
1736 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001737 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001738 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLE);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001739 case Intrinsic::mips_cle_u_b:
1740 case Intrinsic::mips_cle_u_h:
1741 case Intrinsic::mips_cle_u_w:
1742 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001743 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001744 Op->getOperand(2), ISD::SETULE);
1745 case Intrinsic::mips_clei_u_b:
1746 case Intrinsic::mips_clei_u_h:
1747 case Intrinsic::mips_clei_u_w:
1748 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001749 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001750 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1751 case Intrinsic::mips_clt_s_b:
1752 case Intrinsic::mips_clt_s_h:
1753 case Intrinsic::mips_clt_s_w:
1754 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001755 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001756 Op->getOperand(2), ISD::SETLT);
1757 case Intrinsic::mips_clti_s_b:
1758 case Intrinsic::mips_clti_s_h:
1759 case Intrinsic::mips_clti_s_w:
1760 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001761 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Simon Dardis548a53f2017-01-10 16:40:57 +00001762 lowerMSASplatImm(Op, 2, DAG, true), ISD::SETLT);
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001763 case Intrinsic::mips_clt_u_b:
1764 case Intrinsic::mips_clt_u_h:
1765 case Intrinsic::mips_clt_u_w:
1766 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001767 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001768 Op->getOperand(2), ISD::SETULT);
1769 case Intrinsic::mips_clti_u_b:
1770 case Intrinsic::mips_clti_u_h:
1771 case Intrinsic::mips_clti_u_w:
1772 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001773 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001774 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001775 case Intrinsic::mips_copy_s_b:
1776 case Intrinsic::mips_copy_s_h:
1777 case Intrinsic::mips_copy_s_w:
1778 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001779 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001780 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001781 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1782 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1783 else {
1784 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1785 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1786 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1787 Op->getValueType(0), Op->getOperand(1),
1788 Op->getOperand(2));
1789 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001790 case Intrinsic::mips_copy_u_b:
1791 case Intrinsic::mips_copy_u_h:
1792 case Intrinsic::mips_copy_u_w:
1793 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001794 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001795 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001796 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1797 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1798 else {
1799 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1800 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1801 // Note: When i64 is illegal, this results in copy_s.w instructions
1802 // instead of copy_u.w instructions. This makes no difference to the
1803 // behaviour since i64 is only illegal when the register file is 32-bit.
1804 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1805 Op->getValueType(0), Op->getOperand(1),
1806 Op->getOperand(2));
1807 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001808 case Intrinsic::mips_div_s_b:
1809 case Intrinsic::mips_div_s_h:
1810 case Intrinsic::mips_div_s_w:
1811 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001812 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1813 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001814 case Intrinsic::mips_div_u_b:
1815 case Intrinsic::mips_div_u_h:
1816 case Intrinsic::mips_div_u_w:
1817 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001818 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1819 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001820 case Intrinsic::mips_fadd_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001821 case Intrinsic::mips_fadd_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001822 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001823 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1824 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001825 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1826 case Intrinsic::mips_fceq_w:
1827 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001828 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001829 Op->getOperand(2), ISD::SETOEQ);
1830 case Intrinsic::mips_fcle_w:
1831 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001832 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001833 Op->getOperand(2), ISD::SETOLE);
1834 case Intrinsic::mips_fclt_w:
1835 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001836 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001837 Op->getOperand(2), ISD::SETOLT);
1838 case Intrinsic::mips_fcne_w:
1839 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001840 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001841 Op->getOperand(2), ISD::SETONE);
1842 case Intrinsic::mips_fcor_w:
1843 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001844 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001845 Op->getOperand(2), ISD::SETO);
1846 case Intrinsic::mips_fcueq_w:
1847 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001848 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001849 Op->getOperand(2), ISD::SETUEQ);
1850 case Intrinsic::mips_fcule_w:
1851 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001852 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001853 Op->getOperand(2), ISD::SETULE);
1854 case Intrinsic::mips_fcult_w:
1855 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001856 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001857 Op->getOperand(2), ISD::SETULT);
1858 case Intrinsic::mips_fcun_w:
1859 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001860 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001861 Op->getOperand(2), ISD::SETUO);
1862 case Intrinsic::mips_fcune_w:
1863 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001864 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001865 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001866 case Intrinsic::mips_fdiv_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001867 case Intrinsic::mips_fdiv_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001868 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001869 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1870 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001871 case Intrinsic::mips_ffint_u_w:
1872 case Intrinsic::mips_ffint_u_d:
1873 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1874 Op->getOperand(1));
1875 case Intrinsic::mips_ffint_s_w:
1876 case Intrinsic::mips_ffint_s_d:
1877 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1878 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001879 case Intrinsic::mips_fill_b:
1880 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001881 case Intrinsic::mips_fill_w:
1882 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001883 EVT ResTy = Op->getValueType(0);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001884 SmallVector<SDValue, 16> Ops(ResTy.getVectorNumElements(),
1885 Op->getOperand(1));
Daniel Sandersf49dd822013-09-24 13:33:07 +00001886
Daniel Sandersc72593e2013-09-27 13:20:41 +00001887 // If ResTy is v2i64 then the type legalizer will break this node down into
1888 // an equivalent v4i32.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001889 return DAG.getBuildVector(ResTy, DL, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001890 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001891 case Intrinsic::mips_fexp2_w:
1892 case Intrinsic::mips_fexp2_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001893 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sandersa9521602013-10-23 10:36:52 +00001894 EVT ResTy = Op->getValueType(0);
1895 return DAG.getNode(
1896 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1897 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1898 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001899 case Intrinsic::mips_flog2_w:
1900 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001901 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001902 case Intrinsic::mips_fmadd_w:
1903 case Intrinsic::mips_fmadd_d:
1904 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1905 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001906 case Intrinsic::mips_fmul_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001907 case Intrinsic::mips_fmul_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001908 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001909 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1910 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001911 case Intrinsic::mips_fmsub_w:
1912 case Intrinsic::mips_fmsub_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001913 // TODO: If intrinsics have fast-math-flags, propagate them.
Aleksandar Beserminji3546c162018-04-27 13:30:27 +00001914 return DAG.getNode(MipsISD::FMS, SDLoc(Op), Op->getValueType(0),
1915 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001916 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001917 case Intrinsic::mips_frint_w:
1918 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001919 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001920 case Intrinsic::mips_fsqrt_w:
1921 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001922 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001923 case Intrinsic::mips_fsub_w:
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001924 case Intrinsic::mips_fsub_d:
Sanjay Patela2607012015-09-16 16:31:21 +00001925 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001926 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1927 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001928 case Intrinsic::mips_ftrunc_u_w:
1929 case Intrinsic::mips_ftrunc_u_d:
1930 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1931 Op->getOperand(1));
1932 case Intrinsic::mips_ftrunc_s_w:
1933 case Intrinsic::mips_ftrunc_s_d:
1934 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1935 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001936 case Intrinsic::mips_ilvev_b:
1937 case Intrinsic::mips_ilvev_h:
1938 case Intrinsic::mips_ilvev_w:
1939 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001940 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001941 Op->getOperand(1), Op->getOperand(2));
1942 case Intrinsic::mips_ilvl_b:
1943 case Intrinsic::mips_ilvl_h:
1944 case Intrinsic::mips_ilvl_w:
1945 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001946 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001947 Op->getOperand(1), Op->getOperand(2));
1948 case Intrinsic::mips_ilvod_b:
1949 case Intrinsic::mips_ilvod_h:
1950 case Intrinsic::mips_ilvod_w:
1951 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001952 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001953 Op->getOperand(1), Op->getOperand(2));
1954 case Intrinsic::mips_ilvr_b:
1955 case Intrinsic::mips_ilvr_h:
1956 case Intrinsic::mips_ilvr_w:
1957 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001958 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001959 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001960 case Intrinsic::mips_insert_b:
1961 case Intrinsic::mips_insert_h:
1962 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001963 case Intrinsic::mips_insert_d:
1964 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1965 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001966 case Intrinsic::mips_insve_b:
1967 case Intrinsic::mips_insve_h:
1968 case Intrinsic::mips_insve_w:
Simon Dardis548a53f2017-01-10 16:40:57 +00001969 case Intrinsic::mips_insve_d: {
1970 // Report an error for out of range values.
1971 int64_t Max;
1972 switch (Intrinsic) {
1973 case Intrinsic::mips_insve_b: Max = 15; break;
1974 case Intrinsic::mips_insve_h: Max = 7; break;
1975 case Intrinsic::mips_insve_w: Max = 3; break;
1976 case Intrinsic::mips_insve_d: Max = 1; break;
1977 default: llvm_unreachable("Unmatched intrinsic");
1978 }
1979 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
1980 if (Value < 0 || Value > Max)
1981 report_fatal_error("Immediate out of range");
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001982 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1983 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001984 DAG.getConstant(0, DL, MVT::i32));
Simon Dardis548a53f2017-01-10 16:40:57 +00001985 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00001986 case Intrinsic::mips_ldi_b:
1987 case Intrinsic::mips_ldi_h:
1988 case Intrinsic::mips_ldi_w:
1989 case Intrinsic::mips_ldi_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00001990 return lowerMSASplatImm(Op, 1, DAG, true);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001991 case Intrinsic::mips_lsa:
1992 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001993 EVT ResTy = Op->getValueType(0);
1994 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1995 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1996 Op->getOperand(2), Op->getOperand(3)));
1997 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001998 case Intrinsic::mips_maddv_b:
1999 case Intrinsic::mips_maddv_h:
2000 case Intrinsic::mips_maddv_w:
2001 case Intrinsic::mips_maddv_d: {
2002 EVT ResTy = Op->getValueType(0);
2003 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
2004 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2005 Op->getOperand(2), Op->getOperand(3)));
2006 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00002007 case Intrinsic::mips_max_s_b:
2008 case Intrinsic::mips_max_s_h:
2009 case Intrinsic::mips_max_s_w:
2010 case Intrinsic::mips_max_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002011 return DAG.getNode(ISD::SMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002012 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002013 case Intrinsic::mips_max_u_b:
2014 case Intrinsic::mips_max_u_h:
2015 case Intrinsic::mips_max_u_w:
2016 case Intrinsic::mips_max_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002017 return DAG.getNode(ISD::UMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002018 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002019 case Intrinsic::mips_maxi_s_b:
2020 case Intrinsic::mips_maxi_s_h:
2021 case Intrinsic::mips_maxi_s_w:
2022 case Intrinsic::mips_maxi_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002023 return DAG.getNode(ISD::SMAX, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00002024 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002025 case Intrinsic::mips_maxi_u_b:
2026 case Intrinsic::mips_maxi_u_h:
2027 case Intrinsic::mips_maxi_u_w:
2028 case Intrinsic::mips_maxi_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002029 return DAG.getNode(ISD::UMAX, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002030 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002031 case Intrinsic::mips_min_s_b:
2032 case Intrinsic::mips_min_s_h:
2033 case Intrinsic::mips_min_s_w:
2034 case Intrinsic::mips_min_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002035 return DAG.getNode(ISD::SMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002036 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002037 case Intrinsic::mips_min_u_b:
2038 case Intrinsic::mips_min_u_h:
2039 case Intrinsic::mips_min_u_w:
2040 case Intrinsic::mips_min_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002041 return DAG.getNode(ISD::UMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002042 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002043 case Intrinsic::mips_mini_s_b:
2044 case Intrinsic::mips_mini_s_h:
2045 case Intrinsic::mips_mini_s_w:
2046 case Intrinsic::mips_mini_s_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002047 return DAG.getNode(ISD::SMIN, DL, Op->getValueType(0),
Simon Dardis548a53f2017-01-10 16:40:57 +00002048 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG, true));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002049 case Intrinsic::mips_mini_u_b:
2050 case Intrinsic::mips_mini_u_h:
2051 case Intrinsic::mips_mini_u_w:
2052 case Intrinsic::mips_mini_u_d:
Simon Pilgrim386b8dd2018-02-17 21:29:45 +00002053 return DAG.getNode(ISD::UMIN, DL, Op->getValueType(0),
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002054 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00002055 case Intrinsic::mips_mod_s_b:
2056 case Intrinsic::mips_mod_s_h:
2057 case Intrinsic::mips_mod_s_w:
2058 case Intrinsic::mips_mod_s_d:
2059 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
2060 Op->getOperand(2));
2061 case Intrinsic::mips_mod_u_b:
2062 case Intrinsic::mips_mod_u_h:
2063 case Intrinsic::mips_mod_u_w:
2064 case Intrinsic::mips_mod_u_d:
2065 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
2066 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002067 case Intrinsic::mips_mulv_b:
2068 case Intrinsic::mips_mulv_h:
2069 case Intrinsic::mips_mulv_w:
2070 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002071 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
2072 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00002073 case Intrinsic::mips_msubv_b:
2074 case Intrinsic::mips_msubv_h:
2075 case Intrinsic::mips_msubv_w:
2076 case Intrinsic::mips_msubv_d: {
2077 EVT ResTy = Op->getValueType(0);
2078 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
2079 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2080 Op->getOperand(2), Op->getOperand(3)));
2081 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002082 case Intrinsic::mips_nlzc_b:
2083 case Intrinsic::mips_nlzc_h:
2084 case Intrinsic::mips_nlzc_w:
2085 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002086 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002087 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002088 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2089 Op->getOperand(1), Op->getOperand(2));
2090 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002091 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002092 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002093 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2094 Op->getOperand(1),
2095 lowerMSASplatImm(Op, 2, DAG));
2096 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002097 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002098 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002099 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2100 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002101 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002102 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2103 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002104 case Intrinsic::mips_pckev_b:
2105 case Intrinsic::mips_pckev_h:
2106 case Intrinsic::mips_pckev_w:
2107 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002108 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002109 Op->getOperand(1), Op->getOperand(2));
2110 case Intrinsic::mips_pckod_b:
2111 case Intrinsic::mips_pckod_h:
2112 case Intrinsic::mips_pckod_w:
2113 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002114 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002115 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002116 case Intrinsic::mips_pcnt_b:
2117 case Intrinsic::mips_pcnt_h:
2118 case Intrinsic::mips_pcnt_w:
2119 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002120 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002121 case Intrinsic::mips_sat_s_b:
2122 case Intrinsic::mips_sat_s_h:
2123 case Intrinsic::mips_sat_s_w:
2124 case Intrinsic::mips_sat_s_d:
2125 case Intrinsic::mips_sat_u_b:
2126 case Intrinsic::mips_sat_u_h:
2127 case Intrinsic::mips_sat_u_w:
2128 case Intrinsic::mips_sat_u_d: {
2129 // Report an error for out of range values.
2130 int64_t Max;
2131 switch (Intrinsic) {
2132 case Intrinsic::mips_sat_s_b:
2133 case Intrinsic::mips_sat_u_b: Max = 7; break;
2134 case Intrinsic::mips_sat_s_h:
2135 case Intrinsic::mips_sat_u_h: Max = 15; break;
2136 case Intrinsic::mips_sat_s_w:
2137 case Intrinsic::mips_sat_u_w: Max = 31; break;
2138 case Intrinsic::mips_sat_s_d:
2139 case Intrinsic::mips_sat_u_d: Max = 63; break;
2140 default: llvm_unreachable("Unmatched intrinsic");
2141 }
2142 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2143 if (Value < 0 || Value > Max)
2144 report_fatal_error("Immediate out of range");
2145 return SDValue();
2146 }
Daniel Sanders26307182013-09-24 14:20:00 +00002147 case Intrinsic::mips_shf_b:
2148 case Intrinsic::mips_shf_h:
Simon Dardis548a53f2017-01-10 16:40:57 +00002149 case Intrinsic::mips_shf_w: {
2150 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2151 if (Value < 0 || Value > 255)
2152 report_fatal_error("Immediate out of range");
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002153 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002154 Op->getOperand(2), Op->getOperand(1));
Simon Dardis548a53f2017-01-10 16:40:57 +00002155 }
2156 case Intrinsic::mips_sldi_b:
2157 case Intrinsic::mips_sldi_h:
2158 case Intrinsic::mips_sldi_w:
2159 case Intrinsic::mips_sldi_d: {
2160 // Report an error for out of range values.
2161 int64_t Max;
2162 switch (Intrinsic) {
2163 case Intrinsic::mips_sldi_b: Max = 15; break;
2164 case Intrinsic::mips_sldi_h: Max = 7; break;
2165 case Intrinsic::mips_sldi_w: Max = 3; break;
2166 case Intrinsic::mips_sldi_d: Max = 1; break;
2167 default: llvm_unreachable("Unmatched intrinsic");
2168 }
2169 int64_t Value = cast<ConstantSDNode>(Op->getOperand(3))->getSExtValue();
2170 if (Value < 0 || Value > Max)
2171 report_fatal_error("Immediate out of range");
2172 return SDValue();
2173 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002174 case Intrinsic::mips_sll_b:
2175 case Intrinsic::mips_sll_h:
2176 case Intrinsic::mips_sll_w:
2177 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002178 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002179 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002180 case Intrinsic::mips_slli_b:
2181 case Intrinsic::mips_slli_h:
2182 case Intrinsic::mips_slli_w:
2183 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002184 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2185 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002186 case Intrinsic::mips_splat_b:
2187 case Intrinsic::mips_splat_h:
2188 case Intrinsic::mips_splat_w:
2189 case Intrinsic::mips_splat_d:
2190 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2191 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2192 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2193 // Instead we lower to MipsISD::VSHF and match from there.
2194 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002195 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002196 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002197 case Intrinsic::mips_splati_b:
2198 case Intrinsic::mips_splati_h:
2199 case Intrinsic::mips_splati_w:
2200 case Intrinsic::mips_splati_d:
2201 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2202 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2203 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002204 case Intrinsic::mips_sra_b:
2205 case Intrinsic::mips_sra_h:
2206 case Intrinsic::mips_sra_w:
2207 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002208 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002209 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002210 case Intrinsic::mips_srai_b:
2211 case Intrinsic::mips_srai_h:
2212 case Intrinsic::mips_srai_w:
2213 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002214 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2215 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002216 case Intrinsic::mips_srari_b:
2217 case Intrinsic::mips_srari_h:
2218 case Intrinsic::mips_srari_w:
2219 case Intrinsic::mips_srari_d: {
2220 // Report an error for out of range values.
2221 int64_t Max;
2222 switch (Intrinsic) {
2223 case Intrinsic::mips_srari_b: Max = 7; break;
2224 case Intrinsic::mips_srari_h: Max = 15; break;
2225 case Intrinsic::mips_srari_w: Max = 31; break;
2226 case Intrinsic::mips_srari_d: Max = 63; break;
2227 default: llvm_unreachable("Unmatched intrinsic");
2228 }
2229 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2230 if (Value < 0 || Value > Max)
2231 report_fatal_error("Immediate out of range");
2232 return SDValue();
2233 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002234 case Intrinsic::mips_srl_b:
2235 case Intrinsic::mips_srl_h:
2236 case Intrinsic::mips_srl_w:
2237 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002238 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
Petar Jovanovic2b6fe3f2017-04-20 13:26:46 +00002239 truncateVecElts(Op, DAG));
Daniel Sanderscba19222013-09-24 10:28:18 +00002240 case Intrinsic::mips_srli_b:
2241 case Intrinsic::mips_srli_h:
2242 case Intrinsic::mips_srli_w:
2243 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002244 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2245 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Simon Dardis548a53f2017-01-10 16:40:57 +00002246 case Intrinsic::mips_srlri_b:
2247 case Intrinsic::mips_srlri_h:
2248 case Intrinsic::mips_srlri_w:
2249 case Intrinsic::mips_srlri_d: {
2250 // Report an error for out of range values.
2251 int64_t Max;
2252 switch (Intrinsic) {
2253 case Intrinsic::mips_srlri_b: Max = 7; break;
2254 case Intrinsic::mips_srlri_h: Max = 15; break;
2255 case Intrinsic::mips_srlri_w: Max = 31; break;
2256 case Intrinsic::mips_srlri_d: Max = 63; break;
2257 default: llvm_unreachable("Unmatched intrinsic");
2258 }
2259 int64_t Value = cast<ConstantSDNode>(Op->getOperand(2))->getSExtValue();
2260 if (Value < 0 || Value > Max)
2261 report_fatal_error("Immediate out of range");
2262 return SDValue();
2263 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002264 case Intrinsic::mips_subv_b:
2265 case Intrinsic::mips_subv_h:
2266 case Intrinsic::mips_subv_w:
2267 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002268 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2269 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002270 case Intrinsic::mips_subvi_b:
2271 case Intrinsic::mips_subvi_h:
2272 case Intrinsic::mips_subvi_w:
2273 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002274 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2275 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002276 case Intrinsic::mips_vshf_b:
2277 case Intrinsic::mips_vshf_h:
2278 case Intrinsic::mips_vshf_w:
2279 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002280 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002281 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002282 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002283 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2284 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002285 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002286 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2287 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Marcin Koscielnicki7efdca52016-04-27 17:21:49 +00002288 case Intrinsic::thread_pointer: {
2289 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2290 return DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2291 }
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002292 }
2293}
2294
Simon Dardis548a53f2017-01-10 16:40:57 +00002295static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2296 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002297 SDLoc DL(Op);
2298 SDValue ChainIn = Op->getOperand(0);
2299 SDValue Address = Op->getOperand(2);
2300 SDValue Offset = Op->getOperand(3);
2301 EVT ResTy = Op->getValueType(0);
2302 EVT PtrTy = Address->getValueType(0);
2303
Simon Dardis548a53f2017-01-10 16:40:57 +00002304 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2305 // however takes an i32 signed constant offset. The actual type of the
2306 // intrinsic is a scaled signed i10.
2307 if (Subtarget.isABI_N64())
2308 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2309
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002310 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002311 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(),
2312 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002313}
2314
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002315SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2316 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002317 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2318 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002319 default:
2320 return SDValue();
2321 case Intrinsic::mips_extp:
2322 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2323 case Intrinsic::mips_extpdp:
2324 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2325 case Intrinsic::mips_extr_w:
2326 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2327 case Intrinsic::mips_extr_r_w:
2328 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2329 case Intrinsic::mips_extr_rs_w:
2330 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2331 case Intrinsic::mips_extr_s_h:
2332 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2333 case Intrinsic::mips_mthlip:
2334 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2335 case Intrinsic::mips_mulsaq_s_w_ph:
2336 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2337 case Intrinsic::mips_maq_s_w_phl:
2338 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2339 case Intrinsic::mips_maq_s_w_phr:
2340 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2341 case Intrinsic::mips_maq_sa_w_phl:
2342 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2343 case Intrinsic::mips_maq_sa_w_phr:
2344 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2345 case Intrinsic::mips_dpaq_s_w_ph:
2346 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2347 case Intrinsic::mips_dpsq_s_w_ph:
2348 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2349 case Intrinsic::mips_dpaq_sa_l_w:
2350 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2351 case Intrinsic::mips_dpsq_sa_l_w:
2352 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2353 case Intrinsic::mips_dpaqx_s_w_ph:
2354 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2355 case Intrinsic::mips_dpaqx_sa_w_ph:
2356 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2357 case Intrinsic::mips_dpsqx_s_w_ph:
2358 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2359 case Intrinsic::mips_dpsqx_sa_w_ph:
2360 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002361 case Intrinsic::mips_ld_b:
2362 case Intrinsic::mips_ld_h:
2363 case Intrinsic::mips_ld_w:
2364 case Intrinsic::mips_ld_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002365 return lowerMSALoadIntr(Op, DAG, Intr, Subtarget);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002366 }
2367}
2368
Simon Dardis548a53f2017-01-10 16:40:57 +00002369static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr,
2370 const MipsSubtarget &Subtarget) {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002371 SDLoc DL(Op);
2372 SDValue ChainIn = Op->getOperand(0);
2373 SDValue Value = Op->getOperand(2);
2374 SDValue Address = Op->getOperand(3);
2375 SDValue Offset = Op->getOperand(4);
2376 EVT PtrTy = Address->getValueType(0);
2377
Simon Dardis548a53f2017-01-10 16:40:57 +00002378 // For N64 addresses have the underlying type MVT::i64. This intrinsic
2379 // however takes an i32 signed constant offset. The actual type of the
2380 // intrinsic is a scaled signed i10.
2381 if (Subtarget.isABI_N64())
2382 Offset = DAG.getNode(ISD::SIGN_EXTEND, DL, PtrTy, Offset);
2383
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002384 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2385
Justin Lebar9c375812016-07-15 18:27:10 +00002386 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(),
2387 /* Alignment = */ 16);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002388}
2389
2390SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2391 SelectionDAG &DAG) const {
2392 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2393 switch (Intr) {
2394 default:
2395 return SDValue();
2396 case Intrinsic::mips_st_b:
2397 case Intrinsic::mips_st_h:
2398 case Intrinsic::mips_st_w:
2399 case Intrinsic::mips_st_d:
Simon Dardis548a53f2017-01-10 16:40:57 +00002400 return lowerMSAStoreIntr(Op, DAG, Intr, Subtarget);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002401 }
2402}
2403
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002404// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2405//
2406// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2407// choose to sign-extend but we could have equally chosen zero-extend. The
2408// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2409// result into this node later (possibly changing it to a zero-extend in the
2410// process).
2411SDValue MipsSETargetLowering::
2412lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2413 SDLoc DL(Op);
2414 EVT ResTy = Op->getValueType(0);
2415 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002416 EVT VecTy = Op0->getValueType(0);
2417
2418 if (!VecTy.is128BitVector())
2419 return SDValue();
2420
2421 if (ResTy.isInteger()) {
2422 SDValue Op1 = Op->getOperand(1);
2423 EVT EltTy = VecTy.getVectorElementType();
2424 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2425 DAG.getValueType(EltTy));
2426 }
2427
2428 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002429}
2430
Daniel Sandersf49dd822013-09-24 13:33:07 +00002431static bool isConstantOrUndef(const SDValue Op) {
Sanjay Patel57195842016-03-14 17:28:46 +00002432 if (Op->isUndef())
Daniel Sandersf49dd822013-09-24 13:33:07 +00002433 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002434 if (isa<ConstantSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002435 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002436 if (isa<ConstantFPSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002437 return true;
2438 return false;
2439}
2440
2441static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2442 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2443 if (isConstantOrUndef(Op->getOperand(i)))
2444 return true;
2445 return false;
2446}
2447
Daniel Sanders7a289d02013-09-23 12:02:46 +00002448// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2449// backend.
2450//
2451// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002452// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2453// 2 less than or equal to 64 and the value fits into a signed 10-bit
2454// immediate
2455// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2456// is a power of 2 less than or equal to 64 and the value does not fit into a
2457// signed 10-bit immediate
2458// - Non-constant splats are legal as-is.
2459// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2460// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002461SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2462 SelectionDAG &DAG) const {
2463 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2464 EVT ResTy = Op->getValueType(0);
2465 SDLoc DL(Op);
2466 APInt SplatValue, SplatUndef;
2467 unsigned SplatBitSize;
2468 bool HasAnyUndefs;
2469
Eric Christopher1c29a652014-07-18 22:55:25 +00002470 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002471 return SDValue();
2472
2473 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2474 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002475 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002476 // We can only cope with 8, 16, 32, or 64-bit elements
2477 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2478 SplatBitSize != 64)
2479 return SDValue();
2480
Simon Dardis7090d142017-03-10 13:27:14 +00002481 // If the value isn't an integer type we will have to bitcast
2482 // from an integer type first. Also, if there are any undefs, we must
2483 // lower them to defined values first.
2484 if (ResTy.isInteger() && !HasAnyUndefs)
Daniel Sandersf49dd822013-09-24 13:33:07 +00002485 return Op;
2486
2487 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002488
2489 switch (SplatBitSize) {
2490 default:
2491 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002492 case 8:
2493 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002494 break;
2495 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002496 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002497 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002498 case 32:
2499 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002500 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002501 case 64:
2502 // There's no fill.d to fall back on for 64-bit values
2503 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002504 }
2505
Daniel Sanders50b80412013-11-15 12:56:49 +00002506 // SelectionDAG::getConstant will promote SplatValue appropriately.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002507 SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002508
Daniel Sanders50b80412013-11-15 12:56:49 +00002509 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002510 if (ViaVecTy != ResTy)
2511 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002512
2513 return Result;
Simon Pilgrimeb508f82018-12-12 18:32:29 +00002514 } else if (DAG.isSplatValue(Op, /* AllowUndefs */ false))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002515 return Op;
2516 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002517 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2518 // The resulting code is the same length as the expansion, but it doesn't
2519 // use memory operations
2520 EVT ResTy = Node->getValueType(0);
2521
2522 assert(ResTy.isVector());
2523
2524 unsigned NumElts = ResTy.getVectorNumElements();
2525 SDValue Vector = DAG.getUNDEF(ResTy);
2526 for (unsigned i = 0; i < NumElts; ++i) {
2527 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2528 Node->getOperand(i),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002529 DAG.getConstant(i, DL, MVT::i32));
Daniel Sandersf86622b2013-09-24 13:16:15 +00002530 }
2531 return Vector;
2532 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002533
2534 return SDValue();
2535}
2536
Daniel Sanders26307182013-09-24 14:20:00 +00002537// Lower VECTOR_SHUFFLE into SHF (if possible).
2538//
2539// SHF splits the vector into blocks of four elements, then shuffles these
2540// elements according to a <4 x i2> constant (encoded as an integer immediate).
2541//
2542// It is therefore possible to lower into SHF when the mask takes the form:
2543// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2544// When undef's appear they are treated as if they were whatever value is
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002545// necessary in order to fit the above forms.
Daniel Sanders26307182013-09-24 14:20:00 +00002546//
2547// For example:
2548// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2549// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2550// i32 7, i32 6, i32 5, i32 4>
2551// is lowered to:
2552// (SHF_H $w0, $w1, 27)
2553// where the 27 comes from:
2554// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2555static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2556 SmallVector<int, 16> Indices,
2557 SelectionDAG &DAG) {
2558 int SHFIndices[4] = { -1, -1, -1, -1 };
2559
2560 if (Indices.size() < 4)
2561 return SDValue();
2562
2563 for (unsigned i = 0; i < 4; ++i) {
2564 for (unsigned j = i; j < Indices.size(); j += 4) {
2565 int Idx = Indices[j];
2566
2567 // Convert from vector index to 4-element subvector index
2568 // If an index refers to an element outside of the subvector then give up
2569 if (Idx != -1) {
2570 Idx -= 4 * (j / 4);
2571 if (Idx < 0 || Idx >= 4)
2572 return SDValue();
2573 }
2574
2575 // If the mask has an undef, replace it with the current index.
2576 // Note that it might still be undef if the current index is also undef
2577 if (SHFIndices[i] == -1)
2578 SHFIndices[i] = Idx;
2579
2580 // Check that non-undef values are the same as in the mask. If they
2581 // aren't then give up
2582 if (!(Idx == -1 || Idx == SHFIndices[i]))
2583 return SDValue();
2584 }
2585 }
2586
2587 // Calculate the immediate. Replace any remaining undefs with zero
2588 APInt Imm(32, 0);
2589 for (int i = 3; i >= 0; --i) {
2590 int Idx = SHFIndices[i];
2591
2592 if (Idx == -1)
2593 Idx = 0;
2594
2595 Imm <<= 2;
2596 Imm |= Idx & 0x3;
2597 }
2598
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002599 SDLoc DL(Op);
2600 return DAG.getNode(MipsISD::SHF, DL, ResTy,
2601 DAG.getConstant(Imm, DL, MVT::i32), Op->getOperand(0));
Daniel Sanders26307182013-09-24 14:20:00 +00002602}
2603
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002604/// Determine whether a range fits a regular pattern of values.
2605/// This function accounts for the possibility of jumping over the End iterator.
2606template <typename ValType>
2607static bool
2608fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,
2609 unsigned CheckStride,
2610 typename SmallVectorImpl<ValType>::const_iterator End,
2611 ValType ExpectedIndex, unsigned ExpectedIndexStride) {
2612 auto &I = Begin;
2613
2614 while (I != End) {
2615 if (*I != -1 && *I != ExpectedIndex)
2616 return false;
2617 ExpectedIndex += ExpectedIndexStride;
2618
2619 // Incrementing past End is undefined behaviour so we must increment one
2620 // step at a time and check for End at each step.
2621 for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
2622 ; // Empty loop body.
2623 }
2624 return true;
2625}
2626
2627// Determine whether VECTOR_SHUFFLE is a SPLATI.
2628//
2629// It is a SPLATI when the mask is:
2630// <x, x, x, ...>
2631// where x is any valid index.
2632//
2633// When undef's appear in the mask they are treated as if they were whatever
2634// value is necessary in order to fit the above form.
2635static bool isVECTOR_SHUFFLE_SPLATI(SDValue Op, EVT ResTy,
2636 SmallVector<int, 16> Indices,
2637 SelectionDAG &DAG) {
2638 assert((Indices.size() % 2) == 0);
2639
2640 int SplatIndex = -1;
2641 for (const auto &V : Indices) {
2642 if (V != -1) {
2643 SplatIndex = V;
2644 break;
2645 }
2646 }
2647
2648 return fitsRegularPattern<int>(Indices.begin(), 1, Indices.end(), SplatIndex,
2649 0);
2650}
2651
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002652// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2653//
2654// ILVEV interleaves the even elements from each vector.
2655//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002656// It is possible to lower into ILVEV when the mask consists of two of the
2657// following forms interleaved:
2658// <0, 2, 4, ...>
2659// <n, n+2, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002660// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002661// For example:
2662// <0, 0, 2, 2, 4, 4, ...>
2663// <0, n, 2, n+2, 4, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002664//
2665// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002666// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002667static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2668 SmallVector<int, 16> Indices,
2669 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002670 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002671
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002672 SDValue Wt;
2673 SDValue Ws;
2674 const auto &Begin = Indices.begin();
2675 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002676
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002677 // Check even elements are taken from the even elements of one half or the
2678 // other and pick an operand accordingly.
2679 if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
2680 Wt = Op->getOperand(0);
2681 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 2))
2682 Wt = Op->getOperand(1);
2683 else
2684 return SDValue();
2685
2686 // Check odd elements are taken from the even elements of one half or the
2687 // other and pick an operand accordingly.
2688 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
2689 Ws = Op->getOperand(0);
2690 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 2))
2691 Ws = Op->getOperand(1);
2692 else
2693 return SDValue();
2694
2695 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002696}
2697
2698// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2699//
2700// ILVOD interleaves the odd elements from each vector.
2701//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002702// It is possible to lower into ILVOD when the mask consists of two of the
2703// following forms interleaved:
2704// <1, 3, 5, ...>
2705// <n+1, n+3, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002706// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002707// For example:
2708// <1, 1, 3, 3, 5, 5, ...>
2709// <1, n+1, 3, n+3, 5, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002710//
2711// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002712// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002713static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2714 SmallVector<int, 16> Indices,
2715 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002716 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002717
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002718 SDValue Wt;
2719 SDValue Ws;
2720 const auto &Begin = Indices.begin();
2721 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002722
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002723 // Check even elements are taken from the odd elements of one half or the
2724 // other and pick an operand accordingly.
2725 if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
2726 Wt = Op->getOperand(0);
2727 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + 1, 2))
2728 Wt = Op->getOperand(1);
2729 else
2730 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002731
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002732 // Check odd elements are taken from the odd elements of one half or the
2733 // other and pick an operand accordingly.
2734 if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
2735 Ws = Op->getOperand(0);
2736 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + 1, 2))
2737 Ws = Op->getOperand(1);
2738 else
2739 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002740
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002741 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Wt, Ws);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002742}
2743
2744// Lower VECTOR_SHUFFLE into ILVR (if possible).
2745//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002746// ILVR interleaves consecutive elements from the right (lowest-indexed) half of
2747// each vector.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002748//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002749// It is possible to lower into ILVR when the mask consists of two of the
2750// following forms interleaved:
2751// <0, 1, 2, ...>
2752// <n, n+1, n+2, ...>
2753// where n is the number of elements in the vector.
2754// For example:
2755// <0, 0, 1, 1, 2, 2, ...>
2756// <0, n, 1, n+1, 2, n+2, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002757//
2758// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002759// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002760static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2761 SmallVector<int, 16> Indices,
2762 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002763 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002764
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002765 SDValue Wt;
2766 SDValue Ws;
2767 const auto &Begin = Indices.begin();
2768 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002769
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002770 // Check even elements are taken from the right (lowest-indexed) elements of
2771 // one half or the other and pick an operand accordingly.
2772 if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
2773 Wt = Op->getOperand(0);
2774 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 1))
2775 Wt = Op->getOperand(1);
2776 else
2777 return SDValue();
2778
2779 // Check odd elements are taken from the right (lowest-indexed) elements of
2780 // one half or the other and pick an operand accordingly.
2781 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
2782 Ws = Op->getOperand(0);
2783 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 1))
2784 Ws = Op->getOperand(1);
2785 else
2786 return SDValue();
2787
2788 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Ws, Wt);
2789}
2790
2791// Lower VECTOR_SHUFFLE into ILVL (if possible).
2792//
2793// ILVL interleaves consecutive elements from the left (highest-indexed) half
2794// of each vector.
2795//
2796// It is possible to lower into ILVL when the mask consists of two of the
2797// following forms interleaved:
2798// <x, x+1, x+2, ...>
2799// <n+x, n+x+1, n+x+2, ...>
2800// where n is the number of elements in the vector and x is half n.
2801// For example:
2802// <x, x, x+1, x+1, x+2, x+2, ...>
2803// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2804//
2805// When undef's appear in the mask they are treated as if they were whatever
2806// value is necessary in order to fit the above forms.
2807static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2808 SmallVector<int, 16> Indices,
2809 SelectionDAG &DAG) {
2810 assert((Indices.size() % 2) == 0);
2811
2812 unsigned HalfSize = Indices.size() / 2;
2813 SDValue Wt;
2814 SDValue Ws;
2815 const auto &Begin = Indices.begin();
2816 const auto &End = Indices.end();
2817
2818 // Check even elements are taken from the left (highest-indexed) elements of
2819 // one half or the other and pick an operand accordingly.
2820 if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
2821 Wt = Op->getOperand(0);
2822 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + HalfSize, 1))
2823 Wt = Op->getOperand(1);
2824 else
2825 return SDValue();
2826
2827 // Check odd elements are taken from the left (highest-indexed) elements of
2828 // one half or the other and pick an operand accordingly.
2829 if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
2830 Ws = Op->getOperand(0);
2831 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + HalfSize,
2832 1))
2833 Ws = Op->getOperand(1);
2834 else
2835 return SDValue();
2836
2837 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002838}
2839
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002840// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2841//
2842// PCKEV copies the even elements of each vector into the result vector.
2843//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002844// It is possible to lower into PCKEV when the mask consists of two of the
2845// following forms concatenated:
2846// <0, 2, 4, ...>
2847// <n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002848// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002849// For example:
2850// <0, 2, 4, ..., 0, 2, 4, ...>
2851// <0, 2, 4, ..., n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002852//
2853// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002854// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002855static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2856 SmallVector<int, 16> Indices,
2857 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002858 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002859
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002860 SDValue Wt;
2861 SDValue Ws;
2862 const auto &Begin = Indices.begin();
2863 const auto &Mid = Indices.begin() + Indices.size() / 2;
2864 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002865
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002866 if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
2867 Wt = Op->getOperand(0);
2868 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size(), 2))
2869 Wt = Op->getOperand(1);
2870 else
2871 return SDValue();
2872
2873 if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
2874 Ws = Op->getOperand(0);
2875 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size(), 2))
2876 Ws = Op->getOperand(1);
2877 else
2878 return SDValue();
2879
2880 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002881}
2882
2883// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2884//
2885// PCKOD copies the odd elements of each vector into the result vector.
2886//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002887// It is possible to lower into PCKOD when the mask consists of two of the
2888// following forms concatenated:
2889// <1, 3, 5, ...>
2890// <n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002891// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002892// For example:
2893// <1, 3, 5, ..., 1, 3, 5, ...>
2894// <1, 3, 5, ..., n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002895//
2896// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002897// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002898static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2899 SmallVector<int, 16> Indices,
2900 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002901 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002902
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002903 SDValue Wt;
2904 SDValue Ws;
2905 const auto &Begin = Indices.begin();
2906 const auto &Mid = Indices.begin() + Indices.size() / 2;
2907 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002908
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002909 if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
2910 Wt = Op->getOperand(0);
2911 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size() + 1, 2))
2912 Wt = Op->getOperand(1);
2913 else
2914 return SDValue();
2915
2916 if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
2917 Ws = Op->getOperand(0);
2918 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size() + 1, 2))
2919 Ws = Op->getOperand(1);
2920 else
2921 return SDValue();
2922
2923 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002924}
2925
Daniel Sanderse5087042013-09-24 14:02:15 +00002926// Lower VECTOR_SHUFFLE into VSHF.
2927//
2928// This mostly consists of converting the shuffle indices in Indices into a
2929// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2930// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2931// if the type is v8i16 and all the indices are less than 8 then the second
2932// operand is unused and can be replaced with anything. We choose to replace it
2933// with the used operand since this reduces the number of instructions overall.
2934static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2935 SmallVector<int, 16> Indices,
2936 SelectionDAG &DAG) {
2937 SmallVector<SDValue, 16> Ops;
2938 SDValue Op0;
2939 SDValue Op1;
2940 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2941 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2942 bool Using1stVec = false;
2943 bool Using2ndVec = false;
2944 SDLoc DL(Op);
2945 int ResTyNumElts = ResTy.getVectorNumElements();
2946
2947 for (int i = 0; i < ResTyNumElts; ++i) {
2948 // Idx == -1 means UNDEF
2949 int Idx = Indices[i];
2950
2951 if (0 <= Idx && Idx < ResTyNumElts)
2952 Using1stVec = true;
2953 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2954 Using2ndVec = true;
2955 }
2956
2957 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2958 ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002959 Ops.push_back(DAG.getTargetConstant(*I, DL, MaskEltTy));
Daniel Sanderse5087042013-09-24 14:02:15 +00002960
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002961 SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002962
2963 if (Using1stVec && Using2ndVec) {
2964 Op0 = Op->getOperand(0);
2965 Op1 = Op->getOperand(1);
2966 } else if (Using1stVec)
2967 Op0 = Op1 = Op->getOperand(0);
2968 else if (Using2ndVec)
2969 Op0 = Op1 = Op->getOperand(1);
2970 else
2971 llvm_unreachable("shuffle vector mask references neither vector operand?");
2972
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002973 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2974 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2975 // VSHF concatenates the vectors in a bitwise fashion:
2976 // <0b00, 0b01> + <0b10, 0b11> ->
2977 // 0b0100 + 0b1110 -> 0b01001110
2978 // <0b10, 0b11, 0b00, 0b01>
2979 // We must therefore swap the operands to get the correct result.
2980 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002981}
2982
2983// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2984// indices in the shuffle.
2985SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2986 SelectionDAG &DAG) const {
2987 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2988 EVT ResTy = Op->getValueType(0);
2989
2990 if (!ResTy.is128BitVector())
2991 return SDValue();
2992
2993 int ResTyNumElts = ResTy.getVectorNumElements();
2994 SmallVector<int, 16> Indices;
2995
2996 for (int i = 0; i < ResTyNumElts; ++i)
2997 Indices.push_back(Node->getMaskElt(i));
2998
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002999 // splati.[bhwd] is preferable to the others but is matched from
3000 // MipsISD::VSHF.
3001 if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
3002 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003003 SDValue Result;
3004 if ((Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00003005 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003006 if ((Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00003007 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003008 if ((Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00003009 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003010 if ((Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG)))
Daniel Sanders2ed228b2013-09-24 14:36:12 +00003011 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003012 if ((Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00003013 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003014 if ((Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG)))
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00003015 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003016 if ((Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG)))
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00003017 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00003018 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
3019}
3020
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003021MachineBasicBlock *
3022MipsSETargetLowering::emitBPOSGE32(MachineInstr &MI,
3023 MachineBasicBlock *BB) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003024 // $bb:
3025 // bposge32_pseudo $vr0
3026 // =>
3027 // $bb:
3028 // bposge32 $tbb
3029 // $fbb:
3030 // li $vr2, 0
3031 // b $sink
3032 // $tbb:
3033 // li $vr1, 1
3034 // $sink:
3035 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
3036
3037 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003038 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003039 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003040 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003041 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003042 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003043 MachineFunction *F = BB->getParent();
3044 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3045 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3046 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3047 F->insert(It, FBB);
3048 F->insert(It, TBB);
3049 F->insert(It, Sink);
3050
3051 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003052 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003053 BB->end());
3054 Sink->transferSuccessorsAndUpdatePHIs(BB);
3055
3056 // Add successors.
3057 BB->addSuccessor(FBB);
3058 BB->addSuccessor(TBB);
3059 FBB->addSuccessor(Sink);
3060 TBB->addSuccessor(Sink);
3061
3062 // Insert the real bposge32 instruction to $BB.
3063 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +00003064 // Insert the real bposge32c instruction to $BB.
3065 BuildMI(BB, DL, TII->get(Mips::BPOSGE32C_MMR3)).addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003066
3067 // Fill $FBB.
3068 unsigned VR2 = RegInfo.createVirtualRegister(RC);
3069 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
3070 .addReg(Mips::ZERO).addImm(0);
3071 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3072
3073 // Fill $TBB.
3074 unsigned VR1 = RegInfo.createVirtualRegister(RC);
3075 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
3076 .addReg(Mips::ZERO).addImm(1);
3077
3078 // Insert phi function to $Sink.
3079 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003080 MI.getOperand(0).getReg())
3081 .addReg(VR2)
3082 .addMBB(FBB)
3083 .addReg(VR1)
3084 .addMBB(TBB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003085
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003086 MI.eraseFromParent(); // The pseudo instruction is gone now.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003087 return Sink;
3088}
Daniel Sandersce09d072013-08-28 12:14:50 +00003089
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003090MachineBasicBlock *MipsSETargetLowering::emitMSACBranchPseudo(
3091 MachineInstr &MI, MachineBasicBlock *BB, unsigned BranchOp) const {
Daniel Sandersce09d072013-08-28 12:14:50 +00003092 // $bb:
3093 // vany_nonzero $rd, $ws
3094 // =>
3095 // $bb:
3096 // bnz.b $ws, $tbb
3097 // b $fbb
3098 // $fbb:
3099 // li $rd1, 0
3100 // b $sink
3101 // $tbb:
3102 // li $rd2, 1
3103 // $sink:
3104 // $rd = phi($rd1, $fbb, $rd2, $tbb)
3105
3106 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00003107 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersce09d072013-08-28 12:14:50 +00003108 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003109 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersce09d072013-08-28 12:14:50 +00003110 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003111 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00003112 MachineFunction *F = BB->getParent();
3113 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
3114 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
3115 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
3116 F->insert(It, FBB);
3117 F->insert(It, TBB);
3118 F->insert(It, Sink);
3119
3120 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003121 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00003122 BB->end());
3123 Sink->transferSuccessorsAndUpdatePHIs(BB);
3124
3125 // Add successors.
3126 BB->addSuccessor(FBB);
3127 BB->addSuccessor(TBB);
3128 FBB->addSuccessor(Sink);
3129 TBB->addSuccessor(Sink);
3130
3131 // Insert the real bnz.b instruction to $BB.
3132 BuildMI(BB, DL, TII->get(BranchOp))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003133 .addReg(MI.getOperand(1).getReg())
3134 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003135
3136 // Fill $FBB.
3137 unsigned RD1 = RegInfo.createVirtualRegister(RC);
3138 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
3139 .addReg(Mips::ZERO).addImm(0);
3140 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3141
3142 // Fill $TBB.
3143 unsigned RD2 = RegInfo.createVirtualRegister(RC);
3144 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
3145 .addReg(Mips::ZERO).addImm(1);
3146
3147 // Insert phi function to $Sink.
3148 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003149 MI.getOperand(0).getReg())
3150 .addReg(RD1)
3151 .addMBB(FBB)
3152 .addReg(RD2)
3153 .addMBB(TBB);
Daniel Sandersce09d072013-08-28 12:14:50 +00003154
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003155 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersce09d072013-08-28 12:14:50 +00003156 return Sink;
3157}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003158
3159// Emit the COPY_FW pseudo instruction.
3160//
3161// copy_fw_pseudo $fd, $ws, n
3162// =>
3163// copy_u_w $rt, $ws, $n
3164// mtc1 $rt, $fd
3165//
3166// When n is zero, the equivalent operation can be performed with (potentially)
3167// zero instructions due to register overlaps. This optimization is never valid
3168// 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 +00003169MachineBasicBlock *
3170MipsSETargetLowering::emitCOPY_FW(MachineInstr &MI,
3171 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003172 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003173 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003174 DebugLoc DL = MI.getDebugLoc();
3175 unsigned Fd = MI.getOperand(0).getReg();
3176 unsigned Ws = MI.getOperand(1).getReg();
3177 unsigned Lane = MI.getOperand(2).getImm();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003178
Daniel Sandersafe27c72015-02-23 17:22:16 +00003179 if (Lane == 0) {
3180 unsigned Wt = Ws;
3181 if (!Subtarget.useOddSPReg()) {
3182 // We must copy to an even-numbered MSA register so that the
3183 // single-precision sub-register is also guaranteed to be even-numbered.
3184 Wt = RegInfo.createVirtualRegister(&Mips::MSA128WEvensRegClass);
3185
3186 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Wt).addReg(Ws);
3187 }
3188
3189 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3190 } else {
3191 unsigned Wt = RegInfo.createVirtualRegister(
3192 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3193 &Mips::MSA128WEvensRegClass);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003194
Daniel Sandersd9207702014-03-04 13:54:30 +00003195 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003196 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3197 }
3198
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003199 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003200 return BB;
3201}
3202
3203// Emit the COPY_FD pseudo instruction.
3204//
3205// copy_fd_pseudo $fd, $ws, n
3206// =>
3207// splati.d $wt, $ws, $n
3208// copy $fd, $wt:sub_64
3209//
3210// When n is zero, the equivalent operation can be performed with (potentially)
3211// zero instructions due to register overlaps. This optimization is always
3212// valid because FR=1 mode which is the only supported mode in MSA.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003213MachineBasicBlock *
3214MipsSETargetLowering::emitCOPY_FD(MachineInstr &MI,
3215 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003216 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003217
Eric Christopher96e72c62015-01-29 23:27:36 +00003218 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003219 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003220 unsigned Fd = MI.getOperand(0).getReg();
3221 unsigned Ws = MI.getOperand(1).getReg();
3222 unsigned Lane = MI.getOperand(2).getImm() * 2;
3223 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003224
3225 if (Lane == 0)
3226 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
3227 else {
3228 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3229
3230 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
3231 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
3232 }
3233
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003234 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003235 return BB;
3236}
Daniel Sandersa5150702013-09-27 12:31:32 +00003237
3238// Emit the INSERT_FW pseudo instruction.
3239//
3240// insert_fw_pseudo $wd, $wd_in, $n, $fs
3241// =>
3242// subreg_to_reg $wt:sub_lo, $fs
3243// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003244MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003245MipsSETargetLowering::emitINSERT_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003246 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003247 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003248 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003249 DebugLoc DL = MI.getDebugLoc();
3250 unsigned Wd = MI.getOperand(0).getReg();
3251 unsigned Wd_in = MI.getOperand(1).getReg();
3252 unsigned Lane = MI.getOperand(2).getImm();
3253 unsigned Fs = MI.getOperand(3).getReg();
Daniel Sandersafe27c72015-02-23 17:22:16 +00003254 unsigned Wt = RegInfo.createVirtualRegister(
3255 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3256 &Mips::MSA128WEvensRegClass);
Daniel Sandersa5150702013-09-27 12:31:32 +00003257
3258 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003259 .addImm(0)
3260 .addReg(Fs)
3261 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00003262 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003263 .addReg(Wd_in)
3264 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003265 .addReg(Wt)
3266 .addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003267
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003268 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003269 return BB;
3270}
3271
3272// Emit the INSERT_FD pseudo instruction.
3273//
3274// insert_fd_pseudo $wd, $fs, n
3275// =>
3276// subreg_to_reg $wt:sub_64, $fs
3277// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003278MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003279MipsSETargetLowering::emitINSERT_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003280 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003281 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00003282
Eric Christopher96e72c62015-01-29 23:27:36 +00003283 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003284 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003285 DebugLoc DL = MI.getDebugLoc();
3286 unsigned Wd = MI.getOperand(0).getReg();
3287 unsigned Wd_in = MI.getOperand(1).getReg();
3288 unsigned Lane = MI.getOperand(2).getImm();
3289 unsigned Fs = MI.getOperand(3).getReg();
Daniel Sandersa5150702013-09-27 12:31:32 +00003290 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3291
3292 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003293 .addImm(0)
3294 .addReg(Fs)
3295 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00003296 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003297 .addReg(Wd_in)
3298 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003299 .addReg(Wt)
3300 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003301
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003302 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003303 return BB;
3304}
3305
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003306// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3307//
3308// For integer:
3309// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3310// =>
3311// (SLL $lanetmp1, $lane, <log2size)
3312// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3313// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3314// (NEG $lanetmp2, $lanetmp1)
3315// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3316//
3317// For floating point:
3318// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3319// =>
3320// (SUBREG_TO_REG $wt, $fs, <subreg>)
3321// (SLL $lanetmp1, $lane, <log2size)
3322// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3323// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3324// (NEG $lanetmp2, $lanetmp1)
3325// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003326MachineBasicBlock *MipsSETargetLowering::emitINSERT_DF_VIDX(
3327 MachineInstr &MI, MachineBasicBlock *BB, unsigned EltSizeInBytes,
3328 bool IsFP) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003329 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003330 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003331 DebugLoc DL = MI.getDebugLoc();
3332 unsigned Wd = MI.getOperand(0).getReg();
3333 unsigned SrcVecReg = MI.getOperand(1).getReg();
3334 unsigned LaneReg = MI.getOperand(2).getReg();
3335 unsigned SrcValReg = MI.getOperand(3).getReg();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003336
3337 const TargetRegisterClass *VecRC = nullptr;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003338 // FIXME: This should be true for N32 too.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003339 const TargetRegisterClass *GPRRC =
Daniel Sanders4160c802015-05-05 08:48:35 +00003340 Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003341 unsigned SubRegIdx = Subtarget.isABI_N64() ? Mips::sub_32 : 0;
3342 unsigned ShiftOp = Subtarget.isABI_N64() ? Mips::DSLL : Mips::SLL;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003343 unsigned EltLog2Size;
3344 unsigned InsertOp = 0;
3345 unsigned InsveOp = 0;
3346 switch (EltSizeInBytes) {
3347 default:
3348 llvm_unreachable("Unexpected size");
3349 case 1:
3350 EltLog2Size = 0;
3351 InsertOp = Mips::INSERT_B;
3352 InsveOp = Mips::INSVE_B;
3353 VecRC = &Mips::MSA128BRegClass;
3354 break;
3355 case 2:
3356 EltLog2Size = 1;
3357 InsertOp = Mips::INSERT_H;
3358 InsveOp = Mips::INSVE_H;
3359 VecRC = &Mips::MSA128HRegClass;
3360 break;
3361 case 4:
3362 EltLog2Size = 2;
3363 InsertOp = Mips::INSERT_W;
3364 InsveOp = Mips::INSVE_W;
3365 VecRC = &Mips::MSA128WRegClass;
3366 break;
3367 case 8:
3368 EltLog2Size = 3;
3369 InsertOp = Mips::INSERT_D;
3370 InsveOp = Mips::INSVE_D;
3371 VecRC = &Mips::MSA128DRegClass;
3372 break;
3373 }
3374
3375 if (IsFP) {
3376 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3377 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3378 .addImm(0)
3379 .addReg(SrcValReg)
3380 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3381 SrcValReg = Wt;
3382 }
3383
3384 // Convert the lane index into a byte index
3385 if (EltSizeInBytes != 1) {
3386 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003387 BuildMI(*BB, MI, DL, TII->get(ShiftOp), LaneTmp1)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003388 .addReg(LaneReg)
3389 .addImm(EltLog2Size);
3390 LaneReg = LaneTmp1;
3391 }
3392
3393 // Rotate bytes around so that the desired lane is element zero
3394 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3395 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3396 .addReg(SrcVecReg)
3397 .addReg(SrcVecReg)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003398 .addReg(LaneReg, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003399
3400 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3401 if (IsFP) {
3402 // Use insve.df to insert to element zero
3403 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3404 .addReg(WdTmp1)
3405 .addImm(0)
3406 .addReg(SrcValReg)
3407 .addImm(0);
3408 } else {
3409 // Use insert.df to insert to element zero
3410 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3411 .addReg(WdTmp1)
3412 .addReg(SrcValReg)
3413 .addImm(0);
3414 }
3415
3416 // Rotate elements the rest of the way for a full rotation.
3417 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3418 // the lane index to do this.
3419 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sanders4160c802015-05-05 08:48:35 +00003420 BuildMI(*BB, MI, DL, TII->get(Subtarget.isABI_N64() ? Mips::DSUB : Mips::SUB),
3421 LaneTmp2)
3422 .addReg(Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003423 .addReg(LaneReg);
3424 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3425 .addReg(WdTmp2)
3426 .addReg(WdTmp2)
Daniel Sandersd3bb2082016-06-15 08:43:23 +00003427 .addReg(LaneTmp2, 0, SubRegIdx);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003428
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003429 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003430 return BB;
3431}
3432
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003433// Emit the FILL_FW pseudo instruction.
3434//
3435// fill_fw_pseudo $wd, $fs
3436// =>
3437// implicit_def $wt1
3438// insert_subreg $wt2:subreg_lo, $wt1, $fs
3439// splati.w $wd, $wt2[0]
3440MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003441MipsSETargetLowering::emitFILL_FW(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003442 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003443 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003444 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003445 DebugLoc DL = MI.getDebugLoc();
3446 unsigned Wd = MI.getOperand(0).getReg();
3447 unsigned Fs = MI.getOperand(1).getReg();
Simon Dardis0e9e2372017-01-10 15:53:10 +00003448 unsigned Wt1 = RegInfo.createVirtualRegister(
3449 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3450 : &Mips::MSA128WEvensRegClass);
3451 unsigned Wt2 = RegInfo.createVirtualRegister(
3452 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass
3453 : &Mips::MSA128WEvensRegClass);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003454
3455 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3456 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3457 .addReg(Wt1)
3458 .addReg(Fs)
3459 .addImm(Mips::sub_lo);
3460 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3461
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003462 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003463 return BB;
3464}
3465
3466// Emit the FILL_FD pseudo instruction.
3467//
3468// fill_fd_pseudo $wd, $fs
3469// =>
3470// implicit_def $wt1
3471// insert_subreg $wt2:subreg_64, $wt1, $fs
3472// splati.d $wd, $wt2[0]
3473MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003474MipsSETargetLowering::emitFILL_FD(MachineInstr &MI,
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003475 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003476 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003477
Eric Christopher96e72c62015-01-29 23:27:36 +00003478 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003479 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003480 DebugLoc DL = MI.getDebugLoc();
3481 unsigned Wd = MI.getOperand(0).getReg();
3482 unsigned Fs = MI.getOperand(1).getReg();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003483 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3484 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3485
3486 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3487 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3488 .addReg(Wt1)
3489 .addReg(Fs)
3490 .addImm(Mips::sub_64);
3491 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003492
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003493 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003494 return BB;
3495}
Daniel Sandersa9521602013-10-23 10:36:52 +00003496
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003497// Emit the ST_F16_PSEDUO instruction to store a f16 value from an MSA
3498// register.
3499//
3500// STF16 MSA128F16:$wd, mem_simm10:$addr
3501// =>
3502// copy_u.h $rtemp,$wd[0]
3503// sh $rtemp, $addr
3504//
3505// Safety: We can't use st.h & co as they would over write the memory after
3506// the destination. It would require half floats be allocated 16 bytes(!) of
3507// space.
3508MachineBasicBlock *
3509MipsSETargetLowering::emitST_F16_PSEUDO(MachineInstr &MI,
3510 MachineBasicBlock *BB) const {
3511
3512 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3513 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3514 DebugLoc DL = MI.getDebugLoc();
3515 unsigned Ws = MI.getOperand(0).getReg();
3516 unsigned Rt = MI.getOperand(1).getReg();
3517 const MachineMemOperand &MMO = **MI.memoperands_begin();
3518 unsigned Imm = MMO.getOffset();
3519
3520 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3521 // spill and reload can expand as a GPR64 operand. Examine the
3522 // operand in detail and default to ABI.
3523 const TargetRegisterClass *RC =
3524 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3525 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3526 : &Mips::GPR64RegClass);
3527 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003528 unsigned Rs = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003529
3530 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_U_H), Rs).addReg(Ws).addImm(0);
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003531 if(!UsingMips32) {
3532 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR64RegClass);
3533 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Tmp)
3534 .addImm(0)
3535 .addReg(Rs)
3536 .addImm(Mips::sub_32);
3537 Rs = Tmp;
3538 }
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003539 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::SH : Mips::SH64))
3540 .addReg(Rs)
3541 .addReg(Rt)
3542 .addImm(Imm)
3543 .addMemOperand(BB->getParent()->getMachineMemOperand(
3544 &MMO, MMO.getOffset(), MMO.getSize()));
3545
3546 MI.eraseFromParent();
3547 return BB;
3548}
3549
3550// Emit the LD_F16_PSEDUO instruction to load a f16 value into an MSA register.
3551//
3552// LD_F16 MSA128F16:$wd, mem_simm10:$addr
3553// =>
3554// lh $rtemp, $addr
3555// fill.h $wd, $rtemp
3556//
3557// Safety: We can't use ld.h & co as they over-read from the source.
3558// Additionally, if the address is not modulo 16, 2 cases can occur:
3559// a) Segmentation fault as the load instruction reads from a memory page
3560// memory it's not supposed to.
3561// b) The load crosses an implementation specific boundary, requiring OS
3562// intervention.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003563MachineBasicBlock *
3564MipsSETargetLowering::emitLD_F16_PSEUDO(MachineInstr &MI,
3565 MachineBasicBlock *BB) const {
3566
3567 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3568 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3569 DebugLoc DL = MI.getDebugLoc();
3570 unsigned Wd = MI.getOperand(0).getReg();
3571
3572 // Caution: A load via the GOT can expand to a GPR32 operand, a load via
3573 // spill and reload can expand as a GPR64 operand. Examine the
3574 // operand in detail and default to ABI.
3575 const TargetRegisterClass *RC =
3576 MI.getOperand(1).isReg() ? RegInfo.getRegClass(MI.getOperand(1).getReg())
3577 : (Subtarget.isABI_O32() ? &Mips::GPR32RegClass
3578 : &Mips::GPR64RegClass);
3579
3580 const bool UsingMips32 = RC == &Mips::GPR32RegClass;
3581 unsigned Rt = RegInfo.createVirtualRegister(RC);
3582
3583 MachineInstrBuilder MIB =
3584 BuildMI(*BB, MI, DL, TII->get(UsingMips32 ? Mips::LH : Mips::LH64), Rt);
3585 for (unsigned i = 1; i < MI.getNumOperands(); i++)
Diana Picus116bbab2017-01-13 09:58:52 +00003586 MIB.add(MI.getOperand(i));
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003587
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003588 if(!UsingMips32) {
3589 unsigned Tmp = RegInfo.createVirtualRegister(&Mips::GPR32RegClass);
3590 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Tmp).addReg(Rt, 0, Mips::sub_32);
3591 Rt = Tmp;
3592 }
3593
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003594 BuildMI(*BB, MI, DL, TII->get(Mips::FILL_H), Wd).addReg(Rt);
3595
3596 MI.eraseFromParent();
3597 return BB;
3598}
3599
3600// Emit the FPROUND_PSEUDO instruction.
3601//
3602// Round an FGR64Opnd, FGR32Opnd to an f16.
3603//
3604// Safety: Cycle the operand through the GPRs so the result always ends up
3605// the correct MSA register.
3606//
3607// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fs
3608// / FGR64Opnd:$Fs and MSA128F16:$Wd to the same physical register
3609// (which they can be, as the MSA registers are defined to alias the
3610// FPU's 64 bit and 32 bit registers) the result can be accessed using
3611// the correct register class. That requires operands be tie-able across
3612// register classes which have a sub/super register class relationship.
3613//
3614// For FPG32Opnd:
3615//
3616// FPROUND MSA128F16:$wd, FGR32Opnd:$fs
3617// =>
3618// mfc1 $rtemp, $fs
3619// fill.w $rtemp, $wtemp
3620// fexdo.w $wd, $wtemp, $wtemp
3621//
3622// For FPG64Opnd on mips32r2+:
3623//
3624// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3625// =>
3626// mfc1 $rtemp, $fs
3627// fill.w $rtemp, $wtemp
3628// mfhc1 $rtemp2, $fs
3629// insert.w $wtemp[1], $rtemp2
3630// insert.w $wtemp[3], $rtemp2
3631// fexdo.w $wtemp2, $wtemp, $wtemp
3632// fexdo.h $wd, $temp2, $temp2
3633//
3634// For FGR64Opnd on mips64r2+:
3635//
3636// FPROUND MSA128F16:$wd, FGR64Opnd:$fs
3637// =>
3638// dmfc1 $rtemp, $fs
3639// fill.d $rtemp, $wtemp
3640// fexdo.w $wtemp2, $wtemp, $wtemp
3641// fexdo.h $wd, $wtemp2, $wtemp2
3642//
3643// Safety note: As $wtemp is UNDEF, we may provoke a spurious exception if the
3644// undef bits are "just right" and the exception enable bits are
3645// set. By using fill.w to replicate $fs into all elements over
3646// insert.w for one element, we avoid that potiential case. If
3647// fexdo.[hw] causes an exception in, the exception is valid and it
3648// occurs for all elements.
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003649MachineBasicBlock *
3650MipsSETargetLowering::emitFPROUND_PSEUDO(MachineInstr &MI,
3651 MachineBasicBlock *BB,
3652 bool IsFGR64) const {
3653
3654 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3655 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3656 // it.
3657 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3658
3659 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003660 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003661
3662 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3663 DebugLoc DL = MI.getDebugLoc();
3664 unsigned Wd = MI.getOperand(0).getReg();
3665 unsigned Fs = MI.getOperand(1).getReg();
3666
3667 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3668 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3669 const TargetRegisterClass *GPRRC =
3670 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003671 unsigned MFC1Opc = IsFGR64onMips64
3672 ? Mips::DMFC1
3673 : (IsFGR64onMips32 ? Mips::MFC1_D64 : Mips::MFC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003674 unsigned FILLOpc = IsFGR64onMips64 ? Mips::FILL_D : Mips::FILL_W;
3675
3676 // Perform the register class copy as mentioned above.
3677 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3678 BuildMI(*BB, MI, DL, TII->get(MFC1Opc), Rtemp).addReg(Fs);
3679 BuildMI(*BB, MI, DL, TII->get(FILLOpc), Wtemp).addReg(Rtemp);
3680 unsigned WPHI = Wtemp;
3681
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003682 if (IsFGR64onMips32) {
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003683 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3684 BuildMI(*BB, MI, DL, TII->get(Mips::MFHC1_D64), Rtemp2).addReg(Fs);
3685 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3686 unsigned Wtemp3 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3687 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp2)
3688 .addReg(Wtemp)
3689 .addReg(Rtemp2)
3690 .addImm(1);
3691 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_W), Wtemp3)
3692 .addReg(Wtemp2)
3693 .addReg(Rtemp2)
3694 .addImm(3);
3695 WPHI = Wtemp3;
3696 }
3697
3698 if (IsFGR64) {
3699 unsigned Wtemp2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3700 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_W), Wtemp2)
3701 .addReg(WPHI)
3702 .addReg(WPHI);
3703 WPHI = Wtemp2;
3704 }
3705
3706 BuildMI(*BB, MI, DL, TII->get(Mips::FEXDO_H), Wd).addReg(WPHI).addReg(WPHI);
3707
3708 MI.eraseFromParent();
3709 return BB;
3710}
3711
3712// Emit the FPEXTEND_PSEUDO instruction.
3713//
3714// Expand an f16 to either a FGR32Opnd or FGR64Opnd.
3715//
3716// Safety: Cycle the result through the GPRs so the result always ends up
3717// the correct floating point register.
3718//
3719// FIXME: This copying is strictly unnecessary. If we could tie FGR32Opnd:$Fd
3720// / FGR64Opnd:$Fd and MSA128F16:$Ws to the same physical register
3721// (which they can be, as the MSA registers are defined to alias the
3722// FPU's 64 bit and 32 bit registers) the result can be accessed using
3723// the correct register class. That requires operands be tie-able across
3724// register classes which have a sub/super register class relationship. I
3725// haven't checked.
3726//
3727// For FGR32Opnd:
3728//
3729// FPEXTEND FGR32Opnd:$fd, MSA128F16:$ws
3730// =>
3731// fexupr.w $wtemp, $ws
3732// copy_s.w $rtemp, $ws[0]
3733// mtc1 $rtemp, $fd
3734//
3735// For FGR64Opnd on Mips64:
3736//
3737// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3738// =>
3739// fexupr.w $wtemp, $ws
3740// fexupr.d $wtemp2, $wtemp
3741// copy_s.d $rtemp, $wtemp2s[0]
3742// dmtc1 $rtemp, $fd
3743//
3744// For FGR64Opnd on Mips32:
3745//
3746// FPEXTEND FGR64Opnd:$fd, MSA128F16:$ws
3747// =>
3748// fexupr.w $wtemp, $ws
3749// fexupr.d $wtemp2, $wtemp
3750// copy_s.w $rtemp, $wtemp2[0]
3751// mtc1 $rtemp, $ftemp
3752// copy_s.w $rtemp2, $wtemp2[1]
3753// $fd = mthc1 $rtemp2, $ftemp
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003754MachineBasicBlock *
3755MipsSETargetLowering::emitFPEXTEND_PSEUDO(MachineInstr &MI,
3756 MachineBasicBlock *BB,
3757 bool IsFGR64) const {
3758
3759 // Strictly speaking, we need MIPS32R5 to support MSA. We'll be generous
3760 // here. It's technically doable to support MIPS32 here, but the ISA forbids
3761 // it.
3762 assert(Subtarget.hasMSA() && Subtarget.hasMips32r2());
3763
3764 bool IsFGR64onMips64 = Subtarget.hasMips64() && IsFGR64;
3765 bool IsFGR64onMips32 = !Subtarget.hasMips64() && IsFGR64;
3766
3767 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
3768 DebugLoc DL = MI.getDebugLoc();
3769 unsigned Fd = MI.getOperand(0).getReg();
3770 unsigned Ws = MI.getOperand(1).getReg();
3771
3772 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3773 const TargetRegisterClass *GPRRC =
3774 IsFGR64onMips64 ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Stefan Maksimovic58f225b2017-07-18 12:05:35 +00003775 unsigned MTC1Opc = IsFGR64onMips64
3776 ? Mips::DMTC1
3777 : (IsFGR64onMips32 ? Mips::MTC1_D64 : Mips::MTC1);
Simon Dardis0e2ee3b2016-11-18 16:17:44 +00003778 unsigned COPYOpc = IsFGR64onMips64 ? Mips::COPY_S_D : Mips::COPY_S_W;
3779
3780 unsigned Wtemp = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3781 unsigned WPHI = Wtemp;
3782
3783 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_W), Wtemp).addReg(Ws);
3784 if (IsFGR64) {
3785 WPHI = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3786 BuildMI(*BB, MI, DL, TII->get(Mips::FEXUPR_D), WPHI).addReg(Wtemp);
3787 }
3788
3789 // Perform the safety regclass copy mentioned above.
3790 unsigned Rtemp = RegInfo.createVirtualRegister(GPRRC);
3791 unsigned FPRPHI = IsFGR64onMips32
3792 ? RegInfo.createVirtualRegister(&Mips::FGR64RegClass)
3793 : Fd;
3794 BuildMI(*BB, MI, DL, TII->get(COPYOpc), Rtemp).addReg(WPHI).addImm(0);
3795 BuildMI(*BB, MI, DL, TII->get(MTC1Opc), FPRPHI).addReg(Rtemp);
3796
3797 if (IsFGR64onMips32) {
3798 unsigned Rtemp2 = RegInfo.createVirtualRegister(GPRRC);
3799 BuildMI(*BB, MI, DL, TII->get(Mips::COPY_S_W), Rtemp2)
3800 .addReg(WPHI)
3801 .addImm(1);
3802 BuildMI(*BB, MI, DL, TII->get(Mips::MTHC1_D64), Fd)
3803 .addReg(FPRPHI)
3804 .addReg(Rtemp2);
3805 }
3806
3807 MI.eraseFromParent();
3808 return BB;
3809}
3810
Daniel Sandersa9521602013-10-23 10:36:52 +00003811// Emit the FEXP2_W_1 pseudo instructions.
3812//
3813// fexp2_w_1_pseudo $wd, $wt
3814// =>
3815// ldi.w $ws, 1
3816// fexp2.w $wd, $ws, $wt
3817MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003818MipsSETargetLowering::emitFEXP2_W_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003819 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003820 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003821 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3822 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3823 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3824 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003825 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003826
3827 // Splat 1.0 into a vector
3828 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3829 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3830
3831 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003832 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003833 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003834 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003835
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003836 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003837 return BB;
3838}
3839
3840// Emit the FEXP2_D_1 pseudo instructions.
3841//
3842// fexp2_d_1_pseudo $wd, $wt
3843// =>
3844// ldi.d $ws, 1
3845// fexp2.d $wd, $ws, $wt
3846MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003847MipsSETargetLowering::emitFEXP2_D_1(MachineInstr &MI,
Daniel Sandersa9521602013-10-23 10:36:52 +00003848 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003849 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003850 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3851 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3852 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3853 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003854 DebugLoc DL = MI.getDebugLoc();
Daniel Sandersa9521602013-10-23 10:36:52 +00003855
3856 // Splat 1.0 into a vector
3857 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3858 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3859
3860 // Emit 1.0 * fexp2(Wt)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003861 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI.getOperand(0).getReg())
Daniel Sandersa9521602013-10-23 10:36:52 +00003862 .addReg(Ws2)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003863 .addReg(MI.getOperand(1).getReg());
Daniel Sandersa9521602013-10-23 10:36:52 +00003864
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003865 MI.eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa9521602013-10-23 10:36:52 +00003866 return BB;
3867}