blob: 16b414e885659daa55af596d8db8656293cc0d06 [file] [log] [blame]
Akira Hatanaka44ebe002013-03-14 19:09:52 +00001//===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Subclass of MipsTargetLowering specialized for mips32/64.
11//
12//===----------------------------------------------------------------------===//
13#include "MipsSEISelLowering.h"
14#include "MipsRegisterInfo.h"
15#include "MipsTargetMachine.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
Akira Hatanakaa6bbde52013-04-13 02:13:30 +000018#include "llvm/IR/Intrinsics.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000019#include "llvm/Support/CommandLine.h"
Daniel Sanders62aeab82013-10-30 13:31:27 +000020#include "llvm/Support/Debug.h"
Hans Wennborg3e9b1c12013-10-30 16:10:10 +000021#include "llvm/Support/raw_ostream.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000022#include "llvm/Target/TargetInstrInfo.h"
23
24using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "mips-isel"
27
Akira Hatanaka96ca1822013-03-13 00:54:29 +000028static cl::opt<bool>
29EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
30 cl::desc("MIPS: Enable tail calls."), cl::init(false));
31
Akira Hatanaka63791212013-09-07 00:52:30 +000032static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
33 cl::desc("Expand double precision loads and "
34 "stores to their single precision "
35 "counterparts"));
36
Eric Christopher8924d272014-07-18 23:25:04 +000037MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM,
38 const MipsSubtarget &STI)
39 : MipsTargetLowering(TM, STI) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000040 // Set up the register classes
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000041 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000042
Eric Christopher1c29a652014-07-18 22:55:25 +000043 if (Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000044 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000045
Eric Christopher1c29a652014-07-18 22:55:25 +000046 if (Subtarget.hasDSP() || Subtarget.hasMSA()) {
Daniel Sanders36c671e2013-09-27 09:44:59 +000047 // Expand all truncating stores and extending loads.
48 unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
49 unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
50
51 for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
52 for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
53 setTruncStoreAction((MVT::SimpleValueType)VT0,
54 (MVT::SimpleValueType)VT1, Expand);
55
56 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
57 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
58 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
59 }
60 }
61
Eric Christopher1c29a652014-07-18 22:55:25 +000062 if (Subtarget.hasDSP()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000063 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
64
65 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
Akira Hatanaka654655f2013-08-14 00:53:38 +000066 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000067
68 // Expand all builtin opcodes.
69 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
70 setOperationAction(Opc, VecTys[i], Expand);
71
Akira Hatanaka2f088222013-04-13 00:55:41 +000072 setOperationAction(ISD::ADD, VecTys[i], Legal);
73 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000074 setOperationAction(ISD::LOAD, VecTys[i], Legal);
75 setOperationAction(ISD::STORE, VecTys[i], Legal);
76 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
77 }
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +000078
79 setTargetDAGCombine(ISD::SHL);
80 setTargetDAGCombine(ISD::SRA);
81 setTargetDAGCombine(ISD::SRL);
Akira Hatanaka68741cc2013-04-30 22:37:26 +000082 setTargetDAGCombine(ISD::SETCC);
83 setTargetDAGCombine(ISD::VSELECT);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000084 }
85
Eric Christopher1c29a652014-07-18 22:55:25 +000086 if (Subtarget.hasDSPR2())
Akira Hatanaka2f088222013-04-13 00:55:41 +000087 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
88
Eric Christopher1c29a652014-07-18 22:55:25 +000089 if (Subtarget.hasMSA()) {
Daniel Sandersc65f58a2013-09-11 10:15:48 +000090 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
91 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
92 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
93 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
94 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
95 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
96 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
Daniel Sandersf7456c72013-09-23 13:22:24 +000097
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +000098 setTargetDAGCombine(ISD::AND);
Daniel Sanders53fe6c42013-10-30 13:51:01 +000099 setTargetDAGCombine(ISD::OR);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000100 setTargetDAGCombine(ISD::SRA);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000101 setTargetDAGCombine(ISD::VSELECT);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000102 setTargetDAGCombine(ISD::XOR);
Jack Carter3a2c2d42013-08-13 20:54:07 +0000103 }
104
Eric Christopher1c29a652014-07-18 22:55:25 +0000105 if (!Subtarget.abiUsesSoftFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000106 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
107
108 // When dealing with single precision only, use libcalls
Eric Christopher1c29a652014-07-18 22:55:25 +0000109 if (!Subtarget.isSingleFloat()) {
110 if (Subtarget.isFP64bit())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000111 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
112 else
113 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
114 }
115 }
116
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000117 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
118 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
119 setOperationAction(ISD::MULHS, MVT::i32, Custom);
120 setOperationAction(ISD::MULHU, MVT::i32, Custom);
121
Eric Christopher1c29a652014-07-18 22:55:25 +0000122 if (Subtarget.hasCnMips())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000123 setOperationAction(ISD::MUL, MVT::i64, Legal);
Eric Christopher1c29a652014-07-18 22:55:25 +0000124 else if (Subtarget.isGP64bit())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000125 setOperationAction(ISD::MUL, MVT::i64, Custom);
126
Eric Christopher1c29a652014-07-18 22:55:25 +0000127 if (Subtarget.isGP64bit()) {
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000128 setOperationAction(ISD::MULHS, MVT::i64, Custom);
129 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000130 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000131
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000132 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
133 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
134
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000135 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
136 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
137 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
138 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000139 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
140 setOperationAction(ISD::LOAD, MVT::i32, Custom);
141 setOperationAction(ISD::STORE, MVT::i32, Custom);
142
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000143 setTargetDAGCombine(ISD::ADDE);
144 setTargetDAGCombine(ISD::SUBE);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000145 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000146
Daniel Sandersce09d072013-08-28 12:14:50 +0000147 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000148 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
149 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
150
Akira Hatanaka63791212013-09-07 00:52:30 +0000151 if (NoDPLoadStore) {
152 setOperationAction(ISD::LOAD, MVT::f64, Custom);
153 setOperationAction(ISD::STORE, MVT::f64, Custom);
154 }
155
Eric Christopher1c29a652014-07-18 22:55:25 +0000156 if (Subtarget.hasMips32r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000157 // MIPS32r6 replaces the accumulator-based multiplies with a three register
158 // instruction
Daniel Sanders826f8b32014-06-12 10:54:16 +0000159 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
160 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000161 setOperationAction(ISD::MUL, MVT::i32, Legal);
162 setOperationAction(ISD::MULHS, MVT::i32, Legal);
163 setOperationAction(ISD::MULHU, MVT::i32, Legal);
164
165 // MIPS32r6 replaces the accumulator-based division/remainder with separate
166 // three register division and remainder instructions.
167 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
168 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
169 setOperationAction(ISD::SDIV, MVT::i32, Legal);
170 setOperationAction(ISD::UDIV, MVT::i32, Legal);
171 setOperationAction(ISD::SREM, MVT::i32, Legal);
172 setOperationAction(ISD::UREM, MVT::i32, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000173
174 // MIPS32r6 replaces conditional moves with an equivalent that removes the
175 // need for three GPR read ports.
176 setOperationAction(ISD::SETCC, MVT::i32, Legal);
177 setOperationAction(ISD::SELECT, MVT::i32, Legal);
178 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
179
180 setOperationAction(ISD::SETCC, MVT::f32, Legal);
181 setOperationAction(ISD::SELECT, MVT::f32, Legal);
182 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
183
Eric Christopher1c29a652014-07-18 22:55:25 +0000184 assert(Subtarget.isFP64bit() && "FR=1 is required for MIPS32r6");
Daniel Sanders0fa60412014-06-12 13:39:06 +0000185 setOperationAction(ISD::SETCC, MVT::f64, Legal);
186 setOperationAction(ISD::SELECT, MVT::f64, Legal);
187 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
188
Daniel Sanders3d3ea532014-06-12 15:00:17 +0000189 setOperationAction(ISD::BRCOND, MVT::Other, Legal);
190
Daniel Sanders0fa60412014-06-12 13:39:06 +0000191 // Floating point > and >= are supported via < and <=
192 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
193 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
194 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
195 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
196
197 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
198 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
199 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
200 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000201 }
202
Eric Christopher1c29a652014-07-18 22:55:25 +0000203 if (Subtarget.hasMips64r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000204 // MIPS64r6 replaces the accumulator-based multiplies with a three register
205 // instruction
206 setOperationAction(ISD::MUL, MVT::i64, Legal);
207 setOperationAction(ISD::MULHS, MVT::i64, Legal);
208 setOperationAction(ISD::MULHU, MVT::i64, Legal);
209
210 // MIPS32r6 replaces the accumulator-based division/remainder with separate
211 // three register division and remainder instructions.
212 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
213 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
214 setOperationAction(ISD::SDIV, MVT::i64, Legal);
215 setOperationAction(ISD::UDIV, MVT::i64, Legal);
216 setOperationAction(ISD::SREM, MVT::i64, Legal);
217 setOperationAction(ISD::UREM, MVT::i64, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000218
219 // MIPS64r6 replaces conditional moves with an equivalent that removes the
220 // need for three GPR read ports.
221 setOperationAction(ISD::SETCC, MVT::i64, Legal);
222 setOperationAction(ISD::SELECT, MVT::i64, Legal);
223 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000224 }
225
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000226 computeRegisterProperties();
227}
228
229const MipsTargetLowering *
Eric Christopher8924d272014-07-18 23:25:04 +0000230llvm::createMipsSETargetLowering(MipsTargetMachine &TM,
231 const MipsSubtarget &STI) {
232 return new MipsSETargetLowering(TM, STI);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000233}
234
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000235const TargetRegisterClass *
236MipsSETargetLowering::getRepRegClassFor(MVT VT) const {
237 if (VT == MVT::Untyped)
Eric Christopher1c29a652014-07-18 22:55:25 +0000238 return Subtarget.hasDSP() ? &Mips::ACC64DSPRegClass : &Mips::ACC64RegClass;
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000239
240 return TargetLowering::getRepRegClassFor(VT);
241}
242
Daniel Sanders7a289d02013-09-23 12:02:46 +0000243// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000244void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000245addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
246 addRegisterClass(Ty, RC);
247
248 // Expand all builtin opcodes.
249 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
250 setOperationAction(Opc, Ty, Expand);
251
252 setOperationAction(ISD::BITCAST, Ty, Legal);
253 setOperationAction(ISD::LOAD, Ty, Legal);
254 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000255 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
256 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000257 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000258
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000259 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000260 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000261 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000262 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000263 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000264 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000265 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000266 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000267 setOperationAction(ISD::SHL, Ty, Legal);
268 setOperationAction(ISD::SRA, Ty, Legal);
269 setOperationAction(ISD::SRL, Ty, Legal);
270 setOperationAction(ISD::SUB, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000271 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000272 setOperationAction(ISD::UREM, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000273 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000274 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000275 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000276
Daniel Sanders015972b2013-10-11 10:00:06 +0000277 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
278 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
279 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
280 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
281 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
282 }
283
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000284 setOperationAction(ISD::SETCC, Ty, Legal);
285 setCondCodeAction(ISD::SETNE, Ty, Expand);
286 setCondCodeAction(ISD::SETGE, Ty, Expand);
287 setCondCodeAction(ISD::SETGT, Ty, Expand);
288 setCondCodeAction(ISD::SETUGE, Ty, Expand);
289 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000290}
291
Daniel Sanders7a289d02013-09-23 12:02:46 +0000292// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000293void MipsSETargetLowering::
294addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000295 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000296
297 // Expand all builtin opcodes.
298 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
299 setOperationAction(Opc, Ty, Expand);
300
301 setOperationAction(ISD::LOAD, Ty, Legal);
302 setOperationAction(ISD::STORE, Ty, Legal);
303 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000304 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000305 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000306 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000307
308 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000309 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000310 setOperationAction(ISD::FADD, Ty, Legal);
311 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000312 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000313 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000314 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000315 setOperationAction(ISD::FMUL, Ty, Legal);
316 setOperationAction(ISD::FRINT, Ty, Legal);
317 setOperationAction(ISD::FSQRT, Ty, Legal);
318 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000319 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000320
321 setOperationAction(ISD::SETCC, Ty, Legal);
322 setCondCodeAction(ISD::SETOGE, Ty, Expand);
323 setCondCodeAction(ISD::SETOGT, Ty, Expand);
324 setCondCodeAction(ISD::SETUGE, Ty, Expand);
325 setCondCodeAction(ISD::SETUGT, Ty, Expand);
326 setCondCodeAction(ISD::SETGE, Ty, Expand);
327 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000328 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000329}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000330
331bool
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000332MipsSETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
333 unsigned,
334 unsigned,
335 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000336 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
337
Eric Christopher1c29a652014-07-18 22:55:25 +0000338 if (Subtarget.systemSupportsUnalignedAccess()) {
Daniel Sandersac272632014-05-23 13:18:02 +0000339 // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
340 // implementation defined whether this is handled by hardware, software, or
341 // a hybrid of the two but it's expected that most implementations will
342 // handle the majority of cases in hardware.
343 if (Fast)
344 *Fast = true;
345 return true;
346 }
347
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000348 switch (SVT) {
349 case MVT::i64:
350 case MVT::i32:
351 if (Fast)
352 *Fast = true;
353 return true;
354 default:
355 return false;
356 }
357}
358
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000359SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
360 SelectionDAG &DAG) const {
361 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000362 case ISD::LOAD: return lowerLOAD(Op, DAG);
363 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000364 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
365 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
366 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
367 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
368 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
369 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000370 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
371 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000372 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
373 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000374 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000375 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000376 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000377 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000378 }
379
380 return MipsTargetLowering::LowerOperation(Op, DAG);
381}
382
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000383// selectMADD -
384// Transforms a subgraph in CurDAG if the following pattern is found:
385// (addc multLo, Lo0), (adde multHi, Hi0),
386// where,
387// multHi/Lo: product of multiplication
388// Lo0: initial value of Lo register
389// Hi0: initial value of Hi register
390// Return true if pattern matching was successful.
391static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
392 // ADDENode's second operand must be a flag output of an ADDC node in order
393 // for the matching to be successful.
394 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
395
396 if (ADDCNode->getOpcode() != ISD::ADDC)
397 return false;
398
399 SDValue MultHi = ADDENode->getOperand(0);
400 SDValue MultLo = ADDCNode->getOperand(0);
401 SDNode *MultNode = MultHi.getNode();
402 unsigned MultOpc = MultHi.getOpcode();
403
404 // MultHi and MultLo must be generated by the same node,
405 if (MultLo.getNode() != MultNode)
406 return false;
407
408 // and it must be a multiplication.
409 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
410 return false;
411
412 // MultLo amd MultHi must be the first and second output of MultNode
413 // respectively.
414 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
415 return false;
416
417 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
418 // of the values of MultNode, in which case MultNode will be removed in later
419 // phases.
420 // If there exist users other than ADDENode or ADDCNode, this function returns
421 // here, which will result in MultNode being mapped to a single MULT
422 // instruction node rather than a pair of MULT and MADD instructions being
423 // produced.
424 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
425 return false;
426
Andrew Trickef9de2a2013-05-25 02:42:55 +0000427 SDLoc DL(ADDENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000428
429 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000430 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000431 ADDCNode->getOperand(1),
432 ADDENode->getOperand(1));
433
434 // create MipsMAdd(u) node
435 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
436
437 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
438 MultNode->getOperand(0),// Factor 0
439 MultNode->getOperand(1),// Factor 1
440 ACCIn);
441
442 // replace uses of adde and addc here
443 if (!SDValue(ADDCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000444 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000445 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
446 }
447 if (!SDValue(ADDENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000448 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000449 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
450 }
451
452 return true;
453}
454
455// selectMSUB -
456// Transforms a subgraph in CurDAG if the following pattern is found:
457// (addc Lo0, multLo), (sube Hi0, multHi),
458// where,
459// multHi/Lo: product of multiplication
460// Lo0: initial value of Lo register
461// Hi0: initial value of Hi register
462// Return true if pattern matching was successful.
463static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
464 // SUBENode's second operand must be a flag output of an SUBC node in order
465 // for the matching to be successful.
466 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
467
468 if (SUBCNode->getOpcode() != ISD::SUBC)
469 return false;
470
471 SDValue MultHi = SUBENode->getOperand(1);
472 SDValue MultLo = SUBCNode->getOperand(1);
473 SDNode *MultNode = MultHi.getNode();
474 unsigned MultOpc = MultHi.getOpcode();
475
476 // MultHi and MultLo must be generated by the same node,
477 if (MultLo.getNode() != MultNode)
478 return false;
479
480 // and it must be a multiplication.
481 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
482 return false;
483
484 // MultLo amd MultHi must be the first and second output of MultNode
485 // respectively.
486 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
487 return false;
488
489 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
490 // of the values of MultNode, in which case MultNode will be removed in later
491 // phases.
492 // If there exist users other than SUBENode or SUBCNode, this function returns
493 // here, which will result in MultNode being mapped to a single MULT
494 // instruction node rather than a pair of MULT and MSUB instructions being
495 // produced.
496 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
497 return false;
498
Andrew Trickef9de2a2013-05-25 02:42:55 +0000499 SDLoc DL(SUBENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000500
501 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000502 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000503 SUBCNode->getOperand(0),
504 SUBENode->getOperand(0));
505
506 // create MipsSub(u) node
507 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
508
509 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
510 MultNode->getOperand(0),// Factor 0
511 MultNode->getOperand(1),// Factor 1
512 ACCIn);
513
514 // replace uses of sube and subc here
515 if (!SDValue(SUBCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000516 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000517 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
518 }
519 if (!SDValue(SUBENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000520 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000521 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
522 }
523
524 return true;
525}
526
527static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
528 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000529 const MipsSubtarget &Subtarget) {
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000530 if (DCI.isBeforeLegalize())
531 return SDValue();
532
Eric Christopher1c29a652014-07-18 22:55:25 +0000533 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
Daniel Sanders826f8b32014-06-12 10:54:16 +0000534 N->getValueType(0) == MVT::i32 && selectMADD(N, &DAG))
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000535 return SDValue(N, 0);
536
537 return SDValue();
538}
539
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000540// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
541//
542// Performs the following transformations:
543// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
544// sign/zero-extension is completely overwritten by the new one performed by
545// the ISD::AND.
546// - Removes redundant zero extensions performed by an ISD::AND.
547static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
548 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000549 const MipsSubtarget &Subtarget) {
550 if (!Subtarget.hasMSA())
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000551 return SDValue();
552
553 SDValue Op0 = N->getOperand(0);
554 SDValue Op1 = N->getOperand(1);
555 unsigned Op0Opcode = Op0->getOpcode();
556
557 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
558 // where $d + 1 == 2^n and n == 32
559 // or $d + 1 == 2^n and n <= 32 and ZExt
560 // -> (MipsVExtractZExt $a, $b, $c)
561 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
562 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
563 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
564
565 if (!Mask)
566 return SDValue();
567
568 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
569
570 if (Log2IfPositive <= 0)
571 return SDValue(); // Mask+1 is not a power of 2
572
573 SDValue Op0Op2 = Op0->getOperand(2);
574 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
575 unsigned ExtendTySize = ExtendTy.getSizeInBits();
576 unsigned Log2 = Log2IfPositive;
577
578 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
579 Log2 == ExtendTySize) {
580 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
581 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
Craig Topper131de822014-04-27 19:21:16 +0000582 Op0->getVTList(),
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000583 makeArrayRef(Ops, Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000584 return Op0;
585 }
586 }
587
588 return SDValue();
589}
590
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000591// Determine if the specified node is a constant vector splat.
592//
593// Returns true and sets Imm if:
594// * N is a ISD::BUILD_VECTOR representing a constant splat
595//
596// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
597// differences are that it assumes the MSA has already been checked and the
598// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
599// must not be in order for binsri.d to be selectable).
600static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
601 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
602
Craig Topper062a2ba2014-04-25 05:30:21 +0000603 if (!Node)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000604 return false;
605
606 APInt SplatValue, SplatUndef;
607 unsigned SplatBitSize;
608 bool HasAnyUndefs;
609
610 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
611 8, !IsLittleEndian))
612 return false;
613
614 Imm = SplatValue;
615
616 return true;
617}
618
Daniel Sandersab94b532013-10-30 15:20:38 +0000619// Test whether the given node is an all-ones build_vector.
620static bool isVectorAllOnes(SDValue N) {
621 // Look through bitcasts. Endianness doesn't matter because we are looking
622 // for an all-ones value.
623 if (N->getOpcode() == ISD::BITCAST)
624 N = N->getOperand(0);
625
626 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
627
628 if (!BVN)
629 return false;
630
631 APInt SplatValue, SplatUndef;
632 unsigned SplatBitSize;
633 bool HasAnyUndefs;
634
635 // Endianness doesn't matter in this context because we are looking for
636 // an all-ones value.
637 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
638 return SplatValue.isAllOnesValue();
639
640 return false;
641}
642
643// Test whether N is the bitwise inverse of OfNode.
644static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
645 if (N->getOpcode() != ISD::XOR)
646 return false;
647
648 if (isVectorAllOnes(N->getOperand(0)))
649 return N->getOperand(1) == OfNode;
650
651 if (isVectorAllOnes(N->getOperand(1)))
652 return N->getOperand(0) == OfNode;
653
654 return false;
655}
656
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000657// Perform combines where ISD::OR is the root node.
658//
659// Performs the following transformations:
660// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
661// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
662// vector type.
663static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
664 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000665 const MipsSubtarget &Subtarget) {
666 if (!Subtarget.hasMSA())
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000667 return SDValue();
668
669 EVT Ty = N->getValueType(0);
670
671 if (!Ty.is128BitVector())
672 return SDValue();
673
674 SDValue Op0 = N->getOperand(0);
675 SDValue Op1 = N->getOperand(1);
676
677 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
678 SDValue Op0Op0 = Op0->getOperand(0);
679 SDValue Op0Op1 = Op0->getOperand(1);
680 SDValue Op1Op0 = Op1->getOperand(0);
681 SDValue Op1Op1 = Op1->getOperand(1);
Eric Christopher1c29a652014-07-18 22:55:25 +0000682 bool IsLittleEndian = !Subtarget.isLittle();
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000683
684 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000685 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000686 APInt Mask, InvMask;
687
688 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
689 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
690 // looking.
691 // IfClr will be set if we find a valid match.
692 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
693 Cond = Op0Op0;
694 IfSet = Op0Op1;
695
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000696 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
697 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000698 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000699 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
700 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000701 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000702
703 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000704 }
705
706 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
707 // thing again using this mask.
708 // IfClr will be set if we find a valid match.
709 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
710 Cond = Op0Op1;
711 IfSet = Op0Op0;
712
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000713 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
714 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000715 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000716 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
717 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000718 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000719
720 IsConstantMask = true;
721 }
722
723 // If IfClr is not yet set, try looking for a non-constant match.
724 // IfClr will be set if we find a valid match amongst the eight
725 // possibilities.
726 if (!IfClr.getNode()) {
727 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
728 Cond = Op1Op0;
729 IfSet = Op1Op1;
730 IfClr = Op0Op1;
731 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
732 Cond = Op1Op0;
733 IfSet = Op1Op1;
734 IfClr = Op0Op0;
735 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
736 Cond = Op1Op1;
737 IfSet = Op1Op0;
738 IfClr = Op0Op1;
739 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
740 Cond = Op1Op1;
741 IfSet = Op1Op0;
742 IfClr = Op0Op0;
743 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
744 Cond = Op0Op0;
745 IfSet = Op0Op1;
746 IfClr = Op1Op1;
747 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
748 Cond = Op0Op0;
749 IfSet = Op0Op1;
750 IfClr = Op1Op0;
751 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
752 Cond = Op0Op1;
753 IfSet = Op0Op0;
754 IfClr = Op1Op1;
755 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
756 Cond = Op0Op1;
757 IfSet = Op0Op0;
758 IfClr = Op1Op0;
759 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000760 }
761
762 // At this point, IfClr will be set if we have a valid match.
763 if (!IfClr.getNode())
764 return SDValue();
765
766 assert(Cond.getNode() && IfSet.getNode());
767
768 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000769 if (IsConstantMask) {
770 if (Mask.isAllOnesValue())
771 return IfSet;
772 else if (Mask == 0)
773 return IfClr;
774 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000775
776 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000777 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000778 }
779
780 return SDValue();
781}
782
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000783static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
784 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000785 const MipsSubtarget &Subtarget) {
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000786 if (DCI.isBeforeLegalize())
787 return SDValue();
788
Eric Christopher1c29a652014-07-18 22:55:25 +0000789 if (Subtarget.hasMips32() && N->getValueType(0) == MVT::i32 &&
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000790 selectMSUB(N, &DAG))
791 return SDValue(N, 0);
792
793 return SDValue();
794}
795
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000796static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
797 EVT ShiftTy, SelectionDAG &DAG) {
798 // Clear the upper (64 - VT.sizeInBits) bits.
799 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
800
801 // Return 0.
802 if (C == 0)
803 return DAG.getConstant(0, VT);
804
805 // Return x.
806 if (C == 1)
807 return X;
808
809 // If c is power of 2, return (shl x, log2(c)).
810 if (isPowerOf2_64(C))
811 return DAG.getNode(ISD::SHL, DL, VT, X,
812 DAG.getConstant(Log2_64(C), ShiftTy));
813
814 unsigned Log2Ceil = Log2_64_Ceil(C);
815 uint64_t Floor = 1LL << Log2_64(C);
816 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
817
818 // If |c - floor_c| <= |c - ceil_c|,
819 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
820 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
821 if (C - Floor <= Ceil - C) {
822 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
823 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
824 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
825 }
826
827 // If |c - floor_c| > |c - ceil_c|,
828 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
829 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
830 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
831 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
832}
833
834static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
835 const TargetLowering::DAGCombinerInfo &DCI,
836 const MipsSETargetLowering *TL) {
837 EVT VT = N->getValueType(0);
838
839 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
840 if (!VT.isVector())
841 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
842 VT, TL->getScalarShiftAmountTy(VT), DAG);
843
844 return SDValue(N, 0);
845}
846
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000847static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
848 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000849 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000850 // See if this is a vector splat immediate node.
851 APInt SplatValue, SplatUndef;
852 unsigned SplatBitSize;
853 bool HasAnyUndefs;
854 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
855 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
856
Eric Christopher1c29a652014-07-18 22:55:25 +0000857 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000858 return SDValue();
859
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000860 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000861 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000862 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000863 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000864 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000865 return SDValue();
866
Andrew Trickef9de2a2013-05-25 02:42:55 +0000867 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000868 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
869}
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) };
926 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
Craig Topper131de822014-04-27 19:21:16 +0000927 Op0Op0->getVTList(),
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000928 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000929 return Op0Op0;
930 }
931 }
932 }
933
Eric Christopher1c29a652014-07-18 22:55:25 +0000934 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000935 return SDValue();
936
937 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
938}
939
940
941static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
942 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000943 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000944 EVT Ty = N->getValueType(0);
945
Eric Christopher1c29a652014-07-18 22:55:25 +0000946 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000947 return SDValue();
948
949 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
950}
951
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000952static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
953 bool IsV216 = (Ty == MVT::v2i16);
954
955 switch (CC) {
956 case ISD::SETEQ:
957 case ISD::SETNE: return true;
958 case ISD::SETLT:
959 case ISD::SETLE:
960 case ISD::SETGT:
961 case ISD::SETGE: return IsV216;
962 case ISD::SETULT:
963 case ISD::SETULE:
964 case ISD::SETUGT:
965 case ISD::SETUGE: return !IsV216;
966 default: return false;
967 }
968}
969
970static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
971 EVT Ty = N->getValueType(0);
972
973 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
974 return SDValue();
975
976 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
977 return SDValue();
978
Andrew Trickef9de2a2013-05-25 02:42:55 +0000979 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000980 N->getOperand(1), N->getOperand(2));
981}
982
983static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
984 EVT Ty = N->getValueType(0);
985
Daniel Sanders3ce56622013-09-24 12:18:31 +0000986 if (Ty.is128BitVector() && Ty.isInteger()) {
987 // Try the following combines:
988 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
989 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
990 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
991 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
992 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
993 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
994 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
995 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
996 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
997 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
998 // legalizer.
999 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001000
Daniel Sanders3ce56622013-09-24 12:18:31 +00001001 if (Op0->getOpcode() != ISD::SETCC)
1002 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001003
Daniel Sanders3ce56622013-09-24 12:18:31 +00001004 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
1005 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001006
Daniel Sanders3ce56622013-09-24 12:18:31 +00001007 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
1008 Signed = true;
1009 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
1010 Signed = false;
1011 else
1012 return SDValue();
1013
1014 SDValue Op1 = N->getOperand(1);
1015 SDValue Op2 = N->getOperand(2);
1016 SDValue Op0Op0 = Op0->getOperand(0);
1017 SDValue Op0Op1 = Op0->getOperand(1);
1018
1019 if (Op1 == Op0Op0 && Op2 == Op0Op1)
1020 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
1021 Ty, Op1, Op2);
1022 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
1023 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
1024 Ty, Op1, Op2);
1025 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
1026 SDValue SetCC = N->getOperand(0);
1027
1028 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
1029 return SDValue();
1030
1031 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
1032 SetCC.getOperand(0), SetCC.getOperand(1),
1033 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
1034 }
1035
1036 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001037}
1038
Daniel Sandersf7456c72013-09-23 13:22:24 +00001039static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001040 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001041 EVT Ty = N->getValueType(0);
1042
Eric Christopher1c29a652014-07-18 22:55:25 +00001043 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001044 // Try the following combines:
1045 // (xor (or $a, $b), (build_vector allones))
1046 // (xor (or $a, $b), (bitcast (build_vector allones)))
1047 SDValue Op0 = N->getOperand(0);
1048 SDValue Op1 = N->getOperand(1);
1049 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001050
1051 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
1052 NotOp = Op1;
1053 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
1054 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001055 else
1056 return SDValue();
1057
1058 if (NotOp->getOpcode() == ISD::OR)
1059 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
1060 NotOp->getOperand(1));
1061 }
1062
1063 return SDValue();
1064}
1065
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001066SDValue
1067MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1068 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001069 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001070
1071 switch (N->getOpcode()) {
1072 case ISD::ADDE:
1073 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001074 case ISD::AND:
1075 Val = performANDCombine(N, DAG, DCI, Subtarget);
1076 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +00001077 case ISD::OR:
1078 Val = performORCombine(N, DAG, DCI, Subtarget);
1079 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001080 case ISD::SUBE:
1081 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka5832fc62013-06-26 18:48:17 +00001082 case ISD::MUL:
1083 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001084 case ISD::SHL:
1085 return performSHLCombine(N, DAG, DCI, Subtarget);
1086 case ISD::SRA:
1087 return performSRACombine(N, DAG, DCI, Subtarget);
1088 case ISD::SRL:
1089 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001090 case ISD::VSELECT:
1091 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +00001092 case ISD::XOR:
1093 Val = performXORCombine(N, DAG, Subtarget);
1094 break;
1095 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001096 Val = performSETCCCombine(N, DAG);
1097 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001098 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001099
Daniel Sanders62aeab82013-10-30 13:31:27 +00001100 if (Val.getNode()) {
1101 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1102 N->printrWithDepth(dbgs(), &DAG);
1103 dbgs() << "\n=> \n";
1104 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1105 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001106 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001107 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001108
1109 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001110}
1111
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001112MachineBasicBlock *
1113MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1114 MachineBasicBlock *BB) const {
1115 switch (MI->getOpcode()) {
1116 default:
1117 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1118 case Mips::BPOSGE32_PSEUDO:
1119 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001120 case Mips::SNZ_B_PSEUDO:
1121 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1122 case Mips::SNZ_H_PSEUDO:
1123 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1124 case Mips::SNZ_W_PSEUDO:
1125 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1126 case Mips::SNZ_D_PSEUDO:
1127 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1128 case Mips::SNZ_V_PSEUDO:
1129 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1130 case Mips::SZ_B_PSEUDO:
1131 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1132 case Mips::SZ_H_PSEUDO:
1133 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1134 case Mips::SZ_W_PSEUDO:
1135 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1136 case Mips::SZ_D_PSEUDO:
1137 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1138 case Mips::SZ_V_PSEUDO:
1139 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001140 case Mips::COPY_FW_PSEUDO:
1141 return emitCOPY_FW(MI, BB);
1142 case Mips::COPY_FD_PSEUDO:
1143 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001144 case Mips::INSERT_FW_PSEUDO:
1145 return emitINSERT_FW(MI, BB);
1146 case Mips::INSERT_FD_PSEUDO:
1147 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001148 case Mips::INSERT_B_VIDX_PSEUDO:
1149 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1150 case Mips::INSERT_H_VIDX_PSEUDO:
1151 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1152 case Mips::INSERT_W_VIDX_PSEUDO:
1153 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1154 case Mips::INSERT_D_VIDX_PSEUDO:
1155 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1156 case Mips::INSERT_FW_VIDX_PSEUDO:
1157 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1158 case Mips::INSERT_FD_VIDX_PSEUDO:
1159 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001160 case Mips::FILL_FW_PSEUDO:
1161 return emitFILL_FW(MI, BB);
1162 case Mips::FILL_FD_PSEUDO:
1163 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001164 case Mips::FEXP2_W_1_PSEUDO:
1165 return emitFEXP2_W_1(MI, BB);
1166 case Mips::FEXP2_D_1_PSEUDO:
1167 return emitFEXP2_D_1(MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001168 }
1169}
1170
1171bool MipsSETargetLowering::
1172isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
1173 unsigned NextStackOffset,
1174 const MipsFunctionInfo& FI) const {
1175 if (!EnableMipsTailCalls)
1176 return false;
1177
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001178 // Return false if either the callee or caller has a byval argument.
1179 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
1180 return false;
1181
1182 // Return true if the callee's argument area is no larger than the
1183 // caller's.
1184 return NextStackOffset <= FI.getIncomingArgSize();
1185}
1186
1187void MipsSETargetLowering::
1188getOpndList(SmallVectorImpl<SDValue> &Ops,
1189 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
1190 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
1191 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001192 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001193 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
1194 InternalLinkage, CLI, Callee, Chain);
1195}
1196
Akira Hatanaka63791212013-09-07 00:52:30 +00001197SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1198 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1199
1200 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1201 return MipsTargetLowering::lowerLOAD(Op, DAG);
1202
1203 // Replace a double precision load with two i32 loads and a buildpair64.
1204 SDLoc DL(Op);
1205 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1206 EVT PtrVT = Ptr.getValueType();
1207
1208 // i32 load from lower address.
1209 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1210 MachinePointerInfo(), Nd.isVolatile(),
1211 Nd.isNonTemporal(), Nd.isInvariant(),
1212 Nd.getAlignment());
1213
1214 // i32 load from higher address.
1215 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1216 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1217 MachinePointerInfo(), Nd.isVolatile(),
1218 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001219 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka63791212013-09-07 00:52:30 +00001220
Eric Christopher1c29a652014-07-18 22:55:25 +00001221 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001222 std::swap(Lo, Hi);
1223
1224 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1225 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001226 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001227}
1228
1229SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1230 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1231
1232 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1233 return MipsTargetLowering::lowerSTORE(Op, DAG);
1234
1235 // Replace a double precision store with two extractelement64s and i32 stores.
1236 SDLoc DL(Op);
1237 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1238 EVT PtrVT = Ptr.getValueType();
1239 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1240 Val, DAG.getConstant(0, MVT::i32));
1241 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1242 Val, DAG.getConstant(1, MVT::i32));
1243
Eric Christopher1c29a652014-07-18 22:55:25 +00001244 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001245 std::swap(Lo, Hi);
1246
1247 // i32 store to lower address.
1248 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1249 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
Hal Finkelcc39b672014-07-24 12:16:19 +00001250 Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001251
1252 // i32 store to higher address.
1253 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1254 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001255 Nd.isVolatile(), Nd.isNonTemporal(),
Hal Finkelcc39b672014-07-24 12:16:19 +00001256 std::min(Nd.getAlignment(), 4U), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001257}
1258
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001259SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1260 bool HasLo, bool HasHi,
1261 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001262 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001263 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001264
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001265 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001266 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001267 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1268 Op.getOperand(0), Op.getOperand(1));
1269 SDValue Lo, Hi;
1270
1271 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001272 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001273 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001274 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001275
1276 if (!HasLo || !HasHi)
1277 return HasLo ? Lo : Hi;
1278
1279 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001280 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001281}
1282
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001283
Andrew Trickef9de2a2013-05-25 02:42:55 +00001284static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001285 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1286 DAG.getConstant(0, MVT::i32));
1287 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1288 DAG.getConstant(1, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001289 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001290}
1291
Andrew Trickef9de2a2013-05-25 02:42:55 +00001292static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001293 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1294 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001295 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1296}
1297
1298// This function expands mips intrinsic nodes which have 64-bit input operands
1299// or output values.
1300//
1301// out64 = intrinsic-node in64
1302// =>
1303// lo = copy (extract-element (in64, 0))
1304// hi = copy (extract-element (in64, 1))
1305// mips-specific-node
1306// v0 = copy lo
1307// v1 = copy hi
1308// out64 = merge-values (v0, v1)
1309//
1310static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001311 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001312 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1313 SmallVector<SDValue, 3> Ops;
1314 unsigned OpNo = 0;
1315
1316 // See if Op has a chain input.
1317 if (HasChainIn)
1318 Ops.push_back(Op->getOperand(OpNo++));
1319
1320 // The next operand is the intrinsic opcode.
1321 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1322
1323 // See if the next operand has type i64.
1324 SDValue Opnd = Op->getOperand(++OpNo), In64;
1325
1326 if (Opnd.getValueType() == MVT::i64)
1327 In64 = initAccumulator(Opnd, DL, DAG);
1328 else
1329 Ops.push_back(Opnd);
1330
1331 // Push the remaining operands.
1332 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1333 Ops.push_back(Op->getOperand(OpNo));
1334
1335 // Add In64 to the end of the list.
1336 if (In64.getNode())
1337 Ops.push_back(In64);
1338
1339 // Scan output.
1340 SmallVector<EVT, 2> ResTys;
1341
1342 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1343 I != E; ++I)
1344 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1345
1346 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001347 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001348 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1349
1350 if (!HasChainIn)
1351 return Out;
1352
1353 assert(Val->getValueType(1) == MVT::Other);
1354 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001355 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001356}
1357
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001358// Lower an MSA copy intrinsic into the specified SelectionDAG node
1359static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1360 SDLoc DL(Op);
1361 SDValue Vec = Op->getOperand(1);
1362 SDValue Idx = Op->getOperand(2);
1363 EVT ResTy = Op->getValueType(0);
1364 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1365
1366 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1367 DAG.getValueType(EltTy));
1368
1369 return Result;
1370}
1371
Daniel Sanders50b80412013-11-15 12:56:49 +00001372static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1373 EVT ResVecTy = Op->getValueType(0);
1374 EVT ViaVecTy = ResVecTy;
1375 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001376
Daniel Sanders50b80412013-11-15 12:56:49 +00001377 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1378 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1379 // lanes.
1380 SDValue LaneA;
1381 SDValue LaneB = Op->getOperand(2);
1382
1383 if (ResVecTy == MVT::v2i64) {
1384 LaneA = DAG.getConstant(0, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001385 ViaVecTy = MVT::v4i32;
Daniel Sanders50b80412013-11-15 12:56:49 +00001386 } else
1387 LaneA = LaneB;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001388
Daniel Sanders50b80412013-11-15 12:56:49 +00001389 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1390 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001391
Craig Topper48d114b2014-04-26 18:35:24 +00001392 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001393 makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001394
1395 if (ViaVecTy != ResVecTy)
1396 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001397
1398 return Result;
1399}
1400
Daniel Sanders50b80412013-11-15 12:56:49 +00001401static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1402 return DAG.getConstant(Op->getConstantOperandVal(ImmOp), Op->getValueType(0));
1403}
1404
1405static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1406 bool BigEndian, SelectionDAG &DAG) {
1407 EVT ViaVecTy = VecTy;
1408 SDValue SplatValueA = SplatValue;
1409 SDValue SplatValueB = SplatValue;
1410 SDLoc DL(SplatValue);
1411
1412 if (VecTy == MVT::v2i64) {
1413 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1414 ViaVecTy = MVT::v4i32;
1415
1416 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1417 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
1418 DAG.getConstant(32, MVT::i32));
1419 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1420 }
1421
1422 // We currently hold the parts in little endian order. Swap them if
1423 // necessary.
1424 if (BigEndian)
1425 std::swap(SplatValueA, SplatValueB);
1426
1427 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1428 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1429 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1430 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1431
Craig Topper48d114b2014-04-26 18:35:24 +00001432 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001433 makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001434
1435 if (VecTy != ViaVecTy)
1436 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1437
1438 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001439}
1440
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001441static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1442 unsigned Opc, SDValue Imm,
1443 bool BigEndian) {
1444 EVT VecTy = Op->getValueType(0);
1445 SDValue Exp2Imm;
1446 SDLoc DL(Op);
1447
Daniel Sanders50b80412013-11-15 12:56:49 +00001448 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1449 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001450 if (VecTy == MVT::v2i64) {
1451 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1452 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1453
1454 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001455 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), MVT::i32);
1456
1457 if (BigEndian)
1458 std::swap(BitImmLoOp, BitImmHiOp);
1459
1460 Exp2Imm =
1461 DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
1462 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, BitImmLoOp,
1463 BitImmHiOp, BitImmLoOp, BitImmHiOp));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001464 }
1465 }
1466
Craig Topper062a2ba2014-04-25 05:30:21 +00001467 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001468 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001469
1470 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1471 // only values 0-63 are valid.
1472 if (VecTy == MVT::v2i64)
1473 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1474
1475 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1476
1477 Exp2Imm =
1478 DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, VecTy), Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001479 }
1480
1481 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1482}
1483
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001484static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1485 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001486 SDLoc DL(Op);
Daniel Sanders50b80412013-11-15 12:56:49 +00001487 SDValue One = DAG.getConstant(1, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001488 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
1489
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001490 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1491 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001492}
1493
1494static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1495 SDLoc DL(Op);
1496 EVT ResTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001497 APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
1498 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
1499 SDValue BitMask = DAG.getConstant(~BitImm, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001500
1501 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1502}
1503
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001504SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1505 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001506 SDLoc DL(Op);
1507
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001508 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1509 default:
1510 return SDValue();
1511 case Intrinsic::mips_shilo:
1512 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1513 case Intrinsic::mips_dpau_h_qbl:
1514 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1515 case Intrinsic::mips_dpau_h_qbr:
1516 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1517 case Intrinsic::mips_dpsu_h_qbl:
1518 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1519 case Intrinsic::mips_dpsu_h_qbr:
1520 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1521 case Intrinsic::mips_dpa_w_ph:
1522 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1523 case Intrinsic::mips_dps_w_ph:
1524 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1525 case Intrinsic::mips_dpax_w_ph:
1526 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1527 case Intrinsic::mips_dpsx_w_ph:
1528 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1529 case Intrinsic::mips_mulsa_w_ph:
1530 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1531 case Intrinsic::mips_mult:
1532 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1533 case Intrinsic::mips_multu:
1534 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1535 case Intrinsic::mips_madd:
1536 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1537 case Intrinsic::mips_maddu:
1538 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1539 case Intrinsic::mips_msub:
1540 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1541 case Intrinsic::mips_msubu:
1542 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001543 case Intrinsic::mips_addv_b:
1544 case Intrinsic::mips_addv_h:
1545 case Intrinsic::mips_addv_w:
1546 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001547 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1548 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001549 case Intrinsic::mips_addvi_b:
1550 case Intrinsic::mips_addvi_h:
1551 case Intrinsic::mips_addvi_w:
1552 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001553 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1554 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001555 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001556 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1557 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001558 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001559 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1560 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001561 case Intrinsic::mips_bclr_b:
1562 case Intrinsic::mips_bclr_h:
1563 case Intrinsic::mips_bclr_w:
1564 case Intrinsic::mips_bclr_d:
1565 return lowerMSABitClear(Op, DAG);
1566 case Intrinsic::mips_bclri_b:
1567 case Intrinsic::mips_bclri_h:
1568 case Intrinsic::mips_bclri_w:
1569 case Intrinsic::mips_bclri_d:
1570 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001571 case Intrinsic::mips_binsli_b:
1572 case Intrinsic::mips_binsli_h:
1573 case Intrinsic::mips_binsli_w:
1574 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001575 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001576 EVT VecTy = Op->getValueType(0);
1577 EVT EltTy = VecTy.getVectorElementType();
1578 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
1579 Op->getConstantOperandVal(3));
1580 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Daniel Sandersdf2215452014-03-12 11:54:00 +00001581 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1582 Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001583 }
1584 case Intrinsic::mips_binsri_b:
1585 case Intrinsic::mips_binsri_h:
1586 case Intrinsic::mips_binsri_w:
1587 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001588 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001589 EVT VecTy = Op->getValueType(0);
1590 EVT EltTy = VecTy.getVectorElementType();
1591 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
1592 Op->getConstantOperandVal(3));
1593 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Daniel Sandersdf2215452014-03-12 11:54:00 +00001594 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1595 Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001596 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001597 case Intrinsic::mips_bmnz_v:
1598 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1599 Op->getOperand(2), Op->getOperand(1));
1600 case Intrinsic::mips_bmnzi_b:
1601 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1602 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1603 Op->getOperand(1));
1604 case Intrinsic::mips_bmz_v:
1605 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1606 Op->getOperand(1), Op->getOperand(2));
1607 case Intrinsic::mips_bmzi_b:
1608 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1609 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1610 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001611 case Intrinsic::mips_bneg_b:
1612 case Intrinsic::mips_bneg_h:
1613 case Intrinsic::mips_bneg_w:
1614 case Intrinsic::mips_bneg_d: {
1615 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001616 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001617
1618 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1619 DAG.getNode(ISD::SHL, DL, VecTy, One,
1620 Op->getOperand(2)));
1621 }
1622 case Intrinsic::mips_bnegi_b:
1623 case Intrinsic::mips_bnegi_h:
1624 case Intrinsic::mips_bnegi_w:
1625 case Intrinsic::mips_bnegi_d:
1626 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001627 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001628 case Intrinsic::mips_bnz_b:
1629 case Intrinsic::mips_bnz_h:
1630 case Intrinsic::mips_bnz_w:
1631 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001632 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1633 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001634 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001635 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1636 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001637 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001638 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001639 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001640 Op->getOperand(1), Op->getOperand(3),
1641 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001642 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001643 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001644 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001645 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1646 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001647 case Intrinsic::mips_bset_b:
1648 case Intrinsic::mips_bset_h:
1649 case Intrinsic::mips_bset_w:
1650 case Intrinsic::mips_bset_d: {
1651 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001652 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001653
1654 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1655 DAG.getNode(ISD::SHL, DL, VecTy, One,
1656 Op->getOperand(2)));
1657 }
1658 case Intrinsic::mips_bseti_b:
1659 case Intrinsic::mips_bseti_h:
1660 case Intrinsic::mips_bseti_w:
1661 case Intrinsic::mips_bseti_d:
1662 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001663 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001664 case Intrinsic::mips_bz_b:
1665 case Intrinsic::mips_bz_h:
1666 case Intrinsic::mips_bz_w:
1667 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001668 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1669 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001670 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001671 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1672 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001673 case Intrinsic::mips_ceq_b:
1674 case Intrinsic::mips_ceq_h:
1675 case Intrinsic::mips_ceq_w:
1676 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001677 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001678 Op->getOperand(2), ISD::SETEQ);
1679 case Intrinsic::mips_ceqi_b:
1680 case Intrinsic::mips_ceqi_h:
1681 case Intrinsic::mips_ceqi_w:
1682 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001683 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001684 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1685 case Intrinsic::mips_cle_s_b:
1686 case Intrinsic::mips_cle_s_h:
1687 case Intrinsic::mips_cle_s_w:
1688 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001689 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001690 Op->getOperand(2), ISD::SETLE);
1691 case Intrinsic::mips_clei_s_b:
1692 case Intrinsic::mips_clei_s_h:
1693 case Intrinsic::mips_clei_s_w:
1694 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001695 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001696 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1697 case Intrinsic::mips_cle_u_b:
1698 case Intrinsic::mips_cle_u_h:
1699 case Intrinsic::mips_cle_u_w:
1700 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001701 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001702 Op->getOperand(2), ISD::SETULE);
1703 case Intrinsic::mips_clei_u_b:
1704 case Intrinsic::mips_clei_u_h:
1705 case Intrinsic::mips_clei_u_w:
1706 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001707 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001708 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1709 case Intrinsic::mips_clt_s_b:
1710 case Intrinsic::mips_clt_s_h:
1711 case Intrinsic::mips_clt_s_w:
1712 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001713 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001714 Op->getOperand(2), ISD::SETLT);
1715 case Intrinsic::mips_clti_s_b:
1716 case Intrinsic::mips_clti_s_h:
1717 case Intrinsic::mips_clti_s_w:
1718 case Intrinsic::mips_clti_s_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 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1721 case Intrinsic::mips_clt_u_b:
1722 case Intrinsic::mips_clt_u_h:
1723 case Intrinsic::mips_clt_u_w:
1724 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001725 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001726 Op->getOperand(2), ISD::SETULT);
1727 case Intrinsic::mips_clti_u_b:
1728 case Intrinsic::mips_clti_u_h:
1729 case Intrinsic::mips_clti_u_w:
1730 case Intrinsic::mips_clti_u_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 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001733 case Intrinsic::mips_copy_s_b:
1734 case Intrinsic::mips_copy_s_h:
1735 case Intrinsic::mips_copy_s_w:
1736 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001737 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001738 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001739 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1740 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1741 else {
1742 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1743 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1744 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1745 Op->getValueType(0), Op->getOperand(1),
1746 Op->getOperand(2));
1747 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001748 case Intrinsic::mips_copy_u_b:
1749 case Intrinsic::mips_copy_u_h:
1750 case Intrinsic::mips_copy_u_w:
1751 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001752 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001753 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001754 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1755 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1756 else {
1757 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1758 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1759 // Note: When i64 is illegal, this results in copy_s.w instructions
1760 // instead of copy_u.w instructions. This makes no difference to the
1761 // behaviour since i64 is only illegal when the register file is 32-bit.
1762 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1763 Op->getValueType(0), Op->getOperand(1),
1764 Op->getOperand(2));
1765 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001766 case Intrinsic::mips_div_s_b:
1767 case Intrinsic::mips_div_s_h:
1768 case Intrinsic::mips_div_s_w:
1769 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001770 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1771 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001772 case Intrinsic::mips_div_u_b:
1773 case Intrinsic::mips_div_u_h:
1774 case Intrinsic::mips_div_u_w:
1775 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001776 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1777 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001778 case Intrinsic::mips_fadd_w:
1779 case Intrinsic::mips_fadd_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001780 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1781 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001782 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1783 case Intrinsic::mips_fceq_w:
1784 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001785 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001786 Op->getOperand(2), ISD::SETOEQ);
1787 case Intrinsic::mips_fcle_w:
1788 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001789 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001790 Op->getOperand(2), ISD::SETOLE);
1791 case Intrinsic::mips_fclt_w:
1792 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001793 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001794 Op->getOperand(2), ISD::SETOLT);
1795 case Intrinsic::mips_fcne_w:
1796 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001797 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001798 Op->getOperand(2), ISD::SETONE);
1799 case Intrinsic::mips_fcor_w:
1800 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001801 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001802 Op->getOperand(2), ISD::SETO);
1803 case Intrinsic::mips_fcueq_w:
1804 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001805 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001806 Op->getOperand(2), ISD::SETUEQ);
1807 case Intrinsic::mips_fcule_w:
1808 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001809 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001810 Op->getOperand(2), ISD::SETULE);
1811 case Intrinsic::mips_fcult_w:
1812 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001813 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001814 Op->getOperand(2), ISD::SETULT);
1815 case Intrinsic::mips_fcun_w:
1816 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001817 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001818 Op->getOperand(2), ISD::SETUO);
1819 case Intrinsic::mips_fcune_w:
1820 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001821 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001822 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001823 case Intrinsic::mips_fdiv_w:
1824 case Intrinsic::mips_fdiv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001825 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1826 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001827 case Intrinsic::mips_ffint_u_w:
1828 case Intrinsic::mips_ffint_u_d:
1829 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1830 Op->getOperand(1));
1831 case Intrinsic::mips_ffint_s_w:
1832 case Intrinsic::mips_ffint_s_d:
1833 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1834 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001835 case Intrinsic::mips_fill_b:
1836 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001837 case Intrinsic::mips_fill_w:
1838 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001839 SmallVector<SDValue, 16> Ops;
1840 EVT ResTy = Op->getValueType(0);
1841
1842 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1843 Ops.push_back(Op->getOperand(1));
1844
Daniel Sandersc72593e2013-09-27 13:20:41 +00001845 // If ResTy is v2i64 then the type legalizer will break this node down into
1846 // an equivalent v4i32.
Craig Topper48d114b2014-04-26 18:35:24 +00001847 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001848 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001849 case Intrinsic::mips_fexp2_w:
1850 case Intrinsic::mips_fexp2_d: {
1851 EVT ResTy = Op->getValueType(0);
1852 return DAG.getNode(
1853 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1854 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1855 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001856 case Intrinsic::mips_flog2_w:
1857 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001858 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001859 case Intrinsic::mips_fmadd_w:
1860 case Intrinsic::mips_fmadd_d:
1861 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1862 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001863 case Intrinsic::mips_fmul_w:
1864 case Intrinsic::mips_fmul_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001865 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1866 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001867 case Intrinsic::mips_fmsub_w:
1868 case Intrinsic::mips_fmsub_d: {
1869 EVT ResTy = Op->getValueType(0);
1870 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1871 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1872 Op->getOperand(2), Op->getOperand(3)));
1873 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001874 case Intrinsic::mips_frint_w:
1875 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001876 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001877 case Intrinsic::mips_fsqrt_w:
1878 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001879 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001880 case Intrinsic::mips_fsub_w:
1881 case Intrinsic::mips_fsub_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001882 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1883 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001884 case Intrinsic::mips_ftrunc_u_w:
1885 case Intrinsic::mips_ftrunc_u_d:
1886 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1887 Op->getOperand(1));
1888 case Intrinsic::mips_ftrunc_s_w:
1889 case Intrinsic::mips_ftrunc_s_d:
1890 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1891 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001892 case Intrinsic::mips_ilvev_b:
1893 case Intrinsic::mips_ilvev_h:
1894 case Intrinsic::mips_ilvev_w:
1895 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001896 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001897 Op->getOperand(1), Op->getOperand(2));
1898 case Intrinsic::mips_ilvl_b:
1899 case Intrinsic::mips_ilvl_h:
1900 case Intrinsic::mips_ilvl_w:
1901 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001902 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001903 Op->getOperand(1), Op->getOperand(2));
1904 case Intrinsic::mips_ilvod_b:
1905 case Intrinsic::mips_ilvod_h:
1906 case Intrinsic::mips_ilvod_w:
1907 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001908 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001909 Op->getOperand(1), Op->getOperand(2));
1910 case Intrinsic::mips_ilvr_b:
1911 case Intrinsic::mips_ilvr_h:
1912 case Intrinsic::mips_ilvr_w:
1913 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001914 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001915 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001916 case Intrinsic::mips_insert_b:
1917 case Intrinsic::mips_insert_h:
1918 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001919 case Intrinsic::mips_insert_d:
1920 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1921 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001922 case Intrinsic::mips_insve_b:
1923 case Intrinsic::mips_insve_h:
1924 case Intrinsic::mips_insve_w:
1925 case Intrinsic::mips_insve_d:
1926 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1927 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
1928 DAG.getConstant(0, MVT::i32));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001929 case Intrinsic::mips_ldi_b:
1930 case Intrinsic::mips_ldi_h:
1931 case Intrinsic::mips_ldi_w:
1932 case Intrinsic::mips_ldi_d:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001933 return lowerMSASplatImm(Op, 1, DAG);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001934 case Intrinsic::mips_lsa:
1935 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001936 EVT ResTy = Op->getValueType(0);
1937 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1938 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1939 Op->getOperand(2), Op->getOperand(3)));
1940 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001941 case Intrinsic::mips_maddv_b:
1942 case Intrinsic::mips_maddv_h:
1943 case Intrinsic::mips_maddv_w:
1944 case Intrinsic::mips_maddv_d: {
1945 EVT ResTy = Op->getValueType(0);
1946 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1947 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1948 Op->getOperand(2), Op->getOperand(3)));
1949 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001950 case Intrinsic::mips_max_s_b:
1951 case Intrinsic::mips_max_s_h:
1952 case Intrinsic::mips_max_s_w:
1953 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001954 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1955 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001956 case Intrinsic::mips_max_u_b:
1957 case Intrinsic::mips_max_u_h:
1958 case Intrinsic::mips_max_u_w:
1959 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001960 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1961 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001962 case Intrinsic::mips_maxi_s_b:
1963 case Intrinsic::mips_maxi_s_h:
1964 case Intrinsic::mips_maxi_s_w:
1965 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001966 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1967 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001968 case Intrinsic::mips_maxi_u_b:
1969 case Intrinsic::mips_maxi_u_h:
1970 case Intrinsic::mips_maxi_u_w:
1971 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001972 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1973 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001974 case Intrinsic::mips_min_s_b:
1975 case Intrinsic::mips_min_s_h:
1976 case Intrinsic::mips_min_s_w:
1977 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001978 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1979 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001980 case Intrinsic::mips_min_u_b:
1981 case Intrinsic::mips_min_u_h:
1982 case Intrinsic::mips_min_u_w:
1983 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001984 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1985 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001986 case Intrinsic::mips_mini_s_b:
1987 case Intrinsic::mips_mini_s_h:
1988 case Intrinsic::mips_mini_s_w:
1989 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001990 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1991 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001992 case Intrinsic::mips_mini_u_b:
1993 case Intrinsic::mips_mini_u_h:
1994 case Intrinsic::mips_mini_u_w:
1995 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001996 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1997 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001998 case Intrinsic::mips_mod_s_b:
1999 case Intrinsic::mips_mod_s_h:
2000 case Intrinsic::mips_mod_s_w:
2001 case Intrinsic::mips_mod_s_d:
2002 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
2003 Op->getOperand(2));
2004 case Intrinsic::mips_mod_u_b:
2005 case Intrinsic::mips_mod_u_h:
2006 case Intrinsic::mips_mod_u_w:
2007 case Intrinsic::mips_mod_u_d:
2008 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
2009 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002010 case Intrinsic::mips_mulv_b:
2011 case Intrinsic::mips_mulv_h:
2012 case Intrinsic::mips_mulv_w:
2013 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002014 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
2015 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00002016 case Intrinsic::mips_msubv_b:
2017 case Intrinsic::mips_msubv_h:
2018 case Intrinsic::mips_msubv_w:
2019 case Intrinsic::mips_msubv_d: {
2020 EVT ResTy = Op->getValueType(0);
2021 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
2022 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2023 Op->getOperand(2), Op->getOperand(3)));
2024 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002025 case Intrinsic::mips_nlzc_b:
2026 case Intrinsic::mips_nlzc_h:
2027 case Intrinsic::mips_nlzc_w:
2028 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002029 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002030 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002031 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2032 Op->getOperand(1), Op->getOperand(2));
2033 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002034 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002035 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002036 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2037 Op->getOperand(1),
2038 lowerMSASplatImm(Op, 2, DAG));
2039 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002040 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002041 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002042 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2043 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002044 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002045 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2046 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002047 case Intrinsic::mips_pckev_b:
2048 case Intrinsic::mips_pckev_h:
2049 case Intrinsic::mips_pckev_w:
2050 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002051 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002052 Op->getOperand(1), Op->getOperand(2));
2053 case Intrinsic::mips_pckod_b:
2054 case Intrinsic::mips_pckod_h:
2055 case Intrinsic::mips_pckod_w:
2056 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002057 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002058 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002059 case Intrinsic::mips_pcnt_b:
2060 case Intrinsic::mips_pcnt_h:
2061 case Intrinsic::mips_pcnt_w:
2062 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002063 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sanders26307182013-09-24 14:20:00 +00002064 case Intrinsic::mips_shf_b:
2065 case Intrinsic::mips_shf_h:
2066 case Intrinsic::mips_shf_w:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002067 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002068 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002069 case Intrinsic::mips_sll_b:
2070 case Intrinsic::mips_sll_h:
2071 case Intrinsic::mips_sll_w:
2072 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002073 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
2074 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002075 case Intrinsic::mips_slli_b:
2076 case Intrinsic::mips_slli_h:
2077 case Intrinsic::mips_slli_w:
2078 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002079 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2080 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002081 case Intrinsic::mips_splat_b:
2082 case Intrinsic::mips_splat_h:
2083 case Intrinsic::mips_splat_w:
2084 case Intrinsic::mips_splat_d:
2085 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2086 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2087 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2088 // Instead we lower to MipsISD::VSHF and match from there.
2089 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002090 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002091 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002092 case Intrinsic::mips_splati_b:
2093 case Intrinsic::mips_splati_h:
2094 case Intrinsic::mips_splati_w:
2095 case Intrinsic::mips_splati_d:
2096 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2097 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2098 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002099 case Intrinsic::mips_sra_b:
2100 case Intrinsic::mips_sra_h:
2101 case Intrinsic::mips_sra_w:
2102 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002103 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
2104 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002105 case Intrinsic::mips_srai_b:
2106 case Intrinsic::mips_srai_h:
2107 case Intrinsic::mips_srai_w:
2108 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002109 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2110 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002111 case Intrinsic::mips_srl_b:
2112 case Intrinsic::mips_srl_h:
2113 case Intrinsic::mips_srl_w:
2114 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002115 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
2116 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002117 case Intrinsic::mips_srli_b:
2118 case Intrinsic::mips_srli_h:
2119 case Intrinsic::mips_srli_w:
2120 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002121 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2122 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002123 case Intrinsic::mips_subv_b:
2124 case Intrinsic::mips_subv_h:
2125 case Intrinsic::mips_subv_w:
2126 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002127 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2128 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002129 case Intrinsic::mips_subvi_b:
2130 case Intrinsic::mips_subvi_h:
2131 case Intrinsic::mips_subvi_w:
2132 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002133 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2134 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002135 case Intrinsic::mips_vshf_b:
2136 case Intrinsic::mips_vshf_h:
2137 case Intrinsic::mips_vshf_w:
2138 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002139 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002140 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002141 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002142 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2143 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002144 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002145 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2146 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002147 }
2148}
2149
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002150static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2151 SDLoc DL(Op);
2152 SDValue ChainIn = Op->getOperand(0);
2153 SDValue Address = Op->getOperand(2);
2154 SDValue Offset = Op->getOperand(3);
2155 EVT ResTy = Op->getValueType(0);
2156 EVT PtrTy = Address->getValueType(0);
2157
2158 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2159
2160 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
2161 false, false, 16);
2162}
2163
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002164SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2165 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002166 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2167 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002168 default:
2169 return SDValue();
2170 case Intrinsic::mips_extp:
2171 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2172 case Intrinsic::mips_extpdp:
2173 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2174 case Intrinsic::mips_extr_w:
2175 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2176 case Intrinsic::mips_extr_r_w:
2177 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2178 case Intrinsic::mips_extr_rs_w:
2179 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2180 case Intrinsic::mips_extr_s_h:
2181 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2182 case Intrinsic::mips_mthlip:
2183 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2184 case Intrinsic::mips_mulsaq_s_w_ph:
2185 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2186 case Intrinsic::mips_maq_s_w_phl:
2187 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2188 case Intrinsic::mips_maq_s_w_phr:
2189 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2190 case Intrinsic::mips_maq_sa_w_phl:
2191 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2192 case Intrinsic::mips_maq_sa_w_phr:
2193 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2194 case Intrinsic::mips_dpaq_s_w_ph:
2195 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2196 case Intrinsic::mips_dpsq_s_w_ph:
2197 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2198 case Intrinsic::mips_dpaq_sa_l_w:
2199 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2200 case Intrinsic::mips_dpsq_sa_l_w:
2201 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2202 case Intrinsic::mips_dpaqx_s_w_ph:
2203 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2204 case Intrinsic::mips_dpaqx_sa_w_ph:
2205 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2206 case Intrinsic::mips_dpsqx_s_w_ph:
2207 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2208 case Intrinsic::mips_dpsqx_sa_w_ph:
2209 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002210 case Intrinsic::mips_ld_b:
2211 case Intrinsic::mips_ld_h:
2212 case Intrinsic::mips_ld_w:
2213 case Intrinsic::mips_ld_d:
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002214 return lowerMSALoadIntr(Op, DAG, Intr);
2215 }
2216}
2217
2218static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2219 SDLoc DL(Op);
2220 SDValue ChainIn = Op->getOperand(0);
2221 SDValue Value = Op->getOperand(2);
2222 SDValue Address = Op->getOperand(3);
2223 SDValue Offset = Op->getOperand(4);
2224 EVT PtrTy = Address->getValueType(0);
2225
2226 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2227
2228 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
2229 false, 16);
2230}
2231
2232SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2233 SelectionDAG &DAG) const {
2234 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2235 switch (Intr) {
2236 default:
2237 return SDValue();
2238 case Intrinsic::mips_st_b:
2239 case Intrinsic::mips_st_h:
2240 case Intrinsic::mips_st_w:
2241 case Intrinsic::mips_st_d:
Daniel Sandersce09d072013-08-28 12:14:50 +00002242 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002243 }
2244}
2245
Daniel Sanders7a289d02013-09-23 12:02:46 +00002246/// \brief Check if the given BuildVectorSDNode is a splat.
2247/// This method currently relies on DAG nodes being reused when equivalent,
2248/// so it's possible for this to return false even when isConstantSplat returns
2249/// true.
2250static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002251 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002252 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002253
2254 SDValue Operand0 = N->getOperand(0);
2255
2256 for (unsigned int i = 1; i < nOps; ++i) {
2257 if (N->getOperand(i) != Operand0)
2258 return false;
2259 }
2260
2261 return true;
2262}
2263
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002264// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2265//
2266// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2267// choose to sign-extend but we could have equally chosen zero-extend. The
2268// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2269// result into this node later (possibly changing it to a zero-extend in the
2270// process).
2271SDValue MipsSETargetLowering::
2272lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2273 SDLoc DL(Op);
2274 EVT ResTy = Op->getValueType(0);
2275 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002276 EVT VecTy = Op0->getValueType(0);
2277
2278 if (!VecTy.is128BitVector())
2279 return SDValue();
2280
2281 if (ResTy.isInteger()) {
2282 SDValue Op1 = Op->getOperand(1);
2283 EVT EltTy = VecTy.getVectorElementType();
2284 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2285 DAG.getValueType(EltTy));
2286 }
2287
2288 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002289}
2290
Daniel Sandersf49dd822013-09-24 13:33:07 +00002291static bool isConstantOrUndef(const SDValue Op) {
2292 if (Op->getOpcode() == ISD::UNDEF)
2293 return true;
2294 if (dyn_cast<ConstantSDNode>(Op))
2295 return true;
2296 if (dyn_cast<ConstantFPSDNode>(Op))
2297 return true;
2298 return false;
2299}
2300
2301static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2302 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2303 if (isConstantOrUndef(Op->getOperand(i)))
2304 return true;
2305 return false;
2306}
2307
Daniel Sanders7a289d02013-09-23 12:02:46 +00002308// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2309// backend.
2310//
2311// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002312// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2313// 2 less than or equal to 64 and the value fits into a signed 10-bit
2314// immediate
2315// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2316// is a power of 2 less than or equal to 64 and the value does not fit into a
2317// signed 10-bit immediate
2318// - Non-constant splats are legal as-is.
2319// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2320// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002321SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2322 SelectionDAG &DAG) const {
2323 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2324 EVT ResTy = Op->getValueType(0);
2325 SDLoc DL(Op);
2326 APInt SplatValue, SplatUndef;
2327 unsigned SplatBitSize;
2328 bool HasAnyUndefs;
2329
Eric Christopher1c29a652014-07-18 22:55:25 +00002330 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002331 return SDValue();
2332
2333 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2334 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002335 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002336 // We can only cope with 8, 16, 32, or 64-bit elements
2337 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2338 SplatBitSize != 64)
2339 return SDValue();
2340
2341 // If the value fits into a simm10 then we can use ldi.[bhwd]
Daniel Sandersfd8e4162013-11-22 11:24:50 +00002342 // However, if it isn't an integer type we will have to bitcast from an
Daniel Sandersd40aea82013-11-22 13:22:52 +00002343 // integer type first. Also, if there are any undefs, we must lower them
Daniel Sanders630dbe02013-11-22 13:14:06 +00002344 // to defined values first.
2345 if (ResTy.isInteger() && !HasAnyUndefs && SplatValue.isSignedIntN(10))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002346 return Op;
2347
2348 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002349
2350 switch (SplatBitSize) {
2351 default:
2352 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002353 case 8:
2354 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002355 break;
2356 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002357 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002358 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002359 case 32:
2360 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002361 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002362 case 64:
2363 // There's no fill.d to fall back on for 64-bit values
2364 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002365 }
2366
Daniel Sanders50b80412013-11-15 12:56:49 +00002367 // SelectionDAG::getConstant will promote SplatValue appropriately.
2368 SDValue Result = DAG.getConstant(SplatValue, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002369
Daniel Sanders50b80412013-11-15 12:56:49 +00002370 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002371 if (ViaVecTy != ResTy)
2372 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002373
2374 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002375 } else if (isSplatVector(Node))
2376 return Op;
2377 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002378 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2379 // The resulting code is the same length as the expansion, but it doesn't
2380 // use memory operations
2381 EVT ResTy = Node->getValueType(0);
2382
2383 assert(ResTy.isVector());
2384
2385 unsigned NumElts = ResTy.getVectorNumElements();
2386 SDValue Vector = DAG.getUNDEF(ResTy);
2387 for (unsigned i = 0; i < NumElts; ++i) {
2388 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2389 Node->getOperand(i),
2390 DAG.getConstant(i, MVT::i32));
2391 }
2392 return Vector;
2393 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002394
2395 return SDValue();
2396}
2397
Daniel Sanders26307182013-09-24 14:20:00 +00002398// Lower VECTOR_SHUFFLE into SHF (if possible).
2399//
2400// SHF splits the vector into blocks of four elements, then shuffles these
2401// elements according to a <4 x i2> constant (encoded as an integer immediate).
2402//
2403// It is therefore possible to lower into SHF when the mask takes the form:
2404// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2405// When undef's appear they are treated as if they were whatever value is
2406// necessary in order to fit the above form.
2407//
2408// For example:
2409// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2410// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2411// i32 7, i32 6, i32 5, i32 4>
2412// is lowered to:
2413// (SHF_H $w0, $w1, 27)
2414// where the 27 comes from:
2415// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2416static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2417 SmallVector<int, 16> Indices,
2418 SelectionDAG &DAG) {
2419 int SHFIndices[4] = { -1, -1, -1, -1 };
2420
2421 if (Indices.size() < 4)
2422 return SDValue();
2423
2424 for (unsigned i = 0; i < 4; ++i) {
2425 for (unsigned j = i; j < Indices.size(); j += 4) {
2426 int Idx = Indices[j];
2427
2428 // Convert from vector index to 4-element subvector index
2429 // If an index refers to an element outside of the subvector then give up
2430 if (Idx != -1) {
2431 Idx -= 4 * (j / 4);
2432 if (Idx < 0 || Idx >= 4)
2433 return SDValue();
2434 }
2435
2436 // If the mask has an undef, replace it with the current index.
2437 // Note that it might still be undef if the current index is also undef
2438 if (SHFIndices[i] == -1)
2439 SHFIndices[i] = Idx;
2440
2441 // Check that non-undef values are the same as in the mask. If they
2442 // aren't then give up
2443 if (!(Idx == -1 || Idx == SHFIndices[i]))
2444 return SDValue();
2445 }
2446 }
2447
2448 // Calculate the immediate. Replace any remaining undefs with zero
2449 APInt Imm(32, 0);
2450 for (int i = 3; i >= 0; --i) {
2451 int Idx = SHFIndices[i];
2452
2453 if (Idx == -1)
2454 Idx = 0;
2455
2456 Imm <<= 2;
2457 Imm |= Idx & 0x3;
2458 }
2459
2460 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
2461 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
2462}
2463
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002464// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2465//
2466// ILVEV interleaves the even elements from each vector.
2467//
2468// It is possible to lower into ILVEV when the mask takes the form:
2469// <0, n, 2, n+2, 4, n+4, ...>
2470// where n is the number of elements in the vector.
2471//
2472// When undef's appear in the mask they are treated as if they were whatever
2473// value is necessary in order to fit the above form.
2474static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2475 SmallVector<int, 16> Indices,
2476 SelectionDAG &DAG) {
2477 assert ((Indices.size() % 2) == 0);
2478 int WsIdx = 0;
2479 int WtIdx = ResTy.getVectorNumElements();
2480
2481 for (unsigned i = 0; i < Indices.size(); i += 2) {
2482 if (Indices[i] != -1 && Indices[i] != WsIdx)
2483 return SDValue();
2484 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2485 return SDValue();
2486 WsIdx += 2;
2487 WtIdx += 2;
2488 }
2489
2490 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
2491 Op->getOperand(1));
2492}
2493
2494// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2495//
2496// ILVOD interleaves the odd elements from each vector.
2497//
2498// It is possible to lower into ILVOD when the mask takes the form:
2499// <1, n+1, 3, n+3, 5, n+5, ...>
2500// where n is the number of elements in the vector.
2501//
2502// When undef's appear in the mask they are treated as if they were whatever
2503// value is necessary in order to fit the above form.
2504static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2505 SmallVector<int, 16> Indices,
2506 SelectionDAG &DAG) {
2507 assert ((Indices.size() % 2) == 0);
2508 int WsIdx = 1;
2509 int WtIdx = ResTy.getVectorNumElements() + 1;
2510
2511 for (unsigned i = 0; i < Indices.size(); i += 2) {
2512 if (Indices[i] != -1 && Indices[i] != WsIdx)
2513 return SDValue();
2514 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2515 return SDValue();
2516 WsIdx += 2;
2517 WtIdx += 2;
2518 }
2519
2520 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
2521 Op->getOperand(1));
2522}
2523
2524// Lower VECTOR_SHUFFLE into ILVL (if possible).
2525//
2526// ILVL interleaves consecutive elements from the left half of each vector.
2527//
2528// It is possible to lower into ILVL when the mask takes the form:
2529// <0, n, 1, n+1, 2, n+2, ...>
2530// where n is the number of elements in the vector.
2531//
2532// When undef's appear in the mask they are treated as if they were whatever
2533// value is necessary in order to fit the above form.
2534static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2535 SmallVector<int, 16> Indices,
2536 SelectionDAG &DAG) {
2537 assert ((Indices.size() % 2) == 0);
2538 int WsIdx = 0;
2539 int WtIdx = ResTy.getVectorNumElements();
2540
2541 for (unsigned i = 0; i < Indices.size(); i += 2) {
2542 if (Indices[i] != -1 && Indices[i] != WsIdx)
2543 return SDValue();
2544 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2545 return SDValue();
2546 WsIdx ++;
2547 WtIdx ++;
2548 }
2549
2550 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2551 Op->getOperand(1));
2552}
2553
2554// Lower VECTOR_SHUFFLE into ILVR (if possible).
2555//
2556// ILVR interleaves consecutive elements from the right half of each vector.
2557//
2558// It is possible to lower into ILVR when the mask takes the form:
2559// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2560// where n is the number of elements in the vector and x is half n.
2561//
2562// When undef's appear in the mask they are treated as if they were whatever
2563// value is necessary in order to fit the above form.
2564static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2565 SmallVector<int, 16> Indices,
2566 SelectionDAG &DAG) {
2567 assert ((Indices.size() % 2) == 0);
2568 unsigned NumElts = ResTy.getVectorNumElements();
2569 int WsIdx = NumElts / 2;
2570 int WtIdx = NumElts + NumElts / 2;
2571
2572 for (unsigned i = 0; i < Indices.size(); i += 2) {
2573 if (Indices[i] != -1 && Indices[i] != WsIdx)
2574 return SDValue();
2575 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2576 return SDValue();
2577 WsIdx ++;
2578 WtIdx ++;
2579 }
2580
2581 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2582 Op->getOperand(1));
2583}
2584
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002585// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2586//
2587// PCKEV copies the even elements of each vector into the result vector.
2588//
2589// It is possible to lower into PCKEV when the mask takes the form:
2590// <0, 2, 4, ..., n, n+2, n+4, ...>
2591// where n is the number of elements in the vector.
2592//
2593// When undef's appear in the mask they are treated as if they were whatever
2594// value is necessary in order to fit the above form.
2595static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2596 SmallVector<int, 16> Indices,
2597 SelectionDAG &DAG) {
2598 assert ((Indices.size() % 2) == 0);
2599 int Idx = 0;
2600
2601 for (unsigned i = 0; i < Indices.size(); ++i) {
2602 if (Indices[i] != -1 && Indices[i] != Idx)
2603 return SDValue();
2604 Idx += 2;
2605 }
2606
2607 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2608 Op->getOperand(1));
2609}
2610
2611// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2612//
2613// PCKOD copies the odd elements of each vector into the result vector.
2614//
2615// It is possible to lower into PCKOD when the mask takes the form:
2616// <1, 3, 5, ..., n+1, n+3, n+5, ...>
2617// where n is the number of elements in the vector.
2618//
2619// When undef's appear in the mask they are treated as if they were whatever
2620// value is necessary in order to fit the above form.
2621static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2622 SmallVector<int, 16> Indices,
2623 SelectionDAG &DAG) {
2624 assert ((Indices.size() % 2) == 0);
2625 int Idx = 1;
2626
2627 for (unsigned i = 0; i < Indices.size(); ++i) {
2628 if (Indices[i] != -1 && Indices[i] != Idx)
2629 return SDValue();
2630 Idx += 2;
2631 }
2632
2633 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2634 Op->getOperand(1));
2635}
2636
Daniel Sanderse5087042013-09-24 14:02:15 +00002637// Lower VECTOR_SHUFFLE into VSHF.
2638//
2639// This mostly consists of converting the shuffle indices in Indices into a
2640// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2641// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2642// if the type is v8i16 and all the indices are less than 8 then the second
2643// operand is unused and can be replaced with anything. We choose to replace it
2644// with the used operand since this reduces the number of instructions overall.
2645static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2646 SmallVector<int, 16> Indices,
2647 SelectionDAG &DAG) {
2648 SmallVector<SDValue, 16> Ops;
2649 SDValue Op0;
2650 SDValue Op1;
2651 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2652 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2653 bool Using1stVec = false;
2654 bool Using2ndVec = false;
2655 SDLoc DL(Op);
2656 int ResTyNumElts = ResTy.getVectorNumElements();
2657
2658 for (int i = 0; i < ResTyNumElts; ++i) {
2659 // Idx == -1 means UNDEF
2660 int Idx = Indices[i];
2661
2662 if (0 <= Idx && Idx < ResTyNumElts)
2663 Using1stVec = true;
2664 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2665 Using2ndVec = true;
2666 }
2667
2668 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2669 ++I)
2670 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2671
Craig Topper48d114b2014-04-26 18:35:24 +00002672 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002673
2674 if (Using1stVec && Using2ndVec) {
2675 Op0 = Op->getOperand(0);
2676 Op1 = Op->getOperand(1);
2677 } else if (Using1stVec)
2678 Op0 = Op1 = Op->getOperand(0);
2679 else if (Using2ndVec)
2680 Op0 = Op1 = Op->getOperand(1);
2681 else
2682 llvm_unreachable("shuffle vector mask references neither vector operand?");
2683
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002684 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2685 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2686 // VSHF concatenates the vectors in a bitwise fashion:
2687 // <0b00, 0b01> + <0b10, 0b11> ->
2688 // 0b0100 + 0b1110 -> 0b01001110
2689 // <0b10, 0b11, 0b00, 0b01>
2690 // We must therefore swap the operands to get the correct result.
2691 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002692}
2693
2694// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2695// indices in the shuffle.
2696SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2697 SelectionDAG &DAG) const {
2698 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2699 EVT ResTy = Op->getValueType(0);
2700
2701 if (!ResTy.is128BitVector())
2702 return SDValue();
2703
2704 int ResTyNumElts = ResTy.getVectorNumElements();
2705 SmallVector<int, 16> Indices;
2706
2707 for (int i = 0; i < ResTyNumElts; ++i)
2708 Indices.push_back(Node->getMaskElt(i));
2709
Daniel Sanders26307182013-09-24 14:20:00 +00002710 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2711 if (Result.getNode())
2712 return Result;
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002713 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2714 if (Result.getNode())
2715 return Result;
2716 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2717 if (Result.getNode())
2718 return Result;
2719 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2720 if (Result.getNode())
2721 return Result;
2722 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2723 if (Result.getNode())
2724 return Result;
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002725 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2726 if (Result.getNode())
2727 return Result;
2728 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2729 if (Result.getNode())
2730 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002731 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2732}
2733
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002734MachineBasicBlock * MipsSETargetLowering::
2735emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2736 // $bb:
2737 // bposge32_pseudo $vr0
2738 // =>
2739 // $bb:
2740 // bposge32 $tbb
2741 // $fbb:
2742 // li $vr2, 0
2743 // b $sink
2744 // $tbb:
2745 // li $vr1, 1
2746 // $sink:
2747 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2748
2749 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2750 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002751 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002752 DebugLoc DL = MI->getDebugLoc();
2753 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002754 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002755 MachineFunction *F = BB->getParent();
2756 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2757 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2758 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2759 F->insert(It, FBB);
2760 F->insert(It, TBB);
2761 F->insert(It, Sink);
2762
2763 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002764 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002765 BB->end());
2766 Sink->transferSuccessorsAndUpdatePHIs(BB);
2767
2768 // Add successors.
2769 BB->addSuccessor(FBB);
2770 BB->addSuccessor(TBB);
2771 FBB->addSuccessor(Sink);
2772 TBB->addSuccessor(Sink);
2773
2774 // Insert the real bposge32 instruction to $BB.
2775 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2776
2777 // Fill $FBB.
2778 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2779 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2780 .addReg(Mips::ZERO).addImm(0);
2781 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2782
2783 // Fill $TBB.
2784 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2785 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2786 .addReg(Mips::ZERO).addImm(1);
2787
2788 // Insert phi function to $Sink.
2789 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2790 MI->getOperand(0).getReg())
2791 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2792
2793 MI->eraseFromParent(); // The pseudo instruction is gone now.
2794 return Sink;
2795}
Daniel Sandersce09d072013-08-28 12:14:50 +00002796
2797MachineBasicBlock * MipsSETargetLowering::
2798emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2799 unsigned BranchOp) const{
2800 // $bb:
2801 // vany_nonzero $rd, $ws
2802 // =>
2803 // $bb:
2804 // bnz.b $ws, $tbb
2805 // b $fbb
2806 // $fbb:
2807 // li $rd1, 0
2808 // b $sink
2809 // $tbb:
2810 // li $rd2, 1
2811 // $sink:
2812 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2813
2814 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2815 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2816 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2817 DebugLoc DL = MI->getDebugLoc();
2818 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002819 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00002820 MachineFunction *F = BB->getParent();
2821 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2822 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2823 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2824 F->insert(It, FBB);
2825 F->insert(It, TBB);
2826 F->insert(It, Sink);
2827
2828 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002829 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00002830 BB->end());
2831 Sink->transferSuccessorsAndUpdatePHIs(BB);
2832
2833 // Add successors.
2834 BB->addSuccessor(FBB);
2835 BB->addSuccessor(TBB);
2836 FBB->addSuccessor(Sink);
2837 TBB->addSuccessor(Sink);
2838
2839 // Insert the real bnz.b instruction to $BB.
2840 BuildMI(BB, DL, TII->get(BranchOp))
2841 .addReg(MI->getOperand(1).getReg())
2842 .addMBB(TBB);
2843
2844 // Fill $FBB.
2845 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2846 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2847 .addReg(Mips::ZERO).addImm(0);
2848 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2849
2850 // Fill $TBB.
2851 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2852 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2853 .addReg(Mips::ZERO).addImm(1);
2854
2855 // Insert phi function to $Sink.
2856 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2857 MI->getOperand(0).getReg())
2858 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2859
2860 MI->eraseFromParent(); // The pseudo instruction is gone now.
2861 return Sink;
2862}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002863
2864// Emit the COPY_FW pseudo instruction.
2865//
2866// copy_fw_pseudo $fd, $ws, n
2867// =>
2868// copy_u_w $rt, $ws, $n
2869// mtc1 $rt, $fd
2870//
2871// When n is zero, the equivalent operation can be performed with (potentially)
2872// zero instructions due to register overlaps. This optimization is never valid
2873// for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2874MachineBasicBlock * MipsSETargetLowering::
2875emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2876 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2877 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2878 DebugLoc DL = MI->getDebugLoc();
2879 unsigned Fd = MI->getOperand(0).getReg();
2880 unsigned Ws = MI->getOperand(1).getReg();
2881 unsigned Lane = MI->getOperand(2).getImm();
2882
2883 if (Lane == 0)
2884 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2885 else {
2886 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2887
Daniel Sandersd9207702014-03-04 13:54:30 +00002888 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002889 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2890 }
2891
2892 MI->eraseFromParent(); // The pseudo instruction is gone now.
2893 return BB;
2894}
2895
2896// Emit the COPY_FD pseudo instruction.
2897//
2898// copy_fd_pseudo $fd, $ws, n
2899// =>
2900// splati.d $wt, $ws, $n
2901// copy $fd, $wt:sub_64
2902//
2903// When n is zero, the equivalent operation can be performed with (potentially)
2904// zero instructions due to register overlaps. This optimization is always
2905// valid because FR=1 mode which is the only supported mode in MSA.
2906MachineBasicBlock * MipsSETargetLowering::
2907emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
Eric Christopher1c29a652014-07-18 22:55:25 +00002908 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002909
2910 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2911 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2912 unsigned Fd = MI->getOperand(0).getReg();
2913 unsigned Ws = MI->getOperand(1).getReg();
2914 unsigned Lane = MI->getOperand(2).getImm() * 2;
2915 DebugLoc DL = MI->getDebugLoc();
2916
2917 if (Lane == 0)
2918 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2919 else {
2920 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2921
2922 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2923 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2924 }
2925
2926 MI->eraseFromParent(); // The pseudo instruction is gone now.
2927 return BB;
2928}
Daniel Sandersa5150702013-09-27 12:31:32 +00002929
2930// Emit the INSERT_FW pseudo instruction.
2931//
2932// insert_fw_pseudo $wd, $wd_in, $n, $fs
2933// =>
2934// subreg_to_reg $wt:sub_lo, $fs
2935// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002936MachineBasicBlock *
2937MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2938 MachineBasicBlock *BB) const {
Daniel Sandersa5150702013-09-27 12:31:32 +00002939 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2940 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2941 DebugLoc DL = MI->getDebugLoc();
2942 unsigned Wd = MI->getOperand(0).getReg();
2943 unsigned Wd_in = MI->getOperand(1).getReg();
2944 unsigned Lane = MI->getOperand(2).getImm();
2945 unsigned Fs = MI->getOperand(3).getReg();
2946 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2947
2948 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002949 .addImm(0)
2950 .addReg(Fs)
2951 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00002952 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002953 .addReg(Wd_in)
2954 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00002955 .addReg(Wt)
2956 .addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00002957
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002958 MI->eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00002959 return BB;
2960}
2961
2962// Emit the INSERT_FD pseudo instruction.
2963//
2964// insert_fd_pseudo $wd, $fs, n
2965// =>
2966// subreg_to_reg $wt:sub_64, $fs
2967// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002968MachineBasicBlock *
2969MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2970 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002971 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00002972
2973 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2974 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2975 DebugLoc DL = MI->getDebugLoc();
2976 unsigned Wd = MI->getOperand(0).getReg();
2977 unsigned Wd_in = MI->getOperand(1).getReg();
2978 unsigned Lane = MI->getOperand(2).getImm();
2979 unsigned Fs = MI->getOperand(3).getReg();
2980 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2981
2982 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002983 .addImm(0)
2984 .addReg(Fs)
2985 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00002986 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002987 .addReg(Wd_in)
2988 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00002989 .addReg(Wt)
2990 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002991
2992 MI->eraseFromParent(); // The pseudo instruction is gone now.
2993 return BB;
2994}
2995
Daniel Sanderse296a0f2014-04-30 12:09:32 +00002996// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
2997//
2998// For integer:
2999// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3000// =>
3001// (SLL $lanetmp1, $lane, <log2size)
3002// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3003// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3004// (NEG $lanetmp2, $lanetmp1)
3005// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3006//
3007// For floating point:
3008// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3009// =>
3010// (SUBREG_TO_REG $wt, $fs, <subreg>)
3011// (SLL $lanetmp1, $lane, <log2size)
3012// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3013// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3014// (NEG $lanetmp2, $lanetmp1)
3015// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3016MachineBasicBlock *
3017MipsSETargetLowering::emitINSERT_DF_VIDX(MachineInstr *MI,
3018 MachineBasicBlock *BB,
3019 unsigned EltSizeInBytes,
3020 bool IsFP) const {
3021 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3022 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3023 DebugLoc DL = MI->getDebugLoc();
3024 unsigned Wd = MI->getOperand(0).getReg();
3025 unsigned SrcVecReg = MI->getOperand(1).getReg();
3026 unsigned LaneReg = MI->getOperand(2).getReg();
3027 unsigned SrcValReg = MI->getOperand(3).getReg();
3028
3029 const TargetRegisterClass *VecRC = nullptr;
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003030 const TargetRegisterClass *GPRRC =
Eric Christopher1c29a652014-07-18 22:55:25 +00003031 Subtarget.isGP64bit() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003032 unsigned EltLog2Size;
3033 unsigned InsertOp = 0;
3034 unsigned InsveOp = 0;
3035 switch (EltSizeInBytes) {
3036 default:
3037 llvm_unreachable("Unexpected size");
3038 case 1:
3039 EltLog2Size = 0;
3040 InsertOp = Mips::INSERT_B;
3041 InsveOp = Mips::INSVE_B;
3042 VecRC = &Mips::MSA128BRegClass;
3043 break;
3044 case 2:
3045 EltLog2Size = 1;
3046 InsertOp = Mips::INSERT_H;
3047 InsveOp = Mips::INSVE_H;
3048 VecRC = &Mips::MSA128HRegClass;
3049 break;
3050 case 4:
3051 EltLog2Size = 2;
3052 InsertOp = Mips::INSERT_W;
3053 InsveOp = Mips::INSVE_W;
3054 VecRC = &Mips::MSA128WRegClass;
3055 break;
3056 case 8:
3057 EltLog2Size = 3;
3058 InsertOp = Mips::INSERT_D;
3059 InsveOp = Mips::INSVE_D;
3060 VecRC = &Mips::MSA128DRegClass;
3061 break;
3062 }
3063
3064 if (IsFP) {
3065 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3066 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3067 .addImm(0)
3068 .addReg(SrcValReg)
3069 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3070 SrcValReg = Wt;
3071 }
3072
3073 // Convert the lane index into a byte index
3074 if (EltSizeInBytes != 1) {
3075 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
3076 BuildMI(*BB, MI, DL, TII->get(Mips::SLL), LaneTmp1)
3077 .addReg(LaneReg)
3078 .addImm(EltLog2Size);
3079 LaneReg = LaneTmp1;
3080 }
3081
3082 // Rotate bytes around so that the desired lane is element zero
3083 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3084 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3085 .addReg(SrcVecReg)
3086 .addReg(SrcVecReg)
3087 .addReg(LaneReg);
3088
3089 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3090 if (IsFP) {
3091 // Use insve.df to insert to element zero
3092 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3093 .addReg(WdTmp1)
3094 .addImm(0)
3095 .addReg(SrcValReg)
3096 .addImm(0);
3097 } else {
3098 // Use insert.df to insert to element zero
3099 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3100 .addReg(WdTmp1)
3101 .addReg(SrcValReg)
3102 .addImm(0);
3103 }
3104
3105 // Rotate elements the rest of the way for a full rotation.
3106 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3107 // the lane index to do this.
3108 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
3109 BuildMI(*BB, MI, DL, TII->get(Mips::SUB), LaneTmp2)
3110 .addReg(Mips::ZERO)
3111 .addReg(LaneReg);
3112 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3113 .addReg(WdTmp2)
3114 .addReg(WdTmp2)
3115 .addReg(LaneTmp2);
3116
3117 MI->eraseFromParent(); // The pseudo instruction is gone now.
3118 return BB;
3119}
3120
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003121// Emit the FILL_FW pseudo instruction.
3122//
3123// fill_fw_pseudo $wd, $fs
3124// =>
3125// implicit_def $wt1
3126// insert_subreg $wt2:subreg_lo, $wt1, $fs
3127// splati.w $wd, $wt2[0]
3128MachineBasicBlock *
3129MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
3130 MachineBasicBlock *BB) const {
3131 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3132 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3133 DebugLoc DL = MI->getDebugLoc();
3134 unsigned Wd = MI->getOperand(0).getReg();
3135 unsigned Fs = MI->getOperand(1).getReg();
3136 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3137 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3138
3139 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3140 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3141 .addReg(Wt1)
3142 .addReg(Fs)
3143 .addImm(Mips::sub_lo);
3144 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3145
3146 MI->eraseFromParent(); // The pseudo instruction is gone now.
3147 return BB;
3148}
3149
3150// Emit the FILL_FD pseudo instruction.
3151//
3152// fill_fd_pseudo $wd, $fs
3153// =>
3154// implicit_def $wt1
3155// insert_subreg $wt2:subreg_64, $wt1, $fs
3156// splati.d $wd, $wt2[0]
3157MachineBasicBlock *
3158MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
3159 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003160 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003161
3162 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3163 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3164 DebugLoc DL = MI->getDebugLoc();
3165 unsigned Wd = MI->getOperand(0).getReg();
3166 unsigned Fs = MI->getOperand(1).getReg();
3167 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3168 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3169
3170 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3171 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3172 .addReg(Wt1)
3173 .addReg(Fs)
3174 .addImm(Mips::sub_64);
3175 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003176
3177 MI->eraseFromParent(); // The pseudo instruction is gone now.
3178 return BB;
3179}
Daniel Sandersa9521602013-10-23 10:36:52 +00003180
3181// Emit the FEXP2_W_1 pseudo instructions.
3182//
3183// fexp2_w_1_pseudo $wd, $wt
3184// =>
3185// ldi.w $ws, 1
3186// fexp2.w $wd, $ws, $wt
3187MachineBasicBlock *
3188MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
3189 MachineBasicBlock *BB) const {
3190 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3191 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3192 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3193 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3194 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
3195 DebugLoc DL = MI->getDebugLoc();
3196
3197 // Splat 1.0 into a vector
3198 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3199 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3200
3201 // Emit 1.0 * fexp2(Wt)
3202 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
3203 .addReg(Ws2)
3204 .addReg(MI->getOperand(1).getReg());
3205
3206 MI->eraseFromParent(); // The pseudo instruction is gone now.
3207 return BB;
3208}
3209
3210// Emit the FEXP2_D_1 pseudo instructions.
3211//
3212// fexp2_d_1_pseudo $wd, $wt
3213// =>
3214// ldi.d $ws, 1
3215// fexp2.d $wd, $ws, $wt
3216MachineBasicBlock *
3217MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
3218 MachineBasicBlock *BB) const {
3219 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3220 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3221 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3222 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3223 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
3224 DebugLoc DL = MI->getDebugLoc();
3225
3226 // Splat 1.0 into a vector
3227 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3228 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3229
3230 // Emit 1.0 * fexp2(Wt)
3231 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
3232 .addReg(Ws2)
3233 .addReg(MI->getOperand(1).getReg());
3234
3235 MI->eraseFromParent(); // The pseudo instruction is gone now.
3236 return BB;
3237}