blob: c1bbf61712b0effb43ebb06880abc89c7d84610e [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"
Eric Christopher79cc1e32014-09-02 22:28:02 +000014#include "MipsMachineFunction.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000015#include "MipsRegisterInfo.h"
16#include "MipsTargetMachine.h"
17#include "llvm/CodeGen/MachineInstrBuilder.h"
18#include "llvm/CodeGen/MachineRegisterInfo.h"
Akira Hatanakaa6bbde52013-04-13 02:13:30 +000019#include "llvm/IR/Intrinsics.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000020#include "llvm/Support/CommandLine.h"
Daniel Sanders62aeab82013-10-30 13:31:27 +000021#include "llvm/Support/Debug.h"
Hans Wennborg3e9b1c12013-10-30 16:10:10 +000022#include "llvm/Support/raw_ostream.h"
Akira Hatanaka96ca1822013-03-13 00:54:29 +000023#include "llvm/Target/TargetInstrInfo.h"
24
25using namespace llvm;
26
Chandler Carruth84e68b22014-04-22 02:41:26 +000027#define DEBUG_TYPE "mips-isel"
28
Akira Hatanaka96ca1822013-03-13 00:54:29 +000029static cl::opt<bool>
30EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
31 cl::desc("MIPS: Enable tail calls."), cl::init(false));
32
Akira Hatanaka63791212013-09-07 00:52:30 +000033static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
34 cl::desc("Expand double precision loads and "
35 "stores to their single precision "
36 "counterparts"));
37
Eric Christopher8924d272014-07-18 23:25:04 +000038MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM,
39 const MipsSubtarget &STI)
40 : MipsTargetLowering(TM, STI) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000041 // Set up the register classes
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000042 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000043
Eric Christopher1c29a652014-07-18 22:55:25 +000044 if (Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +000045 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000046
Eric Christopher1c29a652014-07-18 22:55:25 +000047 if (Subtarget.hasDSP() || Subtarget.hasMSA()) {
Daniel Sanders36c671e2013-09-27 09:44:59 +000048 // Expand all truncating stores and extending loads.
49 unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
50 unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
51
52 for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
53 for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
54 setTruncStoreAction((MVT::SimpleValueType)VT0,
55 (MVT::SimpleValueType)VT1, Expand);
56
57 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
58 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
59 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
60 }
61 }
62
Eric Christopher1c29a652014-07-18 22:55:25 +000063 if (Subtarget.hasDSP()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000064 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
65
66 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
Akira Hatanaka654655f2013-08-14 00:53:38 +000067 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000068
69 // Expand all builtin opcodes.
70 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
71 setOperationAction(Opc, VecTys[i], Expand);
72
Akira Hatanaka2f088222013-04-13 00:55:41 +000073 setOperationAction(ISD::ADD, VecTys[i], Legal);
74 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000075 setOperationAction(ISD::LOAD, VecTys[i], Legal);
76 setOperationAction(ISD::STORE, VecTys[i], Legal);
77 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
78 }
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +000079
80 setTargetDAGCombine(ISD::SHL);
81 setTargetDAGCombine(ISD::SRA);
82 setTargetDAGCombine(ISD::SRL);
Akira Hatanaka68741cc2013-04-30 22:37:26 +000083 setTargetDAGCombine(ISD::SETCC);
84 setTargetDAGCombine(ISD::VSELECT);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000085 }
86
Eric Christopher1c29a652014-07-18 22:55:25 +000087 if (Subtarget.hasDSPR2())
Akira Hatanaka2f088222013-04-13 00:55:41 +000088 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
89
Eric Christopher1c29a652014-07-18 22:55:25 +000090 if (Subtarget.hasMSA()) {
Daniel Sandersc65f58a2013-09-11 10:15:48 +000091 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
92 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
93 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
94 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
95 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
96 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
97 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
Daniel Sandersf7456c72013-09-23 13:22:24 +000098
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +000099 setTargetDAGCombine(ISD::AND);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000100 setTargetDAGCombine(ISD::OR);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000101 setTargetDAGCombine(ISD::SRA);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000102 setTargetDAGCombine(ISD::VSELECT);
Daniel Sandersf7456c72013-09-23 13:22:24 +0000103 setTargetDAGCombine(ISD::XOR);
Jack Carter3a2c2d42013-08-13 20:54:07 +0000104 }
105
Eric Christopher1c29a652014-07-18 22:55:25 +0000106 if (!Subtarget.abiUsesSoftFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000107 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
108
109 // When dealing with single precision only, use libcalls
Eric Christopher1c29a652014-07-18 22:55:25 +0000110 if (!Subtarget.isSingleFloat()) {
111 if (Subtarget.isFP64bit())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000112 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
113 else
114 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
115 }
116 }
117
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000118 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
119 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
120 setOperationAction(ISD::MULHS, MVT::i32, Custom);
121 setOperationAction(ISD::MULHU, MVT::i32, Custom);
122
Eric Christopher1c29a652014-07-18 22:55:25 +0000123 if (Subtarget.hasCnMips())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000124 setOperationAction(ISD::MUL, MVT::i64, Legal);
Eric Christopher1c29a652014-07-18 22:55:25 +0000125 else if (Subtarget.isGP64bit())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000126 setOperationAction(ISD::MUL, MVT::i64, Custom);
127
Eric Christopher1c29a652014-07-18 22:55:25 +0000128 if (Subtarget.isGP64bit()) {
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000129 setOperationAction(ISD::MULHS, MVT::i64, Custom);
130 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000131 }
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000132
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000133 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
134 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
135
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000136 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
137 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
138 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
139 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000140 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
141 setOperationAction(ISD::LOAD, MVT::i32, Custom);
142 setOperationAction(ISD::STORE, MVT::i32, Custom);
143
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000144 setTargetDAGCombine(ISD::ADDE);
145 setTargetDAGCombine(ISD::SUBE);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000146 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000147
Daniel Sandersce09d072013-08-28 12:14:50 +0000148 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000149 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
150 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
151
Akira Hatanaka63791212013-09-07 00:52:30 +0000152 if (NoDPLoadStore) {
153 setOperationAction(ISD::LOAD, MVT::f64, Custom);
154 setOperationAction(ISD::STORE, MVT::f64, Custom);
155 }
156
Eric Christopher1c29a652014-07-18 22:55:25 +0000157 if (Subtarget.hasMips32r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000158 // MIPS32r6 replaces the accumulator-based multiplies with a three register
159 // instruction
Daniel Sanders826f8b32014-06-12 10:54:16 +0000160 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
161 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000162 setOperationAction(ISD::MUL, MVT::i32, Legal);
163 setOperationAction(ISD::MULHS, MVT::i32, Legal);
164 setOperationAction(ISD::MULHU, MVT::i32, Legal);
165
166 // MIPS32r6 replaces the accumulator-based division/remainder with separate
167 // three register division and remainder instructions.
168 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
169 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
170 setOperationAction(ISD::SDIV, MVT::i32, Legal);
171 setOperationAction(ISD::UDIV, MVT::i32, Legal);
172 setOperationAction(ISD::SREM, MVT::i32, Legal);
173 setOperationAction(ISD::UREM, MVT::i32, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000174
175 // MIPS32r6 replaces conditional moves with an equivalent that removes the
176 // need for three GPR read ports.
177 setOperationAction(ISD::SETCC, MVT::i32, Legal);
178 setOperationAction(ISD::SELECT, MVT::i32, Legal);
179 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
180
181 setOperationAction(ISD::SETCC, MVT::f32, Legal);
182 setOperationAction(ISD::SELECT, MVT::f32, Legal);
183 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
184
Eric Christopher1c29a652014-07-18 22:55:25 +0000185 assert(Subtarget.isFP64bit() && "FR=1 is required for MIPS32r6");
Daniel Sanders0fa60412014-06-12 13:39:06 +0000186 setOperationAction(ISD::SETCC, MVT::f64, Legal);
187 setOperationAction(ISD::SELECT, MVT::f64, Legal);
188 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
189
Daniel Sanders3d3ea532014-06-12 15:00:17 +0000190 setOperationAction(ISD::BRCOND, MVT::Other, Legal);
191
Daniel Sanders0fa60412014-06-12 13:39:06 +0000192 // Floating point > and >= are supported via < and <=
193 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
194 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
195 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
196 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
197
198 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
199 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
200 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
201 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000202 }
203
Eric Christopher1c29a652014-07-18 22:55:25 +0000204 if (Subtarget.hasMips64r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000205 // MIPS64r6 replaces the accumulator-based multiplies with a three register
206 // instruction
207 setOperationAction(ISD::MUL, MVT::i64, Legal);
208 setOperationAction(ISD::MULHS, MVT::i64, Legal);
209 setOperationAction(ISD::MULHU, MVT::i64, Legal);
210
211 // MIPS32r6 replaces the accumulator-based division/remainder with separate
212 // three register division and remainder instructions.
213 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
214 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
215 setOperationAction(ISD::SDIV, MVT::i64, Legal);
216 setOperationAction(ISD::UDIV, MVT::i64, Legal);
217 setOperationAction(ISD::SREM, MVT::i64, Legal);
218 setOperationAction(ISD::UREM, MVT::i64, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000219
220 // MIPS64r6 replaces conditional moves with an equivalent that removes the
221 // need for three GPR read ports.
222 setOperationAction(ISD::SETCC, MVT::i64, Legal);
223 setOperationAction(ISD::SELECT, MVT::i64, Legal);
224 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000225 }
226
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000227 computeRegisterProperties();
228}
229
230const MipsTargetLowering *
Eric Christopher8924d272014-07-18 23:25:04 +0000231llvm::createMipsSETargetLowering(MipsTargetMachine &TM,
232 const MipsSubtarget &STI) {
233 return new MipsSETargetLowering(TM, STI);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000234}
235
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000236const TargetRegisterClass *
237MipsSETargetLowering::getRepRegClassFor(MVT VT) const {
238 if (VT == MVT::Untyped)
Eric Christopher1c29a652014-07-18 22:55:25 +0000239 return Subtarget.hasDSP() ? &Mips::ACC64DSPRegClass : &Mips::ACC64RegClass;
Eric Christopherbf33a3c2014-07-02 23:18:40 +0000240
241 return TargetLowering::getRepRegClassFor(VT);
242}
243
Daniel Sanders7a289d02013-09-23 12:02:46 +0000244// Enable MSA support for the given integer type and Register class.
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000245void MipsSETargetLowering::
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000246addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
247 addRegisterClass(Ty, RC);
248
249 // Expand all builtin opcodes.
250 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
251 setOperationAction(Opc, Ty, Expand);
252
253 setOperationAction(ISD::BITCAST, Ty, Legal);
254 setOperationAction(ISD::LOAD, Ty, Legal);
255 setOperationAction(ISD::STORE, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000256 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
257 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000258 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000259
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +0000260 setOperationAction(ISD::ADD, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000261 setOperationAction(ISD::AND, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000262 setOperationAction(ISD::CTLZ, Ty, Legal);
Daniel Sanders766cb692013-09-23 13:40:21 +0000263 setOperationAction(ISD::CTPOP, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000264 setOperationAction(ISD::MUL, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000265 setOperationAction(ISD::OR, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000266 setOperationAction(ISD::SDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000267 setOperationAction(ISD::SREM, Ty, Legal);
Daniel Sandersfbcb5822013-09-11 11:58:30 +0000268 setOperationAction(ISD::SHL, Ty, Legal);
269 setOperationAction(ISD::SRA, Ty, Legal);
270 setOperationAction(ISD::SRL, Ty, Legal);
271 setOperationAction(ISD::SUB, Ty, Legal);
Daniel Sanders607952b2013-09-11 10:38:58 +0000272 setOperationAction(ISD::UDIV, Ty, Legal);
Daniel Sanders0210dd42013-10-01 10:22:35 +0000273 setOperationAction(ISD::UREM, Ty, Legal);
Daniel Sanderse5087042013-09-24 14:02:15 +0000274 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000275 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sanders8ca81e42013-09-23 12:57:42 +0000276 setOperationAction(ISD::XOR, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000277
Daniel Sanders015972b2013-10-11 10:00:06 +0000278 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
279 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
280 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
281 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
282 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
283 }
284
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000285 setOperationAction(ISD::SETCC, Ty, Legal);
286 setCondCodeAction(ISD::SETNE, Ty, Expand);
287 setCondCodeAction(ISD::SETGE, Ty, Expand);
288 setCondCodeAction(ISD::SETGT, Ty, Expand);
289 setCondCodeAction(ISD::SETUGE, Ty, Expand);
290 setCondCodeAction(ISD::SETUGT, Ty, Expand);
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000291}
292
Daniel Sanders7a289d02013-09-23 12:02:46 +0000293// Enable MSA support for the given floating-point type and Register class.
Daniel Sandersc65f58a2013-09-11 10:15:48 +0000294void MipsSETargetLowering::
295addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
Daniel Sanders3c9a0ad2013-08-23 10:10:13 +0000296 addRegisterClass(Ty, RC);
Jack Carterbabdcc82013-08-15 12:24:57 +0000297
298 // Expand all builtin opcodes.
299 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
300 setOperationAction(Opc, Ty, Expand);
301
302 setOperationAction(ISD::LOAD, Ty, Legal);
303 setOperationAction(ISD::STORE, Ty, Legal);
304 setOperationAction(ISD::BITCAST, Ty, Legal);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000305 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
Daniel Sandersa5150702013-09-27 12:31:32 +0000306 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
Daniel Sanders1dfddc72013-10-15 13:14:41 +0000307 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000308
309 if (Ty != MVT::v8f16) {
Daniel Sanders4f3ff1b2013-09-24 13:02:08 +0000310 setOperationAction(ISD::FABS, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000311 setOperationAction(ISD::FADD, Ty, Legal);
312 setOperationAction(ISD::FDIV, Ty, Legal);
Daniel Sandersa9521602013-10-23 10:36:52 +0000313 setOperationAction(ISD::FEXP2, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000314 setOperationAction(ISD::FLOG2, Ty, Legal);
Daniel Sandersd7103f32013-10-11 10:14:25 +0000315 setOperationAction(ISD::FMA, Ty, Legal);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000316 setOperationAction(ISD::FMUL, Ty, Legal);
317 setOperationAction(ISD::FRINT, Ty, Legal);
318 setOperationAction(ISD::FSQRT, Ty, Legal);
319 setOperationAction(ISD::FSUB, Ty, Legal);
Daniel Sanderse1d24352013-09-24 12:04:44 +0000320 setOperationAction(ISD::VSELECT, Ty, Legal);
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000321
322 setOperationAction(ISD::SETCC, Ty, Legal);
323 setCondCodeAction(ISD::SETOGE, Ty, Expand);
324 setCondCodeAction(ISD::SETOGT, Ty, Expand);
325 setCondCodeAction(ISD::SETUGE, Ty, Expand);
326 setCondCodeAction(ISD::SETUGT, Ty, Expand);
327 setCondCodeAction(ISD::SETGE, Ty, Expand);
328 setCondCodeAction(ISD::SETGT, Ty, Expand);
Daniel Sandersf5bd9372013-09-11 10:51:30 +0000329 }
Jack Carterbabdcc82013-08-15 12:24:57 +0000330}
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000331
332bool
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000333MipsSETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
334 unsigned,
335 unsigned,
336 bool *Fast) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000337 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
338
Eric Christopher1c29a652014-07-18 22:55:25 +0000339 if (Subtarget.systemSupportsUnalignedAccess()) {
Daniel Sandersac272632014-05-23 13:18:02 +0000340 // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
341 // implementation defined whether this is handled by hardware, software, or
342 // a hybrid of the two but it's expected that most implementations will
343 // handle the majority of cases in hardware.
344 if (Fast)
345 *Fast = true;
346 return true;
347 }
348
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000349 switch (SVT) {
350 case MVT::i64:
351 case MVT::i32:
352 if (Fast)
353 *Fast = true;
354 return true;
355 default:
356 return false;
357 }
358}
359
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000360SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
361 SelectionDAG &DAG) const {
362 switch(Op.getOpcode()) {
Akira Hatanaka63791212013-09-07 00:52:30 +0000363 case ISD::LOAD: return lowerLOAD(Op, DAG);
364 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000365 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
366 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
367 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
368 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
369 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
370 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000371 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
372 DAG);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +0000373 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
374 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000375 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000376 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Daniel Sanders7a289d02013-09-23 12:02:46 +0000377 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
Daniel Sanderse5087042013-09-24 14:02:15 +0000378 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000379 }
380
381 return MipsTargetLowering::LowerOperation(Op, DAG);
382}
383
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000384// selectMADD -
385// Transforms a subgraph in CurDAG if the following pattern is found:
386// (addc multLo, Lo0), (adde multHi, Hi0),
387// where,
388// multHi/Lo: product of multiplication
389// Lo0: initial value of Lo register
390// Hi0: initial value of Hi register
391// Return true if pattern matching was successful.
392static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
393 // ADDENode's second operand must be a flag output of an ADDC node in order
394 // for the matching to be successful.
395 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
396
397 if (ADDCNode->getOpcode() != ISD::ADDC)
398 return false;
399
400 SDValue MultHi = ADDENode->getOperand(0);
401 SDValue MultLo = ADDCNode->getOperand(0);
402 SDNode *MultNode = MultHi.getNode();
403 unsigned MultOpc = MultHi.getOpcode();
404
405 // MultHi and MultLo must be generated by the same node,
406 if (MultLo.getNode() != MultNode)
407 return false;
408
409 // and it must be a multiplication.
410 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
411 return false;
412
413 // MultLo amd MultHi must be the first and second output of MultNode
414 // respectively.
415 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
416 return false;
417
418 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
419 // of the values of MultNode, in which case MultNode will be removed in later
420 // phases.
421 // If there exist users other than ADDENode or ADDCNode, this function returns
422 // here, which will result in MultNode being mapped to a single MULT
423 // instruction node rather than a pair of MULT and MADD instructions being
424 // produced.
425 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
426 return false;
427
Andrew Trickef9de2a2013-05-25 02:42:55 +0000428 SDLoc DL(ADDENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000429
430 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000431 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000432 ADDCNode->getOperand(1),
433 ADDENode->getOperand(1));
434
435 // create MipsMAdd(u) node
436 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
437
438 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
439 MultNode->getOperand(0),// Factor 0
440 MultNode->getOperand(1),// Factor 1
441 ACCIn);
442
443 // replace uses of adde and addc here
444 if (!SDValue(ADDCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000445 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000446 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
447 }
448 if (!SDValue(ADDENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000449 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000450 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
451 }
452
453 return true;
454}
455
456// selectMSUB -
457// Transforms a subgraph in CurDAG if the following pattern is found:
458// (addc Lo0, multLo), (sube Hi0, multHi),
459// where,
460// multHi/Lo: product of multiplication
461// Lo0: initial value of Lo register
462// Hi0: initial value of Hi register
463// Return true if pattern matching was successful.
464static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
465 // SUBENode's second operand must be a flag output of an SUBC node in order
466 // for the matching to be successful.
467 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
468
469 if (SUBCNode->getOpcode() != ISD::SUBC)
470 return false;
471
472 SDValue MultHi = SUBENode->getOperand(1);
473 SDValue MultLo = SUBCNode->getOperand(1);
474 SDNode *MultNode = MultHi.getNode();
475 unsigned MultOpc = MultHi.getOpcode();
476
477 // MultHi and MultLo must be generated by the same node,
478 if (MultLo.getNode() != MultNode)
479 return false;
480
481 // and it must be a multiplication.
482 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
483 return false;
484
485 // MultLo amd MultHi must be the first and second output of MultNode
486 // respectively.
487 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
488 return false;
489
490 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
491 // of the values of MultNode, in which case MultNode will be removed in later
492 // phases.
493 // If there exist users other than SUBENode or SUBCNode, this function returns
494 // here, which will result in MultNode being mapped to a single MULT
495 // instruction node rather than a pair of MULT and MSUB instructions being
496 // produced.
497 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
498 return false;
499
Andrew Trickef9de2a2013-05-25 02:42:55 +0000500 SDLoc DL(SUBENode);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000501
502 // Initialize accumulator.
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000503 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000504 SUBCNode->getOperand(0),
505 SUBENode->getOperand(0));
506
507 // create MipsSub(u) node
508 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
509
510 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
511 MultNode->getOperand(0),// Factor 0
512 MultNode->getOperand(1),// Factor 1
513 ACCIn);
514
515 // replace uses of sube and subc here
516 if (!SDValue(SUBCNode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000517 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000518 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
519 }
520 if (!SDValue(SUBENode, 0).use_empty()) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000521 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000522 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
523 }
524
525 return true;
526}
527
528static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
529 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000530 const MipsSubtarget &Subtarget) {
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000531 if (DCI.isBeforeLegalize())
532 return SDValue();
533
Eric Christopher1c29a652014-07-18 22:55:25 +0000534 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
Daniel Sanders826f8b32014-06-12 10:54:16 +0000535 N->getValueType(0) == MVT::i32 && selectMADD(N, &DAG))
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000536 return SDValue(N, 0);
537
538 return SDValue();
539}
540
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000541// Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
542//
543// Performs the following transformations:
544// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
545// sign/zero-extension is completely overwritten by the new one performed by
546// the ISD::AND.
547// - Removes redundant zero extensions performed by an ISD::AND.
548static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
549 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000550 const MipsSubtarget &Subtarget) {
551 if (!Subtarget.hasMSA())
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000552 return SDValue();
553
554 SDValue Op0 = N->getOperand(0);
555 SDValue Op1 = N->getOperand(1);
556 unsigned Op0Opcode = Op0->getOpcode();
557
558 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
559 // where $d + 1 == 2^n and n == 32
560 // or $d + 1 == 2^n and n <= 32 and ZExt
561 // -> (MipsVExtractZExt $a, $b, $c)
562 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
563 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
564 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
565
566 if (!Mask)
567 return SDValue();
568
569 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
570
571 if (Log2IfPositive <= 0)
572 return SDValue(); // Mask+1 is not a power of 2
573
574 SDValue Op0Op2 = Op0->getOperand(2);
575 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
576 unsigned ExtendTySize = ExtendTy.getSizeInBits();
577 unsigned Log2 = Log2IfPositive;
578
579 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
580 Log2 == ExtendTySize) {
581 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000582 return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
583 Op0->getVTList(),
584 makeArrayRef(Ops, Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000585 }
586 }
587
588 return SDValue();
589}
590
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000591// Determine if the specified node is a constant vector splat.
592//
593// Returns true and sets Imm if:
594// * N is a ISD::BUILD_VECTOR representing a constant splat
595//
596// This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
597// differences are that it assumes the MSA has already been checked and the
598// arbitrary requirement for a maximum of 32-bit integers isn't applied (and
599// must not be in order for binsri.d to be selectable).
600static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
601 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
602
Craig Topper062a2ba2014-04-25 05:30:21 +0000603 if (!Node)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000604 return false;
605
606 APInt SplatValue, SplatUndef;
607 unsigned SplatBitSize;
608 bool HasAnyUndefs;
609
610 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
611 8, !IsLittleEndian))
612 return false;
613
614 Imm = SplatValue;
615
616 return true;
617}
618
Daniel Sandersab94b532013-10-30 15:20:38 +0000619// Test whether the given node is an all-ones build_vector.
620static bool isVectorAllOnes(SDValue N) {
621 // Look through bitcasts. Endianness doesn't matter because we are looking
622 // for an all-ones value.
623 if (N->getOpcode() == ISD::BITCAST)
624 N = N->getOperand(0);
625
626 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
627
628 if (!BVN)
629 return false;
630
631 APInt SplatValue, SplatUndef;
632 unsigned SplatBitSize;
633 bool HasAnyUndefs;
634
635 // Endianness doesn't matter in this context because we are looking for
636 // an all-ones value.
637 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
638 return SplatValue.isAllOnesValue();
639
640 return false;
641}
642
643// Test whether N is the bitwise inverse of OfNode.
644static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
645 if (N->getOpcode() != ISD::XOR)
646 return false;
647
648 if (isVectorAllOnes(N->getOperand(0)))
649 return N->getOperand(1) == OfNode;
650
651 if (isVectorAllOnes(N->getOperand(1)))
652 return N->getOperand(0) == OfNode;
653
654 return false;
655}
656
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000657// Perform combines where ISD::OR is the root node.
658//
659// Performs the following transformations:
660// - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
661// where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
662// vector type.
663static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
664 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000665 const MipsSubtarget &Subtarget) {
666 if (!Subtarget.hasMSA())
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000667 return SDValue();
668
669 EVT Ty = N->getValueType(0);
670
671 if (!Ty.is128BitVector())
672 return SDValue();
673
674 SDValue Op0 = N->getOperand(0);
675 SDValue Op1 = N->getOperand(1);
676
677 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
678 SDValue Op0Op0 = Op0->getOperand(0);
679 SDValue Op0Op1 = Op0->getOperand(1);
680 SDValue Op1Op0 = Op1->getOperand(0);
681 SDValue Op1Op1 = Op1->getOperand(1);
Eric Christopher1c29a652014-07-18 22:55:25 +0000682 bool IsLittleEndian = !Subtarget.isLittle();
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000683
684 SDValue IfSet, IfClr, Cond;
Daniel Sandersab94b532013-10-30 15:20:38 +0000685 bool IsConstantMask = false;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000686 APInt Mask, InvMask;
687
688 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
689 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
690 // looking.
691 // IfClr will be set if we find a valid match.
692 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
693 Cond = Op0Op0;
694 IfSet = Op0Op1;
695
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000696 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
697 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000698 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000699 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
700 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000701 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000702
703 IsConstantMask = true;
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000704 }
705
706 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
707 // thing again using this mask.
708 // IfClr will be set if we find a valid match.
709 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
710 Cond = Op0Op1;
711 IfSet = Op0Op0;
712
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000713 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
714 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000715 IfClr = Op1Op1;
Daniel Sandersc8c50fb2013-11-21 16:11:31 +0000716 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
717 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000718 IfClr = Op1Op0;
Daniel Sandersab94b532013-10-30 15:20:38 +0000719
720 IsConstantMask = true;
721 }
722
723 // If IfClr is not yet set, try looking for a non-constant match.
724 // IfClr will be set if we find a valid match amongst the eight
725 // possibilities.
726 if (!IfClr.getNode()) {
727 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
728 Cond = Op1Op0;
729 IfSet = Op1Op1;
730 IfClr = Op0Op1;
731 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
732 Cond = Op1Op0;
733 IfSet = Op1Op1;
734 IfClr = Op0Op0;
735 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
736 Cond = Op1Op1;
737 IfSet = Op1Op0;
738 IfClr = Op0Op1;
739 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
740 Cond = Op1Op1;
741 IfSet = Op1Op0;
742 IfClr = Op0Op0;
743 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
744 Cond = Op0Op0;
745 IfSet = Op0Op1;
746 IfClr = Op1Op1;
747 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
748 Cond = Op0Op0;
749 IfSet = Op0Op1;
750 IfClr = Op1Op0;
751 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
752 Cond = Op0Op1;
753 IfSet = Op0Op0;
754 IfClr = Op1Op1;
755 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
756 Cond = Op0Op1;
757 IfSet = Op0Op0;
758 IfClr = Op1Op0;
759 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000760 }
761
762 // At this point, IfClr will be set if we have a valid match.
763 if (!IfClr.getNode())
764 return SDValue();
765
766 assert(Cond.getNode() && IfSet.getNode());
767
768 // Fold degenerate cases.
Daniel Sandersab94b532013-10-30 15:20:38 +0000769 if (IsConstantMask) {
770 if (Mask.isAllOnesValue())
771 return IfSet;
772 else if (Mask == 0)
773 return IfClr;
774 }
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000775
776 // Transform the DAG into an equivalent VSELECT.
Daniel Sandersdf2215452014-03-12 11:54:00 +0000777 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
Daniel Sanders53fe6c42013-10-30 13:51:01 +0000778 }
779
780 return SDValue();
781}
782
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000783static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
784 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000785 const MipsSubtarget &Subtarget) {
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000786 if (DCI.isBeforeLegalize())
787 return SDValue();
788
Eric Christopher1c29a652014-07-18 22:55:25 +0000789 if (Subtarget.hasMips32() && N->getValueType(0) == MVT::i32 &&
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000790 selectMSUB(N, &DAG))
791 return SDValue(N, 0);
792
793 return SDValue();
794}
795
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000796static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
797 EVT ShiftTy, SelectionDAG &DAG) {
798 // Clear the upper (64 - VT.sizeInBits) bits.
799 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
800
801 // Return 0.
802 if (C == 0)
803 return DAG.getConstant(0, VT);
804
805 // Return x.
806 if (C == 1)
807 return X;
808
809 // If c is power of 2, return (shl x, log2(c)).
810 if (isPowerOf2_64(C))
811 return DAG.getNode(ISD::SHL, DL, VT, X,
812 DAG.getConstant(Log2_64(C), ShiftTy));
813
814 unsigned Log2Ceil = Log2_64_Ceil(C);
815 uint64_t Floor = 1LL << Log2_64(C);
816 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
817
818 // If |c - floor_c| <= |c - ceil_c|,
819 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
820 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
821 if (C - Floor <= Ceil - C) {
822 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
823 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
824 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
825 }
826
827 // If |c - floor_c| > |c - ceil_c|,
828 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
829 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
830 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
831 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
832}
833
834static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
835 const TargetLowering::DAGCombinerInfo &DCI,
836 const MipsSETargetLowering *TL) {
837 EVT VT = N->getValueType(0);
838
839 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
840 if (!VT.isVector())
841 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
842 VT, TL->getScalarShiftAmountTy(VT), DAG);
843
844 return SDValue(N, 0);
845}
846
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000847static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
848 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000849 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000850 // See if this is a vector splat immediate node.
851 APInt SplatValue, SplatUndef;
852 unsigned SplatBitSize;
853 bool HasAnyUndefs;
854 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
855 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
856
Eric Christopher1c29a652014-07-18 22:55:25 +0000857 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000858 return SDValue();
859
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000860 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000861 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000862 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000863 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000864 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000865 return SDValue();
866
Andrew Trickef9de2a2013-05-25 02:42:55 +0000867 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000868 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
869}
870
871static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
872 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000873 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000874 EVT Ty = N->getValueType(0);
875
876 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
877 return SDValue();
878
879 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
880}
881
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000882// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
883// constant splats into MipsISD::SHRA_DSP for DSPr2.
884//
885// Performs the following transformations:
886// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
887// sign/zero-extension is completely overwritten by the new one performed by
888// the ISD::SRA and ISD::SHL nodes.
889// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
890// sequence.
891//
892// See performDSPShiftCombine for more information about the transformation
893// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000894static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
895 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000896 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000897 EVT Ty = N->getValueType(0);
898
Eric Christopher1c29a652014-07-18 22:55:25 +0000899 if (Subtarget.hasMSA()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000900 SDValue Op0 = N->getOperand(0);
901 SDValue Op1 = N->getOperand(1);
902
903 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
904 // where $d + sizeof($c) == 32
905 // or $d + sizeof($c) <= 32 and SExt
906 // -> (MipsVExtractSExt $a, $b, $c)
907 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
908 SDValue Op0Op0 = Op0->getOperand(0);
909 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
910
911 if (!ShAmount)
912 return SDValue();
913
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000914 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
915 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
916 return SDValue();
917
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000918 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
919 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
920
921 if (TotalBits == 32 ||
922 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
923 TotalBits <= 32)) {
924 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
925 Op0Op0->getOperand(2) };
Chandler Carruth356665a2014-08-01 22:09:43 +0000926 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
927 Op0Op0->getVTList(),
928 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000929 }
930 }
931 }
932
Eric Christopher1c29a652014-07-18 22:55:25 +0000933 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000934 return SDValue();
935
936 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
937}
938
939
940static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
941 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000942 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000943 EVT Ty = N->getValueType(0);
944
Eric Christopher1c29a652014-07-18 22:55:25 +0000945 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000946 return SDValue();
947
948 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
949}
950
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000951static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
952 bool IsV216 = (Ty == MVT::v2i16);
953
954 switch (CC) {
955 case ISD::SETEQ:
956 case ISD::SETNE: return true;
957 case ISD::SETLT:
958 case ISD::SETLE:
959 case ISD::SETGT:
960 case ISD::SETGE: return IsV216;
961 case ISD::SETULT:
962 case ISD::SETULE:
963 case ISD::SETUGT:
964 case ISD::SETUGE: return !IsV216;
965 default: return false;
966 }
967}
968
969static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
970 EVT Ty = N->getValueType(0);
971
972 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
973 return SDValue();
974
975 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
976 return SDValue();
977
Andrew Trickef9de2a2013-05-25 02:42:55 +0000978 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000979 N->getOperand(1), N->getOperand(2));
980}
981
982static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
983 EVT Ty = N->getValueType(0);
984
Daniel Sanders3ce56622013-09-24 12:18:31 +0000985 if (Ty.is128BitVector() && Ty.isInteger()) {
986 // Try the following combines:
987 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
988 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
989 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
990 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
991 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
992 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
993 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
994 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
995 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
996 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
997 // legalizer.
998 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000999
Daniel Sanders3ce56622013-09-24 12:18:31 +00001000 if (Op0->getOpcode() != ISD::SETCC)
1001 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001002
Daniel Sanders3ce56622013-09-24 12:18:31 +00001003 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
1004 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001005
Daniel Sanders3ce56622013-09-24 12:18:31 +00001006 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
1007 Signed = true;
1008 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
1009 Signed = false;
1010 else
1011 return SDValue();
1012
1013 SDValue Op1 = N->getOperand(1);
1014 SDValue Op2 = N->getOperand(2);
1015 SDValue Op0Op0 = Op0->getOperand(0);
1016 SDValue Op0Op1 = Op0->getOperand(1);
1017
1018 if (Op1 == Op0Op0 && Op2 == Op0Op1)
1019 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
1020 Ty, Op1, Op2);
1021 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
1022 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
1023 Ty, Op1, Op2);
1024 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
1025 SDValue SetCC = N->getOperand(0);
1026
1027 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
1028 return SDValue();
1029
1030 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
1031 SetCC.getOperand(0), SetCC.getOperand(1),
1032 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
1033 }
1034
1035 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001036}
1037
Daniel Sandersf7456c72013-09-23 13:22:24 +00001038static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001039 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001040 EVT Ty = N->getValueType(0);
1041
Eric Christopher1c29a652014-07-18 22:55:25 +00001042 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001043 // Try the following combines:
1044 // (xor (or $a, $b), (build_vector allones))
1045 // (xor (or $a, $b), (bitcast (build_vector allones)))
1046 SDValue Op0 = N->getOperand(0);
1047 SDValue Op1 = N->getOperand(1);
1048 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001049
1050 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
1051 NotOp = Op1;
1052 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
1053 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001054 else
1055 return SDValue();
1056
1057 if (NotOp->getOpcode() == ISD::OR)
1058 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
1059 NotOp->getOperand(1));
1060 }
1061
1062 return SDValue();
1063}
1064
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001065SDValue
1066MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1067 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001068 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001069
1070 switch (N->getOpcode()) {
1071 case ISD::ADDE:
1072 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001073 case ISD::AND:
1074 Val = performANDCombine(N, DAG, DCI, Subtarget);
1075 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +00001076 case ISD::OR:
1077 Val = performORCombine(N, DAG, DCI, Subtarget);
1078 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001079 case ISD::SUBE:
1080 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka5832fc62013-06-26 18:48:17 +00001081 case ISD::MUL:
1082 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001083 case ISD::SHL:
1084 return performSHLCombine(N, DAG, DCI, Subtarget);
1085 case ISD::SRA:
1086 return performSRACombine(N, DAG, DCI, Subtarget);
1087 case ISD::SRL:
1088 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001089 case ISD::VSELECT:
1090 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +00001091 case ISD::XOR:
1092 Val = performXORCombine(N, DAG, Subtarget);
1093 break;
1094 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001095 Val = performSETCCCombine(N, DAG);
1096 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001097 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001098
Daniel Sanders62aeab82013-10-30 13:31:27 +00001099 if (Val.getNode()) {
1100 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1101 N->printrWithDepth(dbgs(), &DAG);
1102 dbgs() << "\n=> \n";
1103 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1104 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001105 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001106 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001107
1108 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001109}
1110
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001111MachineBasicBlock *
1112MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1113 MachineBasicBlock *BB) const {
1114 switch (MI->getOpcode()) {
1115 default:
1116 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1117 case Mips::BPOSGE32_PSEUDO:
1118 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001119 case Mips::SNZ_B_PSEUDO:
1120 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1121 case Mips::SNZ_H_PSEUDO:
1122 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1123 case Mips::SNZ_W_PSEUDO:
1124 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1125 case Mips::SNZ_D_PSEUDO:
1126 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1127 case Mips::SNZ_V_PSEUDO:
1128 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1129 case Mips::SZ_B_PSEUDO:
1130 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1131 case Mips::SZ_H_PSEUDO:
1132 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1133 case Mips::SZ_W_PSEUDO:
1134 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1135 case Mips::SZ_D_PSEUDO:
1136 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1137 case Mips::SZ_V_PSEUDO:
1138 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001139 case Mips::COPY_FW_PSEUDO:
1140 return emitCOPY_FW(MI, BB);
1141 case Mips::COPY_FD_PSEUDO:
1142 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001143 case Mips::INSERT_FW_PSEUDO:
1144 return emitINSERT_FW(MI, BB);
1145 case Mips::INSERT_FD_PSEUDO:
1146 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001147 case Mips::INSERT_B_VIDX_PSEUDO:
1148 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1149 case Mips::INSERT_H_VIDX_PSEUDO:
1150 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1151 case Mips::INSERT_W_VIDX_PSEUDO:
1152 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1153 case Mips::INSERT_D_VIDX_PSEUDO:
1154 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1155 case Mips::INSERT_FW_VIDX_PSEUDO:
1156 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1157 case Mips::INSERT_FD_VIDX_PSEUDO:
1158 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001159 case Mips::FILL_FW_PSEUDO:
1160 return emitFILL_FW(MI, BB);
1161 case Mips::FILL_FD_PSEUDO:
1162 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001163 case Mips::FEXP2_W_1_PSEUDO:
1164 return emitFEXP2_W_1(MI, BB);
1165 case Mips::FEXP2_D_1_PSEUDO:
1166 return emitFEXP2_D_1(MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001167 }
1168}
1169
1170bool MipsSETargetLowering::
1171isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
1172 unsigned NextStackOffset,
1173 const MipsFunctionInfo& FI) const {
1174 if (!EnableMipsTailCalls)
1175 return false;
1176
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001177 // Return false if either the callee or caller has a byval argument.
1178 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
1179 return false;
1180
1181 // Return true if the callee's argument area is no larger than the
1182 // caller's.
1183 return NextStackOffset <= FI.getIncomingArgSize();
1184}
1185
1186void MipsSETargetLowering::
1187getOpndList(SmallVectorImpl<SDValue> &Ops,
1188 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
1189 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
1190 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001191 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001192 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
1193 InternalLinkage, CLI, Callee, Chain);
1194}
1195
Akira Hatanaka63791212013-09-07 00:52:30 +00001196SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1197 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1198
1199 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1200 return MipsTargetLowering::lowerLOAD(Op, DAG);
1201
1202 // Replace a double precision load with two i32 loads and a buildpair64.
1203 SDLoc DL(Op);
1204 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1205 EVT PtrVT = Ptr.getValueType();
1206
1207 // i32 load from lower address.
1208 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1209 MachinePointerInfo(), Nd.isVolatile(),
1210 Nd.isNonTemporal(), Nd.isInvariant(),
1211 Nd.getAlignment());
1212
1213 // i32 load from higher address.
1214 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1215 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1216 MachinePointerInfo(), Nd.isVolatile(),
1217 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001218 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka63791212013-09-07 00:52:30 +00001219
Eric Christopher1c29a652014-07-18 22:55:25 +00001220 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001221 std::swap(Lo, Hi);
1222
1223 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1224 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001225 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001226}
1227
1228SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1229 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1230
1231 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1232 return MipsTargetLowering::lowerSTORE(Op, DAG);
1233
1234 // Replace a double precision store with two extractelement64s and i32 stores.
1235 SDLoc DL(Op);
1236 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1237 EVT PtrVT = Ptr.getValueType();
1238 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1239 Val, DAG.getConstant(0, MVT::i32));
1240 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1241 Val, DAG.getConstant(1, MVT::i32));
1242
Eric Christopher1c29a652014-07-18 22:55:25 +00001243 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001244 std::swap(Lo, Hi);
1245
1246 // i32 store to lower address.
1247 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1248 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
Hal Finkelcc39b672014-07-24 12:16:19 +00001249 Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001250
1251 // i32 store to higher address.
1252 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1253 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001254 Nd.isVolatile(), Nd.isNonTemporal(),
Hal Finkelcc39b672014-07-24 12:16:19 +00001255 std::min(Nd.getAlignment(), 4U), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001256}
1257
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001258SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1259 bool HasLo, bool HasHi,
1260 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001261 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001262 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001263
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001264 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001265 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001266 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1267 Op.getOperand(0), Op.getOperand(1));
1268 SDValue Lo, Hi;
1269
1270 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001271 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001272 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001273 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001274
1275 if (!HasLo || !HasHi)
1276 return HasLo ? Lo : Hi;
1277
1278 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001279 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001280}
1281
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001282
Andrew Trickef9de2a2013-05-25 02:42:55 +00001283static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001284 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1285 DAG.getConstant(0, MVT::i32));
1286 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1287 DAG.getConstant(1, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001288 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001289}
1290
Andrew Trickef9de2a2013-05-25 02:42:55 +00001291static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001292 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1293 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001294 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1295}
1296
1297// This function expands mips intrinsic nodes which have 64-bit input operands
1298// or output values.
1299//
1300// out64 = intrinsic-node in64
1301// =>
1302// lo = copy (extract-element (in64, 0))
1303// hi = copy (extract-element (in64, 1))
1304// mips-specific-node
1305// v0 = copy lo
1306// v1 = copy hi
1307// out64 = merge-values (v0, v1)
1308//
1309static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001310 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001311 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1312 SmallVector<SDValue, 3> Ops;
1313 unsigned OpNo = 0;
1314
1315 // See if Op has a chain input.
1316 if (HasChainIn)
1317 Ops.push_back(Op->getOperand(OpNo++));
1318
1319 // The next operand is the intrinsic opcode.
1320 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1321
1322 // See if the next operand has type i64.
1323 SDValue Opnd = Op->getOperand(++OpNo), In64;
1324
1325 if (Opnd.getValueType() == MVT::i64)
1326 In64 = initAccumulator(Opnd, DL, DAG);
1327 else
1328 Ops.push_back(Opnd);
1329
1330 // Push the remaining operands.
1331 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1332 Ops.push_back(Op->getOperand(OpNo));
1333
1334 // Add In64 to the end of the list.
1335 if (In64.getNode())
1336 Ops.push_back(In64);
1337
1338 // Scan output.
1339 SmallVector<EVT, 2> ResTys;
1340
1341 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1342 I != E; ++I)
1343 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1344
1345 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001346 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001347 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1348
1349 if (!HasChainIn)
1350 return Out;
1351
1352 assert(Val->getValueType(1) == MVT::Other);
1353 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001354 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001355}
1356
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001357// Lower an MSA copy intrinsic into the specified SelectionDAG node
1358static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1359 SDLoc DL(Op);
1360 SDValue Vec = Op->getOperand(1);
1361 SDValue Idx = Op->getOperand(2);
1362 EVT ResTy = Op->getValueType(0);
1363 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1364
1365 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1366 DAG.getValueType(EltTy));
1367
1368 return Result;
1369}
1370
Daniel Sanders50b80412013-11-15 12:56:49 +00001371static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1372 EVT ResVecTy = Op->getValueType(0);
1373 EVT ViaVecTy = ResVecTy;
1374 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001375
Daniel Sanders50b80412013-11-15 12:56:49 +00001376 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1377 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1378 // lanes.
1379 SDValue LaneA;
1380 SDValue LaneB = Op->getOperand(2);
1381
1382 if (ResVecTy == MVT::v2i64) {
1383 LaneA = DAG.getConstant(0, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001384 ViaVecTy = MVT::v4i32;
Daniel Sanders50b80412013-11-15 12:56:49 +00001385 } else
1386 LaneA = LaneB;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001387
Daniel Sanders50b80412013-11-15 12:56:49 +00001388 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1389 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001390
Craig Topper48d114b2014-04-26 18:35:24 +00001391 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001392 makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001393
1394 if (ViaVecTy != ResVecTy)
1395 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001396
1397 return Result;
1398}
1399
Daniel Sanders50b80412013-11-15 12:56:49 +00001400static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1401 return DAG.getConstant(Op->getConstantOperandVal(ImmOp), Op->getValueType(0));
1402}
1403
1404static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1405 bool BigEndian, SelectionDAG &DAG) {
1406 EVT ViaVecTy = VecTy;
1407 SDValue SplatValueA = SplatValue;
1408 SDValue SplatValueB = SplatValue;
1409 SDLoc DL(SplatValue);
1410
1411 if (VecTy == MVT::v2i64) {
1412 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1413 ViaVecTy = MVT::v4i32;
1414
1415 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1416 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
1417 DAG.getConstant(32, MVT::i32));
1418 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1419 }
1420
1421 // We currently hold the parts in little endian order. Swap them if
1422 // necessary.
1423 if (BigEndian)
1424 std::swap(SplatValueA, SplatValueB);
1425
1426 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1427 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1428 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1429 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1430
Craig Topper48d114b2014-04-26 18:35:24 +00001431 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001432 makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001433
1434 if (VecTy != ViaVecTy)
1435 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1436
1437 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001438}
1439
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001440static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1441 unsigned Opc, SDValue Imm,
1442 bool BigEndian) {
1443 EVT VecTy = Op->getValueType(0);
1444 SDValue Exp2Imm;
1445 SDLoc DL(Op);
1446
Daniel Sanders50b80412013-11-15 12:56:49 +00001447 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1448 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001449 if (VecTy == MVT::v2i64) {
1450 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1451 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1452
1453 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001454 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), MVT::i32);
1455
1456 if (BigEndian)
1457 std::swap(BitImmLoOp, BitImmHiOp);
1458
1459 Exp2Imm =
1460 DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
1461 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, BitImmLoOp,
1462 BitImmHiOp, BitImmLoOp, BitImmHiOp));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001463 }
1464 }
1465
Craig Topper062a2ba2014-04-25 05:30:21 +00001466 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001467 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001468
1469 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1470 // only values 0-63 are valid.
1471 if (VecTy == MVT::v2i64)
1472 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1473
1474 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1475
1476 Exp2Imm =
1477 DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, VecTy), Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001478 }
1479
1480 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1481}
1482
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001483static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1484 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001485 SDLoc DL(Op);
Daniel Sanders50b80412013-11-15 12:56:49 +00001486 SDValue One = DAG.getConstant(1, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001487 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
1488
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001489 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1490 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001491}
1492
1493static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1494 SDLoc DL(Op);
1495 EVT ResTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001496 APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
1497 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
1498 SDValue BitMask = DAG.getConstant(~BitImm, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001499
1500 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1501}
1502
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001503SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1504 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001505 SDLoc DL(Op);
1506
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001507 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1508 default:
1509 return SDValue();
1510 case Intrinsic::mips_shilo:
1511 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1512 case Intrinsic::mips_dpau_h_qbl:
1513 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1514 case Intrinsic::mips_dpau_h_qbr:
1515 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1516 case Intrinsic::mips_dpsu_h_qbl:
1517 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1518 case Intrinsic::mips_dpsu_h_qbr:
1519 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1520 case Intrinsic::mips_dpa_w_ph:
1521 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1522 case Intrinsic::mips_dps_w_ph:
1523 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1524 case Intrinsic::mips_dpax_w_ph:
1525 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1526 case Intrinsic::mips_dpsx_w_ph:
1527 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1528 case Intrinsic::mips_mulsa_w_ph:
1529 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1530 case Intrinsic::mips_mult:
1531 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1532 case Intrinsic::mips_multu:
1533 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1534 case Intrinsic::mips_madd:
1535 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1536 case Intrinsic::mips_maddu:
1537 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1538 case Intrinsic::mips_msub:
1539 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1540 case Intrinsic::mips_msubu:
1541 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001542 case Intrinsic::mips_addv_b:
1543 case Intrinsic::mips_addv_h:
1544 case Intrinsic::mips_addv_w:
1545 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001546 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1547 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001548 case Intrinsic::mips_addvi_b:
1549 case Intrinsic::mips_addvi_h:
1550 case Intrinsic::mips_addvi_w:
1551 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001552 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1553 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001554 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001555 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1556 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001557 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001558 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1559 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001560 case Intrinsic::mips_bclr_b:
1561 case Intrinsic::mips_bclr_h:
1562 case Intrinsic::mips_bclr_w:
1563 case Intrinsic::mips_bclr_d:
1564 return lowerMSABitClear(Op, DAG);
1565 case Intrinsic::mips_bclri_b:
1566 case Intrinsic::mips_bclri_h:
1567 case Intrinsic::mips_bclri_w:
1568 case Intrinsic::mips_bclri_d:
1569 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001570 case Intrinsic::mips_binsli_b:
1571 case Intrinsic::mips_binsli_h:
1572 case Intrinsic::mips_binsli_w:
1573 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001574 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001575 EVT VecTy = Op->getValueType(0);
1576 EVT EltTy = VecTy.getVectorElementType();
1577 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
1578 Op->getConstantOperandVal(3));
1579 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Daniel Sandersdf2215452014-03-12 11:54:00 +00001580 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1581 Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001582 }
1583 case Intrinsic::mips_binsri_b:
1584 case Intrinsic::mips_binsri_h:
1585 case Intrinsic::mips_binsri_w:
1586 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001587 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001588 EVT VecTy = Op->getValueType(0);
1589 EVT EltTy = VecTy.getVectorElementType();
1590 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
1591 Op->getConstantOperandVal(3));
1592 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Daniel Sandersdf2215452014-03-12 11:54:00 +00001593 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1594 Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001595 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001596 case Intrinsic::mips_bmnz_v:
1597 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1598 Op->getOperand(2), Op->getOperand(1));
1599 case Intrinsic::mips_bmnzi_b:
1600 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1601 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1602 Op->getOperand(1));
1603 case Intrinsic::mips_bmz_v:
1604 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1605 Op->getOperand(1), Op->getOperand(2));
1606 case Intrinsic::mips_bmzi_b:
1607 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1608 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1609 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001610 case Intrinsic::mips_bneg_b:
1611 case Intrinsic::mips_bneg_h:
1612 case Intrinsic::mips_bneg_w:
1613 case Intrinsic::mips_bneg_d: {
1614 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001615 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001616
1617 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1618 DAG.getNode(ISD::SHL, DL, VecTy, One,
1619 Op->getOperand(2)));
1620 }
1621 case Intrinsic::mips_bnegi_b:
1622 case Intrinsic::mips_bnegi_h:
1623 case Intrinsic::mips_bnegi_w:
1624 case Intrinsic::mips_bnegi_d:
1625 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001626 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001627 case Intrinsic::mips_bnz_b:
1628 case Intrinsic::mips_bnz_h:
1629 case Intrinsic::mips_bnz_w:
1630 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001631 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1632 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001633 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001634 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1635 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001636 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001637 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001638 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001639 Op->getOperand(1), Op->getOperand(3),
1640 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001641 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001642 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001643 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001644 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1645 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001646 case Intrinsic::mips_bset_b:
1647 case Intrinsic::mips_bset_h:
1648 case Intrinsic::mips_bset_w:
1649 case Intrinsic::mips_bset_d: {
1650 EVT VecTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001651 SDValue One = DAG.getConstant(1, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001652
1653 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1654 DAG.getNode(ISD::SHL, DL, VecTy, One,
1655 Op->getOperand(2)));
1656 }
1657 case Intrinsic::mips_bseti_b:
1658 case Intrinsic::mips_bseti_h:
1659 case Intrinsic::mips_bseti_w:
1660 case Intrinsic::mips_bseti_d:
1661 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001662 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001663 case Intrinsic::mips_bz_b:
1664 case Intrinsic::mips_bz_h:
1665 case Intrinsic::mips_bz_w:
1666 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001667 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1668 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001669 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001670 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1671 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001672 case Intrinsic::mips_ceq_b:
1673 case Intrinsic::mips_ceq_h:
1674 case Intrinsic::mips_ceq_w:
1675 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001676 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001677 Op->getOperand(2), ISD::SETEQ);
1678 case Intrinsic::mips_ceqi_b:
1679 case Intrinsic::mips_ceqi_h:
1680 case Intrinsic::mips_ceqi_w:
1681 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001682 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001683 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1684 case Intrinsic::mips_cle_s_b:
1685 case Intrinsic::mips_cle_s_h:
1686 case Intrinsic::mips_cle_s_w:
1687 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001688 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001689 Op->getOperand(2), ISD::SETLE);
1690 case Intrinsic::mips_clei_s_b:
1691 case Intrinsic::mips_clei_s_h:
1692 case Intrinsic::mips_clei_s_w:
1693 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001694 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001695 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1696 case Intrinsic::mips_cle_u_b:
1697 case Intrinsic::mips_cle_u_h:
1698 case Intrinsic::mips_cle_u_w:
1699 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001700 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001701 Op->getOperand(2), ISD::SETULE);
1702 case Intrinsic::mips_clei_u_b:
1703 case Intrinsic::mips_clei_u_h:
1704 case Intrinsic::mips_clei_u_w:
1705 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001706 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001707 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1708 case Intrinsic::mips_clt_s_b:
1709 case Intrinsic::mips_clt_s_h:
1710 case Intrinsic::mips_clt_s_w:
1711 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001712 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001713 Op->getOperand(2), ISD::SETLT);
1714 case Intrinsic::mips_clti_s_b:
1715 case Intrinsic::mips_clti_s_h:
1716 case Intrinsic::mips_clti_s_w:
1717 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001718 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001719 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1720 case Intrinsic::mips_clt_u_b:
1721 case Intrinsic::mips_clt_u_h:
1722 case Intrinsic::mips_clt_u_w:
1723 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001724 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001725 Op->getOperand(2), ISD::SETULT);
1726 case Intrinsic::mips_clti_u_b:
1727 case Intrinsic::mips_clti_u_h:
1728 case Intrinsic::mips_clti_u_w:
1729 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001730 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001731 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001732 case Intrinsic::mips_copy_s_b:
1733 case Intrinsic::mips_copy_s_h:
1734 case Intrinsic::mips_copy_s_w:
1735 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001736 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001737 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001738 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1739 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1740 else {
1741 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1742 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1743 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1744 Op->getValueType(0), Op->getOperand(1),
1745 Op->getOperand(2));
1746 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001747 case Intrinsic::mips_copy_u_b:
1748 case Intrinsic::mips_copy_u_h:
1749 case Intrinsic::mips_copy_u_w:
1750 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001751 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001752 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001753 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1754 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1755 else {
1756 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1757 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1758 // Note: When i64 is illegal, this results in copy_s.w instructions
1759 // instead of copy_u.w instructions. This makes no difference to the
1760 // behaviour since i64 is only illegal when the register file is 32-bit.
1761 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1762 Op->getValueType(0), Op->getOperand(1),
1763 Op->getOperand(2));
1764 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001765 case Intrinsic::mips_div_s_b:
1766 case Intrinsic::mips_div_s_h:
1767 case Intrinsic::mips_div_s_w:
1768 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001769 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1770 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001771 case Intrinsic::mips_div_u_b:
1772 case Intrinsic::mips_div_u_h:
1773 case Intrinsic::mips_div_u_w:
1774 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001775 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1776 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001777 case Intrinsic::mips_fadd_w:
1778 case Intrinsic::mips_fadd_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001779 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1780 Op->getOperand(2));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001781 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1782 case Intrinsic::mips_fceq_w:
1783 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001784 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001785 Op->getOperand(2), ISD::SETOEQ);
1786 case Intrinsic::mips_fcle_w:
1787 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001788 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001789 Op->getOperand(2), ISD::SETOLE);
1790 case Intrinsic::mips_fclt_w:
1791 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001792 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001793 Op->getOperand(2), ISD::SETOLT);
1794 case Intrinsic::mips_fcne_w:
1795 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001796 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001797 Op->getOperand(2), ISD::SETONE);
1798 case Intrinsic::mips_fcor_w:
1799 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001800 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001801 Op->getOperand(2), ISD::SETO);
1802 case Intrinsic::mips_fcueq_w:
1803 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001804 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001805 Op->getOperand(2), ISD::SETUEQ);
1806 case Intrinsic::mips_fcule_w:
1807 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001808 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001809 Op->getOperand(2), ISD::SETULE);
1810 case Intrinsic::mips_fcult_w:
1811 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001812 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001813 Op->getOperand(2), ISD::SETULT);
1814 case Intrinsic::mips_fcun_w:
1815 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001816 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001817 Op->getOperand(2), ISD::SETUO);
1818 case Intrinsic::mips_fcune_w:
1819 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001820 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001821 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001822 case Intrinsic::mips_fdiv_w:
1823 case Intrinsic::mips_fdiv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001824 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1825 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001826 case Intrinsic::mips_ffint_u_w:
1827 case Intrinsic::mips_ffint_u_d:
1828 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1829 Op->getOperand(1));
1830 case Intrinsic::mips_ffint_s_w:
1831 case Intrinsic::mips_ffint_s_d:
1832 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1833 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001834 case Intrinsic::mips_fill_b:
1835 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001836 case Intrinsic::mips_fill_w:
1837 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001838 SmallVector<SDValue, 16> Ops;
1839 EVT ResTy = Op->getValueType(0);
1840
1841 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1842 Ops.push_back(Op->getOperand(1));
1843
Daniel Sandersc72593e2013-09-27 13:20:41 +00001844 // If ResTy is v2i64 then the type legalizer will break this node down into
1845 // an equivalent v4i32.
Craig Topper48d114b2014-04-26 18:35:24 +00001846 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001847 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001848 case Intrinsic::mips_fexp2_w:
1849 case Intrinsic::mips_fexp2_d: {
1850 EVT ResTy = Op->getValueType(0);
1851 return DAG.getNode(
1852 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1853 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1854 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001855 case Intrinsic::mips_flog2_w:
1856 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001857 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001858 case Intrinsic::mips_fmadd_w:
1859 case Intrinsic::mips_fmadd_d:
1860 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1861 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001862 case Intrinsic::mips_fmul_w:
1863 case Intrinsic::mips_fmul_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001864 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1865 Op->getOperand(2));
Daniel Sanderse67bd872013-10-11 10:27:32 +00001866 case Intrinsic::mips_fmsub_w:
1867 case Intrinsic::mips_fmsub_d: {
1868 EVT ResTy = Op->getValueType(0);
1869 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1870 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1871 Op->getOperand(2), Op->getOperand(3)));
1872 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001873 case Intrinsic::mips_frint_w:
1874 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001875 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001876 case Intrinsic::mips_fsqrt_w:
1877 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001878 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001879 case Intrinsic::mips_fsub_w:
1880 case Intrinsic::mips_fsub_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001881 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1882 Op->getOperand(2));
Daniel Sanders015972b2013-10-11 10:00:06 +00001883 case Intrinsic::mips_ftrunc_u_w:
1884 case Intrinsic::mips_ftrunc_u_d:
1885 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1886 Op->getOperand(1));
1887 case Intrinsic::mips_ftrunc_s_w:
1888 case Intrinsic::mips_ftrunc_s_d:
1889 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1890 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001891 case Intrinsic::mips_ilvev_b:
1892 case Intrinsic::mips_ilvev_h:
1893 case Intrinsic::mips_ilvev_w:
1894 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001895 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001896 Op->getOperand(1), Op->getOperand(2));
1897 case Intrinsic::mips_ilvl_b:
1898 case Intrinsic::mips_ilvl_h:
1899 case Intrinsic::mips_ilvl_w:
1900 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001901 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001902 Op->getOperand(1), Op->getOperand(2));
1903 case Intrinsic::mips_ilvod_b:
1904 case Intrinsic::mips_ilvod_h:
1905 case Intrinsic::mips_ilvod_w:
1906 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001907 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001908 Op->getOperand(1), Op->getOperand(2));
1909 case Intrinsic::mips_ilvr_b:
1910 case Intrinsic::mips_ilvr_h:
1911 case Intrinsic::mips_ilvr_w:
1912 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001913 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001914 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001915 case Intrinsic::mips_insert_b:
1916 case Intrinsic::mips_insert_h:
1917 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001918 case Intrinsic::mips_insert_d:
1919 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1920 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001921 case Intrinsic::mips_insve_b:
1922 case Intrinsic::mips_insve_h:
1923 case Intrinsic::mips_insve_w:
1924 case Intrinsic::mips_insve_d:
1925 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1926 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
1927 DAG.getConstant(0, MVT::i32));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001928 case Intrinsic::mips_ldi_b:
1929 case Intrinsic::mips_ldi_h:
1930 case Intrinsic::mips_ldi_w:
1931 case Intrinsic::mips_ldi_d:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001932 return lowerMSASplatImm(Op, 1, DAG);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001933 case Intrinsic::mips_lsa:
1934 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001935 EVT ResTy = Op->getValueType(0);
1936 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1937 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1938 Op->getOperand(2), Op->getOperand(3)));
1939 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001940 case Intrinsic::mips_maddv_b:
1941 case Intrinsic::mips_maddv_h:
1942 case Intrinsic::mips_maddv_w:
1943 case Intrinsic::mips_maddv_d: {
1944 EVT ResTy = Op->getValueType(0);
1945 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1946 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1947 Op->getOperand(2), Op->getOperand(3)));
1948 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001949 case Intrinsic::mips_max_s_b:
1950 case Intrinsic::mips_max_s_h:
1951 case Intrinsic::mips_max_s_w:
1952 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001953 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1954 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001955 case Intrinsic::mips_max_u_b:
1956 case Intrinsic::mips_max_u_h:
1957 case Intrinsic::mips_max_u_w:
1958 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001959 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1960 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001961 case Intrinsic::mips_maxi_s_b:
1962 case Intrinsic::mips_maxi_s_h:
1963 case Intrinsic::mips_maxi_s_w:
1964 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001965 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1966 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001967 case Intrinsic::mips_maxi_u_b:
1968 case Intrinsic::mips_maxi_u_h:
1969 case Intrinsic::mips_maxi_u_w:
1970 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001971 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1972 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001973 case Intrinsic::mips_min_s_b:
1974 case Intrinsic::mips_min_s_h:
1975 case Intrinsic::mips_min_s_w:
1976 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001977 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1978 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001979 case Intrinsic::mips_min_u_b:
1980 case Intrinsic::mips_min_u_h:
1981 case Intrinsic::mips_min_u_w:
1982 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001983 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1984 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001985 case Intrinsic::mips_mini_s_b:
1986 case Intrinsic::mips_mini_s_h:
1987 case Intrinsic::mips_mini_s_w:
1988 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001989 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1990 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001991 case Intrinsic::mips_mini_u_b:
1992 case Intrinsic::mips_mini_u_h:
1993 case Intrinsic::mips_mini_u_w:
1994 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001995 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1996 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00001997 case Intrinsic::mips_mod_s_b:
1998 case Intrinsic::mips_mod_s_h:
1999 case Intrinsic::mips_mod_s_w:
2000 case Intrinsic::mips_mod_s_d:
2001 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
2002 Op->getOperand(2));
2003 case Intrinsic::mips_mod_u_b:
2004 case Intrinsic::mips_mod_u_h:
2005 case Intrinsic::mips_mod_u_w:
2006 case Intrinsic::mips_mod_u_d:
2007 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
2008 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002009 case Intrinsic::mips_mulv_b:
2010 case Intrinsic::mips_mulv_h:
2011 case Intrinsic::mips_mulv_w:
2012 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002013 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
2014 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00002015 case Intrinsic::mips_msubv_b:
2016 case Intrinsic::mips_msubv_h:
2017 case Intrinsic::mips_msubv_w:
2018 case Intrinsic::mips_msubv_d: {
2019 EVT ResTy = Op->getValueType(0);
2020 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
2021 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2022 Op->getOperand(2), Op->getOperand(3)));
2023 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002024 case Intrinsic::mips_nlzc_b:
2025 case Intrinsic::mips_nlzc_h:
2026 case Intrinsic::mips_nlzc_w:
2027 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002028 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002029 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002030 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2031 Op->getOperand(1), Op->getOperand(2));
2032 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002033 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002034 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002035 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2036 Op->getOperand(1),
2037 lowerMSASplatImm(Op, 2, DAG));
2038 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002039 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002040 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002041 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2042 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002043 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002044 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2045 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002046 case Intrinsic::mips_pckev_b:
2047 case Intrinsic::mips_pckev_h:
2048 case Intrinsic::mips_pckev_w:
2049 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002050 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002051 Op->getOperand(1), Op->getOperand(2));
2052 case Intrinsic::mips_pckod_b:
2053 case Intrinsic::mips_pckod_h:
2054 case Intrinsic::mips_pckod_w:
2055 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002056 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002057 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002058 case Intrinsic::mips_pcnt_b:
2059 case Intrinsic::mips_pcnt_h:
2060 case Intrinsic::mips_pcnt_w:
2061 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002062 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sanders26307182013-09-24 14:20:00 +00002063 case Intrinsic::mips_shf_b:
2064 case Intrinsic::mips_shf_h:
2065 case Intrinsic::mips_shf_w:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002066 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002067 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002068 case Intrinsic::mips_sll_b:
2069 case Intrinsic::mips_sll_h:
2070 case Intrinsic::mips_sll_w:
2071 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002072 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
2073 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002074 case Intrinsic::mips_slli_b:
2075 case Intrinsic::mips_slli_h:
2076 case Intrinsic::mips_slli_w:
2077 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002078 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2079 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002080 case Intrinsic::mips_splat_b:
2081 case Intrinsic::mips_splat_h:
2082 case Intrinsic::mips_splat_w:
2083 case Intrinsic::mips_splat_d:
2084 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2085 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2086 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2087 // Instead we lower to MipsISD::VSHF and match from there.
2088 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002089 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002090 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002091 case Intrinsic::mips_splati_b:
2092 case Intrinsic::mips_splati_h:
2093 case Intrinsic::mips_splati_w:
2094 case Intrinsic::mips_splati_d:
2095 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2096 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2097 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002098 case Intrinsic::mips_sra_b:
2099 case Intrinsic::mips_sra_h:
2100 case Intrinsic::mips_sra_w:
2101 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002102 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
2103 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002104 case Intrinsic::mips_srai_b:
2105 case Intrinsic::mips_srai_h:
2106 case Intrinsic::mips_srai_w:
2107 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002108 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2109 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002110 case Intrinsic::mips_srl_b:
2111 case Intrinsic::mips_srl_h:
2112 case Intrinsic::mips_srl_w:
2113 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002114 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
2115 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002116 case Intrinsic::mips_srli_b:
2117 case Intrinsic::mips_srli_h:
2118 case Intrinsic::mips_srli_w:
2119 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002120 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2121 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002122 case Intrinsic::mips_subv_b:
2123 case Intrinsic::mips_subv_h:
2124 case Intrinsic::mips_subv_w:
2125 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002126 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2127 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002128 case Intrinsic::mips_subvi_b:
2129 case Intrinsic::mips_subvi_h:
2130 case Intrinsic::mips_subvi_w:
2131 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002132 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2133 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002134 case Intrinsic::mips_vshf_b:
2135 case Intrinsic::mips_vshf_h:
2136 case Intrinsic::mips_vshf_w:
2137 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002138 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002139 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002140 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002141 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2142 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002143 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002144 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2145 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002146 }
2147}
2148
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002149static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2150 SDLoc DL(Op);
2151 SDValue ChainIn = Op->getOperand(0);
2152 SDValue Address = Op->getOperand(2);
2153 SDValue Offset = Op->getOperand(3);
2154 EVT ResTy = Op->getValueType(0);
2155 EVT PtrTy = Address->getValueType(0);
2156
2157 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2158
2159 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
2160 false, false, 16);
2161}
2162
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002163SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2164 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002165 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2166 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002167 default:
2168 return SDValue();
2169 case Intrinsic::mips_extp:
2170 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2171 case Intrinsic::mips_extpdp:
2172 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2173 case Intrinsic::mips_extr_w:
2174 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2175 case Intrinsic::mips_extr_r_w:
2176 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2177 case Intrinsic::mips_extr_rs_w:
2178 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2179 case Intrinsic::mips_extr_s_h:
2180 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2181 case Intrinsic::mips_mthlip:
2182 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2183 case Intrinsic::mips_mulsaq_s_w_ph:
2184 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2185 case Intrinsic::mips_maq_s_w_phl:
2186 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2187 case Intrinsic::mips_maq_s_w_phr:
2188 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2189 case Intrinsic::mips_maq_sa_w_phl:
2190 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2191 case Intrinsic::mips_maq_sa_w_phr:
2192 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2193 case Intrinsic::mips_dpaq_s_w_ph:
2194 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2195 case Intrinsic::mips_dpsq_s_w_ph:
2196 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2197 case Intrinsic::mips_dpaq_sa_l_w:
2198 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2199 case Intrinsic::mips_dpsq_sa_l_w:
2200 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2201 case Intrinsic::mips_dpaqx_s_w_ph:
2202 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2203 case Intrinsic::mips_dpaqx_sa_w_ph:
2204 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2205 case Intrinsic::mips_dpsqx_s_w_ph:
2206 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2207 case Intrinsic::mips_dpsqx_sa_w_ph:
2208 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002209 case Intrinsic::mips_ld_b:
2210 case Intrinsic::mips_ld_h:
2211 case Intrinsic::mips_ld_w:
2212 case Intrinsic::mips_ld_d:
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002213 return lowerMSALoadIntr(Op, DAG, Intr);
2214 }
2215}
2216
2217static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2218 SDLoc DL(Op);
2219 SDValue ChainIn = Op->getOperand(0);
2220 SDValue Value = Op->getOperand(2);
2221 SDValue Address = Op->getOperand(3);
2222 SDValue Offset = Op->getOperand(4);
2223 EVT PtrTy = Address->getValueType(0);
2224
2225 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2226
2227 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
2228 false, 16);
2229}
2230
2231SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2232 SelectionDAG &DAG) const {
2233 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2234 switch (Intr) {
2235 default:
2236 return SDValue();
2237 case Intrinsic::mips_st_b:
2238 case Intrinsic::mips_st_h:
2239 case Intrinsic::mips_st_w:
2240 case Intrinsic::mips_st_d:
Daniel Sandersce09d072013-08-28 12:14:50 +00002241 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002242 }
2243}
2244
Daniel Sanders7a289d02013-09-23 12:02:46 +00002245/// \brief Check if the given BuildVectorSDNode is a splat.
2246/// This method currently relies on DAG nodes being reused when equivalent,
2247/// so it's possible for this to return false even when isConstantSplat returns
2248/// true.
2249static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002250 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002251 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002252
2253 SDValue Operand0 = N->getOperand(0);
2254
2255 for (unsigned int i = 1; i < nOps; ++i) {
2256 if (N->getOperand(i) != Operand0)
2257 return false;
2258 }
2259
2260 return true;
2261}
2262
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002263// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2264//
2265// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2266// choose to sign-extend but we could have equally chosen zero-extend. The
2267// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2268// result into this node later (possibly changing it to a zero-extend in the
2269// process).
2270SDValue MipsSETargetLowering::
2271lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2272 SDLoc DL(Op);
2273 EVT ResTy = Op->getValueType(0);
2274 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002275 EVT VecTy = Op0->getValueType(0);
2276
2277 if (!VecTy.is128BitVector())
2278 return SDValue();
2279
2280 if (ResTy.isInteger()) {
2281 SDValue Op1 = Op->getOperand(1);
2282 EVT EltTy = VecTy.getVectorElementType();
2283 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2284 DAG.getValueType(EltTy));
2285 }
2286
2287 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002288}
2289
Daniel Sandersf49dd822013-09-24 13:33:07 +00002290static bool isConstantOrUndef(const SDValue Op) {
2291 if (Op->getOpcode() == ISD::UNDEF)
2292 return true;
2293 if (dyn_cast<ConstantSDNode>(Op))
2294 return true;
2295 if (dyn_cast<ConstantFPSDNode>(Op))
2296 return true;
2297 return false;
2298}
2299
2300static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2301 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2302 if (isConstantOrUndef(Op->getOperand(i)))
2303 return true;
2304 return false;
2305}
2306
Daniel Sanders7a289d02013-09-23 12:02:46 +00002307// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2308// backend.
2309//
2310// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002311// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2312// 2 less than or equal to 64 and the value fits into a signed 10-bit
2313// immediate
2314// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2315// is a power of 2 less than or equal to 64 and the value does not fit into a
2316// signed 10-bit immediate
2317// - Non-constant splats are legal as-is.
2318// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2319// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002320SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2321 SelectionDAG &DAG) const {
2322 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2323 EVT ResTy = Op->getValueType(0);
2324 SDLoc DL(Op);
2325 APInt SplatValue, SplatUndef;
2326 unsigned SplatBitSize;
2327 bool HasAnyUndefs;
2328
Eric Christopher1c29a652014-07-18 22:55:25 +00002329 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002330 return SDValue();
2331
2332 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2333 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002334 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002335 // We can only cope with 8, 16, 32, or 64-bit elements
2336 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2337 SplatBitSize != 64)
2338 return SDValue();
2339
2340 // If the value fits into a simm10 then we can use ldi.[bhwd]
Daniel Sandersfd8e4162013-11-22 11:24:50 +00002341 // However, if it isn't an integer type we will have to bitcast from an
Daniel Sandersd40aea82013-11-22 13:22:52 +00002342 // integer type first. Also, if there are any undefs, we must lower them
Daniel Sanders630dbe02013-11-22 13:14:06 +00002343 // to defined values first.
2344 if (ResTy.isInteger() && !HasAnyUndefs && SplatValue.isSignedIntN(10))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002345 return Op;
2346
2347 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002348
2349 switch (SplatBitSize) {
2350 default:
2351 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002352 case 8:
2353 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002354 break;
2355 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002356 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002357 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002358 case 32:
2359 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002360 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002361 case 64:
2362 // There's no fill.d to fall back on for 64-bit values
2363 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002364 }
2365
Daniel Sanders50b80412013-11-15 12:56:49 +00002366 // SelectionDAG::getConstant will promote SplatValue appropriately.
2367 SDValue Result = DAG.getConstant(SplatValue, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002368
Daniel Sanders50b80412013-11-15 12:56:49 +00002369 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002370 if (ViaVecTy != ResTy)
2371 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002372
2373 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002374 } else if (isSplatVector(Node))
2375 return Op;
2376 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002377 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2378 // The resulting code is the same length as the expansion, but it doesn't
2379 // use memory operations
2380 EVT ResTy = Node->getValueType(0);
2381
2382 assert(ResTy.isVector());
2383
2384 unsigned NumElts = ResTy.getVectorNumElements();
2385 SDValue Vector = DAG.getUNDEF(ResTy);
2386 for (unsigned i = 0; i < NumElts; ++i) {
2387 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2388 Node->getOperand(i),
2389 DAG.getConstant(i, MVT::i32));
2390 }
2391 return Vector;
2392 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002393
2394 return SDValue();
2395}
2396
Daniel Sanders26307182013-09-24 14:20:00 +00002397// Lower VECTOR_SHUFFLE into SHF (if possible).
2398//
2399// SHF splits the vector into blocks of four elements, then shuffles these
2400// elements according to a <4 x i2> constant (encoded as an integer immediate).
2401//
2402// It is therefore possible to lower into SHF when the mask takes the form:
2403// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2404// When undef's appear they are treated as if they were whatever value is
2405// necessary in order to fit the above form.
2406//
2407// For example:
2408// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2409// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2410// i32 7, i32 6, i32 5, i32 4>
2411// is lowered to:
2412// (SHF_H $w0, $w1, 27)
2413// where the 27 comes from:
2414// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2415static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2416 SmallVector<int, 16> Indices,
2417 SelectionDAG &DAG) {
2418 int SHFIndices[4] = { -1, -1, -1, -1 };
2419
2420 if (Indices.size() < 4)
2421 return SDValue();
2422
2423 for (unsigned i = 0; i < 4; ++i) {
2424 for (unsigned j = i; j < Indices.size(); j += 4) {
2425 int Idx = Indices[j];
2426
2427 // Convert from vector index to 4-element subvector index
2428 // If an index refers to an element outside of the subvector then give up
2429 if (Idx != -1) {
2430 Idx -= 4 * (j / 4);
2431 if (Idx < 0 || Idx >= 4)
2432 return SDValue();
2433 }
2434
2435 // If the mask has an undef, replace it with the current index.
2436 // Note that it might still be undef if the current index is also undef
2437 if (SHFIndices[i] == -1)
2438 SHFIndices[i] = Idx;
2439
2440 // Check that non-undef values are the same as in the mask. If they
2441 // aren't then give up
2442 if (!(Idx == -1 || Idx == SHFIndices[i]))
2443 return SDValue();
2444 }
2445 }
2446
2447 // Calculate the immediate. Replace any remaining undefs with zero
2448 APInt Imm(32, 0);
2449 for (int i = 3; i >= 0; --i) {
2450 int Idx = SHFIndices[i];
2451
2452 if (Idx == -1)
2453 Idx = 0;
2454
2455 Imm <<= 2;
2456 Imm |= Idx & 0x3;
2457 }
2458
2459 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
2460 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
2461}
2462
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002463// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2464//
2465// ILVEV interleaves the even elements from each vector.
2466//
2467// It is possible to lower into ILVEV when the mask takes the form:
2468// <0, n, 2, n+2, 4, n+4, ...>
2469// where n is the number of elements in the vector.
2470//
2471// When undef's appear in the mask they are treated as if they were whatever
2472// value is necessary in order to fit the above form.
2473static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2474 SmallVector<int, 16> Indices,
2475 SelectionDAG &DAG) {
2476 assert ((Indices.size() % 2) == 0);
2477 int WsIdx = 0;
2478 int WtIdx = ResTy.getVectorNumElements();
2479
2480 for (unsigned i = 0; i < Indices.size(); i += 2) {
2481 if (Indices[i] != -1 && Indices[i] != WsIdx)
2482 return SDValue();
2483 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2484 return SDValue();
2485 WsIdx += 2;
2486 WtIdx += 2;
2487 }
2488
2489 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
2490 Op->getOperand(1));
2491}
2492
2493// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2494//
2495// ILVOD interleaves the odd elements from each vector.
2496//
2497// It is possible to lower into ILVOD when the mask takes the form:
2498// <1, n+1, 3, n+3, 5, n+5, ...>
2499// where n is the number of elements in the vector.
2500//
2501// When undef's appear in the mask they are treated as if they were whatever
2502// value is necessary in order to fit the above form.
2503static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2504 SmallVector<int, 16> Indices,
2505 SelectionDAG &DAG) {
2506 assert ((Indices.size() % 2) == 0);
2507 int WsIdx = 1;
2508 int WtIdx = ResTy.getVectorNumElements() + 1;
2509
2510 for (unsigned i = 0; i < Indices.size(); i += 2) {
2511 if (Indices[i] != -1 && Indices[i] != WsIdx)
2512 return SDValue();
2513 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2514 return SDValue();
2515 WsIdx += 2;
2516 WtIdx += 2;
2517 }
2518
2519 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
2520 Op->getOperand(1));
2521}
2522
2523// Lower VECTOR_SHUFFLE into ILVL (if possible).
2524//
2525// ILVL interleaves consecutive elements from the left half of each vector.
2526//
2527// It is possible to lower into ILVL when the mask takes the form:
2528// <0, n, 1, n+1, 2, n+2, ...>
2529// where n is the number of elements in the vector.
2530//
2531// When undef's appear in the mask they are treated as if they were whatever
2532// value is necessary in order to fit the above form.
2533static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2534 SmallVector<int, 16> Indices,
2535 SelectionDAG &DAG) {
2536 assert ((Indices.size() % 2) == 0);
2537 int WsIdx = 0;
2538 int WtIdx = ResTy.getVectorNumElements();
2539
2540 for (unsigned i = 0; i < Indices.size(); i += 2) {
2541 if (Indices[i] != -1 && Indices[i] != WsIdx)
2542 return SDValue();
2543 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2544 return SDValue();
2545 WsIdx ++;
2546 WtIdx ++;
2547 }
2548
2549 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2550 Op->getOperand(1));
2551}
2552
2553// Lower VECTOR_SHUFFLE into ILVR (if possible).
2554//
2555// ILVR interleaves consecutive elements from the right half of each vector.
2556//
2557// It is possible to lower into ILVR when the mask takes the form:
2558// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2559// where n is the number of elements in the vector and x is half n.
2560//
2561// When undef's appear in the mask they are treated as if they were whatever
2562// value is necessary in order to fit the above form.
2563static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2564 SmallVector<int, 16> Indices,
2565 SelectionDAG &DAG) {
2566 assert ((Indices.size() % 2) == 0);
2567 unsigned NumElts = ResTy.getVectorNumElements();
2568 int WsIdx = NumElts / 2;
2569 int WtIdx = NumElts + NumElts / 2;
2570
2571 for (unsigned i = 0; i < Indices.size(); i += 2) {
2572 if (Indices[i] != -1 && Indices[i] != WsIdx)
2573 return SDValue();
2574 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2575 return SDValue();
2576 WsIdx ++;
2577 WtIdx ++;
2578 }
2579
2580 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2581 Op->getOperand(1));
2582}
2583
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002584// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2585//
2586// PCKEV copies the even elements of each vector into the result vector.
2587//
2588// It is possible to lower into PCKEV when the mask takes the form:
2589// <0, 2, 4, ..., n, n+2, n+4, ...>
2590// where n is the number of elements in the vector.
2591//
2592// When undef's appear in the mask they are treated as if they were whatever
2593// value is necessary in order to fit the above form.
2594static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2595 SmallVector<int, 16> Indices,
2596 SelectionDAG &DAG) {
2597 assert ((Indices.size() % 2) == 0);
2598 int Idx = 0;
2599
2600 for (unsigned i = 0; i < Indices.size(); ++i) {
2601 if (Indices[i] != -1 && Indices[i] != Idx)
2602 return SDValue();
2603 Idx += 2;
2604 }
2605
2606 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2607 Op->getOperand(1));
2608}
2609
2610// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2611//
2612// PCKOD copies the odd elements of each vector into the result vector.
2613//
2614// It is possible to lower into PCKOD when the mask takes the form:
2615// <1, 3, 5, ..., n+1, n+3, n+5, ...>
2616// where n is the number of elements in the vector.
2617//
2618// When undef's appear in the mask they are treated as if they were whatever
2619// value is necessary in order to fit the above form.
2620static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2621 SmallVector<int, 16> Indices,
2622 SelectionDAG &DAG) {
2623 assert ((Indices.size() % 2) == 0);
2624 int Idx = 1;
2625
2626 for (unsigned i = 0; i < Indices.size(); ++i) {
2627 if (Indices[i] != -1 && Indices[i] != Idx)
2628 return SDValue();
2629 Idx += 2;
2630 }
2631
2632 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2633 Op->getOperand(1));
2634}
2635
Daniel Sanderse5087042013-09-24 14:02:15 +00002636// Lower VECTOR_SHUFFLE into VSHF.
2637//
2638// This mostly consists of converting the shuffle indices in Indices into a
2639// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2640// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2641// if the type is v8i16 and all the indices are less than 8 then the second
2642// operand is unused and can be replaced with anything. We choose to replace it
2643// with the used operand since this reduces the number of instructions overall.
2644static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2645 SmallVector<int, 16> Indices,
2646 SelectionDAG &DAG) {
2647 SmallVector<SDValue, 16> Ops;
2648 SDValue Op0;
2649 SDValue Op1;
2650 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2651 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2652 bool Using1stVec = false;
2653 bool Using2ndVec = false;
2654 SDLoc DL(Op);
2655 int ResTyNumElts = ResTy.getVectorNumElements();
2656
2657 for (int i = 0; i < ResTyNumElts; ++i) {
2658 // Idx == -1 means UNDEF
2659 int Idx = Indices[i];
2660
2661 if (0 <= Idx && Idx < ResTyNumElts)
2662 Using1stVec = true;
2663 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2664 Using2ndVec = true;
2665 }
2666
2667 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2668 ++I)
2669 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2670
Craig Topper48d114b2014-04-26 18:35:24 +00002671 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002672
2673 if (Using1stVec && Using2ndVec) {
2674 Op0 = Op->getOperand(0);
2675 Op1 = Op->getOperand(1);
2676 } else if (Using1stVec)
2677 Op0 = Op1 = Op->getOperand(0);
2678 else if (Using2ndVec)
2679 Op0 = Op1 = Op->getOperand(1);
2680 else
2681 llvm_unreachable("shuffle vector mask references neither vector operand?");
2682
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002683 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2684 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2685 // VSHF concatenates the vectors in a bitwise fashion:
2686 // <0b00, 0b01> + <0b10, 0b11> ->
2687 // 0b0100 + 0b1110 -> 0b01001110
2688 // <0b10, 0b11, 0b00, 0b01>
2689 // We must therefore swap the operands to get the correct result.
2690 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002691}
2692
2693// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2694// indices in the shuffle.
2695SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2696 SelectionDAG &DAG) const {
2697 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2698 EVT ResTy = Op->getValueType(0);
2699
2700 if (!ResTy.is128BitVector())
2701 return SDValue();
2702
2703 int ResTyNumElts = ResTy.getVectorNumElements();
2704 SmallVector<int, 16> Indices;
2705
2706 for (int i = 0; i < ResTyNumElts; ++i)
2707 Indices.push_back(Node->getMaskElt(i));
2708
Daniel Sanders26307182013-09-24 14:20:00 +00002709 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2710 if (Result.getNode())
2711 return Result;
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002712 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2713 if (Result.getNode())
2714 return Result;
2715 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2716 if (Result.getNode())
2717 return Result;
2718 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2719 if (Result.getNode())
2720 return Result;
2721 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2722 if (Result.getNode())
2723 return Result;
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002724 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2725 if (Result.getNode())
2726 return Result;
2727 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2728 if (Result.getNode())
2729 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002730 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2731}
2732
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002733MachineBasicBlock * MipsSETargetLowering::
2734emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2735 // $bb:
2736 // bposge32_pseudo $vr0
2737 // =>
2738 // $bb:
2739 // bposge32 $tbb
2740 // $fbb:
2741 // li $vr2, 0
2742 // b $sink
2743 // $tbb:
2744 // li $vr1, 1
2745 // $sink:
2746 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2747
2748 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00002749 const TargetInstrInfo *TII =
2750 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002751 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002752 DebugLoc DL = MI->getDebugLoc();
2753 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002754 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002755 MachineFunction *F = BB->getParent();
2756 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2757 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2758 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2759 F->insert(It, FBB);
2760 F->insert(It, TBB);
2761 F->insert(It, Sink);
2762
2763 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002764 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002765 BB->end());
2766 Sink->transferSuccessorsAndUpdatePHIs(BB);
2767
2768 // Add successors.
2769 BB->addSuccessor(FBB);
2770 BB->addSuccessor(TBB);
2771 FBB->addSuccessor(Sink);
2772 TBB->addSuccessor(Sink);
2773
2774 // Insert the real bposge32 instruction to $BB.
2775 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2776
2777 // Fill $FBB.
2778 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2779 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2780 .addReg(Mips::ZERO).addImm(0);
2781 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2782
2783 // Fill $TBB.
2784 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2785 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2786 .addReg(Mips::ZERO).addImm(1);
2787
2788 // Insert phi function to $Sink.
2789 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2790 MI->getOperand(0).getReg())
2791 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2792
2793 MI->eraseFromParent(); // The pseudo instruction is gone now.
2794 return Sink;
2795}
Daniel Sandersce09d072013-08-28 12:14:50 +00002796
2797MachineBasicBlock * MipsSETargetLowering::
2798emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2799 unsigned BranchOp) const{
2800 // $bb:
2801 // vany_nonzero $rd, $ws
2802 // =>
2803 // $bb:
2804 // bnz.b $ws, $tbb
2805 // b $fbb
2806 // $fbb:
2807 // li $rd1, 0
2808 // b $sink
2809 // $tbb:
2810 // li $rd2, 1
2811 // $sink:
2812 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2813
2814 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00002815 const TargetInstrInfo *TII =
2816 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sandersce09d072013-08-28 12:14:50 +00002817 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2818 DebugLoc DL = MI->getDebugLoc();
2819 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002820 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00002821 MachineFunction *F = BB->getParent();
2822 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2823 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2824 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2825 F->insert(It, FBB);
2826 F->insert(It, TBB);
2827 F->insert(It, Sink);
2828
2829 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002830 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00002831 BB->end());
2832 Sink->transferSuccessorsAndUpdatePHIs(BB);
2833
2834 // Add successors.
2835 BB->addSuccessor(FBB);
2836 BB->addSuccessor(TBB);
2837 FBB->addSuccessor(Sink);
2838 TBB->addSuccessor(Sink);
2839
2840 // Insert the real bnz.b instruction to $BB.
2841 BuildMI(BB, DL, TII->get(BranchOp))
2842 .addReg(MI->getOperand(1).getReg())
2843 .addMBB(TBB);
2844
2845 // Fill $FBB.
2846 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2847 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2848 .addReg(Mips::ZERO).addImm(0);
2849 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2850
2851 // Fill $TBB.
2852 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2853 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2854 .addReg(Mips::ZERO).addImm(1);
2855
2856 // Insert phi function to $Sink.
2857 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2858 MI->getOperand(0).getReg())
2859 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2860
2861 MI->eraseFromParent(); // The pseudo instruction is gone now.
2862 return Sink;
2863}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002864
2865// Emit the COPY_FW pseudo instruction.
2866//
2867// copy_fw_pseudo $fd, $ws, n
2868// =>
2869// copy_u_w $rt, $ws, $n
2870// mtc1 $rt, $fd
2871//
2872// When n is zero, the equivalent operation can be performed with (potentially)
2873// zero instructions due to register overlaps. This optimization is never valid
2874// for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2875MachineBasicBlock * MipsSETargetLowering::
2876emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
Eric Christopherd9134482014-08-04 21:25:23 +00002877 const TargetInstrInfo *TII =
2878 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002879 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2880 DebugLoc DL = MI->getDebugLoc();
2881 unsigned Fd = MI->getOperand(0).getReg();
2882 unsigned Ws = MI->getOperand(1).getReg();
2883 unsigned Lane = MI->getOperand(2).getImm();
2884
2885 if (Lane == 0)
2886 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2887 else {
2888 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2889
Daniel Sandersd9207702014-03-04 13:54:30 +00002890 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002891 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2892 }
2893
2894 MI->eraseFromParent(); // The pseudo instruction is gone now.
2895 return BB;
2896}
2897
2898// Emit the COPY_FD pseudo instruction.
2899//
2900// copy_fd_pseudo $fd, $ws, n
2901// =>
2902// splati.d $wt, $ws, $n
2903// copy $fd, $wt:sub_64
2904//
2905// When n is zero, the equivalent operation can be performed with (potentially)
2906// zero instructions due to register overlaps. This optimization is always
2907// valid because FR=1 mode which is the only supported mode in MSA.
2908MachineBasicBlock * MipsSETargetLowering::
2909emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
Eric Christopher1c29a652014-07-18 22:55:25 +00002910 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002911
Eric Christopherd9134482014-08-04 21:25:23 +00002912 const TargetInstrInfo *TII =
2913 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002914 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2915 unsigned Fd = MI->getOperand(0).getReg();
2916 unsigned Ws = MI->getOperand(1).getReg();
2917 unsigned Lane = MI->getOperand(2).getImm() * 2;
2918 DebugLoc DL = MI->getDebugLoc();
2919
2920 if (Lane == 0)
2921 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2922 else {
2923 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2924
2925 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2926 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2927 }
2928
2929 MI->eraseFromParent(); // The pseudo instruction is gone now.
2930 return BB;
2931}
Daniel Sandersa5150702013-09-27 12:31:32 +00002932
2933// Emit the INSERT_FW pseudo instruction.
2934//
2935// insert_fw_pseudo $wd, $wd_in, $n, $fs
2936// =>
2937// subreg_to_reg $wt:sub_lo, $fs
2938// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002939MachineBasicBlock *
2940MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2941 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00002942 const TargetInstrInfo *TII =
2943 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00002944 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2945 DebugLoc DL = MI->getDebugLoc();
2946 unsigned Wd = MI->getOperand(0).getReg();
2947 unsigned Wd_in = MI->getOperand(1).getReg();
2948 unsigned Lane = MI->getOperand(2).getImm();
2949 unsigned Fs = MI->getOperand(3).getReg();
2950 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2951
2952 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002953 .addImm(0)
2954 .addReg(Fs)
2955 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00002956 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002957 .addReg(Wd_in)
2958 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00002959 .addReg(Wt)
2960 .addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00002961
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002962 MI->eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00002963 return BB;
2964}
2965
2966// Emit the INSERT_FD pseudo instruction.
2967//
2968// insert_fd_pseudo $wd, $fs, n
2969// =>
2970// subreg_to_reg $wt:sub_64, $fs
2971// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002972MachineBasicBlock *
2973MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2974 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002975 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00002976
Eric Christopherd9134482014-08-04 21:25:23 +00002977 const TargetInstrInfo *TII =
2978 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00002979 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2980 DebugLoc DL = MI->getDebugLoc();
2981 unsigned Wd = MI->getOperand(0).getReg();
2982 unsigned Wd_in = MI->getOperand(1).getReg();
2983 unsigned Lane = MI->getOperand(2).getImm();
2984 unsigned Fs = MI->getOperand(3).getReg();
2985 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2986
2987 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002988 .addImm(0)
2989 .addReg(Fs)
2990 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00002991 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002992 .addReg(Wd_in)
2993 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00002994 .addReg(Wt)
2995 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00002996
2997 MI->eraseFromParent(); // The pseudo instruction is gone now.
2998 return BB;
2999}
3000
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003001// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3002//
3003// For integer:
3004// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3005// =>
3006// (SLL $lanetmp1, $lane, <log2size)
3007// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3008// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3009// (NEG $lanetmp2, $lanetmp1)
3010// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3011//
3012// For floating point:
3013// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3014// =>
3015// (SUBREG_TO_REG $wt, $fs, <subreg>)
3016// (SLL $lanetmp1, $lane, <log2size)
3017// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3018// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3019// (NEG $lanetmp2, $lanetmp1)
3020// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3021MachineBasicBlock *
3022MipsSETargetLowering::emitINSERT_DF_VIDX(MachineInstr *MI,
3023 MachineBasicBlock *BB,
3024 unsigned EltSizeInBytes,
3025 bool IsFP) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003026 const TargetInstrInfo *TII =
3027 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003028 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3029 DebugLoc DL = MI->getDebugLoc();
3030 unsigned Wd = MI->getOperand(0).getReg();
3031 unsigned SrcVecReg = MI->getOperand(1).getReg();
3032 unsigned LaneReg = MI->getOperand(2).getReg();
3033 unsigned SrcValReg = MI->getOperand(3).getReg();
3034
3035 const TargetRegisterClass *VecRC = nullptr;
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003036 const TargetRegisterClass *GPRRC =
Eric Christopher1c29a652014-07-18 22:55:25 +00003037 Subtarget.isGP64bit() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003038 unsigned EltLog2Size;
3039 unsigned InsertOp = 0;
3040 unsigned InsveOp = 0;
3041 switch (EltSizeInBytes) {
3042 default:
3043 llvm_unreachable("Unexpected size");
3044 case 1:
3045 EltLog2Size = 0;
3046 InsertOp = Mips::INSERT_B;
3047 InsveOp = Mips::INSVE_B;
3048 VecRC = &Mips::MSA128BRegClass;
3049 break;
3050 case 2:
3051 EltLog2Size = 1;
3052 InsertOp = Mips::INSERT_H;
3053 InsveOp = Mips::INSVE_H;
3054 VecRC = &Mips::MSA128HRegClass;
3055 break;
3056 case 4:
3057 EltLog2Size = 2;
3058 InsertOp = Mips::INSERT_W;
3059 InsveOp = Mips::INSVE_W;
3060 VecRC = &Mips::MSA128WRegClass;
3061 break;
3062 case 8:
3063 EltLog2Size = 3;
3064 InsertOp = Mips::INSERT_D;
3065 InsveOp = Mips::INSVE_D;
3066 VecRC = &Mips::MSA128DRegClass;
3067 break;
3068 }
3069
3070 if (IsFP) {
3071 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3072 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3073 .addImm(0)
3074 .addReg(SrcValReg)
3075 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3076 SrcValReg = Wt;
3077 }
3078
3079 // Convert the lane index into a byte index
3080 if (EltSizeInBytes != 1) {
3081 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
3082 BuildMI(*BB, MI, DL, TII->get(Mips::SLL), LaneTmp1)
3083 .addReg(LaneReg)
3084 .addImm(EltLog2Size);
3085 LaneReg = LaneTmp1;
3086 }
3087
3088 // Rotate bytes around so that the desired lane is element zero
3089 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3090 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3091 .addReg(SrcVecReg)
3092 .addReg(SrcVecReg)
3093 .addReg(LaneReg);
3094
3095 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3096 if (IsFP) {
3097 // Use insve.df to insert to element zero
3098 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3099 .addReg(WdTmp1)
3100 .addImm(0)
3101 .addReg(SrcValReg)
3102 .addImm(0);
3103 } else {
3104 // Use insert.df to insert to element zero
3105 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3106 .addReg(WdTmp1)
3107 .addReg(SrcValReg)
3108 .addImm(0);
3109 }
3110
3111 // Rotate elements the rest of the way for a full rotation.
3112 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3113 // the lane index to do this.
3114 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
3115 BuildMI(*BB, MI, DL, TII->get(Mips::SUB), LaneTmp2)
3116 .addReg(Mips::ZERO)
3117 .addReg(LaneReg);
3118 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3119 .addReg(WdTmp2)
3120 .addReg(WdTmp2)
3121 .addReg(LaneTmp2);
3122
3123 MI->eraseFromParent(); // The pseudo instruction is gone now.
3124 return BB;
3125}
3126
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003127// Emit the FILL_FW pseudo instruction.
3128//
3129// fill_fw_pseudo $wd, $fs
3130// =>
3131// implicit_def $wt1
3132// insert_subreg $wt2:subreg_lo, $wt1, $fs
3133// splati.w $wd, $wt2[0]
3134MachineBasicBlock *
3135MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
3136 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003137 const TargetInstrInfo *TII =
3138 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003139 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3140 DebugLoc DL = MI->getDebugLoc();
3141 unsigned Wd = MI->getOperand(0).getReg();
3142 unsigned Fs = MI->getOperand(1).getReg();
3143 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3144 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3145
3146 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3147 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3148 .addReg(Wt1)
3149 .addReg(Fs)
3150 .addImm(Mips::sub_lo);
3151 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3152
3153 MI->eraseFromParent(); // The pseudo instruction is gone now.
3154 return BB;
3155}
3156
3157// Emit the FILL_FD pseudo instruction.
3158//
3159// fill_fd_pseudo $wd, $fs
3160// =>
3161// implicit_def $wt1
3162// insert_subreg $wt2:subreg_64, $wt1, $fs
3163// splati.d $wd, $wt2[0]
3164MachineBasicBlock *
3165MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
3166 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003167 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003168
Eric Christopherd9134482014-08-04 21:25:23 +00003169 const TargetInstrInfo *TII =
3170 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003171 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3172 DebugLoc DL = MI->getDebugLoc();
3173 unsigned Wd = MI->getOperand(0).getReg();
3174 unsigned Fs = MI->getOperand(1).getReg();
3175 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3176 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3177
3178 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3179 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3180 .addReg(Wt1)
3181 .addReg(Fs)
3182 .addImm(Mips::sub_64);
3183 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003184
3185 MI->eraseFromParent(); // The pseudo instruction is gone now.
3186 return BB;
3187}
Daniel Sandersa9521602013-10-23 10:36:52 +00003188
3189// Emit the FEXP2_W_1 pseudo instructions.
3190//
3191// fexp2_w_1_pseudo $wd, $wt
3192// =>
3193// ldi.w $ws, 1
3194// fexp2.w $wd, $ws, $wt
3195MachineBasicBlock *
3196MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
3197 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003198 const TargetInstrInfo *TII =
3199 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003200 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3201 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3202 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3203 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
3204 DebugLoc DL = MI->getDebugLoc();
3205
3206 // Splat 1.0 into a vector
3207 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3208 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3209
3210 // Emit 1.0 * fexp2(Wt)
3211 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
3212 .addReg(Ws2)
3213 .addReg(MI->getOperand(1).getReg());
3214
3215 MI->eraseFromParent(); // The pseudo instruction is gone now.
3216 return BB;
3217}
3218
3219// Emit the FEXP2_D_1 pseudo instructions.
3220//
3221// fexp2_d_1_pseudo $wd, $wt
3222// =>
3223// ldi.d $ws, 1
3224// fexp2.d $wd, $ws, $wt
3225MachineBasicBlock *
3226MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
3227 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003228 const TargetInstrInfo *TII =
3229 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003230 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3231 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3232 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3233 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
3234 DebugLoc DL = MI->getDebugLoc();
3235
3236 // Splat 1.0 into a vector
3237 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3238 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3239
3240 // Emit 1.0 * fexp2(Wt)
3241 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
3242 .addReg(Ws2)
3243 .addReg(MI->getOperand(1).getReg());
3244
3245 MI->eraseFromParent(); // The pseudo instruction is gone now.
3246 return BB;
3247}