blob: 0733a62cc8a4a79a7d1d0b9381b20fac8b26b447 [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 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000581 return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
582 Op0->getVTList(),
583 makeArrayRef(Ops, Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000584 }
585 }
586
587 return SDValue();
588}
589
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000590// Determine if the specified node is a constant vector splat.
591//
592// Returns true and sets Imm if:
593// * N is a ISD::BUILD_VECTOR representing a constant splat
594//
595// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
596// differences are that it assumes the MSA has already been checked and the
597// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
598// must not be in order for binsri.d to be selectable).
599static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
600 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
601
Craig Topper062a2ba2014-04-25 05:30:21 +0000602 if (!Node)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000603 return false;
604
605 APInt SplatValue, SplatUndef;
606 unsigned SplatBitSize;
607 bool HasAnyUndefs;
608
609 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
610 8, !IsLittleEndian))
611 return false;
612
613 Imm = SplatValue;
614
615 return true;
616}
617
Daniel Sandersab94b532013-10-30 15:20:38 +0000618// Test whether the given node is an all-ones build_vector.
619static bool isVectorAllOnes(SDValue N) {
620 // Look through bitcasts. Endianness doesn't matter because we are looking
621 // for an all-ones value.
622 if (N->getOpcode() == ISD::BITCAST)
623 N = N->getOperand(0);
624
625 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
626
627 if (!BVN)
628 return false;
629
630 APInt SplatValue, SplatUndef;
631 unsigned SplatBitSize;
632 bool HasAnyUndefs;
633
634 // Endianness doesn't matter in this context because we are looking for
635 // an all-ones value.
636 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
637 return SplatValue.isAllOnesValue();
638
639 return false;
640}
641
642// Test whether N is the bitwise inverse of OfNode.
643static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
644 if (N->getOpcode() != ISD::XOR)
645 return false;
646
647 if (isVectorAllOnes(N->getOperand(0)))
648 return N->getOperand(1) == OfNode;
649
650 if (isVectorAllOnes(N->getOperand(1)))
651 return N->getOperand(0) == OfNode;
652
653 return false;
654}
655
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000656// Perform combines where ISD::OR is the root node.
657//
658// Performs the following transformations:
659// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
660// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
661// vector type.
662static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
663 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000664 const MipsSubtarget &Subtarget) {
665 if (!Subtarget.hasMSA())
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000666 return SDValue();
667
668 EVT Ty = N->getValueType(0);
669
670 if (!Ty.is128BitVector())
671 return SDValue();
672
673 SDValue Op0 = N->getOperand(0);
674 SDValue Op1 = N->getOperand(1);
675
676 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
677 SDValue Op0Op0 = Op0->getOperand(0);
678 SDValue Op0Op1 = Op0->getOperand(1);
679 SDValue Op1Op0 = Op1->getOperand(0);
680 SDValue Op1Op1 = Op1->getOperand(1);
Eric Christopher1c29a652014-07-18 22:55:25 +0000681 bool IsLittleEndian = !Subtarget.isLittle();
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000682
683 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000684 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000685 APInt Mask, InvMask;
686
687 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
688 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
689 // looking.
690 // IfClr will be set if we find a valid match.
691 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
692 Cond = Op0Op0;
693 IfSet = Op0Op1;
694
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000695 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
696 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000697 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000698 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
699 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000700 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000701
702 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000703 }
704
705 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
706 // thing again using this mask.
707 // IfClr will be set if we find a valid match.
708 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
709 Cond = Op0Op1;
710 IfSet = Op0Op0;
711
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000712 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
713 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000714 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000715 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
716 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000717 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000718
719 IsConstantMask = true;
720 }
721
722 // If IfClr is not yet set, try looking for a non-constant match.
723 // IfClr will be set if we find a valid match amongst the eight
724 // possibilities.
725 if (!IfClr.getNode()) {
726 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
727 Cond = Op1Op0;
728 IfSet = Op1Op1;
729 IfClr = Op0Op1;
730 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
731 Cond = Op1Op0;
732 IfSet = Op1Op1;
733 IfClr = Op0Op0;
734 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
735 Cond = Op1Op1;
736 IfSet = Op1Op0;
737 IfClr = Op0Op1;
738 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
739 Cond = Op1Op1;
740 IfSet = Op1Op0;
741 IfClr = Op0Op0;
742 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
743 Cond = Op0Op0;
744 IfSet = Op0Op1;
745 IfClr = Op1Op1;
746 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
747 Cond = Op0Op0;
748 IfSet = Op0Op1;
749 IfClr = Op1Op0;
750 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
751 Cond = Op0Op1;
752 IfSet = Op0Op0;
753 IfClr = Op1Op1;
754 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
755 Cond = Op0Op1;
756 IfSet = Op0Op0;
757 IfClr = Op1Op0;
758 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000759 }
760
761 // At this point, IfClr will be set if we have a valid match.
762 if (!IfClr.getNode())
763 return SDValue();
764
765 assert(Cond.getNode() && IfSet.getNode());
766
767 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000768 if (IsConstantMask) {
769 if (Mask.isAllOnesValue())
770 return IfSet;
771 else if (Mask == 0)
772 return IfClr;
773 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000774
775 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000776 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000777 }
778
779 return SDValue();
780}
781
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000782static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
783 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000784 const MipsSubtarget &Subtarget) {
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000785 if (DCI.isBeforeLegalize())
786 return SDValue();
787
Eric Christopher1c29a652014-07-18 22:55:25 +0000788 if (Subtarget.hasMips32() && N->getValueType(0) == MVT::i32 &&
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000789 selectMSUB(N, &DAG))
790 return SDValue(N, 0);
791
792 return SDValue();
793}
794
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000795static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
796 EVT ShiftTy, SelectionDAG &DAG) {
797 // Clear the upper (64 - VT.sizeInBits) bits.
798 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
799
800 // Return 0.
801 if (C == 0)
802 return DAG.getConstant(0, VT);
803
804 // Return x.
805 if (C == 1)
806 return X;
807
808 // If c is power of 2, return (shl x, log2(c)).
809 if (isPowerOf2_64(C))
810 return DAG.getNode(ISD::SHL, DL, VT, X,
811 DAG.getConstant(Log2_64(C), ShiftTy));
812
813 unsigned Log2Ceil = Log2_64_Ceil(C);
814 uint64_t Floor = 1LL << Log2_64(C);
815 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
816
817 // If |c - floor_c| <= |c - ceil_c|,
818 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
819 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
820 if (C - Floor <= Ceil - C) {
821 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
822 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
823 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
824 }
825
826 // If |c - floor_c| > |c - ceil_c|,
827 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
828 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
829 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
830 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
831}
832
833static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
834 const TargetLowering::DAGCombinerInfo &DCI,
835 const MipsSETargetLowering *TL) {
836 EVT VT = N->getValueType(0);
837
838 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
839 if (!VT.isVector())
840 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
841 VT, TL->getScalarShiftAmountTy(VT), DAG);
842
843 return SDValue(N, 0);
844}
845
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000846static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
847 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000848 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000849 // See if this is a vector splat immediate node.
850 APInt SplatValue, SplatUndef;
851 unsigned SplatBitSize;
852 bool HasAnyUndefs;
853 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
854 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
855
Eric Christopher1c29a652014-07-18 22:55:25 +0000856 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000857 return SDValue();
858
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000859 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000860 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000861 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000862 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000863 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000864 return SDValue();
865
Andrew Trickef9de2a2013-05-25 02:42:55 +0000866 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000867 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
868}
869
870static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
871 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000872 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000873 EVT Ty = N->getValueType(0);
874
875 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
876 return SDValue();
877
878 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
879}
880
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000881// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
882// constant splats into MipsISD::SHRA_DSP for DSPr2.
883//
884// Performs the following transformations:
885// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
886// sign/zero-extension is completely overwritten by the new one performed by
887// the ISD::SRA and ISD::SHL nodes.
888// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
889// sequence.
890//
891// See performDSPShiftCombine for more information about the transformation
892// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000893static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
894 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000895 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000896 EVT Ty = N->getValueType(0);
897
Eric Christopher1c29a652014-07-18 22:55:25 +0000898 if (Subtarget.hasMSA()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000899 SDValue Op0 = N->getOperand(0);
900 SDValue Op1 = N->getOperand(1);
901
902 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
903 // where $d + sizeof($c) == 32
904 // or $d + sizeof($c) <= 32 and SExt
905 // -> (MipsVExtractSExt $a, $b, $c)
906 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
907 SDValue Op0Op0 = Op0->getOperand(0);
908 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
909
910 if (!ShAmount)
911 return SDValue();
912
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000913 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
914 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
915 return SDValue();
916
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000917 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
918 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
919
920 if (TotalBits == 32 ||
921 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
922 TotalBits <= 32)) {
923 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
924 Op0Op0->getOperand(2) };
Chandler Carruth356665a2014-08-01 22:09:43 +0000925 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
926 Op0Op0->getVTList(),
927 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000928 }
929 }
930 }
931
Eric Christopher1c29a652014-07-18 22:55:25 +0000932 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000933 return SDValue();
934
935 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
936}
937
938
939static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
940 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000941 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000942 EVT Ty = N->getValueType(0);
943
Eric Christopher1c29a652014-07-18 22:55:25 +0000944 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000945 return SDValue();
946
947 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
948}
949
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000950static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
951 bool IsV216 = (Ty == MVT::v2i16);
952
953 switch (CC) {
954 case ISD::SETEQ:
955 case ISD::SETNE: return true;
956 case ISD::SETLT:
957 case ISD::SETLE:
958 case ISD::SETGT:
959 case ISD::SETGE: return IsV216;
960 case ISD::SETULT:
961 case ISD::SETULE:
962 case ISD::SETUGT:
963 case ISD::SETUGE: return !IsV216;
964 default: return false;
965 }
966}
967
968static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
969 EVT Ty = N->getValueType(0);
970
971 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
972 return SDValue();
973
974 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
975 return SDValue();
976
Andrew Trickef9de2a2013-05-25 02:42:55 +0000977 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000978 N->getOperand(1), N->getOperand(2));
979}
980
981static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
982 EVT Ty = N->getValueType(0);
983
Daniel Sanders3ce56622013-09-24 12:18:31 +0000984 if (Ty.is128BitVector() && Ty.isInteger()) {
985 // Try the following combines:
986 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
987 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
988 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
989 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
990 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
991 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
992 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
993 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
994 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
995 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
996 // legalizer.
997 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000998
Daniel Sanders3ce56622013-09-24 12:18:31 +0000999 if (Op0->getOpcode() != ISD::SETCC)
1000 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001001
Daniel Sanders3ce56622013-09-24 12:18:31 +00001002 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
1003 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001004
Daniel Sanders3ce56622013-09-24 12:18:31 +00001005 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
1006 Signed = true;
1007 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
1008 Signed = false;
1009 else
1010 return SDValue();
1011
1012 SDValue Op1 = N->getOperand(1);
1013 SDValue Op2 = N->getOperand(2);
1014 SDValue Op0Op0 = Op0->getOperand(0);
1015 SDValue Op0Op1 = Op0->getOperand(1);
1016
1017 if (Op1 == Op0Op0 && Op2 == Op0Op1)
1018 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
1019 Ty, Op1, Op2);
1020 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
1021 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
1022 Ty, Op1, Op2);
1023 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
1024 SDValue SetCC = N->getOperand(0);
1025
1026 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
1027 return SDValue();
1028
1029 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
1030 SetCC.getOperand(0), SetCC.getOperand(1),
1031 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
1032 }
1033
1034 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001035}
1036
Daniel Sandersf7456c72013-09-23 13:22:24 +00001037static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001038 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001039 EVT Ty = N->getValueType(0);
1040
Eric Christopher1c29a652014-07-18 22:55:25 +00001041 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001042 // Try the following combines:
1043 // (xor (or $a, $b), (build_vector allones))
1044 // (xor (or $a, $b), (bitcast (build_vector allones)))
1045 SDValue Op0 = N->getOperand(0);
1046 SDValue Op1 = N->getOperand(1);
1047 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001048
1049 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
1050 NotOp = Op1;
1051 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
1052 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001053 else
1054 return SDValue();
1055
1056 if (NotOp->getOpcode() == ISD::OR)
1057 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
1058 NotOp->getOperand(1));
1059 }
1060
1061 return SDValue();
1062}
1063
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001064SDValue
1065MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1066 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001067 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001068
1069 switch (N->getOpcode()) {
1070 case ISD::ADDE:
1071 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001072 case ISD::AND:
1073 Val = performANDCombine(N, DAG, DCI, Subtarget);
1074 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +00001075 case ISD::OR:
1076 Val = performORCombine(N, DAG, DCI, Subtarget);
1077 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001078 case ISD::SUBE:
1079 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka5832fc62013-06-26 18:48:17 +00001080 case ISD::MUL:
1081 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001082 case ISD::SHL:
1083 return performSHLCombine(N, DAG, DCI, Subtarget);
1084 case ISD::SRA:
1085 return performSRACombine(N, DAG, DCI, Subtarget);
1086 case ISD::SRL:
1087 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001088 case ISD::VSELECT:
1089 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +00001090 case ISD::XOR:
1091 Val = performXORCombine(N, DAG, Subtarget);
1092 break;
1093 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001094 Val = performSETCCCombine(N, DAG);
1095 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001096 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001097
Daniel Sanders62aeab82013-10-30 13:31:27 +00001098 if (Val.getNode()) {
1099 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1100 N->printrWithDepth(dbgs(), &DAG);
1101 dbgs() << "\n=> \n";
1102 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1103 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001104 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001105 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001106
1107 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001108}
1109
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001110MachineBasicBlock *
1111MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1112 MachineBasicBlock *BB) const {
1113 switch (MI->getOpcode()) {
1114 default:
1115 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1116 case Mips::BPOSGE32_PSEUDO:
1117 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001118 case Mips::SNZ_B_PSEUDO:
1119 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1120 case Mips::SNZ_H_PSEUDO:
1121 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1122 case Mips::SNZ_W_PSEUDO:
1123 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1124 case Mips::SNZ_D_PSEUDO:
1125 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1126 case Mips::SNZ_V_PSEUDO:
1127 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1128 case Mips::SZ_B_PSEUDO:
1129 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1130 case Mips::SZ_H_PSEUDO:
1131 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1132 case Mips::SZ_W_PSEUDO:
1133 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1134 case Mips::SZ_D_PSEUDO:
1135 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1136 case Mips::SZ_V_PSEUDO:
1137 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001138 case Mips::COPY_FW_PSEUDO:
1139 return emitCOPY_FW(MI, BB);
1140 case Mips::COPY_FD_PSEUDO:
1141 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001142 case Mips::INSERT_FW_PSEUDO:
1143 return emitINSERT_FW(MI, BB);
1144 case Mips::INSERT_FD_PSEUDO:
1145 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001146 case Mips::INSERT_B_VIDX_PSEUDO:
1147 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1148 case Mips::INSERT_H_VIDX_PSEUDO:
1149 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1150 case Mips::INSERT_W_VIDX_PSEUDO:
1151 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1152 case Mips::INSERT_D_VIDX_PSEUDO:
1153 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1154 case Mips::INSERT_FW_VIDX_PSEUDO:
1155 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1156 case Mips::INSERT_FD_VIDX_PSEUDO:
1157 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001158 case Mips::FILL_FW_PSEUDO:
1159 return emitFILL_FW(MI, BB);
1160 case Mips::FILL_FD_PSEUDO:
1161 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001162 case Mips::FEXP2_W_1_PSEUDO:
1163 return emitFEXP2_W_1(MI, BB);
1164 case Mips::FEXP2_D_1_PSEUDO:
1165 return emitFEXP2_D_1(MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001166 }
1167}
1168
1169bool MipsSETargetLowering::
1170isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
1171 unsigned NextStackOffset,
1172 const MipsFunctionInfo& FI) const {
1173 if (!EnableMipsTailCalls)
1174 return false;
1175
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001176 // Return false if either the callee or caller has a byval argument.
1177 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
1178 return false;
1179
1180 // Return true if the callee's argument area is no larger than the
1181 // caller's.
1182 return NextStackOffset <= FI.getIncomingArgSize();
1183}
1184
1185void MipsSETargetLowering::
1186getOpndList(SmallVectorImpl<SDValue> &Ops,
1187 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
1188 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
1189 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001190 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001191 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
1192 InternalLinkage, CLI, Callee, Chain);
1193}
1194
Akira Hatanaka63791212013-09-07 00:52:30 +00001195SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1196 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1197
1198 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1199 return MipsTargetLowering::lowerLOAD(Op, DAG);
1200
1201 // Replace a double precision load with two i32 loads and a buildpair64.
1202 SDLoc DL(Op);
1203 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1204 EVT PtrVT = Ptr.getValueType();
1205
1206 // i32 load from lower address.
1207 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1208 MachinePointerInfo(), Nd.isVolatile(),
1209 Nd.isNonTemporal(), Nd.isInvariant(),
1210 Nd.getAlignment());
1211
1212 // i32 load from higher address.
1213 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1214 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1215 MachinePointerInfo(), Nd.isVolatile(),
1216 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001217 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka63791212013-09-07 00:52:30 +00001218
Eric Christopher1c29a652014-07-18 22:55:25 +00001219 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001220 std::swap(Lo, Hi);
1221
1222 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1223 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001224 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001225}
1226
1227SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1228 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1229
1230 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1231 return MipsTargetLowering::lowerSTORE(Op, DAG);
1232
1233 // Replace a double precision store with two extractelement64s and i32 stores.
1234 SDLoc DL(Op);
1235 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1236 EVT PtrVT = Ptr.getValueType();
1237 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1238 Val, DAG.getConstant(0, MVT::i32));
1239 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1240 Val, DAG.getConstant(1, MVT::i32));
1241
Eric Christopher1c29a652014-07-18 22:55:25 +00001242 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001243 std::swap(Lo, Hi);
1244
1245 // i32 store to lower address.
1246 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1247 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
Hal Finkelcc39b672014-07-24 12:16:19 +00001248 Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001249
1250 // i32 store to higher address.
1251 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1252 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001253 Nd.isVolatile(), Nd.isNonTemporal(),
Hal Finkelcc39b672014-07-24 12:16:19 +00001254 std::min(Nd.getAlignment(), 4U), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001255}
1256
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001257SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1258 bool HasLo, bool HasHi,
1259 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001260 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001261 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001262
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001263 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001264 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001265 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1266 Op.getOperand(0), Op.getOperand(1));
1267 SDValue Lo, Hi;
1268
1269 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001270 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001271 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001272 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001273
1274 if (!HasLo || !HasHi)
1275 return HasLo ? Lo : Hi;
1276
1277 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001278 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001279}
1280
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001281
Andrew Trickef9de2a2013-05-25 02:42:55 +00001282static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001283 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1284 DAG.getConstant(0, MVT::i32));
1285 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1286 DAG.getConstant(1, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001287 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001288}
1289
Andrew Trickef9de2a2013-05-25 02:42:55 +00001290static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001291 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1292 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001293 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1294}
1295
1296// This function expands mips intrinsic nodes which have 64-bit input operands
1297// or output values.
1298//
1299// out64 = intrinsic-node in64
1300// =>
1301// lo = copy (extract-element (in64, 0))
1302// hi = copy (extract-element (in64, 1))
1303// mips-specific-node
1304// v0 = copy lo
1305// v1 = copy hi
1306// out64 = merge-values (v0, v1)
1307//
1308static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001309 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001310 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1311 SmallVector<SDValue, 3> Ops;
1312 unsigned OpNo = 0;
1313
1314 // See if Op has a chain input.
1315 if (HasChainIn)
1316 Ops.push_back(Op->getOperand(OpNo++));
1317
1318 // The next operand is the intrinsic opcode.
1319 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1320
1321 // See if the next operand has type i64.
1322 SDValue Opnd = Op->getOperand(++OpNo), In64;
1323
1324 if (Opnd.getValueType() == MVT::i64)
1325 In64 = initAccumulator(Opnd, DL, DAG);
1326 else
1327 Ops.push_back(Opnd);
1328
1329 // Push the remaining operands.
1330 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1331 Ops.push_back(Op->getOperand(OpNo));
1332
1333 // Add In64 to the end of the list.
1334 if (In64.getNode())
1335 Ops.push_back(In64);
1336
1337 // Scan output.
1338 SmallVector<EVT, 2> ResTys;
1339
1340 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1341 I != E; ++I)
1342 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1343
1344 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001345 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001346 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1347
1348 if (!HasChainIn)
1349 return Out;
1350
1351 assert(Val->getValueType(1) == MVT::Other);
1352 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001353 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001354}
1355
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001356// Lower an MSA copy intrinsic into the specified SelectionDAG node
1357static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1358 SDLoc DL(Op);
1359 SDValue Vec = Op->getOperand(1);
1360 SDValue Idx = Op->getOperand(2);
1361 EVT ResTy = Op->getValueType(0);
1362 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1363
1364 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1365 DAG.getValueType(EltTy));
1366
1367 return Result;
1368}
1369
Daniel Sanders50b80412013-11-15 12:56:49 +00001370static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1371 EVT ResVecTy = Op->getValueType(0);
1372 EVT ViaVecTy = ResVecTy;
1373 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001374
Daniel Sanders50b80412013-11-15 12:56:49 +00001375 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1376 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1377 // lanes.
1378 SDValue LaneA;
1379 SDValue LaneB = Op->getOperand(2);
1380
1381 if (ResVecTy == MVT::v2i64) {
1382 LaneA = DAG.getConstant(0, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001383 ViaVecTy = MVT::v4i32;
Daniel Sanders50b80412013-11-15 12:56:49 +00001384 } else
1385 LaneA = LaneB;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001386
Daniel Sanders50b80412013-11-15 12:56:49 +00001387 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1388 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001389
Craig Topper48d114b2014-04-26 18:35:24 +00001390 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001391 makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001392
1393 if (ViaVecTy != ResVecTy)
1394 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001395
1396 return Result;
1397}
1398
Daniel Sanders50b80412013-11-15 12:56:49 +00001399static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1400 return DAG.getConstant(Op->getConstantOperandVal(ImmOp), Op->getValueType(0));
1401}
1402
1403static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1404 bool BigEndian, SelectionDAG &DAG) {
1405 EVT ViaVecTy = VecTy;
1406 SDValue SplatValueA = SplatValue;
1407 SDValue SplatValueB = SplatValue;
1408 SDLoc DL(SplatValue);
1409
1410 if (VecTy == MVT::v2i64) {
1411 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1412 ViaVecTy = MVT::v4i32;
1413
1414 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1415 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
1416 DAG.getConstant(32, MVT::i32));
1417 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1418 }
1419
1420 // We currently hold the parts in little endian order. Swap them if
1421 // necessary.
1422 if (BigEndian)
1423 std::swap(SplatValueA, SplatValueB);
1424
1425 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1426 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1427 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1428 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1429
Craig Topper48d114b2014-04-26 18:35:24 +00001430 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001431 makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001432
1433 if (VecTy != ViaVecTy)
1434 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1435
1436 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001437}
1438
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001439static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1440 unsigned Opc, SDValue Imm,
1441 bool BigEndian) {
1442 EVT VecTy = Op->getValueType(0);
1443 SDValue Exp2Imm;
1444 SDLoc DL(Op);
1445
Daniel Sanders50b80412013-11-15 12:56:49 +00001446 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1447 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001448 if (VecTy == MVT::v2i64) {
1449 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1450 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1451
1452 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001453 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), MVT::i32);
1454
1455 if (BigEndian)
1456 std::swap(BitImmLoOp, BitImmHiOp);
1457
1458 Exp2Imm =
1459 DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
1460 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, BitImmLoOp,
1461 BitImmHiOp, BitImmLoOp, BitImmHiOp));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001462 }
1463 }
1464
Craig Topper062a2ba2014-04-25 05:30:21 +00001465 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001466 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001467
1468 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1469 // only values 0-63 are valid.
1470 if (VecTy == MVT::v2i64)
1471 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1472
1473 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1474
1475 Exp2Imm =
1476 DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, VecTy), Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001477 }
1478
1479 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1480}
1481
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001482static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1483 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001484 SDLoc DL(Op);
Daniel Sanders50b80412013-11-15 12:56:49 +00001485 SDValue One = DAG.getConstant(1, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001486 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
1487
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001488 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1489 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001490}
1491
1492static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1493 SDLoc DL(Op);
1494 EVT ResTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001495 APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
1496 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
1497 SDValue BitMask = DAG.getConstant(~BitImm, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001498
1499 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1500}
1501
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001502SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1503 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001504 SDLoc DL(Op);
1505
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001506 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1507 default:
1508 return SDValue();
1509 case Intrinsic::mips_shilo:
1510 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1511 case Intrinsic::mips_dpau_h_qbl:
1512 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1513 case Intrinsic::mips_dpau_h_qbr:
1514 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1515 case Intrinsic::mips_dpsu_h_qbl:
1516 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1517 case Intrinsic::mips_dpsu_h_qbr:
1518 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1519 case Intrinsic::mips_dpa_w_ph:
1520 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1521 case Intrinsic::mips_dps_w_ph:
1522 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1523 case Intrinsic::mips_dpax_w_ph:
1524 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1525 case Intrinsic::mips_dpsx_w_ph:
1526 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1527 case Intrinsic::mips_mulsa_w_ph:
1528 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1529 case Intrinsic::mips_mult:
1530 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1531 case Intrinsic::mips_multu:
1532 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1533 case Intrinsic::mips_madd:
1534 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1535 case Intrinsic::mips_maddu:
1536 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1537 case Intrinsic::mips_msub:
1538 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1539 case Intrinsic::mips_msubu:
1540 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001541 case Intrinsic::mips_addv_b:
1542 case Intrinsic::mips_addv_h:
1543 case Intrinsic::mips_addv_w:
1544 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001545 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1546 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001547 case Intrinsic::mips_addvi_b:
1548 case Intrinsic::mips_addvi_h:
1549 case Intrinsic::mips_addvi_w:
1550 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001551 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1552 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001553 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001554 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1555 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001556 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001557 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1558 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001559 case Intrinsic::mips_bclr_b:
1560 case Intrinsic::mips_bclr_h:
1561 case Intrinsic::mips_bclr_w:
1562 case Intrinsic::mips_bclr_d:
1563 return lowerMSABitClear(Op, DAG);
1564 case Intrinsic::mips_bclri_b:
1565 case Intrinsic::mips_bclri_h:
1566 case Intrinsic::mips_bclri_w:
1567 case Intrinsic::mips_bclri_d:
1568 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001569 case Intrinsic::mips_binsli_b:
1570 case Intrinsic::mips_binsli_h:
1571 case Intrinsic::mips_binsli_w:
1572 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001573 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001574 EVT VecTy = Op->getValueType(0);
1575 EVT EltTy = VecTy.getVectorElementType();
1576 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
1577 Op->getConstantOperandVal(3));
1578 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Daniel Sandersdf2215452014-03-12 11:54:00 +00001579 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1580 Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001581 }
1582 case Intrinsic::mips_binsri_b:
1583 case Intrinsic::mips_binsri_h:
1584 case Intrinsic::mips_binsri_w:
1585 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001586 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001587 EVT VecTy = Op->getValueType(0);
1588 EVT EltTy = VecTy.getVectorElementType();
1589 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
1590 Op->getConstantOperandVal(3));
1591 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Daniel Sandersdf2215452014-03-12 11:54:00 +00001592 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1593 Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001594 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001595 case Intrinsic::mips_bmnz_v:
1596 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1597 Op->getOperand(2), Op->getOperand(1));
1598 case Intrinsic::mips_bmnzi_b:
1599 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1600 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1601 Op->getOperand(1));
1602 case Intrinsic::mips_bmz_v:
1603 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1604 Op->getOperand(1), Op->getOperand(2));
1605 case Intrinsic::mips_bmzi_b:
1606 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1607 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1608 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001609 case Intrinsic::mips_bneg_b:
1610 case Intrinsic::mips_bneg_h:
1611 case Intrinsic::mips_bneg_w:
1612 case Intrinsic::mips_bneg_d: {
1613 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001614 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001615
1616 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1617 DAG.getNode(ISD::SHL, DL, VecTy, One,
1618 Op->getOperand(2)));
1619 }
1620 case Intrinsic::mips_bnegi_b:
1621 case Intrinsic::mips_bnegi_h:
1622 case Intrinsic::mips_bnegi_w:
1623 case Intrinsic::mips_bnegi_d:
1624 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001625 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001626 case Intrinsic::mips_bnz_b:
1627 case Intrinsic::mips_bnz_h:
1628 case Intrinsic::mips_bnz_w:
1629 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001630 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1631 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001632 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001633 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1634 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001635 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001636 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001637 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001638 Op->getOperand(1), Op->getOperand(3),
1639 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001640 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001641 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001642 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001643 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1644 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001645 case Intrinsic::mips_bset_b:
1646 case Intrinsic::mips_bset_h:
1647 case Intrinsic::mips_bset_w:
1648 case Intrinsic::mips_bset_d: {
1649 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001650 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001651
1652 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1653 DAG.getNode(ISD::SHL, DL, VecTy, One,
1654 Op->getOperand(2)));
1655 }
1656 case Intrinsic::mips_bseti_b:
1657 case Intrinsic::mips_bseti_h:
1658 case Intrinsic::mips_bseti_w:
1659 case Intrinsic::mips_bseti_d:
1660 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001661 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001662 case Intrinsic::mips_bz_b:
1663 case Intrinsic::mips_bz_h:
1664 case Intrinsic::mips_bz_w:
1665 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001666 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1667 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001668 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001669 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1670 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001671 case Intrinsic::mips_ceq_b:
1672 case Intrinsic::mips_ceq_h:
1673 case Intrinsic::mips_ceq_w:
1674 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001675 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001676 Op->getOperand(2), ISD::SETEQ);
1677 case Intrinsic::mips_ceqi_b:
1678 case Intrinsic::mips_ceqi_h:
1679 case Intrinsic::mips_ceqi_w:
1680 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001681 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001682 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1683 case Intrinsic::mips_cle_s_b:
1684 case Intrinsic::mips_cle_s_h:
1685 case Intrinsic::mips_cle_s_w:
1686 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001687 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001688 Op->getOperand(2), ISD::SETLE);
1689 case Intrinsic::mips_clei_s_b:
1690 case Intrinsic::mips_clei_s_h:
1691 case Intrinsic::mips_clei_s_w:
1692 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001693 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001694 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1695 case Intrinsic::mips_cle_u_b:
1696 case Intrinsic::mips_cle_u_h:
1697 case Intrinsic::mips_cle_u_w:
1698 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001699 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001700 Op->getOperand(2), ISD::SETULE);
1701 case Intrinsic::mips_clei_u_b:
1702 case Intrinsic::mips_clei_u_h:
1703 case Intrinsic::mips_clei_u_w:
1704 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001705 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001706 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1707 case Intrinsic::mips_clt_s_b:
1708 case Intrinsic::mips_clt_s_h:
1709 case Intrinsic::mips_clt_s_w:
1710 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001711 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001712 Op->getOperand(2), ISD::SETLT);
1713 case Intrinsic::mips_clti_s_b:
1714 case Intrinsic::mips_clti_s_h:
1715 case Intrinsic::mips_clti_s_w:
1716 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001717 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001718 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1719 case Intrinsic::mips_clt_u_b:
1720 case Intrinsic::mips_clt_u_h:
1721 case Intrinsic::mips_clt_u_w:
1722 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001723 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001724 Op->getOperand(2), ISD::SETULT);
1725 case Intrinsic::mips_clti_u_b:
1726 case Intrinsic::mips_clti_u_h:
1727 case Intrinsic::mips_clti_u_w:
1728 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001729 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001730 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001731 case Intrinsic::mips_copy_s_b:
1732 case Intrinsic::mips_copy_s_h:
1733 case Intrinsic::mips_copy_s_w:
1734 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001735 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001736 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001737 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1738 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1739 else {
1740 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1741 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1742 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1743 Op->getValueType(0), Op->getOperand(1),
1744 Op->getOperand(2));
1745 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001746 case Intrinsic::mips_copy_u_b:
1747 case Intrinsic::mips_copy_u_h:
1748 case Intrinsic::mips_copy_u_w:
1749 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001750 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001751 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001752 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1753 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1754 else {
1755 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1756 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1757 // Note: When i64 is illegal, this results in copy_s.w instructions
1758 // instead of copy_u.w instructions. This makes no difference to the
1759 // behaviour since i64 is only illegal when the register file is 32-bit.
1760 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1761 Op->getValueType(0), Op->getOperand(1),
1762 Op->getOperand(2));
1763 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001764 case Intrinsic::mips_div_s_b:
1765 case Intrinsic::mips_div_s_h:
1766 case Intrinsic::mips_div_s_w:
1767 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001768 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1769 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001770 case Intrinsic::mips_div_u_b:
1771 case Intrinsic::mips_div_u_h:
1772 case Intrinsic::mips_div_u_w:
1773 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001774 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1775 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001776 case Intrinsic::mips_fadd_w:
1777 case Intrinsic::mips_fadd_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001778 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1779 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001780 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1781 case Intrinsic::mips_fceq_w:
1782 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001783 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001784 Op->getOperand(2), ISD::SETOEQ);
1785 case Intrinsic::mips_fcle_w:
1786 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001787 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001788 Op->getOperand(2), ISD::SETOLE);
1789 case Intrinsic::mips_fclt_w:
1790 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001791 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001792 Op->getOperand(2), ISD::SETOLT);
1793 case Intrinsic::mips_fcne_w:
1794 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001795 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001796 Op->getOperand(2), ISD::SETONE);
1797 case Intrinsic::mips_fcor_w:
1798 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001799 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001800 Op->getOperand(2), ISD::SETO);
1801 case Intrinsic::mips_fcueq_w:
1802 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001803 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001804 Op->getOperand(2), ISD::SETUEQ);
1805 case Intrinsic::mips_fcule_w:
1806 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001807 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001808 Op->getOperand(2), ISD::SETULE);
1809 case Intrinsic::mips_fcult_w:
1810 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001811 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001812 Op->getOperand(2), ISD::SETULT);
1813 case Intrinsic::mips_fcun_w:
1814 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001815 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001816 Op->getOperand(2), ISD::SETUO);
1817 case Intrinsic::mips_fcune_w:
1818 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001819 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001820 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001821 case Intrinsic::mips_fdiv_w:
1822 case Intrinsic::mips_fdiv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001823 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1824 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001825 case Intrinsic::mips_ffint_u_w:
1826 case Intrinsic::mips_ffint_u_d:
1827 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1828 Op->getOperand(1));
1829 case Intrinsic::mips_ffint_s_w:
1830 case Intrinsic::mips_ffint_s_d:
1831 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1832 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001833 case Intrinsic::mips_fill_b:
1834 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001835 case Intrinsic::mips_fill_w:
1836 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001837 SmallVector<SDValue, 16> Ops;
1838 EVT ResTy = Op->getValueType(0);
1839
1840 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1841 Ops.push_back(Op->getOperand(1));
1842
Daniel Sandersc72593e2013-09-27 13:20:41 +00001843 // If ResTy is v2i64 then the type legalizer will break this node down into
1844 // an equivalent v4i32.
Craig Topper48d114b2014-04-26 18:35:24 +00001845 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001846 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001847 case Intrinsic::mips_fexp2_w:
1848 case Intrinsic::mips_fexp2_d: {
1849 EVT ResTy = Op->getValueType(0);
1850 return DAG.getNode(
1851 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1852 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1853 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001854 case Intrinsic::mips_flog2_w:
1855 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001856 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001857 case Intrinsic::mips_fmadd_w:
1858 case Intrinsic::mips_fmadd_d:
1859 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1860 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001861 case Intrinsic::mips_fmul_w:
1862 case Intrinsic::mips_fmul_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001863 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1864 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001865 case Intrinsic::mips_fmsub_w:
1866 case Intrinsic::mips_fmsub_d: {
1867 EVT ResTy = Op->getValueType(0);
1868 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1869 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1870 Op->getOperand(2), Op->getOperand(3)));
1871 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001872 case Intrinsic::mips_frint_w:
1873 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001874 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001875 case Intrinsic::mips_fsqrt_w:
1876 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001877 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001878 case Intrinsic::mips_fsub_w:
1879 case Intrinsic::mips_fsub_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001880 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1881 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001882 case Intrinsic::mips_ftrunc_u_w:
1883 case Intrinsic::mips_ftrunc_u_d:
1884 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1885 Op->getOperand(1));
1886 case Intrinsic::mips_ftrunc_s_w:
1887 case Intrinsic::mips_ftrunc_s_d:
1888 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1889 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001890 case Intrinsic::mips_ilvev_b:
1891 case Intrinsic::mips_ilvev_h:
1892 case Intrinsic::mips_ilvev_w:
1893 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001894 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001895 Op->getOperand(1), Op->getOperand(2));
1896 case Intrinsic::mips_ilvl_b:
1897 case Intrinsic::mips_ilvl_h:
1898 case Intrinsic::mips_ilvl_w:
1899 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001900 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001901 Op->getOperand(1), Op->getOperand(2));
1902 case Intrinsic::mips_ilvod_b:
1903 case Intrinsic::mips_ilvod_h:
1904 case Intrinsic::mips_ilvod_w:
1905 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001906 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001907 Op->getOperand(1), Op->getOperand(2));
1908 case Intrinsic::mips_ilvr_b:
1909 case Intrinsic::mips_ilvr_h:
1910 case Intrinsic::mips_ilvr_w:
1911 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001912 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001913 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001914 case Intrinsic::mips_insert_b:
1915 case Intrinsic::mips_insert_h:
1916 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001917 case Intrinsic::mips_insert_d:
1918 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1919 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001920 case Intrinsic::mips_insve_b:
1921 case Intrinsic::mips_insve_h:
1922 case Intrinsic::mips_insve_w:
1923 case Intrinsic::mips_insve_d:
1924 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1925 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
1926 DAG.getConstant(0, MVT::i32));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001927 case Intrinsic::mips_ldi_b:
1928 case Intrinsic::mips_ldi_h:
1929 case Intrinsic::mips_ldi_w:
1930 case Intrinsic::mips_ldi_d:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001931 return lowerMSASplatImm(Op, 1, DAG);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001932 case Intrinsic::mips_lsa:
1933 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001934 EVT ResTy = Op->getValueType(0);
1935 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1936 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1937 Op->getOperand(2), Op->getOperand(3)));
1938 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001939 case Intrinsic::mips_maddv_b:
1940 case Intrinsic::mips_maddv_h:
1941 case Intrinsic::mips_maddv_w:
1942 case Intrinsic::mips_maddv_d: {
1943 EVT ResTy = Op->getValueType(0);
1944 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1945 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1946 Op->getOperand(2), Op->getOperand(3)));
1947 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001948 case Intrinsic::mips_max_s_b:
1949 case Intrinsic::mips_max_s_h:
1950 case Intrinsic::mips_max_s_w:
1951 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001952 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1953 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001954 case Intrinsic::mips_max_u_b:
1955 case Intrinsic::mips_max_u_h:
1956 case Intrinsic::mips_max_u_w:
1957 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001958 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1959 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001960 case Intrinsic::mips_maxi_s_b:
1961 case Intrinsic::mips_maxi_s_h:
1962 case Intrinsic::mips_maxi_s_w:
1963 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001964 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1965 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001966 case Intrinsic::mips_maxi_u_b:
1967 case Intrinsic::mips_maxi_u_h:
1968 case Intrinsic::mips_maxi_u_w:
1969 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001970 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1971 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001972 case Intrinsic::mips_min_s_b:
1973 case Intrinsic::mips_min_s_h:
1974 case Intrinsic::mips_min_s_w:
1975 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001976 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1977 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001978 case Intrinsic::mips_min_u_b:
1979 case Intrinsic::mips_min_u_h:
1980 case Intrinsic::mips_min_u_w:
1981 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001982 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1983 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001984 case Intrinsic::mips_mini_s_b:
1985 case Intrinsic::mips_mini_s_h:
1986 case Intrinsic::mips_mini_s_w:
1987 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001988 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1989 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001990 case Intrinsic::mips_mini_u_b:
1991 case Intrinsic::mips_mini_u_h:
1992 case Intrinsic::mips_mini_u_w:
1993 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001994 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1995 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001996 case Intrinsic::mips_mod_s_b:
1997 case Intrinsic::mips_mod_s_h:
1998 case Intrinsic::mips_mod_s_w:
1999 case Intrinsic::mips_mod_s_d:
2000 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
2001 Op->getOperand(2));
2002 case Intrinsic::mips_mod_u_b:
2003 case Intrinsic::mips_mod_u_h:
2004 case Intrinsic::mips_mod_u_w:
2005 case Intrinsic::mips_mod_u_d:
2006 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
2007 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002008 case Intrinsic::mips_mulv_b:
2009 case Intrinsic::mips_mulv_h:
2010 case Intrinsic::mips_mulv_w:
2011 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002012 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
2013 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00002014 case Intrinsic::mips_msubv_b:
2015 case Intrinsic::mips_msubv_h:
2016 case Intrinsic::mips_msubv_w:
2017 case Intrinsic::mips_msubv_d: {
2018 EVT ResTy = Op->getValueType(0);
2019 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
2020 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2021 Op->getOperand(2), Op->getOperand(3)));
2022 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002023 case Intrinsic::mips_nlzc_b:
2024 case Intrinsic::mips_nlzc_h:
2025 case Intrinsic::mips_nlzc_w:
2026 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002027 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002028 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002029 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2030 Op->getOperand(1), Op->getOperand(2));
2031 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002032 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002033 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002034 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2035 Op->getOperand(1),
2036 lowerMSASplatImm(Op, 2, DAG));
2037 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002038 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002039 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002040 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2041 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002042 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002043 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2044 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002045 case Intrinsic::mips_pckev_b:
2046 case Intrinsic::mips_pckev_h:
2047 case Intrinsic::mips_pckev_w:
2048 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002049 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002050 Op->getOperand(1), Op->getOperand(2));
2051 case Intrinsic::mips_pckod_b:
2052 case Intrinsic::mips_pckod_h:
2053 case Intrinsic::mips_pckod_w:
2054 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002055 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002056 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002057 case Intrinsic::mips_pcnt_b:
2058 case Intrinsic::mips_pcnt_h:
2059 case Intrinsic::mips_pcnt_w:
2060 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002061 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sanders26307182013-09-24 14:20:00 +00002062 case Intrinsic::mips_shf_b:
2063 case Intrinsic::mips_shf_h:
2064 case Intrinsic::mips_shf_w:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002065 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002066 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002067 case Intrinsic::mips_sll_b:
2068 case Intrinsic::mips_sll_h:
2069 case Intrinsic::mips_sll_w:
2070 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002071 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
2072 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002073 case Intrinsic::mips_slli_b:
2074 case Intrinsic::mips_slli_h:
2075 case Intrinsic::mips_slli_w:
2076 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002077 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2078 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002079 case Intrinsic::mips_splat_b:
2080 case Intrinsic::mips_splat_h:
2081 case Intrinsic::mips_splat_w:
2082 case Intrinsic::mips_splat_d:
2083 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2084 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2085 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2086 // Instead we lower to MipsISD::VSHF and match from there.
2087 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002088 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002089 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002090 case Intrinsic::mips_splati_b:
2091 case Intrinsic::mips_splati_h:
2092 case Intrinsic::mips_splati_w:
2093 case Intrinsic::mips_splati_d:
2094 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2095 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2096 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002097 case Intrinsic::mips_sra_b:
2098 case Intrinsic::mips_sra_h:
2099 case Intrinsic::mips_sra_w:
2100 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002101 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
2102 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002103 case Intrinsic::mips_srai_b:
2104 case Intrinsic::mips_srai_h:
2105 case Intrinsic::mips_srai_w:
2106 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002107 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2108 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002109 case Intrinsic::mips_srl_b:
2110 case Intrinsic::mips_srl_h:
2111 case Intrinsic::mips_srl_w:
2112 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002113 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
2114 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002115 case Intrinsic::mips_srli_b:
2116 case Intrinsic::mips_srli_h:
2117 case Intrinsic::mips_srli_w:
2118 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002119 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2120 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002121 case Intrinsic::mips_subv_b:
2122 case Intrinsic::mips_subv_h:
2123 case Intrinsic::mips_subv_w:
2124 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002125 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2126 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002127 case Intrinsic::mips_subvi_b:
2128 case Intrinsic::mips_subvi_h:
2129 case Intrinsic::mips_subvi_w:
2130 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002131 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2132 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002133 case Intrinsic::mips_vshf_b:
2134 case Intrinsic::mips_vshf_h:
2135 case Intrinsic::mips_vshf_w:
2136 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002137 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002138 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002139 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002140 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2141 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002142 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002143 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2144 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002145 }
2146}
2147
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002148static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2149 SDLoc DL(Op);
2150 SDValue ChainIn = Op->getOperand(0);
2151 SDValue Address = Op->getOperand(2);
2152 SDValue Offset = Op->getOperand(3);
2153 EVT ResTy = Op->getValueType(0);
2154 EVT PtrTy = Address->getValueType(0);
2155
2156 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2157
2158 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
2159 false, false, 16);
2160}
2161
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002162SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2163 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002164 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2165 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002166 default:
2167 return SDValue();
2168 case Intrinsic::mips_extp:
2169 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2170 case Intrinsic::mips_extpdp:
2171 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2172 case Intrinsic::mips_extr_w:
2173 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2174 case Intrinsic::mips_extr_r_w:
2175 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2176 case Intrinsic::mips_extr_rs_w:
2177 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2178 case Intrinsic::mips_extr_s_h:
2179 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2180 case Intrinsic::mips_mthlip:
2181 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2182 case Intrinsic::mips_mulsaq_s_w_ph:
2183 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2184 case Intrinsic::mips_maq_s_w_phl:
2185 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2186 case Intrinsic::mips_maq_s_w_phr:
2187 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2188 case Intrinsic::mips_maq_sa_w_phl:
2189 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2190 case Intrinsic::mips_maq_sa_w_phr:
2191 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2192 case Intrinsic::mips_dpaq_s_w_ph:
2193 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2194 case Intrinsic::mips_dpsq_s_w_ph:
2195 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2196 case Intrinsic::mips_dpaq_sa_l_w:
2197 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2198 case Intrinsic::mips_dpsq_sa_l_w:
2199 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2200 case Intrinsic::mips_dpaqx_s_w_ph:
2201 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2202 case Intrinsic::mips_dpaqx_sa_w_ph:
2203 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2204 case Intrinsic::mips_dpsqx_s_w_ph:
2205 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2206 case Intrinsic::mips_dpsqx_sa_w_ph:
2207 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002208 case Intrinsic::mips_ld_b:
2209 case Intrinsic::mips_ld_h:
2210 case Intrinsic::mips_ld_w:
2211 case Intrinsic::mips_ld_d:
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002212 return lowerMSALoadIntr(Op, DAG, Intr);
2213 }
2214}
2215
2216static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2217 SDLoc DL(Op);
2218 SDValue ChainIn = Op->getOperand(0);
2219 SDValue Value = Op->getOperand(2);
2220 SDValue Address = Op->getOperand(3);
2221 SDValue Offset = Op->getOperand(4);
2222 EVT PtrTy = Address->getValueType(0);
2223
2224 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2225
2226 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
2227 false, 16);
2228}
2229
2230SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2231 SelectionDAG &DAG) const {
2232 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2233 switch (Intr) {
2234 default:
2235 return SDValue();
2236 case Intrinsic::mips_st_b:
2237 case Intrinsic::mips_st_h:
2238 case Intrinsic::mips_st_w:
2239 case Intrinsic::mips_st_d:
Daniel Sandersce09d072013-08-28 12:14:50 +00002240 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002241 }
2242}
2243
Daniel Sanders7a289d02013-09-23 12:02:46 +00002244/// \brief Check if the given BuildVectorSDNode is a splat.
2245/// This method currently relies on DAG nodes being reused when equivalent,
2246/// so it's possible for this to return false even when isConstantSplat returns
2247/// true.
2248static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002249 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002250 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002251
2252 SDValue Operand0 = N->getOperand(0);
2253
2254 for (unsigned int i = 1; i < nOps; ++i) {
2255 if (N->getOperand(i) != Operand0)
2256 return false;
2257 }
2258
2259 return true;
2260}
2261
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002262// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2263//
2264// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2265// choose to sign-extend but we could have equally chosen zero-extend. The
2266// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2267// result into this node later (possibly changing it to a zero-extend in the
2268// process).
2269SDValue MipsSETargetLowering::
2270lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2271 SDLoc DL(Op);
2272 EVT ResTy = Op->getValueType(0);
2273 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002274 EVT VecTy = Op0->getValueType(0);
2275
2276 if (!VecTy.is128BitVector())
2277 return SDValue();
2278
2279 if (ResTy.isInteger()) {
2280 SDValue Op1 = Op->getOperand(1);
2281 EVT EltTy = VecTy.getVectorElementType();
2282 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2283 DAG.getValueType(EltTy));
2284 }
2285
2286 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002287}
2288
Daniel Sandersf49dd822013-09-24 13:33:07 +00002289static bool isConstantOrUndef(const SDValue Op) {
2290 if (Op->getOpcode() == ISD::UNDEF)
2291 return true;
2292 if (dyn_cast<ConstantSDNode>(Op))
2293 return true;
2294 if (dyn_cast<ConstantFPSDNode>(Op))
2295 return true;
2296 return false;
2297}
2298
2299static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2300 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2301 if (isConstantOrUndef(Op->getOperand(i)))
2302 return true;
2303 return false;
2304}
2305
Daniel Sanders7a289d02013-09-23 12:02:46 +00002306// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2307// backend.
2308//
2309// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002310// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2311// 2 less than or equal to 64 and the value fits into a signed 10-bit
2312// immediate
2313// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2314// is a power of 2 less than or equal to 64 and the value does not fit into a
2315// signed 10-bit immediate
2316// - Non-constant splats are legal as-is.
2317// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2318// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002319SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2320 SelectionDAG &DAG) const {
2321 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2322 EVT ResTy = Op->getValueType(0);
2323 SDLoc DL(Op);
2324 APInt SplatValue, SplatUndef;
2325 unsigned SplatBitSize;
2326 bool HasAnyUndefs;
2327
Eric Christopher1c29a652014-07-18 22:55:25 +00002328 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002329 return SDValue();
2330
2331 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2332 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002333 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002334 // We can only cope with 8, 16, 32, or 64-bit elements
2335 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2336 SplatBitSize != 64)
2337 return SDValue();
2338
2339 // If the value fits into a simm10 then we can use ldi.[bhwd]
Daniel Sandersfd8e4162013-11-22 11:24:50 +00002340 // However, if it isn't an integer type we will have to bitcast from an
Daniel Sandersd40aea82013-11-22 13:22:52 +00002341 // integer type first. Also, if there are any undefs, we must lower them
Daniel Sanders630dbe02013-11-22 13:14:06 +00002342 // to defined values first.
2343 if (ResTy.isInteger() && !HasAnyUndefs && SplatValue.isSignedIntN(10))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002344 return Op;
2345
2346 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002347
2348 switch (SplatBitSize) {
2349 default:
2350 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002351 case 8:
2352 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002353 break;
2354 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002355 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002356 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002357 case 32:
2358 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002359 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002360 case 64:
2361 // There's no fill.d to fall back on for 64-bit values
2362 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002363 }
2364
Daniel Sanders50b80412013-11-15 12:56:49 +00002365 // SelectionDAG::getConstant will promote SplatValue appropriately.
2366 SDValue Result = DAG.getConstant(SplatValue, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002367
Daniel Sanders50b80412013-11-15 12:56:49 +00002368 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002369 if (ViaVecTy != ResTy)
2370 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002371
2372 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002373 } else if (isSplatVector(Node))
2374 return Op;
2375 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002376 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2377 // The resulting code is the same length as the expansion, but it doesn't
2378 // use memory operations
2379 EVT ResTy = Node->getValueType(0);
2380
2381 assert(ResTy.isVector());
2382
2383 unsigned NumElts = ResTy.getVectorNumElements();
2384 SDValue Vector = DAG.getUNDEF(ResTy);
2385 for (unsigned i = 0; i < NumElts; ++i) {
2386 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2387 Node->getOperand(i),
2388 DAG.getConstant(i, MVT::i32));
2389 }
2390 return Vector;
2391 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002392
2393 return SDValue();
2394}
2395
Daniel Sanders26307182013-09-24 14:20:00 +00002396// Lower VECTOR_SHUFFLE into SHF (if possible).
2397//
2398// SHF splits the vector into blocks of four elements, then shuffles these
2399// elements according to a <4 x i2> constant (encoded as an integer immediate).
2400//
2401// It is therefore possible to lower into SHF when the mask takes the form:
2402// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2403// When undef's appear they are treated as if they were whatever value is
2404// necessary in order to fit the above form.
2405//
2406// For example:
2407// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2408// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2409// i32 7, i32 6, i32 5, i32 4>
2410// is lowered to:
2411// (SHF_H $w0, $w1, 27)
2412// where the 27 comes from:
2413// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2414static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2415 SmallVector<int, 16> Indices,
2416 SelectionDAG &DAG) {
2417 int SHFIndices[4] = { -1, -1, -1, -1 };
2418
2419 if (Indices.size() < 4)
2420 return SDValue();
2421
2422 for (unsigned i = 0; i < 4; ++i) {
2423 for (unsigned j = i; j < Indices.size(); j += 4) {
2424 int Idx = Indices[j];
2425
2426 // Convert from vector index to 4-element subvector index
2427 // If an index refers to an element outside of the subvector then give up
2428 if (Idx != -1) {
2429 Idx -= 4 * (j / 4);
2430 if (Idx < 0 || Idx >= 4)
2431 return SDValue();
2432 }
2433
2434 // If the mask has an undef, replace it with the current index.
2435 // Note that it might still be undef if the current index is also undef
2436 if (SHFIndices[i] == -1)
2437 SHFIndices[i] = Idx;
2438
2439 // Check that non-undef values are the same as in the mask. If they
2440 // aren't then give up
2441 if (!(Idx == -1 || Idx == SHFIndices[i]))
2442 return SDValue();
2443 }
2444 }
2445
2446 // Calculate the immediate. Replace any remaining undefs with zero
2447 APInt Imm(32, 0);
2448 for (int i = 3; i >= 0; --i) {
2449 int Idx = SHFIndices[i];
2450
2451 if (Idx == -1)
2452 Idx = 0;
2453
2454 Imm <<= 2;
2455 Imm |= Idx & 0x3;
2456 }
2457
2458 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
2459 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
2460}
2461
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002462// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2463//
2464// ILVEV interleaves the even elements from each vector.
2465//
2466// It is possible to lower into ILVEV when the mask takes the form:
2467// <0, n, 2, n+2, 4, n+4, ...>
2468// where n is the number of elements in the vector.
2469//
2470// When undef's appear in the mask they are treated as if they were whatever
2471// value is necessary in order to fit the above form.
2472static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2473 SmallVector<int, 16> Indices,
2474 SelectionDAG &DAG) {
2475 assert ((Indices.size() % 2) == 0);
2476 int WsIdx = 0;
2477 int WtIdx = ResTy.getVectorNumElements();
2478
2479 for (unsigned i = 0; i < Indices.size(); i += 2) {
2480 if (Indices[i] != -1 && Indices[i] != WsIdx)
2481 return SDValue();
2482 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2483 return SDValue();
2484 WsIdx += 2;
2485 WtIdx += 2;
2486 }
2487
2488 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
2489 Op->getOperand(1));
2490}
2491
2492// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2493//
2494// ILVOD interleaves the odd elements from each vector.
2495//
2496// It is possible to lower into ILVOD when the mask takes the form:
2497// <1, n+1, 3, n+3, 5, n+5, ...>
2498// where n is the number of elements in the vector.
2499//
2500// When undef's appear in the mask they are treated as if they were whatever
2501// value is necessary in order to fit the above form.
2502static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2503 SmallVector<int, 16> Indices,
2504 SelectionDAG &DAG) {
2505 assert ((Indices.size() % 2) == 0);
2506 int WsIdx = 1;
2507 int WtIdx = ResTy.getVectorNumElements() + 1;
2508
2509 for (unsigned i = 0; i < Indices.size(); i += 2) {
2510 if (Indices[i] != -1 && Indices[i] != WsIdx)
2511 return SDValue();
2512 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2513 return SDValue();
2514 WsIdx += 2;
2515 WtIdx += 2;
2516 }
2517
2518 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
2519 Op->getOperand(1));
2520}
2521
2522// Lower VECTOR_SHUFFLE into ILVL (if possible).
2523//
2524// ILVL interleaves consecutive elements from the left half of each vector.
2525//
2526// It is possible to lower into ILVL when the mask takes the form:
2527// <0, n, 1, n+1, 2, n+2, ...>
2528// where n is the number of elements in the vector.
2529//
2530// When undef's appear in the mask they are treated as if they were whatever
2531// value is necessary in order to fit the above form.
2532static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2533 SmallVector<int, 16> Indices,
2534 SelectionDAG &DAG) {
2535 assert ((Indices.size() % 2) == 0);
2536 int WsIdx = 0;
2537 int WtIdx = ResTy.getVectorNumElements();
2538
2539 for (unsigned i = 0; i < Indices.size(); i += 2) {
2540 if (Indices[i] != -1 && Indices[i] != WsIdx)
2541 return SDValue();
2542 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2543 return SDValue();
2544 WsIdx ++;
2545 WtIdx ++;
2546 }
2547
2548 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2549 Op->getOperand(1));
2550}
2551
2552// Lower VECTOR_SHUFFLE into ILVR (if possible).
2553//
2554// ILVR interleaves consecutive elements from the right half of each vector.
2555//
2556// It is possible to lower into ILVR when the mask takes the form:
2557// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2558// where n is the number of elements in the vector and x is half n.
2559//
2560// When undef's appear in the mask they are treated as if they were whatever
2561// value is necessary in order to fit the above form.
2562static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2563 SmallVector<int, 16> Indices,
2564 SelectionDAG &DAG) {
2565 assert ((Indices.size() % 2) == 0);
2566 unsigned NumElts = ResTy.getVectorNumElements();
2567 int WsIdx = NumElts / 2;
2568 int WtIdx = NumElts + NumElts / 2;
2569
2570 for (unsigned i = 0; i < Indices.size(); i += 2) {
2571 if (Indices[i] != -1 && Indices[i] != WsIdx)
2572 return SDValue();
2573 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2574 return SDValue();
2575 WsIdx ++;
2576 WtIdx ++;
2577 }
2578
2579 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2580 Op->getOperand(1));
2581}
2582
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002583// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2584//
2585// PCKEV copies the even elements of each vector into the result vector.
2586//
2587// It is possible to lower into PCKEV when the mask takes the form:
2588// <0, 2, 4, ..., n, n+2, n+4, ...>
2589// where n is the number of elements in the vector.
2590//
2591// When undef's appear in the mask they are treated as if they were whatever
2592// value is necessary in order to fit the above form.
2593static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2594 SmallVector<int, 16> Indices,
2595 SelectionDAG &DAG) {
2596 assert ((Indices.size() % 2) == 0);
2597 int Idx = 0;
2598
2599 for (unsigned i = 0; i < Indices.size(); ++i) {
2600 if (Indices[i] != -1 && Indices[i] != Idx)
2601 return SDValue();
2602 Idx += 2;
2603 }
2604
2605 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2606 Op->getOperand(1));
2607}
2608
2609// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2610//
2611// PCKOD copies the odd elements of each vector into the result vector.
2612//
2613// It is possible to lower into PCKOD when the mask takes the form:
2614// <1, 3, 5, ..., n+1, n+3, n+5, ...>
2615// where n is the number of elements in the vector.
2616//
2617// When undef's appear in the mask they are treated as if they were whatever
2618// value is necessary in order to fit the above form.
2619static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2620 SmallVector<int, 16> Indices,
2621 SelectionDAG &DAG) {
2622 assert ((Indices.size() % 2) == 0);
2623 int Idx = 1;
2624
2625 for (unsigned i = 0; i < Indices.size(); ++i) {
2626 if (Indices[i] != -1 && Indices[i] != Idx)
2627 return SDValue();
2628 Idx += 2;
2629 }
2630
2631 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2632 Op->getOperand(1));
2633}
2634
Daniel Sanderse5087042013-09-24 14:02:15 +00002635// Lower VECTOR_SHUFFLE into VSHF.
2636//
2637// This mostly consists of converting the shuffle indices in Indices into a
2638// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2639// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2640// if the type is v8i16 and all the indices are less than 8 then the second
2641// operand is unused and can be replaced with anything. We choose to replace it
2642// with the used operand since this reduces the number of instructions overall.
2643static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2644 SmallVector<int, 16> Indices,
2645 SelectionDAG &DAG) {
2646 SmallVector<SDValue, 16> Ops;
2647 SDValue Op0;
2648 SDValue Op1;
2649 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2650 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2651 bool Using1stVec = false;
2652 bool Using2ndVec = false;
2653 SDLoc DL(Op);
2654 int ResTyNumElts = ResTy.getVectorNumElements();
2655
2656 for (int i = 0; i < ResTyNumElts; ++i) {
2657 // Idx == -1 means UNDEF
2658 int Idx = Indices[i];
2659
2660 if (0 <= Idx && Idx < ResTyNumElts)
2661 Using1stVec = true;
2662 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2663 Using2ndVec = true;
2664 }
2665
2666 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2667 ++I)
2668 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2669
Craig Topper48d114b2014-04-26 18:35:24 +00002670 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002671
2672 if (Using1stVec && Using2ndVec) {
2673 Op0 = Op->getOperand(0);
2674 Op1 = Op->getOperand(1);
2675 } else if (Using1stVec)
2676 Op0 = Op1 = Op->getOperand(0);
2677 else if (Using2ndVec)
2678 Op0 = Op1 = Op->getOperand(1);
2679 else
2680 llvm_unreachable("shuffle vector mask references neither vector operand?");
2681
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002682 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2683 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2684 // VSHF concatenates the vectors in a bitwise fashion:
2685 // <0b00, 0b01> + <0b10, 0b11> ->
2686 // 0b0100 + 0b1110 -> 0b01001110
2687 // <0b10, 0b11, 0b00, 0b01>
2688 // We must therefore swap the operands to get the correct result.
2689 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002690}
2691
2692// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2693// indices in the shuffle.
2694SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2695 SelectionDAG &DAG) const {
2696 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2697 EVT ResTy = Op->getValueType(0);
2698
2699 if (!ResTy.is128BitVector())
2700 return SDValue();
2701
2702 int ResTyNumElts = ResTy.getVectorNumElements();
2703 SmallVector<int, 16> Indices;
2704
2705 for (int i = 0; i < ResTyNumElts; ++i)
2706 Indices.push_back(Node->getMaskElt(i));
2707
Daniel Sanders26307182013-09-24 14:20:00 +00002708 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2709 if (Result.getNode())
2710 return Result;
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002711 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2712 if (Result.getNode())
2713 return Result;
2714 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2715 if (Result.getNode())
2716 return Result;
2717 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2718 if (Result.getNode())
2719 return Result;
2720 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2721 if (Result.getNode())
2722 return Result;
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002723 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2724 if (Result.getNode())
2725 return Result;
2726 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2727 if (Result.getNode())
2728 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002729 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2730}
2731
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002732MachineBasicBlock * MipsSETargetLowering::
2733emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2734 // $bb:
2735 // bposge32_pseudo $vr0
2736 // =>
2737 // $bb:
2738 // bposge32 $tbb
2739 // $fbb:
2740 // li $vr2, 0
2741 // b $sink
2742 // $tbb:
2743 // li $vr1, 1
2744 // $sink:
2745 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2746
2747 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00002748 const TargetInstrInfo *TII =
2749 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002750 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002751 DebugLoc DL = MI->getDebugLoc();
2752 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002753 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002754 MachineFunction *F = BB->getParent();
2755 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2756 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2757 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2758 F->insert(It, FBB);
2759 F->insert(It, TBB);
2760 F->insert(It, Sink);
2761
2762 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002763 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002764 BB->end());
2765 Sink->transferSuccessorsAndUpdatePHIs(BB);
2766
2767 // Add successors.
2768 BB->addSuccessor(FBB);
2769 BB->addSuccessor(TBB);
2770 FBB->addSuccessor(Sink);
2771 TBB->addSuccessor(Sink);
2772
2773 // Insert the real bposge32 instruction to $BB.
2774 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2775
2776 // Fill $FBB.
2777 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2778 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2779 .addReg(Mips::ZERO).addImm(0);
2780 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2781
2782 // Fill $TBB.
2783 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2784 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2785 .addReg(Mips::ZERO).addImm(1);
2786
2787 // Insert phi function to $Sink.
2788 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2789 MI->getOperand(0).getReg())
2790 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2791
2792 MI->eraseFromParent(); // The pseudo instruction is gone now.
2793 return Sink;
2794}
Daniel Sandersce09d072013-08-28 12:14:50 +00002795
2796MachineBasicBlock * MipsSETargetLowering::
2797emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2798 unsigned BranchOp) const{
2799 // $bb:
2800 // vany_nonzero $rd, $ws
2801 // =>
2802 // $bb:
2803 // bnz.b $ws, $tbb
2804 // b $fbb
2805 // $fbb:
2806 // li $rd1, 0
2807 // b $sink
2808 // $tbb:
2809 // li $rd2, 1
2810 // $sink:
2811 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2812
2813 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00002814 const TargetInstrInfo *TII =
2815 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sandersce09d072013-08-28 12:14:50 +00002816 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{
Eric Christopherd9134482014-08-04 21:25:23 +00002876 const TargetInstrInfo *TII =
2877 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002878 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2879 DebugLoc DL = MI->getDebugLoc();
2880 unsigned Fd = MI->getOperand(0).getReg();
2881 unsigned Ws = MI->getOperand(1).getReg();
2882 unsigned Lane = MI->getOperand(2).getImm();
2883
2884 if (Lane == 0)
2885 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2886 else {
2887 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2888
Daniel Sandersd9207702014-03-04 13:54:30 +00002889 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002890 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2891 }
2892
2893 MI->eraseFromParent(); // The pseudo instruction is gone now.
2894 return BB;
2895}
2896
2897// Emit the COPY_FD pseudo instruction.
2898//
2899// copy_fd_pseudo $fd, $ws, n
2900// =>
2901// splati.d $wt, $ws, $n
2902// copy $fd, $wt:sub_64
2903//
2904// When n is zero, the equivalent operation can be performed with (potentially)
2905// zero instructions due to register overlaps. This optimization is always
2906// valid because FR=1 mode which is the only supported mode in MSA.
2907MachineBasicBlock * MipsSETargetLowering::
2908emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
Eric Christopher1c29a652014-07-18 22:55:25 +00002909 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002910
Eric Christopherd9134482014-08-04 21:25:23 +00002911 const TargetInstrInfo *TII =
2912 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002913 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2914 unsigned Fd = MI->getOperand(0).getReg();
2915 unsigned Ws = MI->getOperand(1).getReg();
2916 unsigned Lane = MI->getOperand(2).getImm() * 2;
2917 DebugLoc DL = MI->getDebugLoc();
2918
2919 if (Lane == 0)
2920 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2921 else {
2922 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2923
2924 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2925 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2926 }
2927
2928 MI->eraseFromParent(); // The pseudo instruction is gone now.
2929 return BB;
2930}
Daniel Sandersa5150702013-09-27 12:31:32 +00002931
2932// Emit the INSERT_FW pseudo instruction.
2933//
2934// insert_fw_pseudo $wd, $wd_in, $n, $fs
2935// =>
2936// subreg_to_reg $wt:sub_lo, $fs
2937// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002938MachineBasicBlock *
2939MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2940 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00002941 const TargetInstrInfo *TII =
2942 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00002943 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2944 DebugLoc DL = MI->getDebugLoc();
2945 unsigned Wd = MI->getOperand(0).getReg();
2946 unsigned Wd_in = MI->getOperand(1).getReg();
2947 unsigned Lane = MI->getOperand(2).getImm();
2948 unsigned Fs = MI->getOperand(3).getReg();
2949 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2950
2951 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002952 .addImm(0)
2953 .addReg(Fs)
2954 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00002955 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002956 .addReg(Wd_in)
2957 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00002958 .addReg(Wt)
2959 .addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00002960
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002961 MI->eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00002962 return BB;
2963}
2964
2965// Emit the INSERT_FD pseudo instruction.
2966//
2967// insert_fd_pseudo $wd, $fs, n
2968// =>
2969// subreg_to_reg $wt:sub_64, $fs
2970// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002971MachineBasicBlock *
2972MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2973 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002974 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00002975
Eric Christopherd9134482014-08-04 21:25:23 +00002976 const TargetInstrInfo *TII =
2977 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00002978 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2979 DebugLoc DL = MI->getDebugLoc();
2980 unsigned Wd = MI->getOperand(0).getReg();
2981 unsigned Wd_in = MI->getOperand(1).getReg();
2982 unsigned Lane = MI->getOperand(2).getImm();
2983 unsigned Fs = MI->getOperand(3).getReg();
2984 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2985
2986 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002987 .addImm(0)
2988 .addReg(Fs)
2989 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00002990 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002991 .addReg(Wd_in)
2992 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00002993 .addReg(Wt)
2994 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002995
2996 MI->eraseFromParent(); // The pseudo instruction is gone now.
2997 return BB;
2998}
2999
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003000// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3001//
3002// For integer:
3003// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3004// =>
3005// (SLL $lanetmp1, $lane, <log2size)
3006// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3007// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3008// (NEG $lanetmp2, $lanetmp1)
3009// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3010//
3011// For floating point:
3012// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3013// =>
3014// (SUBREG_TO_REG $wt, $fs, <subreg>)
3015// (SLL $lanetmp1, $lane, <log2size)
3016// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3017// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3018// (NEG $lanetmp2, $lanetmp1)
3019// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3020MachineBasicBlock *
3021MipsSETargetLowering::emitINSERT_DF_VIDX(MachineInstr *MI,
3022 MachineBasicBlock *BB,
3023 unsigned EltSizeInBytes,
3024 bool IsFP) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003025 const TargetInstrInfo *TII =
3026 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003027 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3028 DebugLoc DL = MI->getDebugLoc();
3029 unsigned Wd = MI->getOperand(0).getReg();
3030 unsigned SrcVecReg = MI->getOperand(1).getReg();
3031 unsigned LaneReg = MI->getOperand(2).getReg();
3032 unsigned SrcValReg = MI->getOperand(3).getReg();
3033
3034 const TargetRegisterClass *VecRC = nullptr;
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003035 const TargetRegisterClass *GPRRC =
Eric Christopher1c29a652014-07-18 22:55:25 +00003036 Subtarget.isGP64bit() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003037 unsigned EltLog2Size;
3038 unsigned InsertOp = 0;
3039 unsigned InsveOp = 0;
3040 switch (EltSizeInBytes) {
3041 default:
3042 llvm_unreachable("Unexpected size");
3043 case 1:
3044 EltLog2Size = 0;
3045 InsertOp = Mips::INSERT_B;
3046 InsveOp = Mips::INSVE_B;
3047 VecRC = &Mips::MSA128BRegClass;
3048 break;
3049 case 2:
3050 EltLog2Size = 1;
3051 InsertOp = Mips::INSERT_H;
3052 InsveOp = Mips::INSVE_H;
3053 VecRC = &Mips::MSA128HRegClass;
3054 break;
3055 case 4:
3056 EltLog2Size = 2;
3057 InsertOp = Mips::INSERT_W;
3058 InsveOp = Mips::INSVE_W;
3059 VecRC = &Mips::MSA128WRegClass;
3060 break;
3061 case 8:
3062 EltLog2Size = 3;
3063 InsertOp = Mips::INSERT_D;
3064 InsveOp = Mips::INSVE_D;
3065 VecRC = &Mips::MSA128DRegClass;
3066 break;
3067 }
3068
3069 if (IsFP) {
3070 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3071 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3072 .addImm(0)
3073 .addReg(SrcValReg)
3074 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3075 SrcValReg = Wt;
3076 }
3077
3078 // Convert the lane index into a byte index
3079 if (EltSizeInBytes != 1) {
3080 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
3081 BuildMI(*BB, MI, DL, TII->get(Mips::SLL), LaneTmp1)
3082 .addReg(LaneReg)
3083 .addImm(EltLog2Size);
3084 LaneReg = LaneTmp1;
3085 }
3086
3087 // Rotate bytes around so that the desired lane is element zero
3088 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3089 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3090 .addReg(SrcVecReg)
3091 .addReg(SrcVecReg)
3092 .addReg(LaneReg);
3093
3094 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3095 if (IsFP) {
3096 // Use insve.df to insert to element zero
3097 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3098 .addReg(WdTmp1)
3099 .addImm(0)
3100 .addReg(SrcValReg)
3101 .addImm(0);
3102 } else {
3103 // Use insert.df to insert to element zero
3104 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3105 .addReg(WdTmp1)
3106 .addReg(SrcValReg)
3107 .addImm(0);
3108 }
3109
3110 // Rotate elements the rest of the way for a full rotation.
3111 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3112 // the lane index to do this.
3113 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
3114 BuildMI(*BB, MI, DL, TII->get(Mips::SUB), LaneTmp2)
3115 .addReg(Mips::ZERO)
3116 .addReg(LaneReg);
3117 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3118 .addReg(WdTmp2)
3119 .addReg(WdTmp2)
3120 .addReg(LaneTmp2);
3121
3122 MI->eraseFromParent(); // The pseudo instruction is gone now.
3123 return BB;
3124}
3125
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003126// Emit the FILL_FW pseudo instruction.
3127//
3128// fill_fw_pseudo $wd, $fs
3129// =>
3130// implicit_def $wt1
3131// insert_subreg $wt2:subreg_lo, $wt1, $fs
3132// splati.w $wd, $wt2[0]
3133MachineBasicBlock *
3134MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
3135 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003136 const TargetInstrInfo *TII =
3137 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003138 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3139 DebugLoc DL = MI->getDebugLoc();
3140 unsigned Wd = MI->getOperand(0).getReg();
3141 unsigned Fs = MI->getOperand(1).getReg();
3142 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3143 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3144
3145 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3146 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3147 .addReg(Wt1)
3148 .addReg(Fs)
3149 .addImm(Mips::sub_lo);
3150 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3151
3152 MI->eraseFromParent(); // The pseudo instruction is gone now.
3153 return BB;
3154}
3155
3156// Emit the FILL_FD pseudo instruction.
3157//
3158// fill_fd_pseudo $wd, $fs
3159// =>
3160// implicit_def $wt1
3161// insert_subreg $wt2:subreg_64, $wt1, $fs
3162// splati.d $wd, $wt2[0]
3163MachineBasicBlock *
3164MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
3165 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003166 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003167
Eric Christopherd9134482014-08-04 21:25:23 +00003168 const TargetInstrInfo *TII =
3169 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003170 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3171 DebugLoc DL = MI->getDebugLoc();
3172 unsigned Wd = MI->getOperand(0).getReg();
3173 unsigned Fs = MI->getOperand(1).getReg();
3174 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3175 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3176
3177 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3178 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3179 .addReg(Wt1)
3180 .addReg(Fs)
3181 .addImm(Mips::sub_64);
3182 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003183
3184 MI->eraseFromParent(); // The pseudo instruction is gone now.
3185 return BB;
3186}
Daniel Sandersa9521602013-10-23 10:36:52 +00003187
3188// Emit the FEXP2_W_1 pseudo instructions.
3189//
3190// fexp2_w_1_pseudo $wd, $wt
3191// =>
3192// ldi.w $ws, 1
3193// fexp2.w $wd, $ws, $wt
3194MachineBasicBlock *
3195MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
3196 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003197 const TargetInstrInfo *TII =
3198 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003199 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3200 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3201 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3202 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
3203 DebugLoc DL = MI->getDebugLoc();
3204
3205 // Splat 1.0 into a vector
3206 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3207 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3208
3209 // Emit 1.0 * fexp2(Wt)
3210 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
3211 .addReg(Ws2)
3212 .addReg(MI->getOperand(1).getReg());
3213
3214 MI->eraseFromParent(); // The pseudo instruction is gone now.
3215 return BB;
3216}
3217
3218// Emit the FEXP2_D_1 pseudo instructions.
3219//
3220// fexp2_d_1_pseudo $wd, $wt
3221// =>
3222// ldi.d $ws, 1
3223// fexp2.d $wd, $ws, $wt
3224MachineBasicBlock *
3225MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
3226 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003227 const TargetInstrInfo *TII =
3228 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003229 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3230 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3231 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3232 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
3233 DebugLoc DL = MI->getDebugLoc();
3234
3235 // Splat 1.0 into a vector
3236 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3237 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3238
3239 // Emit 1.0 * fexp2(Wt)
3240 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
3241 .addReg(Ws2)
3242 .addReg(MI->getOperand(1).getReg());
3243
3244 MI->eraseFromParent(); // The pseudo instruction is gone now.
3245 return BB;
3246}