blob: 5167b6baf98db5aa5557d3b2485655c380c1d2d4 [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 Christopherb1526602014-09-19 23:30:42 +000038MipsSETargetLowering::MipsSETargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +000039 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.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000049 for (MVT VT0 : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000050 for (MVT VT1 : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000051 setTruncStoreAction(VT0, VT1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +000052 setLoadExtAction(ISD::SEXTLOAD, VT0, VT1, Expand);
53 setLoadExtAction(ISD::ZEXTLOAD, VT0, VT1, Expand);
54 setLoadExtAction(ISD::EXTLOAD, VT0, VT1, Expand);
55 }
Daniel Sanders36c671e2013-09-27 09:44:59 +000056 }
57 }
58
Eric Christopher1c29a652014-07-18 22:55:25 +000059 if (Subtarget.hasDSP()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +000060 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
61
62 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
Akira Hatanaka654655f2013-08-14 00:53:38 +000063 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000064
65 // Expand all builtin opcodes.
66 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
67 setOperationAction(Opc, VecTys[i], Expand);
68
Akira Hatanaka2f088222013-04-13 00:55:41 +000069 setOperationAction(ISD::ADD, VecTys[i], Legal);
70 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000071 setOperationAction(ISD::LOAD, VecTys[i], Legal);
72 setOperationAction(ISD::STORE, VecTys[i], Legal);
73 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
74 }
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +000075
76 setTargetDAGCombine(ISD::SHL);
77 setTargetDAGCombine(ISD::SRA);
78 setTargetDAGCombine(ISD::SRL);
Akira Hatanaka68741cc2013-04-30 22:37:26 +000079 setTargetDAGCombine(ISD::SETCC);
80 setTargetDAGCombine(ISD::VSELECT);
Akira Hatanaka96ca1822013-03-13 00:54:29 +000081 }
82
Eric Christopher1c29a652014-07-18 22:55:25 +000083 if (Subtarget.hasDSPR2())
Akira Hatanaka2f088222013-04-13 00:55:41 +000084 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
85
Eric Christopher1c29a652014-07-18 22:55:25 +000086 if (Subtarget.hasMSA()) {
Daniel Sandersc65f58a2013-09-11 10:15:48 +000087 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
88 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
89 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
90 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
91 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
92 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
93 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
Daniel Sandersf7456c72013-09-23 13:22:24 +000094
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +000095 setTargetDAGCombine(ISD::AND);
Daniel Sanders53fe6c42013-10-30 13:51:01 +000096 setTargetDAGCombine(ISD::OR);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +000097 setTargetDAGCombine(ISD::SRA);
Daniel Sanderse1d24352013-09-24 12:04:44 +000098 setTargetDAGCombine(ISD::VSELECT);
Daniel Sandersf7456c72013-09-23 13:22:24 +000099 setTargetDAGCombine(ISD::XOR);
Jack Carter3a2c2d42013-08-13 20:54:07 +0000100 }
101
Eric Christophere8ae3e32015-05-07 23:10:21 +0000102 if (!Subtarget.useSoftFloat()) {
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000103 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
104
105 // When dealing with single precision only, use libcalls
Eric Christopher1c29a652014-07-18 22:55:25 +0000106 if (!Subtarget.isSingleFloat()) {
107 if (Subtarget.isFP64bit())
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000108 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
109 else
110 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
111 }
112 }
113
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000114 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
115 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
116 setOperationAction(ISD::MULHS, MVT::i32, Custom);
117 setOperationAction(ISD::MULHU, MVT::i32, Custom);
118
Eric Christopher1c29a652014-07-18 22:55:25 +0000119 if (Subtarget.hasCnMips())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000120 setOperationAction(ISD::MUL, MVT::i64, Legal);
Eric Christopher1c29a652014-07-18 22:55:25 +0000121 else if (Subtarget.isGP64bit())
Kai Nacke93fe5e82014-03-20 11:51:58 +0000122 setOperationAction(ISD::MUL, MVT::i64, Custom);
123
Eric Christopher1c29a652014-07-18 22:55:25 +0000124 if (Subtarget.isGP64bit()) {
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000125 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Custom);
126 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Custom);
Akira Hatanaka4f1130e2013-04-11 19:29:26 +0000127 setOperationAction(ISD::MULHS, MVT::i64, Custom);
128 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Jan Vesely54468a5a2014-10-17 14:45:28 +0000129 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
130 setOperationAction(ISD::UDIVREM, 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);
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000138 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
139 setOperationAction(ISD::LOAD, MVT::i32, Custom);
140 setOperationAction(ISD::STORE, MVT::i32, Custom);
141
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000142 setTargetDAGCombine(ISD::ADDE);
143 setTargetDAGCombine(ISD::SUBE);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000144 setTargetDAGCombine(ISD::MUL);
Akira Hatanaka9efcd762013-03-30 01:42:24 +0000145
Daniel Sandersce09d072013-08-28 12:14:50 +0000146 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +0000147 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
148 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
149
Akira Hatanaka63791212013-09-07 00:52:30 +0000150 if (NoDPLoadStore) {
151 setOperationAction(ISD::LOAD, MVT::f64, Custom);
152 setOperationAction(ISD::STORE, MVT::f64, Custom);
153 }
154
Eric Christopher1c29a652014-07-18 22:55:25 +0000155 if (Subtarget.hasMips32r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000156 // MIPS32r6 replaces the accumulator-based multiplies with a three register
157 // instruction
Daniel Sanders826f8b32014-06-12 10:54:16 +0000158 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
159 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000160 setOperationAction(ISD::MUL, MVT::i32, Legal);
161 setOperationAction(ISD::MULHS, MVT::i32, Legal);
162 setOperationAction(ISD::MULHU, MVT::i32, Legal);
163
164 // MIPS32r6 replaces the accumulator-based division/remainder with separate
165 // three register division and remainder instructions.
166 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
167 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
168 setOperationAction(ISD::SDIV, MVT::i32, Legal);
169 setOperationAction(ISD::UDIV, MVT::i32, Legal);
170 setOperationAction(ISD::SREM, MVT::i32, Legal);
171 setOperationAction(ISD::UREM, MVT::i32, Legal);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000172
173 // MIPS32r6 replaces conditional moves with an equivalent that removes the
174 // need for three GPR read ports.
175 setOperationAction(ISD::SETCC, MVT::i32, Legal);
176 setOperationAction(ISD::SELECT, MVT::i32, Legal);
177 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
178
179 setOperationAction(ISD::SETCC, MVT::f32, Legal);
180 setOperationAction(ISD::SELECT, MVT::f32, Legal);
181 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
182
Eric Christopher1c29a652014-07-18 22:55:25 +0000183 assert(Subtarget.isFP64bit() && "FR=1 is required for MIPS32r6");
Daniel Sanders0fa60412014-06-12 13:39:06 +0000184 setOperationAction(ISD::SETCC, MVT::f64, Legal);
185 setOperationAction(ISD::SELECT, MVT::f64, Legal);
186 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
187
Daniel Sanders3d3ea532014-06-12 15:00:17 +0000188 setOperationAction(ISD::BRCOND, MVT::Other, Legal);
189
Daniel Sanders0fa60412014-06-12 13:39:06 +0000190 // Floating point > and >= are supported via < and <=
191 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
192 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
193 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
194 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
195
196 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
197 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
198 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
199 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000200 }
201
Eric Christopher1c29a652014-07-18 22:55:25 +0000202 if (Subtarget.hasMips64r6()) {
Daniel Sanders308181e2014-06-12 10:44:10 +0000203 // MIPS64r6 replaces the accumulator-based multiplies with a three register
204 // instruction
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000205 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
206 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
Daniel Sanders308181e2014-06-12 10:44:10 +0000207 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
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000227 computeRegisterProperties(Subtarget.getRegisterInfo());
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000228}
229
230const MipsTargetLowering *
Eric Christopherb1526602014-09-19 23:30:42 +0000231llvm::createMipsSETargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000232 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)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000803 return DAG.getConstant(0, DL, VT);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000804
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,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000812 DAG.getConstant(Log2_64(C), DL, ShiftTy));
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000813
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())
Mehdi Amini9639d652015-07-09 02:09:20 +0000841 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N), VT,
Mehdi Aminieaabc512015-07-09 15:12:23 +0000842 TL->getScalarShiftAmountTy(DAG.getDataLayout(), VT),
843 DAG);
Akira Hatanaka5832fc62013-06-26 18:48:17 +0000844
845 return SDValue(N, 0);
846}
847
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000848static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
849 SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +0000850 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000851 // See if this is a vector splat immediate node.
852 APInt SplatValue, SplatUndef;
853 unsigned SplatBitSize;
854 bool HasAnyUndefs;
855 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
856 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
857
Eric Christopher1c29a652014-07-18 22:55:25 +0000858 if (!Subtarget.hasDSP())
Daniel Sanders6e664bc2013-11-21 11:40:14 +0000859 return SDValue();
860
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000861 if (!BV ||
Akira Hatanakad8fb0322013-04-22 20:13:37 +0000862 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
Eric Christopher1c29a652014-07-18 22:55:25 +0000863 EltSize, !Subtarget.isLittle()) ||
Akira Hatanaka0d6964c2013-04-22 19:58:23 +0000864 (SplatBitSize != EltSize) ||
Akira Hatanakae9d0b312013-04-23 18:09:42 +0000865 (SplatValue.getZExtValue() >= EltSize))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000866 return SDValue();
867
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000868 SDLoc DL(N);
869 return DAG.getNode(Opc, DL, Ty, N->getOperand(0),
870 DAG.getConstant(SplatValue.getZExtValue(), DL, MVT::i32));
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000871}
872
873static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
874 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000875 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000876 EVT Ty = N->getValueType(0);
877
878 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
879 return SDValue();
880
881 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
882}
883
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000884// Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
885// constant splats into MipsISD::SHRA_DSP for DSPr2.
886//
887// Performs the following transformations:
888// - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
889// sign/zero-extension is completely overwritten by the new one performed by
890// the ISD::SRA and ISD::SHL nodes.
891// - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
892// sequence.
893//
894// See performDSPShiftCombine for more information about the transformation
895// used for DSPr2.
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000896static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
897 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000898 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000899 EVT Ty = N->getValueType(0);
900
Eric Christopher1c29a652014-07-18 22:55:25 +0000901 if (Subtarget.hasMSA()) {
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000902 SDValue Op0 = N->getOperand(0);
903 SDValue Op1 = N->getOperand(1);
904
905 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
906 // where $d + sizeof($c) == 32
907 // or $d + sizeof($c) <= 32 and SExt
908 // -> (MipsVExtractSExt $a, $b, $c)
909 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
910 SDValue Op0Op0 = Op0->getOperand(0);
911 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
912
913 if (!ShAmount)
914 return SDValue();
915
Daniel Sandersf4f1a872013-09-27 09:25:29 +0000916 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
917 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
918 return SDValue();
919
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000920 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
921 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
922
923 if (TotalBits == 32 ||
924 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
925 TotalBits <= 32)) {
926 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
927 Op0Op0->getOperand(2) };
Chandler Carruth356665a2014-08-01 22:09:43 +0000928 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
929 Op0Op0->getVTList(),
930 makeArrayRef(Ops, Op0Op0->getNumOperands()));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000931 }
932 }
933 }
934
Eric Christopher1c29a652014-07-18 22:55:25 +0000935 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000936 return SDValue();
937
938 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
939}
940
941
942static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
943 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000944 const MipsSubtarget &Subtarget) {
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000945 EVT Ty = N->getValueType(0);
946
Eric Christopher1c29a652014-07-18 22:55:25 +0000947 if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000948 return SDValue();
949
950 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
951}
952
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000953static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
954 bool IsV216 = (Ty == MVT::v2i16);
955
956 switch (CC) {
957 case ISD::SETEQ:
958 case ISD::SETNE: return true;
959 case ISD::SETLT:
960 case ISD::SETLE:
961 case ISD::SETGT:
962 case ISD::SETGE: return IsV216;
963 case ISD::SETULT:
964 case ISD::SETULE:
965 case ISD::SETUGT:
966 case ISD::SETUGE: return !IsV216;
967 default: return false;
968 }
969}
970
971static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
972 EVT Ty = N->getValueType(0);
973
974 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
975 return SDValue();
976
977 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
978 return SDValue();
979
Andrew Trickef9de2a2013-05-25 02:42:55 +0000980 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000981 N->getOperand(1), N->getOperand(2));
982}
983
984static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
985 EVT Ty = N->getValueType(0);
986
Daniel Sanders3ce56622013-09-24 12:18:31 +0000987 if (Ty.is128BitVector() && Ty.isInteger()) {
988 // Try the following combines:
989 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
990 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
991 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
992 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
993 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
994 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
995 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
996 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
997 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
998 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
999 // legalizer.
1000 SDValue Op0 = N->getOperand(0);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001001
Daniel Sanders3ce56622013-09-24 12:18:31 +00001002 if (Op0->getOpcode() != ISD::SETCC)
1003 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001004
Daniel Sanders3ce56622013-09-24 12:18:31 +00001005 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
1006 bool Signed;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001007
Daniel Sanders3ce56622013-09-24 12:18:31 +00001008 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
1009 Signed = true;
1010 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
1011 Signed = false;
1012 else
1013 return SDValue();
1014
1015 SDValue Op1 = N->getOperand(1);
1016 SDValue Op2 = N->getOperand(2);
1017 SDValue Op0Op0 = Op0->getOperand(0);
1018 SDValue Op0Op1 = Op0->getOperand(1);
1019
1020 if (Op1 == Op0Op0 && Op2 == Op0Op1)
1021 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
1022 Ty, Op1, Op2);
1023 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
1024 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
1025 Ty, Op1, Op2);
1026 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
1027 SDValue SetCC = N->getOperand(0);
1028
1029 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
1030 return SDValue();
1031
1032 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
1033 SetCC.getOperand(0), SetCC.getOperand(1),
1034 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
1035 }
1036
1037 return SDValue();
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001038}
1039
Daniel Sandersf7456c72013-09-23 13:22:24 +00001040static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001041 const MipsSubtarget &Subtarget) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001042 EVT Ty = N->getValueType(0);
1043
Eric Christopher1c29a652014-07-18 22:55:25 +00001044 if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
Daniel Sandersf7456c72013-09-23 13:22:24 +00001045 // Try the following combines:
1046 // (xor (or $a, $b), (build_vector allones))
1047 // (xor (or $a, $b), (bitcast (build_vector allones)))
1048 SDValue Op0 = N->getOperand(0);
1049 SDValue Op1 = N->getOperand(1);
1050 SDValue NotOp;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001051
1052 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
1053 NotOp = Op1;
1054 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
1055 NotOp = Op0;
Daniel Sandersf7456c72013-09-23 13:22:24 +00001056 else
1057 return SDValue();
1058
1059 if (NotOp->getOpcode() == ISD::OR)
1060 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
1061 NotOp->getOperand(1));
1062 }
1063
1064 return SDValue();
1065}
1066
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001067SDValue
1068MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1069 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001070 SDValue Val;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001071
1072 switch (N->getOpcode()) {
1073 case ISD::ADDE:
1074 return performADDECombine(N, DAG, DCI, Subtarget);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001075 case ISD::AND:
1076 Val = performANDCombine(N, DAG, DCI, Subtarget);
1077 break;
Daniel Sanders53fe6c42013-10-30 13:51:01 +00001078 case ISD::OR:
1079 Val = performORCombine(N, DAG, DCI, Subtarget);
1080 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001081 case ISD::SUBE:
1082 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka5832fc62013-06-26 18:48:17 +00001083 case ISD::MUL:
1084 return performMULCombine(N, DAG, DCI, this);
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +00001085 case ISD::SHL:
1086 return performSHLCombine(N, DAG, DCI, Subtarget);
1087 case ISD::SRA:
1088 return performSRACombine(N, DAG, DCI, Subtarget);
1089 case ISD::SRL:
1090 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001091 case ISD::VSELECT:
1092 return performVSELECTCombine(N, DAG);
Daniel Sandersf7456c72013-09-23 13:22:24 +00001093 case ISD::XOR:
1094 Val = performXORCombine(N, DAG, Subtarget);
1095 break;
1096 case ISD::SETCC:
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001097 Val = performSETCCCombine(N, DAG);
1098 break;
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001099 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001100
Daniel Sanders62aeab82013-10-30 13:31:27 +00001101 if (Val.getNode()) {
1102 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1103 N->printrWithDepth(dbgs(), &DAG);
1104 dbgs() << "\n=> \n";
1105 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1106 dbgs() << "\n");
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001107 return Val;
Daniel Sanders62aeab82013-10-30 13:31:27 +00001108 }
Akira Hatanaka68741cc2013-04-30 22:37:26 +00001109
1110 return MipsTargetLowering::PerformDAGCombine(N, DCI);
Akira Hatanaka9efcd762013-03-30 01:42:24 +00001111}
1112
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001113MachineBasicBlock *
1114MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1115 MachineBasicBlock *BB) const {
1116 switch (MI->getOpcode()) {
1117 default:
1118 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1119 case Mips::BPOSGE32_PSEUDO:
1120 return emitBPOSGE32(MI, BB);
Daniel Sandersce09d072013-08-28 12:14:50 +00001121 case Mips::SNZ_B_PSEUDO:
1122 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1123 case Mips::SNZ_H_PSEUDO:
1124 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1125 case Mips::SNZ_W_PSEUDO:
1126 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1127 case Mips::SNZ_D_PSEUDO:
1128 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1129 case Mips::SNZ_V_PSEUDO:
1130 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1131 case Mips::SZ_B_PSEUDO:
1132 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1133 case Mips::SZ_H_PSEUDO:
1134 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1135 case Mips::SZ_W_PSEUDO:
1136 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1137 case Mips::SZ_D_PSEUDO:
1138 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1139 case Mips::SZ_V_PSEUDO:
1140 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00001141 case Mips::COPY_FW_PSEUDO:
1142 return emitCOPY_FW(MI, BB);
1143 case Mips::COPY_FD_PSEUDO:
1144 return emitCOPY_FD(MI, BB);
Daniel Sandersa5150702013-09-27 12:31:32 +00001145 case Mips::INSERT_FW_PSEUDO:
1146 return emitINSERT_FW(MI, BB);
1147 case Mips::INSERT_FD_PSEUDO:
1148 return emitINSERT_FD(MI, BB);
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001149 case Mips::INSERT_B_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001150 case Mips::INSERT_B_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001151 return emitINSERT_DF_VIDX(MI, BB, 1, false);
1152 case Mips::INSERT_H_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001153 case Mips::INSERT_H_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001154 return emitINSERT_DF_VIDX(MI, BB, 2, false);
1155 case Mips::INSERT_W_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001156 case Mips::INSERT_W_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001157 return emitINSERT_DF_VIDX(MI, BB, 4, false);
1158 case Mips::INSERT_D_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001159 case Mips::INSERT_D_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001160 return emitINSERT_DF_VIDX(MI, BB, 8, false);
1161 case Mips::INSERT_FW_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001162 case Mips::INSERT_FW_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001163 return emitINSERT_DF_VIDX(MI, BB, 4, true);
1164 case Mips::INSERT_FD_VIDX_PSEUDO:
Daniel Sanderseda60d22015-05-05 10:32:24 +00001165 case Mips::INSERT_FD_VIDX64_PSEUDO:
Daniel Sanderse296a0f2014-04-30 12:09:32 +00001166 return emitINSERT_DF_VIDX(MI, BB, 8, true);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00001167 case Mips::FILL_FW_PSEUDO:
1168 return emitFILL_FW(MI, BB);
1169 case Mips::FILL_FD_PSEUDO:
1170 return emitFILL_FD(MI, BB);
Daniel Sandersa9521602013-10-23 10:36:52 +00001171 case Mips::FEXP2_W_1_PSEUDO:
1172 return emitFEXP2_W_1(MI, BB);
1173 case Mips::FEXP2_D_1_PSEUDO:
1174 return emitFEXP2_D_1(MI, BB);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001175 }
1176}
1177
Daniel Sanders23e98772014-11-02 16:09:29 +00001178bool MipsSETargetLowering::isEligibleForTailCallOptimization(
1179 const CCState &CCInfo, unsigned NextStackOffset,
1180 const MipsFunctionInfo &FI) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001181 if (!EnableMipsTailCalls)
1182 return false;
1183
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001184 // Return false if either the callee or caller has a byval argument.
Daniel Sanders23e98772014-11-02 16:09:29 +00001185 if (CCInfo.getInRegsParamsCount() > 0 || FI.hasByvalArg())
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001186 return false;
1187
1188 // Return true if the callee's argument area is no larger than the
1189 // caller's.
1190 return NextStackOffset <= FI.getIncomingArgSize();
1191}
1192
1193void MipsSETargetLowering::
1194getOpndList(SmallVectorImpl<SDValue> &Ops,
1195 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
1196 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001197 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
1198 SDValue Chain) const {
Akira Hatanaka168d4e52013-11-27 23:38:42 +00001199 Ops.push_back(Callee);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001200 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
Sasa Stankovic7072a792014-10-01 08:22:21 +00001201 InternalLinkage, IsCallReloc, CLI, Callee,
1202 Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00001203}
1204
Akira Hatanaka63791212013-09-07 00:52:30 +00001205SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1206 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1207
1208 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1209 return MipsTargetLowering::lowerLOAD(Op, DAG);
1210
1211 // Replace a double precision load with two i32 loads and a buildpair64.
1212 SDLoc DL(Op);
1213 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1214 EVT PtrVT = Ptr.getValueType();
1215
1216 // i32 load from lower address.
1217 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1218 MachinePointerInfo(), Nd.isVolatile(),
1219 Nd.isNonTemporal(), Nd.isInvariant(),
1220 Nd.getAlignment());
1221
1222 // i32 load from higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001223 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Akira Hatanaka63791212013-09-07 00:52:30 +00001224 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1225 MachinePointerInfo(), Nd.isVolatile(),
1226 Nd.isNonTemporal(), Nd.isInvariant(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001227 std::min(Nd.getAlignment(), 4U));
Akira Hatanaka63791212013-09-07 00:52:30 +00001228
Eric Christopher1c29a652014-07-18 22:55:25 +00001229 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001230 std::swap(Lo, Hi);
1231
1232 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1233 SDValue Ops[2] = {BP, Hi.getValue(1)};
Craig Topper64941d92014-04-27 19:20:57 +00001234 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka63791212013-09-07 00:52:30 +00001235}
1236
1237SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1238 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1239
1240 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1241 return MipsTargetLowering::lowerSTORE(Op, DAG);
1242
1243 // Replace a double precision store with two extractelement64s and i32 stores.
1244 SDLoc DL(Op);
1245 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1246 EVT PtrVT = Ptr.getValueType();
1247 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001248 Val, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001249 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001250 Val, DAG.getConstant(1, DL, MVT::i32));
Akira Hatanaka63791212013-09-07 00:52:30 +00001251
Eric Christopher1c29a652014-07-18 22:55:25 +00001252 if (!Subtarget.isLittle())
Akira Hatanaka63791212013-09-07 00:52:30 +00001253 std::swap(Lo, Hi);
1254
1255 // i32 store to lower address.
1256 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1257 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
Hal Finkelcc39b672014-07-24 12:16:19 +00001258 Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001259
1260 // i32 store to higher address.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001261 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
Akira Hatanaka63791212013-09-07 00:52:30 +00001262 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
Akira Hatanaka9cf069f2013-09-09 17:59:32 +00001263 Nd.isVolatile(), Nd.isNonTemporal(),
Hal Finkelcc39b672014-07-24 12:16:19 +00001264 std::min(Nd.getAlignment(), 4U), Nd.getAAInfo());
Akira Hatanaka63791212013-09-07 00:52:30 +00001265}
1266
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001267SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1268 bool HasLo, bool HasHi,
1269 SelectionDAG &DAG) const {
Daniel Sanders308181e2014-06-12 10:44:10 +00001270 // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
Eric Christopher1c29a652014-07-18 22:55:25 +00001271 assert(!Subtarget.hasMips32r6());
Daniel Sanders308181e2014-06-12 10:44:10 +00001272
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001273 EVT Ty = Op.getOperand(0).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001274 SDLoc DL(Op);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001275 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1276 Op.getOperand(0), Op.getOperand(1));
1277 SDValue Lo, Hi;
1278
1279 if (HasLo)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001280 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001281 if (HasHi)
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001282 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001283
1284 if (!HasLo || !HasHi)
1285 return HasLo ? Lo : Hi;
1286
1287 SDValue Vals[] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00001288 return DAG.getMergeValues(Vals, DL);
Akira Hatanakabe8612f2013-03-30 01:36:35 +00001289}
1290
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001291
Andrew Trickef9de2a2013-05-25 02:42:55 +00001292static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001293 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001294 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001295 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001296 DAG.getConstant(1, DL, MVT::i32));
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001297 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001298}
1299
Andrew Trickef9de2a2013-05-25 02:42:55 +00001300static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
Akira Hatanakad98c99f2013-10-15 01:12:50 +00001301 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1302 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001303 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1304}
1305
1306// This function expands mips intrinsic nodes which have 64-bit input operands
1307// or output values.
1308//
1309// out64 = intrinsic-node in64
1310// =>
1311// lo = copy (extract-element (in64, 0))
1312// hi = copy (extract-element (in64, 1))
1313// mips-specific-node
1314// v0 = copy lo
1315// v1 = copy hi
1316// out64 = merge-values (v0, v1)
1317//
1318static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001319 SDLoc DL(Op);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001320 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1321 SmallVector<SDValue, 3> Ops;
1322 unsigned OpNo = 0;
1323
1324 // See if Op has a chain input.
1325 if (HasChainIn)
1326 Ops.push_back(Op->getOperand(OpNo++));
1327
1328 // The next operand is the intrinsic opcode.
1329 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1330
1331 // See if the next operand has type i64.
1332 SDValue Opnd = Op->getOperand(++OpNo), In64;
1333
1334 if (Opnd.getValueType() == MVT::i64)
1335 In64 = initAccumulator(Opnd, DL, DAG);
1336 else
1337 Ops.push_back(Opnd);
1338
1339 // Push the remaining operands.
1340 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1341 Ops.push_back(Op->getOperand(OpNo));
1342
1343 // Add In64 to the end of the list.
1344 if (In64.getNode())
1345 Ops.push_back(In64);
1346
1347 // Scan output.
1348 SmallVector<EVT, 2> ResTys;
1349
1350 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1351 I != E; ++I)
1352 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1353
1354 // Create node.
Craig Topper48d114b2014-04-26 18:35:24 +00001355 SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001356 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1357
1358 if (!HasChainIn)
1359 return Out;
1360
1361 assert(Val->getValueType(1) == MVT::Other);
1362 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
Craig Topper64941d92014-04-27 19:20:57 +00001363 return DAG.getMergeValues(Vals, DL);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001364}
1365
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001366// Lower an MSA copy intrinsic into the specified SelectionDAG node
1367static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1368 SDLoc DL(Op);
1369 SDValue Vec = Op->getOperand(1);
1370 SDValue Idx = Op->getOperand(2);
1371 EVT ResTy = Op->getValueType(0);
1372 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1373
1374 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1375 DAG.getValueType(EltTy));
1376
1377 return Result;
1378}
1379
Daniel Sanders50b80412013-11-15 12:56:49 +00001380static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1381 EVT ResVecTy = Op->getValueType(0);
1382 EVT ViaVecTy = ResVecTy;
1383 SDLoc DL(Op);
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001384
Daniel Sanders50b80412013-11-15 12:56:49 +00001385 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1386 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1387 // lanes.
1388 SDValue LaneA;
1389 SDValue LaneB = Op->getOperand(2);
1390
1391 if (ResVecTy == MVT::v2i64) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001392 LaneA = DAG.getConstant(0, DL, MVT::i32);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001393 ViaVecTy = MVT::v4i32;
Daniel Sanders50b80412013-11-15 12:56:49 +00001394 } else
1395 LaneA = LaneB;
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001396
Daniel Sanders50b80412013-11-15 12:56:49 +00001397 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1398 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
Daniel Sandersf49dd822013-09-24 13:33:07 +00001399
Craig Topper48d114b2014-04-26 18:35:24 +00001400 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001401 makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001402
1403 if (ViaVecTy != ResVecTy)
1404 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001405
1406 return Result;
1407}
1408
Daniel Sanders50b80412013-11-15 12:56:49 +00001409static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001410 return DAG.getConstant(Op->getConstantOperandVal(ImmOp), SDLoc(Op),
1411 Op->getValueType(0));
Daniel Sanders50b80412013-11-15 12:56:49 +00001412}
1413
1414static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1415 bool BigEndian, SelectionDAG &DAG) {
1416 EVT ViaVecTy = VecTy;
1417 SDValue SplatValueA = SplatValue;
1418 SDValue SplatValueB = SplatValue;
1419 SDLoc DL(SplatValue);
1420
1421 if (VecTy == MVT::v2i64) {
1422 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1423 ViaVecTy = MVT::v4i32;
1424
1425 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1426 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001427 DAG.getConstant(32, DL, MVT::i32));
Daniel Sanders50b80412013-11-15 12:56:49 +00001428 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1429 }
1430
1431 // We currently hold the parts in little endian order. Swap them if
1432 // necessary.
1433 if (BigEndian)
1434 std::swap(SplatValueA, SplatValueB);
1435
1436 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1437 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1438 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1439 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1440
Craig Topper48d114b2014-04-26 18:35:24 +00001441 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001442 makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
Daniel Sanders50b80412013-11-15 12:56:49 +00001443
1444 if (VecTy != ViaVecTy)
1445 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1446
1447 return Result;
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001448}
1449
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001450static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1451 unsigned Opc, SDValue Imm,
1452 bool BigEndian) {
1453 EVT VecTy = Op->getValueType(0);
1454 SDValue Exp2Imm;
1455 SDLoc DL(Op);
1456
Daniel Sanders50b80412013-11-15 12:56:49 +00001457 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1458 // here for now.
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001459 if (VecTy == MVT::v2i64) {
1460 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1461 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1462
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001463 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), DL,
1464 MVT::i32);
1465 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), DL, MVT::i32);
Daniel Sanders50b80412013-11-15 12:56:49 +00001466
1467 if (BigEndian)
1468 std::swap(BitImmLoOp, BitImmHiOp);
1469
1470 Exp2Imm =
1471 DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
1472 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, BitImmLoOp,
1473 BitImmHiOp, BitImmLoOp, BitImmHiOp));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001474 }
1475 }
1476
Craig Topper062a2ba2014-04-25 05:30:21 +00001477 if (!Exp2Imm.getNode()) {
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001478 // We couldnt constant fold, do a vector shift instead
Daniel Sanders50b80412013-11-15 12:56:49 +00001479
1480 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1481 // only values 0-63 are valid.
1482 if (VecTy == MVT::v2i64)
1483 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1484
1485 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1486
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001487 Exp2Imm = DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, DL, VecTy),
1488 Exp2Imm);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001489 }
1490
1491 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1492}
1493
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001494static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1495 EVT ResTy = Op->getValueType(0);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001496 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001497 SDValue One = DAG.getConstant(1, DL, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001498 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
1499
Daniel Sanders71ce0ca2013-11-15 16:02:04 +00001500 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1501 DAG.getNOT(DL, Bit, ResTy));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001502}
1503
1504static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1505 SDLoc DL(Op);
1506 EVT ResTy = Op->getValueType(0);
Daniel Sanders50b80412013-11-15 12:56:49 +00001507 APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
1508 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001509 SDValue BitMask = DAG.getConstant(~BitImm, DL, ResTy);
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001510
1511 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1512}
1513
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001514SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1515 SelectionDAG &DAG) const {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001516 SDLoc DL(Op);
1517
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00001518 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1519 default:
1520 return SDValue();
1521 case Intrinsic::mips_shilo:
1522 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1523 case Intrinsic::mips_dpau_h_qbl:
1524 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1525 case Intrinsic::mips_dpau_h_qbr:
1526 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1527 case Intrinsic::mips_dpsu_h_qbl:
1528 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1529 case Intrinsic::mips_dpsu_h_qbr:
1530 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1531 case Intrinsic::mips_dpa_w_ph:
1532 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1533 case Intrinsic::mips_dps_w_ph:
1534 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1535 case Intrinsic::mips_dpax_w_ph:
1536 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1537 case Intrinsic::mips_dpsx_w_ph:
1538 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1539 case Intrinsic::mips_mulsa_w_ph:
1540 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1541 case Intrinsic::mips_mult:
1542 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1543 case Intrinsic::mips_multu:
1544 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1545 case Intrinsic::mips_madd:
1546 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1547 case Intrinsic::mips_maddu:
1548 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1549 case Intrinsic::mips_msub:
1550 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1551 case Intrinsic::mips_msubu:
1552 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
Daniel Sandersfa5ab1c2013-09-11 10:28:16 +00001553 case Intrinsic::mips_addv_b:
1554 case Intrinsic::mips_addv_h:
1555 case Intrinsic::mips_addv_w:
1556 case Intrinsic::mips_addv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001557 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1558 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00001559 case Intrinsic::mips_addvi_b:
1560 case Intrinsic::mips_addvi_h:
1561 case Intrinsic::mips_addvi_w:
1562 case Intrinsic::mips_addvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001563 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1564 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00001565 case Intrinsic::mips_and_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001566 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1567 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00001568 case Intrinsic::mips_andi_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001569 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1570 lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3f6eb542013-11-12 10:45:18 +00001571 case Intrinsic::mips_bclr_b:
1572 case Intrinsic::mips_bclr_h:
1573 case Intrinsic::mips_bclr_w:
1574 case Intrinsic::mips_bclr_d:
1575 return lowerMSABitClear(Op, DAG);
1576 case Intrinsic::mips_bclri_b:
1577 case Intrinsic::mips_bclri_h:
1578 case Intrinsic::mips_bclri_w:
1579 case Intrinsic::mips_bclri_d:
1580 return lowerMSABitClearImm(Op, DAG);
Daniel Sandersd74b1302013-10-30 14:45:14 +00001581 case Intrinsic::mips_binsli_b:
1582 case Intrinsic::mips_binsli_h:
1583 case Intrinsic::mips_binsli_w:
1584 case Intrinsic::mips_binsli_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001585 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001586 EVT VecTy = Op->getValueType(0);
1587 EVT EltTy = VecTy.getVectorElementType();
1588 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
1589 Op->getConstantOperandVal(3));
1590 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001591 DAG.getConstant(Mask, DL, VecTy, true),
1592 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001593 }
1594 case Intrinsic::mips_binsri_b:
1595 case Intrinsic::mips_binsri_h:
1596 case Intrinsic::mips_binsri_w:
1597 case Intrinsic::mips_binsri_d: {
Daniel Sandersdf2215452014-03-12 11:54:00 +00001598 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
Daniel Sandersd74b1302013-10-30 14:45:14 +00001599 EVT VecTy = Op->getValueType(0);
1600 EVT EltTy = VecTy.getVectorElementType();
1601 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
1602 Op->getConstantOperandVal(3));
1603 return DAG.getNode(ISD::VSELECT, DL, VecTy,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001604 DAG.getConstant(Mask, DL, VecTy, true),
1605 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersd74b1302013-10-30 14:45:14 +00001606 }
Daniel Sandersab94b532013-10-30 15:20:38 +00001607 case Intrinsic::mips_bmnz_v:
1608 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1609 Op->getOperand(2), Op->getOperand(1));
1610 case Intrinsic::mips_bmnzi_b:
1611 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1612 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1613 Op->getOperand(1));
1614 case Intrinsic::mips_bmz_v:
1615 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1616 Op->getOperand(1), Op->getOperand(2));
1617 case Intrinsic::mips_bmzi_b:
1618 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1619 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1620 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001621 case Intrinsic::mips_bneg_b:
1622 case Intrinsic::mips_bneg_h:
1623 case Intrinsic::mips_bneg_w:
1624 case Intrinsic::mips_bneg_d: {
1625 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001626 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001627
1628 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1629 DAG.getNode(ISD::SHL, DL, VecTy, One,
1630 Op->getOperand(2)));
1631 }
1632 case Intrinsic::mips_bnegi_b:
1633 case Intrinsic::mips_bnegi_h:
1634 case Intrinsic::mips_bnegi_w:
1635 case Intrinsic::mips_bnegi_d:
1636 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001637 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001638 case Intrinsic::mips_bnz_b:
1639 case Intrinsic::mips_bnz_h:
1640 case Intrinsic::mips_bnz_w:
1641 case Intrinsic::mips_bnz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001642 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1643 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001644 case Intrinsic::mips_bnz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001645 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1646 Op->getOperand(1));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001647 case Intrinsic::mips_bsel_v:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001648 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001649 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001650 Op->getOperand(1), Op->getOperand(3),
1651 Op->getOperand(2));
Daniel Sanderse1d24352013-09-24 12:04:44 +00001652 case Intrinsic::mips_bseli_b:
Daniel Sandersdf2215452014-03-12 11:54:00 +00001653 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001654 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
Daniel Sandersdf2215452014-03-12 11:54:00 +00001655 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1656 Op->getOperand(2));
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001657 case Intrinsic::mips_bset_b:
1658 case Intrinsic::mips_bset_h:
1659 case Intrinsic::mips_bset_w:
1660 case Intrinsic::mips_bset_d: {
1661 EVT VecTy = Op->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001662 SDValue One = DAG.getConstant(1, DL, VecTy);
Daniel Sandersa5bc99f2013-11-12 10:31:49 +00001663
1664 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1665 DAG.getNode(ISD::SHL, DL, VecTy, One,
1666 Op->getOperand(2)));
1667 }
1668 case Intrinsic::mips_bseti_b:
1669 case Intrinsic::mips_bseti_h:
1670 case Intrinsic::mips_bseti_w:
1671 case Intrinsic::mips_bseti_d:
1672 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
Eric Christopher1c29a652014-07-18 22:55:25 +00001673 !Subtarget.isLittle());
Daniel Sandersce09d072013-08-28 12:14:50 +00001674 case Intrinsic::mips_bz_b:
1675 case Intrinsic::mips_bz_h:
1676 case Intrinsic::mips_bz_w:
1677 case Intrinsic::mips_bz_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001678 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1679 Op->getOperand(1));
Daniel Sandersce09d072013-08-28 12:14:50 +00001680 case Intrinsic::mips_bz_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001681 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1682 Op->getOperand(1));
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001683 case Intrinsic::mips_ceq_b:
1684 case Intrinsic::mips_ceq_h:
1685 case Intrinsic::mips_ceq_w:
1686 case Intrinsic::mips_ceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001687 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001688 Op->getOperand(2), ISD::SETEQ);
1689 case Intrinsic::mips_ceqi_b:
1690 case Intrinsic::mips_ceqi_h:
1691 case Intrinsic::mips_ceqi_w:
1692 case Intrinsic::mips_ceqi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001693 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001694 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1695 case Intrinsic::mips_cle_s_b:
1696 case Intrinsic::mips_cle_s_h:
1697 case Intrinsic::mips_cle_s_w:
1698 case Intrinsic::mips_cle_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001699 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001700 Op->getOperand(2), ISD::SETLE);
1701 case Intrinsic::mips_clei_s_b:
1702 case Intrinsic::mips_clei_s_h:
1703 case Intrinsic::mips_clei_s_w:
1704 case Intrinsic::mips_clei_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001705 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001706 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1707 case Intrinsic::mips_cle_u_b:
1708 case Intrinsic::mips_cle_u_h:
1709 case Intrinsic::mips_cle_u_w:
1710 case Intrinsic::mips_cle_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001711 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001712 Op->getOperand(2), ISD::SETULE);
1713 case Intrinsic::mips_clei_u_b:
1714 case Intrinsic::mips_clei_u_h:
1715 case Intrinsic::mips_clei_u_w:
1716 case Intrinsic::mips_clei_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001717 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001718 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1719 case Intrinsic::mips_clt_s_b:
1720 case Intrinsic::mips_clt_s_h:
1721 case Intrinsic::mips_clt_s_w:
1722 case Intrinsic::mips_clt_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001723 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001724 Op->getOperand(2), ISD::SETLT);
1725 case Intrinsic::mips_clti_s_b:
1726 case Intrinsic::mips_clti_s_h:
1727 case Intrinsic::mips_clti_s_w:
1728 case Intrinsic::mips_clti_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001729 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001730 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1731 case Intrinsic::mips_clt_u_b:
1732 case Intrinsic::mips_clt_u_h:
1733 case Intrinsic::mips_clt_u_w:
1734 case Intrinsic::mips_clt_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001735 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001736 Op->getOperand(2), ISD::SETULT);
1737 case Intrinsic::mips_clti_u_b:
1738 case Intrinsic::mips_clti_u_h:
1739 case Intrinsic::mips_clti_u_w:
1740 case Intrinsic::mips_clti_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001741 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001742 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001743 case Intrinsic::mips_copy_s_b:
1744 case Intrinsic::mips_copy_s_h:
1745 case Intrinsic::mips_copy_s_w:
1746 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001747 case Intrinsic::mips_copy_s_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001748 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001749 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1750 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1751 else {
1752 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1753 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1754 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1755 Op->getValueType(0), Op->getOperand(1),
1756 Op->getOperand(2));
1757 }
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001758 case Intrinsic::mips_copy_u_b:
1759 case Intrinsic::mips_copy_u_h:
1760 case Intrinsic::mips_copy_u_w:
1761 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
Daniel Sanders7f3d9462013-09-27 13:04:21 +00001762 case Intrinsic::mips_copy_u_d:
Eric Christopher1c29a652014-07-18 22:55:25 +00001763 if (Subtarget.hasMips64())
Matheus Almeida74070322014-01-29 14:05:28 +00001764 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1765 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1766 else {
1767 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1768 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1769 // Note: When i64 is illegal, this results in copy_s.w instructions
1770 // instead of copy_u.w instructions. This makes no difference to the
1771 // behaviour since i64 is only illegal when the register file is 32-bit.
1772 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1773 Op->getValueType(0), Op->getOperand(1),
1774 Op->getOperand(2));
1775 }
Daniel Sanders607952b2013-09-11 10:38:58 +00001776 case Intrinsic::mips_div_s_b:
1777 case Intrinsic::mips_div_s_h:
1778 case Intrinsic::mips_div_s_w:
1779 case Intrinsic::mips_div_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001780 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1781 Op->getOperand(2));
Daniel Sanders607952b2013-09-11 10:38:58 +00001782 case Intrinsic::mips_div_u_b:
1783 case Intrinsic::mips_div_u_h:
1784 case Intrinsic::mips_div_u_w:
1785 case Intrinsic::mips_div_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001786 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1787 Op->getOperand(2));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001788 case Intrinsic::mips_fadd_w:
Sanjay Patela2607012015-09-16 16:31:21 +00001789 case Intrinsic::mips_fadd_d: {
1790 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001791 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1792 Op->getOperand(2));
Sanjay Patela2607012015-09-16 16:31:21 +00001793 }
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001794 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1795 case Intrinsic::mips_fceq_w:
1796 case Intrinsic::mips_fceq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001797 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001798 Op->getOperand(2), ISD::SETOEQ);
1799 case Intrinsic::mips_fcle_w:
1800 case Intrinsic::mips_fcle_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001801 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001802 Op->getOperand(2), ISD::SETOLE);
1803 case Intrinsic::mips_fclt_w:
1804 case Intrinsic::mips_fclt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001805 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001806 Op->getOperand(2), ISD::SETOLT);
1807 case Intrinsic::mips_fcne_w:
1808 case Intrinsic::mips_fcne_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001809 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001810 Op->getOperand(2), ISD::SETONE);
1811 case Intrinsic::mips_fcor_w:
1812 case Intrinsic::mips_fcor_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001813 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001814 Op->getOperand(2), ISD::SETO);
1815 case Intrinsic::mips_fcueq_w:
1816 case Intrinsic::mips_fcueq_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001817 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001818 Op->getOperand(2), ISD::SETUEQ);
1819 case Intrinsic::mips_fcule_w:
1820 case Intrinsic::mips_fcule_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001821 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001822 Op->getOperand(2), ISD::SETULE);
1823 case Intrinsic::mips_fcult_w:
1824 case Intrinsic::mips_fcult_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001825 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001826 Op->getOperand(2), ISD::SETULT);
1827 case Intrinsic::mips_fcun_w:
1828 case Intrinsic::mips_fcun_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001829 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001830 Op->getOperand(2), ISD::SETUO);
1831 case Intrinsic::mips_fcune_w:
1832 case Intrinsic::mips_fcune_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001833 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
Daniel Sandersfd538dc2013-09-24 10:46:19 +00001834 Op->getOperand(2), ISD::SETUNE);
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001835 case Intrinsic::mips_fdiv_w:
Sanjay Patela2607012015-09-16 16:31:21 +00001836 case Intrinsic::mips_fdiv_d: {
1837 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001838 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1839 Op->getOperand(2));
Sanjay Patela2607012015-09-16 16:31:21 +00001840 }
Daniel Sanders015972b2013-10-11 10:00:06 +00001841 case Intrinsic::mips_ffint_u_w:
1842 case Intrinsic::mips_ffint_u_d:
1843 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1844 Op->getOperand(1));
1845 case Intrinsic::mips_ffint_s_w:
1846 case Intrinsic::mips_ffint_s_d:
1847 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1848 Op->getOperand(1));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001849 case Intrinsic::mips_fill_b:
1850 case Intrinsic::mips_fill_h:
Daniel Sandersc72593e2013-09-27 13:20:41 +00001851 case Intrinsic::mips_fill_w:
1852 case Intrinsic::mips_fill_d: {
Daniel Sandersf49dd822013-09-24 13:33:07 +00001853 EVT ResTy = Op->getValueType(0);
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001854 SmallVector<SDValue, 16> Ops(ResTy.getVectorNumElements(),
1855 Op->getOperand(1));
Daniel Sandersf49dd822013-09-24 13:33:07 +00001856
Daniel Sandersc72593e2013-09-27 13:20:41 +00001857 // If ResTy is v2i64 then the type legalizer will break this node down into
1858 // an equivalent v4i32.
Craig Topper48d114b2014-04-26 18:35:24 +00001859 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, Ops);
Daniel Sandersf49dd822013-09-24 13:33:07 +00001860 }
Daniel Sandersa9521602013-10-23 10:36:52 +00001861 case Intrinsic::mips_fexp2_w:
1862 case Intrinsic::mips_fexp2_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001863 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sandersa9521602013-10-23 10:36:52 +00001864 EVT ResTy = Op->getValueType(0);
1865 return DAG.getNode(
1866 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1867 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1868 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001869 case Intrinsic::mips_flog2_w:
1870 case Intrinsic::mips_flog2_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001871 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersd7103f32013-10-11 10:14:25 +00001872 case Intrinsic::mips_fmadd_w:
1873 case Intrinsic::mips_fmadd_d:
1874 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1875 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001876 case Intrinsic::mips_fmul_w:
Sanjay Patela2607012015-09-16 16:31:21 +00001877 case Intrinsic::mips_fmul_d: {
1878 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001879 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1880 Op->getOperand(2));
Sanjay Patela2607012015-09-16 16:31:21 +00001881 }
Daniel Sanderse67bd872013-10-11 10:27:32 +00001882 case Intrinsic::mips_fmsub_w:
1883 case Intrinsic::mips_fmsub_d: {
Sanjay Patela2607012015-09-16 16:31:21 +00001884 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanderse67bd872013-10-11 10:27:32 +00001885 EVT ResTy = Op->getValueType(0);
1886 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1887 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1888 Op->getOperand(2), Op->getOperand(3)));
1889 }
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001890 case Intrinsic::mips_frint_w:
1891 case Intrinsic::mips_frint_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001892 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001893 case Intrinsic::mips_fsqrt_w:
1894 case Intrinsic::mips_fsqrt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001895 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf5bd9372013-09-11 10:51:30 +00001896 case Intrinsic::mips_fsub_w:
Sanjay Patela2607012015-09-16 16:31:21 +00001897 case Intrinsic::mips_fsub_d: {
1898 // TODO: If intrinsics have fast-math-flags, propagate them.
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001899 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1900 Op->getOperand(2));
Sanjay Patela2607012015-09-16 16:31:21 +00001901 }
Daniel Sanders015972b2013-10-11 10:00:06 +00001902 case Intrinsic::mips_ftrunc_u_w:
1903 case Intrinsic::mips_ftrunc_u_d:
1904 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1905 Op->getOperand(1));
1906 case Intrinsic::mips_ftrunc_s_w:
1907 case Intrinsic::mips_ftrunc_s_d:
1908 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1909 Op->getOperand(1));
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001910 case Intrinsic::mips_ilvev_b:
1911 case Intrinsic::mips_ilvev_h:
1912 case Intrinsic::mips_ilvev_w:
1913 case Intrinsic::mips_ilvev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001914 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001915 Op->getOperand(1), Op->getOperand(2));
1916 case Intrinsic::mips_ilvl_b:
1917 case Intrinsic::mips_ilvl_h:
1918 case Intrinsic::mips_ilvl_w:
1919 case Intrinsic::mips_ilvl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001920 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001921 Op->getOperand(1), Op->getOperand(2));
1922 case Intrinsic::mips_ilvod_b:
1923 case Intrinsic::mips_ilvod_h:
1924 case Intrinsic::mips_ilvod_w:
1925 case Intrinsic::mips_ilvod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001926 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001927 Op->getOperand(1), Op->getOperand(2));
1928 case Intrinsic::mips_ilvr_b:
1929 case Intrinsic::mips_ilvr_h:
1930 case Intrinsic::mips_ilvr_w:
1931 case Intrinsic::mips_ilvr_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001932 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
Daniel Sanders2ed228b2013-09-24 14:36:12 +00001933 Op->getOperand(1), Op->getOperand(2));
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00001934 case Intrinsic::mips_insert_b:
1935 case Intrinsic::mips_insert_h:
1936 case Intrinsic::mips_insert_w:
Daniel Sanders6098b332013-09-27 13:36:54 +00001937 case Intrinsic::mips_insert_d:
1938 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1939 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
Daniel Sandersb50ccf82014-04-01 10:35:28 +00001940 case Intrinsic::mips_insve_b:
1941 case Intrinsic::mips_insve_h:
1942 case Intrinsic::mips_insve_w:
1943 case Intrinsic::mips_insve_d:
1944 return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
1945 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001946 DAG.getConstant(0, DL, MVT::i32));
Daniel Sanders7a289d02013-09-23 12:02:46 +00001947 case Intrinsic::mips_ldi_b:
1948 case Intrinsic::mips_ldi_h:
1949 case Intrinsic::mips_ldi_w:
1950 case Intrinsic::mips_ldi_d:
Daniel Sandersf49dd822013-09-24 13:33:07 +00001951 return lowerMSASplatImm(Op, 1, DAG);
Matheus Almeida4b27eb52014-02-10 12:05:17 +00001952 case Intrinsic::mips_lsa:
1953 case Intrinsic::mips_dlsa: {
Daniel Sandersa4eaf592013-10-17 13:38:20 +00001954 EVT ResTy = Op->getValueType(0);
1955 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1956 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1957 Op->getOperand(2), Op->getOperand(3)));
1958 }
Daniel Sanders50e5ed32013-10-11 10:50:42 +00001959 case Intrinsic::mips_maddv_b:
1960 case Intrinsic::mips_maddv_h:
1961 case Intrinsic::mips_maddv_w:
1962 case Intrinsic::mips_maddv_d: {
1963 EVT ResTy = Op->getValueType(0);
1964 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1965 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1966 Op->getOperand(2), Op->getOperand(3)));
1967 }
Daniel Sanders3ce56622013-09-24 12:18:31 +00001968 case Intrinsic::mips_max_s_b:
1969 case Intrinsic::mips_max_s_h:
1970 case Intrinsic::mips_max_s_w:
1971 case Intrinsic::mips_max_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001972 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1973 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001974 case Intrinsic::mips_max_u_b:
1975 case Intrinsic::mips_max_u_h:
1976 case Intrinsic::mips_max_u_w:
1977 case Intrinsic::mips_max_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001978 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1979 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001980 case Intrinsic::mips_maxi_s_b:
1981 case Intrinsic::mips_maxi_s_h:
1982 case Intrinsic::mips_maxi_s_w:
1983 case Intrinsic::mips_maxi_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001984 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1985 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001986 case Intrinsic::mips_maxi_u_b:
1987 case Intrinsic::mips_maxi_u_h:
1988 case Intrinsic::mips_maxi_u_w:
1989 case Intrinsic::mips_maxi_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001990 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1991 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001992 case Intrinsic::mips_min_s_b:
1993 case Intrinsic::mips_min_s_h:
1994 case Intrinsic::mips_min_s_w:
1995 case Intrinsic::mips_min_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00001996 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1997 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00001998 case Intrinsic::mips_min_u_b:
1999 case Intrinsic::mips_min_u_h:
2000 case Intrinsic::mips_min_u_w:
2001 case Intrinsic::mips_min_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002002 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
2003 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002004 case Intrinsic::mips_mini_s_b:
2005 case Intrinsic::mips_mini_s_h:
2006 case Intrinsic::mips_mini_s_w:
2007 case Intrinsic::mips_mini_s_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002008 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
2009 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders3ce56622013-09-24 12:18:31 +00002010 case Intrinsic::mips_mini_u_b:
2011 case Intrinsic::mips_mini_u_h:
2012 case Intrinsic::mips_mini_u_w:
2013 case Intrinsic::mips_mini_u_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002014 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
2015 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanders0210dd42013-10-01 10:22:35 +00002016 case Intrinsic::mips_mod_s_b:
2017 case Intrinsic::mips_mod_s_h:
2018 case Intrinsic::mips_mod_s_w:
2019 case Intrinsic::mips_mod_s_d:
2020 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
2021 Op->getOperand(2));
2022 case Intrinsic::mips_mod_u_b:
2023 case Intrinsic::mips_mod_u_h:
2024 case Intrinsic::mips_mod_u_w:
2025 case Intrinsic::mips_mod_u_d:
2026 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
2027 Op->getOperand(2));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002028 case Intrinsic::mips_mulv_b:
2029 case Intrinsic::mips_mulv_h:
2030 case Intrinsic::mips_mulv_w:
2031 case Intrinsic::mips_mulv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002032 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
2033 Op->getOperand(2));
Daniel Sanders50e5ed32013-10-11 10:50:42 +00002034 case Intrinsic::mips_msubv_b:
2035 case Intrinsic::mips_msubv_h:
2036 case Intrinsic::mips_msubv_w:
2037 case Intrinsic::mips_msubv_d: {
2038 EVT ResTy = Op->getValueType(0);
2039 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
2040 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
2041 Op->getOperand(2), Op->getOperand(3)));
2042 }
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002043 case Intrinsic::mips_nlzc_b:
2044 case Intrinsic::mips_nlzc_h:
2045 case Intrinsic::mips_nlzc_w:
2046 case Intrinsic::mips_nlzc_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002047 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002048 case Intrinsic::mips_nor_v: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002049 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2050 Op->getOperand(1), Op->getOperand(2));
2051 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersf7456c72013-09-23 13:22:24 +00002052 }
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002053 case Intrinsic::mips_nori_b: {
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002054 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2055 Op->getOperand(1),
2056 lowerMSASplatImm(Op, 2, DAG));
2057 return DAG.getNOT(DL, Res, Res->getValueType(0));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002058 }
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002059 case Intrinsic::mips_or_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002060 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
2061 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002062 case Intrinsic::mips_ori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002063 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
2064 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002065 case Intrinsic::mips_pckev_b:
2066 case Intrinsic::mips_pckev_h:
2067 case Intrinsic::mips_pckev_w:
2068 case Intrinsic::mips_pckev_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002069 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002070 Op->getOperand(1), Op->getOperand(2));
2071 case Intrinsic::mips_pckod_b:
2072 case Intrinsic::mips_pckod_h:
2073 case Intrinsic::mips_pckod_w:
2074 case Intrinsic::mips_pckod_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002075 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002076 Op->getOperand(1), Op->getOperand(2));
Daniel Sanders766cb692013-09-23 13:40:21 +00002077 case Intrinsic::mips_pcnt_b:
2078 case Intrinsic::mips_pcnt_h:
2079 case Intrinsic::mips_pcnt_w:
2080 case Intrinsic::mips_pcnt_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002081 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
Daniel Sanders26307182013-09-24 14:20:00 +00002082 case Intrinsic::mips_shf_b:
2083 case Intrinsic::mips_shf_h:
2084 case Intrinsic::mips_shf_w:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002085 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
Daniel Sanders26307182013-09-24 14:20:00 +00002086 Op->getOperand(2), Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002087 case Intrinsic::mips_sll_b:
2088 case Intrinsic::mips_sll_h:
2089 case Intrinsic::mips_sll_w:
2090 case Intrinsic::mips_sll_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002091 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
2092 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002093 case Intrinsic::mips_slli_b:
2094 case Intrinsic::mips_slli_h:
2095 case Intrinsic::mips_slli_w:
2096 case Intrinsic::mips_slli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002097 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
2098 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002099 case Intrinsic::mips_splat_b:
2100 case Intrinsic::mips_splat_h:
2101 case Intrinsic::mips_splat_w:
2102 case Intrinsic::mips_splat_d:
2103 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
2104 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
2105 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
2106 // Instead we lower to MipsISD::VSHF and match from there.
2107 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanders50b80412013-11-15 12:56:49 +00002108 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
Daniel Sanderse7ef0c82013-10-30 13:07:44 +00002109 Op->getOperand(1));
Daniel Sanders7e51fe12013-09-27 11:48:57 +00002110 case Intrinsic::mips_splati_b:
2111 case Intrinsic::mips_splati_h:
2112 case Intrinsic::mips_splati_w:
2113 case Intrinsic::mips_splati_d:
2114 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2115 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
2116 Op->getOperand(1));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002117 case Intrinsic::mips_sra_b:
2118 case Intrinsic::mips_sra_h:
2119 case Intrinsic::mips_sra_w:
2120 case Intrinsic::mips_sra_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002121 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
2122 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002123 case Intrinsic::mips_srai_b:
2124 case Intrinsic::mips_srai_h:
2125 case Intrinsic::mips_srai_w:
2126 case Intrinsic::mips_srai_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002127 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
2128 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002129 case Intrinsic::mips_srl_b:
2130 case Intrinsic::mips_srl_h:
2131 case Intrinsic::mips_srl_w:
2132 case Intrinsic::mips_srl_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002133 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
2134 Op->getOperand(2));
Daniel Sanderscba19222013-09-24 10:28:18 +00002135 case Intrinsic::mips_srli_b:
2136 case Intrinsic::mips_srli_h:
2137 case Intrinsic::mips_srli_w:
2138 case Intrinsic::mips_srli_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002139 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2140 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sandersfbcb5822013-09-11 11:58:30 +00002141 case Intrinsic::mips_subv_b:
2142 case Intrinsic::mips_subv_h:
2143 case Intrinsic::mips_subv_w:
2144 case Intrinsic::mips_subv_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002145 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2146 Op->getOperand(2));
Daniel Sanders86d0c8d2013-09-23 14:29:55 +00002147 case Intrinsic::mips_subvi_b:
2148 case Intrinsic::mips_subvi_h:
2149 case Intrinsic::mips_subvi_w:
2150 case Intrinsic::mips_subvi_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002151 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2152 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Daniel Sanderse5087042013-09-24 14:02:15 +00002153 case Intrinsic::mips_vshf_b:
2154 case Intrinsic::mips_vshf_h:
2155 case Intrinsic::mips_vshf_w:
2156 case Intrinsic::mips_vshf_d:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002157 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
Daniel Sanderse5087042013-09-24 14:02:15 +00002158 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
Daniel Sanders8ca81e42013-09-23 12:57:42 +00002159 case Intrinsic::mips_xor_v:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002160 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2161 Op->getOperand(2));
Daniel Sandersbfc39ce2013-09-24 12:32:47 +00002162 case Intrinsic::mips_xori_b:
Daniel Sanders84e7caf2013-09-27 10:25:41 +00002163 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2164 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002165 }
2166}
2167
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002168static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2169 SDLoc DL(Op);
2170 SDValue ChainIn = Op->getOperand(0);
2171 SDValue Address = Op->getOperand(2);
2172 SDValue Offset = Op->getOperand(3);
2173 EVT ResTy = Op->getValueType(0);
2174 EVT PtrTy = Address->getValueType(0);
2175
2176 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2177
2178 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
2179 false, false, 16);
2180}
2181
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002182SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2183 SelectionDAG &DAG) const {
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002184 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2185 switch (Intr) {
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002186 default:
2187 return SDValue();
2188 case Intrinsic::mips_extp:
2189 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2190 case Intrinsic::mips_extpdp:
2191 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2192 case Intrinsic::mips_extr_w:
2193 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2194 case Intrinsic::mips_extr_r_w:
2195 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2196 case Intrinsic::mips_extr_rs_w:
2197 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2198 case Intrinsic::mips_extr_s_h:
2199 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2200 case Intrinsic::mips_mthlip:
2201 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2202 case Intrinsic::mips_mulsaq_s_w_ph:
2203 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2204 case Intrinsic::mips_maq_s_w_phl:
2205 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2206 case Intrinsic::mips_maq_s_w_phr:
2207 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2208 case Intrinsic::mips_maq_sa_w_phl:
2209 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2210 case Intrinsic::mips_maq_sa_w_phr:
2211 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2212 case Intrinsic::mips_dpaq_s_w_ph:
2213 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2214 case Intrinsic::mips_dpsq_s_w_ph:
2215 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2216 case Intrinsic::mips_dpaq_sa_l_w:
2217 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2218 case Intrinsic::mips_dpsq_sa_l_w:
2219 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2220 case Intrinsic::mips_dpaqx_s_w_ph:
2221 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2222 case Intrinsic::mips_dpaqx_sa_w_ph:
2223 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2224 case Intrinsic::mips_dpsqx_s_w_ph:
2225 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2226 case Intrinsic::mips_dpsqx_sa_w_ph:
2227 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002228 case Intrinsic::mips_ld_b:
2229 case Intrinsic::mips_ld_h:
2230 case Intrinsic::mips_ld_w:
2231 case Intrinsic::mips_ld_d:
Daniel Sanderse6ed5b72013-08-28 12:04:29 +00002232 return lowerMSALoadIntr(Op, DAG, Intr);
2233 }
2234}
2235
2236static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2237 SDLoc DL(Op);
2238 SDValue ChainIn = Op->getOperand(0);
2239 SDValue Value = Op->getOperand(2);
2240 SDValue Address = Op->getOperand(3);
2241 SDValue Offset = Op->getOperand(4);
2242 EVT PtrTy = Address->getValueType(0);
2243
2244 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2245
2246 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
2247 false, 16);
2248}
2249
2250SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2251 SelectionDAG &DAG) const {
2252 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2253 switch (Intr) {
2254 default:
2255 return SDValue();
2256 case Intrinsic::mips_st_b:
2257 case Intrinsic::mips_st_h:
2258 case Intrinsic::mips_st_w:
2259 case Intrinsic::mips_st_d:
Daniel Sandersce09d072013-08-28 12:14:50 +00002260 return lowerMSAStoreIntr(Op, DAG, Intr);
Akira Hatanakaa6bbde52013-04-13 02:13:30 +00002261 }
2262}
2263
Daniel Sanders7a289d02013-09-23 12:02:46 +00002264/// \brief Check if the given BuildVectorSDNode is a splat.
2265/// This method currently relies on DAG nodes being reused when equivalent,
2266/// so it's possible for this to return false even when isConstantSplat returns
2267/// true.
2268static bool isSplatVector(const BuildVectorSDNode *N) {
Daniel Sanders7a289d02013-09-23 12:02:46 +00002269 unsigned int nOps = N->getNumOperands();
Daniel Sandersab94b532013-10-30 15:20:38 +00002270 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
Daniel Sanders7a289d02013-09-23 12:02:46 +00002271
2272 SDValue Operand0 = N->getOperand(0);
2273
2274 for (unsigned int i = 1; i < nOps; ++i) {
2275 if (N->getOperand(i) != Operand0)
2276 return false;
2277 }
2278
2279 return true;
2280}
2281
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002282// Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2283//
2284// The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2285// choose to sign-extend but we could have equally chosen zero-extend. The
2286// DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2287// result into this node later (possibly changing it to a zero-extend in the
2288// process).
2289SDValue MipsSETargetLowering::
2290lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2291 SDLoc DL(Op);
2292 EVT ResTy = Op->getValueType(0);
2293 SDValue Op0 = Op->getOperand(0);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00002294 EVT VecTy = Op0->getValueType(0);
2295
2296 if (!VecTy.is128BitVector())
2297 return SDValue();
2298
2299 if (ResTy.isInteger()) {
2300 SDValue Op1 = Op->getOperand(1);
2301 EVT EltTy = VecTy.getVectorElementType();
2302 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2303 DAG.getValueType(EltTy));
2304 }
2305
2306 return Op;
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +00002307}
2308
Daniel Sandersf49dd822013-09-24 13:33:07 +00002309static bool isConstantOrUndef(const SDValue Op) {
2310 if (Op->getOpcode() == ISD::UNDEF)
2311 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002312 if (isa<ConstantSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002313 return true;
Vasileios Kalintiris46963f62015-02-13 19:12:16 +00002314 if (isa<ConstantFPSDNode>(Op))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002315 return true;
2316 return false;
2317}
2318
2319static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2320 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2321 if (isConstantOrUndef(Op->getOperand(i)))
2322 return true;
2323 return false;
2324}
2325
Daniel Sanders7a289d02013-09-23 12:02:46 +00002326// Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2327// backend.
2328//
2329// Lowers according to the following rules:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002330// - Constant splats are legal as-is as long as the SplatBitSize is a power of
2331// 2 less than or equal to 64 and the value fits into a signed 10-bit
2332// immediate
2333// - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2334// is a power of 2 less than or equal to 64 and the value does not fit into a
2335// signed 10-bit immediate
2336// - Non-constant splats are legal as-is.
2337// - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2338// - All others are illegal and must be expanded.
Daniel Sanders7a289d02013-09-23 12:02:46 +00002339SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2340 SelectionDAG &DAG) const {
2341 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2342 EVT ResTy = Op->getValueType(0);
2343 SDLoc DL(Op);
2344 APInt SplatValue, SplatUndef;
2345 unsigned SplatBitSize;
2346 bool HasAnyUndefs;
2347
Eric Christopher1c29a652014-07-18 22:55:25 +00002348 if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
Daniel Sanders7a289d02013-09-23 12:02:46 +00002349 return SDValue();
2350
2351 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2352 HasAnyUndefs, 8,
Eric Christopher1c29a652014-07-18 22:55:25 +00002353 !Subtarget.isLittle()) && SplatBitSize <= 64) {
Daniel Sandersf49dd822013-09-24 13:33:07 +00002354 // We can only cope with 8, 16, 32, or 64-bit elements
2355 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2356 SplatBitSize != 64)
2357 return SDValue();
2358
2359 // If the value fits into a simm10 then we can use ldi.[bhwd]
Daniel Sandersfd8e4162013-11-22 11:24:50 +00002360 // However, if it isn't an integer type we will have to bitcast from an
Daniel Sandersd40aea82013-11-22 13:22:52 +00002361 // integer type first. Also, if there are any undefs, we must lower them
Daniel Sanders630dbe02013-11-22 13:14:06 +00002362 // to defined values first.
2363 if (ResTy.isInteger() && !HasAnyUndefs && SplatValue.isSignedIntN(10))
Daniel Sandersf49dd822013-09-24 13:33:07 +00002364 return Op;
2365
2366 EVT ViaVecTy;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002367
2368 switch (SplatBitSize) {
2369 default:
2370 return SDValue();
Daniel Sandersf49dd822013-09-24 13:33:07 +00002371 case 8:
2372 ViaVecTy = MVT::v16i8;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002373 break;
2374 case 16:
Daniel Sandersf49dd822013-09-24 13:33:07 +00002375 ViaVecTy = MVT::v8i16;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002376 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002377 case 32:
2378 ViaVecTy = MVT::v4i32;
Daniel Sanders7a289d02013-09-23 12:02:46 +00002379 break;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002380 case 64:
2381 // There's no fill.d to fall back on for 64-bit values
2382 return SDValue();
Daniel Sanders7a289d02013-09-23 12:02:46 +00002383 }
2384
Daniel Sanders50b80412013-11-15 12:56:49 +00002385 // SelectionDAG::getConstant will promote SplatValue appropriately.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002386 SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
Daniel Sandersf49dd822013-09-24 13:33:07 +00002387
Daniel Sanders50b80412013-11-15 12:56:49 +00002388 // Bitcast to the type we originally wanted
Daniel Sandersf49dd822013-09-24 13:33:07 +00002389 if (ViaVecTy != ResTy)
2390 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
Daniel Sanders7a289d02013-09-23 12:02:46 +00002391
2392 return Result;
Daniel Sandersf49dd822013-09-24 13:33:07 +00002393 } else if (isSplatVector(Node))
2394 return Op;
2395 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
Daniel Sandersf86622b2013-09-24 13:16:15 +00002396 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2397 // The resulting code is the same length as the expansion, but it doesn't
2398 // use memory operations
2399 EVT ResTy = Node->getValueType(0);
2400
2401 assert(ResTy.isVector());
2402
2403 unsigned NumElts = ResTy.getVectorNumElements();
2404 SDValue Vector = DAG.getUNDEF(ResTy);
2405 for (unsigned i = 0; i < NumElts; ++i) {
2406 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2407 Node->getOperand(i),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002408 DAG.getConstant(i, DL, MVT::i32));
Daniel Sandersf86622b2013-09-24 13:16:15 +00002409 }
2410 return Vector;
2411 }
Daniel Sanders7a289d02013-09-23 12:02:46 +00002412
2413 return SDValue();
2414}
2415
Daniel Sanders26307182013-09-24 14:20:00 +00002416// Lower VECTOR_SHUFFLE into SHF (if possible).
2417//
2418// SHF splits the vector into blocks of four elements, then shuffles these
2419// elements according to a <4 x i2> constant (encoded as an integer immediate).
2420//
2421// It is therefore possible to lower into SHF when the mask takes the form:
2422// <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2423// When undef's appear they are treated as if they were whatever value is
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002424// necessary in order to fit the above forms.
Daniel Sanders26307182013-09-24 14:20:00 +00002425//
2426// For example:
2427// %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2428// <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2429// i32 7, i32 6, i32 5, i32 4>
2430// is lowered to:
2431// (SHF_H $w0, $w1, 27)
2432// where the 27 comes from:
2433// 3 + (2 << 2) + (1 << 4) + (0 << 6)
2434static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2435 SmallVector<int, 16> Indices,
2436 SelectionDAG &DAG) {
2437 int SHFIndices[4] = { -1, -1, -1, -1 };
2438
2439 if (Indices.size() < 4)
2440 return SDValue();
2441
2442 for (unsigned i = 0; i < 4; ++i) {
2443 for (unsigned j = i; j < Indices.size(); j += 4) {
2444 int Idx = Indices[j];
2445
2446 // Convert from vector index to 4-element subvector index
2447 // If an index refers to an element outside of the subvector then give up
2448 if (Idx != -1) {
2449 Idx -= 4 * (j / 4);
2450 if (Idx < 0 || Idx >= 4)
2451 return SDValue();
2452 }
2453
2454 // If the mask has an undef, replace it with the current index.
2455 // Note that it might still be undef if the current index is also undef
2456 if (SHFIndices[i] == -1)
2457 SHFIndices[i] = Idx;
2458
2459 // Check that non-undef values are the same as in the mask. If they
2460 // aren't then give up
2461 if (!(Idx == -1 || Idx == SHFIndices[i]))
2462 return SDValue();
2463 }
2464 }
2465
2466 // Calculate the immediate. Replace any remaining undefs with zero
2467 APInt Imm(32, 0);
2468 for (int i = 3; i >= 0; --i) {
2469 int Idx = SHFIndices[i];
2470
2471 if (Idx == -1)
2472 Idx = 0;
2473
2474 Imm <<= 2;
2475 Imm |= Idx & 0x3;
2476 }
2477
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002478 SDLoc DL(Op);
2479 return DAG.getNode(MipsISD::SHF, DL, ResTy,
2480 DAG.getConstant(Imm, DL, MVT::i32), Op->getOperand(0));
Daniel Sanders26307182013-09-24 14:20:00 +00002481}
2482
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002483/// Determine whether a range fits a regular pattern of values.
2484/// This function accounts for the possibility of jumping over the End iterator.
2485template <typename ValType>
2486static bool
2487fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,
2488 unsigned CheckStride,
2489 typename SmallVectorImpl<ValType>::const_iterator End,
2490 ValType ExpectedIndex, unsigned ExpectedIndexStride) {
2491 auto &I = Begin;
2492
2493 while (I != End) {
2494 if (*I != -1 && *I != ExpectedIndex)
2495 return false;
2496 ExpectedIndex += ExpectedIndexStride;
2497
2498 // Incrementing past End is undefined behaviour so we must increment one
2499 // step at a time and check for End at each step.
2500 for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
2501 ; // Empty loop body.
2502 }
2503 return true;
2504}
2505
2506// Determine whether VECTOR_SHUFFLE is a SPLATI.
2507//
2508// It is a SPLATI when the mask is:
2509// <x, x, x, ...>
2510// where x is any valid index.
2511//
2512// When undef's appear in the mask they are treated as if they were whatever
2513// value is necessary in order to fit the above form.
2514static bool isVECTOR_SHUFFLE_SPLATI(SDValue Op, EVT ResTy,
2515 SmallVector<int, 16> Indices,
2516 SelectionDAG &DAG) {
2517 assert((Indices.size() % 2) == 0);
2518
2519 int SplatIndex = -1;
2520 for (const auto &V : Indices) {
2521 if (V != -1) {
2522 SplatIndex = V;
2523 break;
2524 }
2525 }
2526
2527 return fitsRegularPattern<int>(Indices.begin(), 1, Indices.end(), SplatIndex,
2528 0);
2529}
2530
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002531// Lower VECTOR_SHUFFLE into ILVEV (if possible).
2532//
2533// ILVEV interleaves the even elements from each vector.
2534//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002535// It is possible to lower into ILVEV when the mask consists of two of the
2536// following forms interleaved:
2537// <0, 2, 4, ...>
2538// <n, n+2, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002539// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002540// For example:
2541// <0, 0, 2, 2, 4, 4, ...>
2542// <0, n, 2, n+2, 4, n+4, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002543//
2544// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002545// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002546static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2547 SmallVector<int, 16> Indices,
2548 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002549 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002550
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002551 SDValue Wt;
2552 SDValue Ws;
2553 const auto &Begin = Indices.begin();
2554 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002555
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002556 // Check even elements are taken from the even elements of one half or the
2557 // other and pick an operand accordingly.
2558 if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
2559 Wt = Op->getOperand(0);
2560 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 2))
2561 Wt = Op->getOperand(1);
2562 else
2563 return SDValue();
2564
2565 // Check odd elements are taken from the even elements of one half or the
2566 // other and pick an operand accordingly.
2567 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
2568 Ws = Op->getOperand(0);
2569 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 2))
2570 Ws = Op->getOperand(1);
2571 else
2572 return SDValue();
2573
2574 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002575}
2576
2577// Lower VECTOR_SHUFFLE into ILVOD (if possible).
2578//
2579// ILVOD interleaves the odd elements from each vector.
2580//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002581// It is possible to lower into ILVOD when the mask consists of two of the
2582// following forms interleaved:
2583// <1, 3, 5, ...>
2584// <n+1, n+3, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002585// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002586// For example:
2587// <1, 1, 3, 3, 5, 5, ...>
2588// <1, n+1, 3, n+3, 5, n+5, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002589//
2590// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002591// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002592static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2593 SmallVector<int, 16> Indices,
2594 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002595 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002596
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002597 SDValue Wt;
2598 SDValue Ws;
2599 const auto &Begin = Indices.begin();
2600 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002601
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002602 // Check even elements are taken from the odd elements of one half or the
2603 // other and pick an operand accordingly.
2604 if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
2605 Wt = Op->getOperand(0);
2606 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + 1, 2))
2607 Wt = Op->getOperand(1);
2608 else
2609 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002610
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002611 // Check odd elements are taken from the odd elements of one half or the
2612 // other and pick an operand accordingly.
2613 if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
2614 Ws = Op->getOperand(0);
2615 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + 1, 2))
2616 Ws = Op->getOperand(1);
2617 else
2618 return SDValue();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002619
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002620 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Wt, Ws);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002621}
2622
2623// Lower VECTOR_SHUFFLE into ILVR (if possible).
2624//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002625// ILVR interleaves consecutive elements from the right (lowest-indexed) half of
2626// each vector.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002627//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002628// It is possible to lower into ILVR when the mask consists of two of the
2629// following forms interleaved:
2630// <0, 1, 2, ...>
2631// <n, n+1, n+2, ...>
2632// where n is the number of elements in the vector.
2633// For example:
2634// <0, 0, 1, 1, 2, 2, ...>
2635// <0, n, 1, n+1, 2, n+2, ...>
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002636//
2637// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002638// value is necessary in order to fit the above forms.
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002639static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2640 SmallVector<int, 16> Indices,
2641 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002642 assert((Indices.size() % 2) == 0);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002643
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002644 SDValue Wt;
2645 SDValue Ws;
2646 const auto &Begin = Indices.begin();
2647 const auto &End = Indices.end();
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002648
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002649 // Check even elements are taken from the right (lowest-indexed) elements of
2650 // one half or the other and pick an operand accordingly.
2651 if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
2652 Wt = Op->getOperand(0);
2653 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 1))
2654 Wt = Op->getOperand(1);
2655 else
2656 return SDValue();
2657
2658 // Check odd elements are taken from the right (lowest-indexed) elements of
2659 // one half or the other and pick an operand accordingly.
2660 if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
2661 Ws = Op->getOperand(0);
2662 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 1))
2663 Ws = Op->getOperand(1);
2664 else
2665 return SDValue();
2666
2667 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Ws, Wt);
2668}
2669
2670// Lower VECTOR_SHUFFLE into ILVL (if possible).
2671//
2672// ILVL interleaves consecutive elements from the left (highest-indexed) half
2673// of each vector.
2674//
2675// It is possible to lower into ILVL when the mask consists of two of the
2676// following forms interleaved:
2677// <x, x+1, x+2, ...>
2678// <n+x, n+x+1, n+x+2, ...>
2679// where n is the number of elements in the vector and x is half n.
2680// For example:
2681// <x, x, x+1, x+1, x+2, x+2, ...>
2682// <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2683//
2684// When undef's appear in the mask they are treated as if they were whatever
2685// value is necessary in order to fit the above forms.
2686static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2687 SmallVector<int, 16> Indices,
2688 SelectionDAG &DAG) {
2689 assert((Indices.size() % 2) == 0);
2690
2691 unsigned HalfSize = Indices.size() / 2;
2692 SDValue Wt;
2693 SDValue Ws;
2694 const auto &Begin = Indices.begin();
2695 const auto &End = Indices.end();
2696
2697 // Check even elements are taken from the left (highest-indexed) elements of
2698 // one half or the other and pick an operand accordingly.
2699 if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
2700 Wt = Op->getOperand(0);
2701 else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + HalfSize, 1))
2702 Wt = Op->getOperand(1);
2703 else
2704 return SDValue();
2705
2706 // Check odd elements are taken from the left (highest-indexed) elements of
2707 // one half or the other and pick an operand accordingly.
2708 if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
2709 Ws = Op->getOperand(0);
2710 else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + HalfSize,
2711 1))
2712 Ws = Op->getOperand(1);
2713 else
2714 return SDValue();
2715
2716 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002717}
2718
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002719// Lower VECTOR_SHUFFLE into PCKEV (if possible).
2720//
2721// PCKEV copies the even elements of each vector into the result vector.
2722//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002723// It is possible to lower into PCKEV when the mask consists of two of the
2724// following forms concatenated:
2725// <0, 2, 4, ...>
2726// <n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002727// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002728// For example:
2729// <0, 2, 4, ..., 0, 2, 4, ...>
2730// <0, 2, 4, ..., n, n+2, n+4, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002731//
2732// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002733// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002734static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2735 SmallVector<int, 16> Indices,
2736 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002737 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002738
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002739 SDValue Wt;
2740 SDValue Ws;
2741 const auto &Begin = Indices.begin();
2742 const auto &Mid = Indices.begin() + Indices.size() / 2;
2743 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002744
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002745 if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
2746 Wt = Op->getOperand(0);
2747 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size(), 2))
2748 Wt = Op->getOperand(1);
2749 else
2750 return SDValue();
2751
2752 if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
2753 Ws = Op->getOperand(0);
2754 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size(), 2))
2755 Ws = Op->getOperand(1);
2756 else
2757 return SDValue();
2758
2759 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002760}
2761
2762// Lower VECTOR_SHUFFLE into PCKOD (if possible).
2763//
2764// PCKOD copies the odd elements of each vector into the result vector.
2765//
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002766// It is possible to lower into PCKOD when the mask consists of two of the
2767// following forms concatenated:
2768// <1, 3, 5, ...>
2769// <n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002770// where n is the number of elements in the vector.
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002771// For example:
2772// <1, 3, 5, ..., 1, 3, 5, ...>
2773// <1, 3, 5, ..., n+1, n+3, n+5, ...>
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002774//
2775// When undef's appear in the mask they are treated as if they were whatever
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002776// value is necessary in order to fit the above forms.
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002777static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2778 SmallVector<int, 16> Indices,
2779 SelectionDAG &DAG) {
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002780 assert((Indices.size() % 2) == 0);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002781
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002782 SDValue Wt;
2783 SDValue Ws;
2784 const auto &Begin = Indices.begin();
2785 const auto &Mid = Indices.begin() + Indices.size() / 2;
2786 const auto &End = Indices.end();
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002787
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002788 if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
2789 Wt = Op->getOperand(0);
2790 else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size() + 1, 2))
2791 Wt = Op->getOperand(1);
2792 else
2793 return SDValue();
2794
2795 if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
2796 Ws = Op->getOperand(0);
2797 else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size() + 1, 2))
2798 Ws = Op->getOperand(1);
2799 else
2800 return SDValue();
2801
2802 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Ws, Wt);
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002803}
2804
Daniel Sanderse5087042013-09-24 14:02:15 +00002805// Lower VECTOR_SHUFFLE into VSHF.
2806//
2807// This mostly consists of converting the shuffle indices in Indices into a
2808// BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2809// also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2810// if the type is v8i16 and all the indices are less than 8 then the second
2811// operand is unused and can be replaced with anything. We choose to replace it
2812// with the used operand since this reduces the number of instructions overall.
2813static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2814 SmallVector<int, 16> Indices,
2815 SelectionDAG &DAG) {
2816 SmallVector<SDValue, 16> Ops;
2817 SDValue Op0;
2818 SDValue Op1;
2819 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2820 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2821 bool Using1stVec = false;
2822 bool Using2ndVec = false;
2823 SDLoc DL(Op);
2824 int ResTyNumElts = ResTy.getVectorNumElements();
2825
2826 for (int i = 0; i < ResTyNumElts; ++i) {
2827 // Idx == -1 means UNDEF
2828 int Idx = Indices[i];
2829
2830 if (0 <= Idx && Idx < ResTyNumElts)
2831 Using1stVec = true;
2832 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2833 Using2ndVec = true;
2834 }
2835
2836 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2837 ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002838 Ops.push_back(DAG.getTargetConstant(*I, DL, MaskEltTy));
Daniel Sanderse5087042013-09-24 14:02:15 +00002839
Craig Topper48d114b2014-04-26 18:35:24 +00002840 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, Ops);
Daniel Sanderse5087042013-09-24 14:02:15 +00002841
2842 if (Using1stVec && Using2ndVec) {
2843 Op0 = Op->getOperand(0);
2844 Op1 = Op->getOperand(1);
2845 } else if (Using1stVec)
2846 Op0 = Op1 = Op->getOperand(0);
2847 else if (Using2ndVec)
2848 Op0 = Op1 = Op->getOperand(1);
2849 else
2850 llvm_unreachable("shuffle vector mask references neither vector operand?");
2851
Daniel Sandersf88a29e2014-03-21 16:56:51 +00002852 // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
2853 // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
2854 // VSHF concatenates the vectors in a bitwise fashion:
2855 // <0b00, 0b01> + <0b10, 0b11> ->
2856 // 0b0100 + 0b1110 -> 0b01001110
2857 // <0b10, 0b11, 0b00, 0b01>
2858 // We must therefore swap the operands to get the correct result.
2859 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
Daniel Sanderse5087042013-09-24 14:02:15 +00002860}
2861
2862// Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2863// indices in the shuffle.
2864SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2865 SelectionDAG &DAG) const {
2866 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2867 EVT ResTy = Op->getValueType(0);
2868
2869 if (!ResTy.is128BitVector())
2870 return SDValue();
2871
2872 int ResTyNumElts = ResTy.getVectorNumElements();
2873 SmallVector<int, 16> Indices;
2874
2875 for (int i = 0; i < ResTyNumElts; ++i)
2876 Indices.push_back(Node->getMaskElt(i));
2877
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002878 // splati.[bhwd] is preferable to the others but is matched from
2879 // MipsISD::VSHF.
2880 if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
2881 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2882 SDValue Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
Daniel Sanders2ed228b2013-09-24 14:36:12 +00002883 if (Result.getNode())
2884 return Result;
2885 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2886 if (Result.getNode())
2887 return Result;
2888 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2889 if (Result.getNode())
2890 return Result;
2891 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2892 if (Result.getNode())
2893 return Result;
Daniel Sandersfae5f2a2013-09-24 14:53:25 +00002894 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2895 if (Result.getNode())
2896 return Result;
2897 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2898 if (Result.getNode())
2899 return Result;
Daniel Sandersc8cd58f2015-05-19 12:24:52 +00002900 Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2901 if (Result.getNode())
2902 return Result;
Daniel Sanderse5087042013-09-24 14:02:15 +00002903 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2904}
2905
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002906MachineBasicBlock * MipsSETargetLowering::
2907emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2908 // $bb:
2909 // bposge32_pseudo $vr0
2910 // =>
2911 // $bb:
2912 // bposge32 $tbb
2913 // $fbb:
2914 // li $vr2, 0
2915 // b $sink
2916 // $tbb:
2917 // li $vr1, 1
2918 // $sink:
2919 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2920
2921 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002922 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002923 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002924 DebugLoc DL = MI->getDebugLoc();
2925 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002926 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002927 MachineFunction *F = BB->getParent();
2928 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2929 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2930 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2931 F->insert(It, FBB);
2932 F->insert(It, TBB);
2933 F->insert(It, Sink);
2934
2935 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002936 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002937 BB->end());
2938 Sink->transferSuccessorsAndUpdatePHIs(BB);
2939
2940 // Add successors.
2941 BB->addSuccessor(FBB);
2942 BB->addSuccessor(TBB);
2943 FBB->addSuccessor(Sink);
2944 TBB->addSuccessor(Sink);
2945
2946 // Insert the real bposge32 instruction to $BB.
2947 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2948
2949 // Fill $FBB.
2950 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2951 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2952 .addReg(Mips::ZERO).addImm(0);
2953 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2954
2955 // Fill $TBB.
2956 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2957 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2958 .addReg(Mips::ZERO).addImm(1);
2959
2960 // Insert phi function to $Sink.
2961 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2962 MI->getOperand(0).getReg())
2963 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2964
2965 MI->eraseFromParent(); // The pseudo instruction is gone now.
2966 return Sink;
2967}
Daniel Sandersce09d072013-08-28 12:14:50 +00002968
2969MachineBasicBlock * MipsSETargetLowering::
2970emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2971 unsigned BranchOp) const{
2972 // $bb:
2973 // vany_nonzero $rd, $ws
2974 // =>
2975 // $bb:
2976 // bnz.b $ws, $tbb
2977 // b $fbb
2978 // $fbb:
2979 // li $rd1, 0
2980 // b $sink
2981 // $tbb:
2982 // li $rd2, 1
2983 // $sink:
2984 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2985
2986 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002987 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersce09d072013-08-28 12:14:50 +00002988 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2989 DebugLoc DL = MI->getDebugLoc();
2990 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002991 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
Daniel Sandersce09d072013-08-28 12:14:50 +00002992 MachineFunction *F = BB->getParent();
2993 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2994 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2995 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2996 F->insert(It, FBB);
2997 F->insert(It, TBB);
2998 F->insert(It, Sink);
2999
3000 // Transfer the remainder of BB and its successor edges to Sink.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003001 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
Daniel Sandersce09d072013-08-28 12:14:50 +00003002 BB->end());
3003 Sink->transferSuccessorsAndUpdatePHIs(BB);
3004
3005 // Add successors.
3006 BB->addSuccessor(FBB);
3007 BB->addSuccessor(TBB);
3008 FBB->addSuccessor(Sink);
3009 TBB->addSuccessor(Sink);
3010
3011 // Insert the real bnz.b instruction to $BB.
3012 BuildMI(BB, DL, TII->get(BranchOp))
3013 .addReg(MI->getOperand(1).getReg())
3014 .addMBB(TBB);
3015
3016 // Fill $FBB.
3017 unsigned RD1 = RegInfo.createVirtualRegister(RC);
3018 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
3019 .addReg(Mips::ZERO).addImm(0);
3020 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
3021
3022 // Fill $TBB.
3023 unsigned RD2 = RegInfo.createVirtualRegister(RC);
3024 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
3025 .addReg(Mips::ZERO).addImm(1);
3026
3027 // Insert phi function to $Sink.
3028 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
3029 MI->getOperand(0).getReg())
3030 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
3031
3032 MI->eraseFromParent(); // The pseudo instruction is gone now.
3033 return Sink;
3034}
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003035
3036// Emit the COPY_FW pseudo instruction.
3037//
3038// copy_fw_pseudo $fd, $ws, n
3039// =>
3040// copy_u_w $rt, $ws, $n
3041// mtc1 $rt, $fd
3042//
3043// When n is zero, the equivalent operation can be performed with (potentially)
3044// zero instructions due to register overlaps. This optimization is never valid
3045// for lane 1 because it would require FR=0 mode which isn't supported by MSA.
3046MachineBasicBlock * MipsSETargetLowering::
3047emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
Eric Christopher96e72c62015-01-29 23:27:36 +00003048 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003049 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3050 DebugLoc DL = MI->getDebugLoc();
3051 unsigned Fd = MI->getOperand(0).getReg();
3052 unsigned Ws = MI->getOperand(1).getReg();
3053 unsigned Lane = MI->getOperand(2).getImm();
3054
Daniel Sandersafe27c72015-02-23 17:22:16 +00003055 if (Lane == 0) {
3056 unsigned Wt = Ws;
3057 if (!Subtarget.useOddSPReg()) {
3058 // We must copy to an even-numbered MSA register so that the
3059 // single-precision sub-register is also guaranteed to be even-numbered.
3060 Wt = RegInfo.createVirtualRegister(&Mips::MSA128WEvensRegClass);
3061
3062 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Wt).addReg(Ws);
3063 }
3064
3065 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3066 } else {
3067 unsigned Wt = RegInfo.createVirtualRegister(
3068 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3069 &Mips::MSA128WEvensRegClass);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003070
Daniel Sandersd9207702014-03-04 13:54:30 +00003071 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003072 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
3073 }
3074
3075 MI->eraseFromParent(); // The pseudo instruction is gone now.
3076 return BB;
3077}
3078
3079// Emit the COPY_FD pseudo instruction.
3080//
3081// copy_fd_pseudo $fd, $ws, n
3082// =>
3083// splati.d $wt, $ws, $n
3084// copy $fd, $wt:sub_64
3085//
3086// When n is zero, the equivalent operation can be performed with (potentially)
3087// zero instructions due to register overlaps. This optimization is always
3088// valid because FR=1 mode which is the only supported mode in MSA.
3089MachineBasicBlock * MipsSETargetLowering::
3090emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
Eric Christopher1c29a652014-07-18 22:55:25 +00003091 assert(Subtarget.isFP64bit());
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003092
Eric Christopher96e72c62015-01-29 23:27:36 +00003093 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders39bb8ba2013-09-27 12:17:32 +00003094 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3095 unsigned Fd = MI->getOperand(0).getReg();
3096 unsigned Ws = MI->getOperand(1).getReg();
3097 unsigned Lane = MI->getOperand(2).getImm() * 2;
3098 DebugLoc DL = MI->getDebugLoc();
3099
3100 if (Lane == 0)
3101 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
3102 else {
3103 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3104
3105 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
3106 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
3107 }
3108
3109 MI->eraseFromParent(); // The pseudo instruction is gone now.
3110 return BB;
3111}
Daniel Sandersa5150702013-09-27 12:31:32 +00003112
3113// Emit the INSERT_FW pseudo instruction.
3114//
3115// insert_fw_pseudo $wd, $wd_in, $n, $fs
3116// =>
3117// subreg_to_reg $wt:sub_lo, $fs
3118// insve_w $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003119MachineBasicBlock *
3120MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
3121 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003122 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003123 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3124 DebugLoc DL = MI->getDebugLoc();
3125 unsigned Wd = MI->getOperand(0).getReg();
3126 unsigned Wd_in = MI->getOperand(1).getReg();
3127 unsigned Lane = MI->getOperand(2).getImm();
3128 unsigned Fs = MI->getOperand(3).getReg();
Daniel Sandersafe27c72015-02-23 17:22:16 +00003129 unsigned Wt = RegInfo.createVirtualRegister(
3130 Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
3131 &Mips::MSA128WEvensRegClass);
Daniel Sandersa5150702013-09-27 12:31:32 +00003132
3133 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003134 .addImm(0)
3135 .addReg(Fs)
3136 .addImm(Mips::sub_lo);
Daniel Sandersa5150702013-09-27 12:31:32 +00003137 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003138 .addReg(Wd_in)
3139 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003140 .addReg(Wt)
3141 .addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003142
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003143 MI->eraseFromParent(); // The pseudo instruction is gone now.
Daniel Sandersa5150702013-09-27 12:31:32 +00003144 return BB;
3145}
3146
3147// Emit the INSERT_FD pseudo instruction.
3148//
3149// insert_fd_pseudo $wd, $fs, n
3150// =>
3151// subreg_to_reg $wt:sub_64, $fs
3152// insve_d $wd[$n], $wd_in, $wt[0]
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003153MachineBasicBlock *
3154MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
3155 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003156 assert(Subtarget.isFP64bit());
Daniel Sandersa5150702013-09-27 12:31:32 +00003157
Eric Christopher96e72c62015-01-29 23:27:36 +00003158 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa5150702013-09-27 12:31:32 +00003159 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3160 DebugLoc DL = MI->getDebugLoc();
3161 unsigned Wd = MI->getOperand(0).getReg();
3162 unsigned Wd_in = MI->getOperand(1).getReg();
3163 unsigned Lane = MI->getOperand(2).getImm();
3164 unsigned Fs = MI->getOperand(3).getReg();
3165 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3166
3167 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003168 .addImm(0)
3169 .addReg(Fs)
3170 .addImm(Mips::sub_64);
Daniel Sandersa5150702013-09-27 12:31:32 +00003171 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003172 .addReg(Wd_in)
3173 .addImm(Lane)
Daniel Sandersb50ccf82014-04-01 10:35:28 +00003174 .addReg(Wt)
3175 .addImm(0);
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003176
3177 MI->eraseFromParent(); // The pseudo instruction is gone now.
3178 return BB;
3179}
3180
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003181// Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
3182//
3183// For integer:
3184// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
3185// =>
3186// (SLL $lanetmp1, $lane, <log2size)
3187// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3188// (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
3189// (NEG $lanetmp2, $lanetmp1)
3190// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3191//
3192// For floating point:
3193// (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
3194// =>
3195// (SUBREG_TO_REG $wt, $fs, <subreg>)
3196// (SLL $lanetmp1, $lane, <log2size)
3197// (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
3198// (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
3199// (NEG $lanetmp2, $lanetmp1)
3200// (SLD_B $wd, $wdtmp2, $wdtmp2, $lanetmp2)
3201MachineBasicBlock *
3202MipsSETargetLowering::emitINSERT_DF_VIDX(MachineInstr *MI,
3203 MachineBasicBlock *BB,
3204 unsigned EltSizeInBytes,
3205 bool IsFP) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003206 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003207 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3208 DebugLoc DL = MI->getDebugLoc();
3209 unsigned Wd = MI->getOperand(0).getReg();
3210 unsigned SrcVecReg = MI->getOperand(1).getReg();
3211 unsigned LaneReg = MI->getOperand(2).getReg();
3212 unsigned SrcValReg = MI->getOperand(3).getReg();
3213
3214 const TargetRegisterClass *VecRC = nullptr;
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003215 const TargetRegisterClass *GPRRC =
Daniel Sanders4160c802015-05-05 08:48:35 +00003216 Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003217 unsigned EltLog2Size;
3218 unsigned InsertOp = 0;
3219 unsigned InsveOp = 0;
3220 switch (EltSizeInBytes) {
3221 default:
3222 llvm_unreachable("Unexpected size");
3223 case 1:
3224 EltLog2Size = 0;
3225 InsertOp = Mips::INSERT_B;
3226 InsveOp = Mips::INSVE_B;
3227 VecRC = &Mips::MSA128BRegClass;
3228 break;
3229 case 2:
3230 EltLog2Size = 1;
3231 InsertOp = Mips::INSERT_H;
3232 InsveOp = Mips::INSVE_H;
3233 VecRC = &Mips::MSA128HRegClass;
3234 break;
3235 case 4:
3236 EltLog2Size = 2;
3237 InsertOp = Mips::INSERT_W;
3238 InsveOp = Mips::INSVE_W;
3239 VecRC = &Mips::MSA128WRegClass;
3240 break;
3241 case 8:
3242 EltLog2Size = 3;
3243 InsertOp = Mips::INSERT_D;
3244 InsveOp = Mips::INSVE_D;
3245 VecRC = &Mips::MSA128DRegClass;
3246 break;
3247 }
3248
3249 if (IsFP) {
3250 unsigned Wt = RegInfo.createVirtualRegister(VecRC);
3251 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
3252 .addImm(0)
3253 .addReg(SrcValReg)
3254 .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
3255 SrcValReg = Wt;
3256 }
3257
3258 // Convert the lane index into a byte index
3259 if (EltSizeInBytes != 1) {
3260 unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
3261 BuildMI(*BB, MI, DL, TII->get(Mips::SLL), LaneTmp1)
3262 .addReg(LaneReg)
3263 .addImm(EltLog2Size);
3264 LaneReg = LaneTmp1;
3265 }
3266
3267 // Rotate bytes around so that the desired lane is element zero
3268 unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
3269 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
3270 .addReg(SrcVecReg)
3271 .addReg(SrcVecReg)
3272 .addReg(LaneReg);
3273
3274 unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
3275 if (IsFP) {
3276 // Use insve.df to insert to element zero
3277 BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
3278 .addReg(WdTmp1)
3279 .addImm(0)
3280 .addReg(SrcValReg)
3281 .addImm(0);
3282 } else {
3283 // Use insert.df to insert to element zero
3284 BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
3285 .addReg(WdTmp1)
3286 .addReg(SrcValReg)
3287 .addImm(0);
3288 }
3289
3290 // Rotate elements the rest of the way for a full rotation.
3291 // sld.df inteprets $rt modulo the number of columns so we only need to negate
3292 // the lane index to do this.
3293 unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
Daniel Sanders4160c802015-05-05 08:48:35 +00003294 BuildMI(*BB, MI, DL, TII->get(Subtarget.isABI_N64() ? Mips::DSUB : Mips::SUB),
3295 LaneTmp2)
3296 .addReg(Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO)
Daniel Sanderse296a0f2014-04-30 12:09:32 +00003297 .addReg(LaneReg);
3298 BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
3299 .addReg(WdTmp2)
3300 .addReg(WdTmp2)
3301 .addReg(LaneTmp2);
3302
3303 MI->eraseFromParent(); // The pseudo instruction is gone now.
3304 return BB;
3305}
3306
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003307// Emit the FILL_FW pseudo instruction.
3308//
3309// fill_fw_pseudo $wd, $fs
3310// =>
3311// implicit_def $wt1
3312// insert_subreg $wt2:subreg_lo, $wt1, $fs
3313// splati.w $wd, $wt2[0]
3314MachineBasicBlock *
3315MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
3316 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003317 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003318 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3319 DebugLoc DL = MI->getDebugLoc();
3320 unsigned Wd = MI->getOperand(0).getReg();
3321 unsigned Fs = MI->getOperand(1).getReg();
3322 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3323 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
3324
3325 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3326 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3327 .addReg(Wt1)
3328 .addReg(Fs)
3329 .addImm(Mips::sub_lo);
3330 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
3331
3332 MI->eraseFromParent(); // The pseudo instruction is gone now.
3333 return BB;
3334}
3335
3336// Emit the FILL_FD pseudo instruction.
3337//
3338// fill_fd_pseudo $wd, $fs
3339// =>
3340// implicit_def $wt1
3341// insert_subreg $wt2:subreg_64, $wt1, $fs
3342// splati.d $wd, $wt2[0]
3343MachineBasicBlock *
3344MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
3345 MachineBasicBlock *BB) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003346 assert(Subtarget.isFP64bit());
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003347
Eric Christopher96e72c62015-01-29 23:27:36 +00003348 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders1dfddc72013-10-15 13:14:41 +00003349 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3350 DebugLoc DL = MI->getDebugLoc();
3351 unsigned Wd = MI->getOperand(0).getReg();
3352 unsigned Fs = MI->getOperand(1).getReg();
3353 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3354 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
3355
3356 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
3357 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
3358 .addReg(Wt1)
3359 .addReg(Fs)
3360 .addImm(Mips::sub_64);
3361 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
Daniel Sandersa5150702013-09-27 12:31:32 +00003362
3363 MI->eraseFromParent(); // The pseudo instruction is gone now.
3364 return BB;
3365}
Daniel Sandersa9521602013-10-23 10:36:52 +00003366
3367// Emit the FEXP2_W_1 pseudo instructions.
3368//
3369// fexp2_w_1_pseudo $wd, $wt
3370// =>
3371// ldi.w $ws, 1
3372// fexp2.w $wd, $ws, $wt
3373MachineBasicBlock *
3374MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
3375 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003376 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003377 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3378 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
3379 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3380 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
3381 DebugLoc DL = MI->getDebugLoc();
3382
3383 // Splat 1.0 into a vector
3384 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
3385 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
3386
3387 // Emit 1.0 * fexp2(Wt)
3388 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
3389 .addReg(Ws2)
3390 .addReg(MI->getOperand(1).getReg());
3391
3392 MI->eraseFromParent(); // The pseudo instruction is gone now.
3393 return BB;
3394}
3395
3396// Emit the FEXP2_D_1 pseudo instructions.
3397//
3398// fexp2_d_1_pseudo $wd, $wt
3399// =>
3400// ldi.d $ws, 1
3401// fexp2.d $wd, $ws, $wt
3402MachineBasicBlock *
3403MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
3404 MachineBasicBlock *BB) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003405 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sandersa9521602013-10-23 10:36:52 +00003406 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
3407 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
3408 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
3409 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
3410 DebugLoc DL = MI->getDebugLoc();
3411
3412 // Splat 1.0 into a vector
3413 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
3414 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
3415
3416 // Emit 1.0 * fexp2(Wt)
3417 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
3418 .addReg(Ws2)
3419 .addReg(MI->getOperand(1).getReg());
3420
3421 MI->eraseFromParent(); // The pseudo instruction is gone now.
3422 return BB;
3423}