blob: 031d068c543bdcb0b2f3c3c8ee39aa3d5b2ffd02 [file] [log] [blame]
Scott Michel8efdca42007-12-04 22:23:35 +00001//===-- SPUISelLowering.cpp - Cell SPU DAG Lowering Implementation --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Scott Michel8efdca42007-12-04 22:23:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SPUTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPURegisterNames.h"
15#include "SPUISelLowering.h"
16#include "SPUTargetMachine.h"
Scott Michelbc5fbc12008-04-30 00:30:08 +000017#include "SPUFrameInfo.h"
Scott Michel8efdca42007-12-04 22:23:35 +000018#include "llvm/ADT/VectorExtras.h"
Scott Michel8efdca42007-12-04 22:23:35 +000019#include "llvm/CodeGen/CallingConvLower.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Scott Michel8efdca42007-12-04 22:23:35 +000024#include "llvm/CodeGen/SelectionDAG.h"
Scott Michel8efdca42007-12-04 22:23:35 +000025#include "llvm/Constants.h"
26#include "llvm/Function.h"
27#include "llvm/Intrinsics.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
30#include "llvm/Target/TargetOptions.h"
Scott Michel2c261072008-12-09 03:37:19 +000031#include "llvm/CodeGen/SchedulerRegistry.h"
Scott Michel8efdca42007-12-04 22:23:35 +000032
33#include <map>
34
35using namespace llvm;
36
37// Used in getTargetNodeName() below
38namespace {
39 std::map<unsigned, const char *> node_names;
40
Duncan Sands92c43912008-06-06 12:08:01 +000041 //! MVT mapping to useful data for Cell SPU
Scott Michel8efdca42007-12-04 22:23:35 +000042 struct valtype_map_s {
Scott Michel56a125e2008-11-22 23:50:42 +000043 const MVT valtype;
44 const int prefslot_byte;
Scott Michel8efdca42007-12-04 22:23:35 +000045 };
Scott Michel4ec722e2008-07-16 17:17:29 +000046
Scott Michel8efdca42007-12-04 22:23:35 +000047 const valtype_map_s valtype_map[] = {
48 { MVT::i1, 3 },
49 { MVT::i8, 3 },
50 { MVT::i16, 2 },
51 { MVT::i32, 0 },
52 { MVT::f32, 0 },
53 { MVT::i64, 0 },
54 { MVT::f64, 0 },
55 { MVT::i128, 0 }
56 };
57
58 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
59
Duncan Sands92c43912008-06-06 12:08:01 +000060 const valtype_map_s *getValueTypeMapEntry(MVT VT) {
Scott Michel8efdca42007-12-04 22:23:35 +000061 const valtype_map_s *retval = 0;
62
63 for (size_t i = 0; i < n_valtype_map; ++i) {
64 if (valtype_map[i].valtype == VT) {
Scott Michel5a6f17b2008-01-30 02:55:46 +000065 retval = valtype_map + i;
66 break;
Scott Michel8efdca42007-12-04 22:23:35 +000067 }
68 }
69
70#ifndef NDEBUG
71 if (retval == 0) {
72 cerr << "getValueTypeMapEntry returns NULL for "
Duncan Sands92c43912008-06-06 12:08:01 +000073 << VT.getMVTString()
Scott Michel5a6f17b2008-01-30 02:55:46 +000074 << "\n";
Scott Michel8efdca42007-12-04 22:23:35 +000075 abort();
76 }
77#endif
78
79 return retval;
80 }
81
82 //! Predicate that returns true if operand is a memory target
83 /*!
84 \arg Op Operand to test
85 \return true if the operand is a memory target (i.e., global
Scott Micheldbac4cf2008-01-11 02:53:15 +000086 address, external symbol, constant pool) or an A-form
Scott Michel8efdca42007-12-04 22:23:35 +000087 address.
88 */
Dan Gohman8181bd12008-07-27 21:46:04 +000089 bool isMemoryOperand(const SDValue &Op)
Scott Michel8efdca42007-12-04 22:23:35 +000090 {
91 const unsigned Opc = Op.getOpcode();
92 return (Opc == ISD::GlobalAddress
93 || Opc == ISD::GlobalTLSAddress
Scott Michel8efdca42007-12-04 22:23:35 +000094 || Opc == ISD::JumpTable
95 || Opc == ISD::ConstantPool
Bill Wendlingfef06052008-09-16 21:48:12 +000096 || Opc == ISD::ExternalSymbol
Scott Michel8efdca42007-12-04 22:23:35 +000097 || Opc == ISD::TargetGlobalAddress
98 || Opc == ISD::TargetGlobalTLSAddress
Scott Michel8efdca42007-12-04 22:23:35 +000099 || Opc == ISD::TargetJumpTable
100 || Opc == ISD::TargetConstantPool
Bill Wendlingfef06052008-09-16 21:48:12 +0000101 || Opc == ISD::TargetExternalSymbol
Scott Micheldbac4cf2008-01-11 02:53:15 +0000102 || Opc == SPUISD::AFormAddr);
Scott Michel8efdca42007-12-04 22:23:35 +0000103 }
Scott Michel394e26d2008-01-17 20:38:41 +0000104
105 //! Predicate that returns true if the operand is an indirect target
Dan Gohman8181bd12008-07-27 21:46:04 +0000106 bool isIndirectOperand(const SDValue &Op)
Scott Michel394e26d2008-01-17 20:38:41 +0000107 {
108 const unsigned Opc = Op.getOpcode();
109 return (Opc == ISD::Register
Scott Michel5a6f17b2008-01-30 02:55:46 +0000110 || Opc == SPUISD::LDRESULT);
Scott Michel394e26d2008-01-17 20:38:41 +0000111 }
Scott Michel8efdca42007-12-04 22:23:35 +0000112}
113
114SPUTargetLowering::SPUTargetLowering(SPUTargetMachine &TM)
115 : TargetLowering(TM),
116 SPUTM(TM)
117{
118 // Fold away setcc operations if possible.
119 setPow2DivIsCheap();
120
121 // Use _setjmp/_longjmp instead of setjmp/longjmp.
122 setUseUnderscoreSetJmp(true);
123 setUseUnderscoreLongJmp(true);
Scott Michel4ec722e2008-07-16 17:17:29 +0000124
Scott Michel8efdca42007-12-04 22:23:35 +0000125 // Set up the SPU's register classes:
Scott Michel438be252007-12-17 22:32:34 +0000126 addRegisterClass(MVT::i8, SPU::R8CRegisterClass);
127 addRegisterClass(MVT::i16, SPU::R16CRegisterClass);
128 addRegisterClass(MVT::i32, SPU::R32CRegisterClass);
129 addRegisterClass(MVT::i64, SPU::R64CRegisterClass);
130 addRegisterClass(MVT::f32, SPU::R32FPRegisterClass);
131 addRegisterClass(MVT::f64, SPU::R64FPRegisterClass);
Scott Michel8efdca42007-12-04 22:23:35 +0000132 addRegisterClass(MVT::i128, SPU::GPRCRegisterClass);
Scott Michel4ec722e2008-07-16 17:17:29 +0000133
Scott Michelabb8ca12008-11-20 16:36:33 +0000134 // Initialize libcalls:
135 setLibcallName(RTLIB::MUL_I64, "__muldi3");
136
Scott Michel8efdca42007-12-04 22:23:35 +0000137 // SPU has no sign or zero extended loads for i1, i8, i16:
Evan Cheng08c171a2008-10-14 21:26:46 +0000138 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
139 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
140 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
Scott Michel8efdca42007-12-04 22:23:35 +0000141
Evan Cheng08c171a2008-10-14 21:26:46 +0000142 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
143 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
144 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
Scott Michel55252632008-11-20 04:26:21 +0000145 setTruncStoreAction(MVT::i8, MVT::i8, Custom);
146 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
147 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
148 setTruncStoreAction(MVT::i64, MVT::i8, Custom);
149 setTruncStoreAction(MVT::i128, MVT::i8, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000150
Evan Cheng08c171a2008-10-14 21:26:46 +0000151 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
152 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
153 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000154
Scott Michelec8c82e2008-12-02 19:53:53 +0000155 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Custom);
156
Scott Michel8efdca42007-12-04 22:23:35 +0000157 // SPU constant load actions are custom lowered:
158 setOperationAction(ISD::Constant, MVT::i64, Custom);
Nate Begeman78125042008-02-14 18:43:04 +0000159 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
Scott Michel8efdca42007-12-04 22:23:35 +0000160 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
161
162 // SPU's loads and stores have to be custom lowered:
Scott Michele1006032008-11-19 17:45:08 +0000163 for (unsigned sctype = (unsigned) MVT::i8; sctype < (unsigned) MVT::f128;
Scott Michel8efdca42007-12-04 22:23:35 +0000164 ++sctype) {
Duncan Sands92c43912008-06-06 12:08:01 +0000165 MVT VT = (MVT::SimpleValueType)sctype;
166
167 setOperationAction(ISD::LOAD, VT, Custom);
168 setOperationAction(ISD::STORE, VT, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000169 }
170
Scott Michel33d73eb2008-11-21 02:56:16 +0000171 // Custom lower BRCOND for i8 to "promote" the result to i16
Scott Michel394e26d2008-01-17 20:38:41 +0000172 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000173
174 // Expand the jumptable branches
175 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
176 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Scott Michel56a125e2008-11-22 23:50:42 +0000177
178 // Custom lower SELECT_CC for most cases, but expand by default
Scott Michel4ec722e2008-07-16 17:17:29 +0000179 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Scott Michel56a125e2008-11-22 23:50:42 +0000180 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
181 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
182 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Scott Michelfa888632008-11-25 00:23:16 +0000183#if 0
Scott Michel56a125e2008-11-22 23:50:42 +0000184 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Scott Michelfa888632008-11-25 00:23:16 +0000185#endif
Scott Michel8efdca42007-12-04 22:23:35 +0000186
187 // SPU has no intrinsics for these particular operations:
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000188 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
189
Scott Michel8efdca42007-12-04 22:23:35 +0000190 // PowerPC has no SREM/UREM instructions
191 setOperationAction(ISD::SREM, MVT::i32, Expand);
192 setOperationAction(ISD::UREM, MVT::i32, Expand);
193 setOperationAction(ISD::SREM, MVT::i64, Expand);
194 setOperationAction(ISD::UREM, MVT::i64, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000195
Scott Michel8efdca42007-12-04 22:23:35 +0000196 // We don't support sin/cos/sqrt/fmod
197 setOperationAction(ISD::FSIN , MVT::f64, Expand);
198 setOperationAction(ISD::FCOS , MVT::f64, Expand);
199 setOperationAction(ISD::FREM , MVT::f64, Expand);
200 setOperationAction(ISD::FSIN , MVT::f32, Expand);
201 setOperationAction(ISD::FCOS , MVT::f32, Expand);
202 setOperationAction(ISD::FREM , MVT::f32, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000203
Scott Michel8efdca42007-12-04 22:23:35 +0000204 // If we're enabling GP optimizations, use hardware square root
205 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
206 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000207
Scott Michel8efdca42007-12-04 22:23:35 +0000208 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
209 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
210
211 // SPU can do rotate right and left, so legalize it... but customize for i8
212 // because instructions don't exist.
Bill Wendling965299c2008-08-31 02:59:23 +0000213
214 // FIXME: Change from "expand" to appropriate type once ROTR is supported in
215 // .td files.
216 setOperationAction(ISD::ROTR, MVT::i32, Expand /*Legal*/);
217 setOperationAction(ISD::ROTR, MVT::i16, Expand /*Legal*/);
218 setOperationAction(ISD::ROTR, MVT::i8, Expand /*Custom*/);
219
Scott Michel8efdca42007-12-04 22:23:35 +0000220 setOperationAction(ISD::ROTL, MVT::i32, Legal);
221 setOperationAction(ISD::ROTL, MVT::i16, Legal);
222 setOperationAction(ISD::ROTL, MVT::i8, Custom);
Scott Michelabb8ca12008-11-20 16:36:33 +0000223
Scott Michel8efdca42007-12-04 22:23:35 +0000224 // SPU has no native version of shift left/right for i8
225 setOperationAction(ISD::SHL, MVT::i8, Custom);
226 setOperationAction(ISD::SRL, MVT::i8, Custom);
227 setOperationAction(ISD::SRA, MVT::i8, Custom);
Scott Michel33d73eb2008-11-21 02:56:16 +0000228
229 // SPU needs custom lowering for shift left/right for i64
Scott Michel97872d32008-02-23 18:41:37 +0000230 setOperationAction(ISD::SHL, MVT::i64, Custom);
231 setOperationAction(ISD::SRL, MVT::i64, Custom);
232 setOperationAction(ISD::SRA, MVT::i64, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000233
Scott Michel4ec722e2008-07-16 17:17:29 +0000234 // Custom lower i8, i32 and i64 multiplications
235 setOperationAction(ISD::MUL, MVT::i8, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000236 setOperationAction(ISD::MUL, MVT::i32, Custom);
Scott Michel33d73eb2008-11-21 02:56:16 +0000237 setOperationAction(ISD::MUL, MVT::i64, Expand); // libcall
238
239 // SMUL_LOHI, UMUL_LOHI
240 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
241 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
242 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Custom);
243 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000244
Scott Michel67224b22008-06-02 22:18:03 +0000245 // Need to custom handle (some) common i8, i64 math ops
246 setOperationAction(ISD::ADD, MVT::i64, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000247 setOperationAction(ISD::SUB, MVT::i8, Custom);
Scott Michel67224b22008-06-02 22:18:03 +0000248 setOperationAction(ISD::SUB, MVT::i64, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000249
Scott Michel8efdca42007-12-04 22:23:35 +0000250 // SPU does not have BSWAP. It does have i32 support CTLZ.
251 // CTPOP has to be custom lowered.
252 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
253 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
254
255 setOperationAction(ISD::CTPOP, MVT::i8, Custom);
256 setOperationAction(ISD::CTPOP, MVT::i16, Custom);
257 setOperationAction(ISD::CTPOP, MVT::i32, Custom);
258 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
259
260 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
261 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
262
263 setOperationAction(ISD::CTLZ , MVT::i32, Legal);
Scott Michel4ec722e2008-07-16 17:17:29 +0000264
Scott Michel67224b22008-06-02 22:18:03 +0000265 // SPU has a version of select that implements (a&~c)|(b&c), just like
Scott Michel978b96f2008-03-10 23:49:09 +0000266 // select ought to work:
Scott Michel53ab7792008-03-10 16:58:52 +0000267 setOperationAction(ISD::SELECT, MVT::i8, Legal);
Scott Michel6baba072008-03-05 23:02:02 +0000268 setOperationAction(ISD::SELECT, MVT::i16, Legal);
269 setOperationAction(ISD::SELECT, MVT::i32, Legal);
Scott Michel8efdca42007-12-04 22:23:35 +0000270 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Scott Michel8efdca42007-12-04 22:23:35 +0000271
Scott Michel53ab7792008-03-10 16:58:52 +0000272 setOperationAction(ISD::SETCC, MVT::i8, Legal);
273 setOperationAction(ISD::SETCC, MVT::i16, Legal);
274 setOperationAction(ISD::SETCC, MVT::i32, Legal);
275 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Scott Michel6baba072008-03-05 23:02:02 +0000276
Scott Michel97872d32008-02-23 18:41:37 +0000277 // Zero extension and sign extension for i64 have to be
278 // custom legalized
279 setOperationAction(ISD::ZERO_EXTEND, MVT::i64, Custom);
280 setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom);
281 setOperationAction(ISD::ANY_EXTEND, MVT::i64, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000282
Scott Michelec8c82e2008-12-02 19:53:53 +0000283 // Custom lower truncates
284 setOperationAction(ISD::TRUNCATE, MVT::i8, Custom);
285 setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
286 setOperationAction(ISD::TRUNCATE, MVT::i32, Custom);
287 setOperationAction(ISD::TRUNCATE, MVT::i64, Custom);
288
Scott Michel8efdca42007-12-04 22:23:35 +0000289 // SPU has a legal FP -> signed INT instruction
290 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Legal);
291 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
292 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Legal);
293 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
294
295 // FDIV on SPU requires custom lowering
296 setOperationAction(ISD::FDIV, MVT::f32, Custom);
297 //setOperationAction(ISD::FDIV, MVT::f64, Custom);
298
299 // SPU has [U|S]INT_TO_FP
300 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Legal);
301 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
302 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
303 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Legal);
304 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
305 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
306 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
307 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
308
Scott Michel754d8662007-12-20 00:44:13 +0000309 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Legal);
310 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Legal);
311 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Legal);
312 setOperationAction(ISD::BIT_CONVERT, MVT::f64, Legal);
Scott Michel8efdca42007-12-04 22:23:35 +0000313
314 // We cannot sextinreg(i1). Expand to shifts.
315 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000316
Scott Michel8efdca42007-12-04 22:23:35 +0000317 // Support label based line numbers.
Dan Gohman472d12c2008-06-30 20:59:49 +0000318 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Scott Michel8efdca42007-12-04 22:23:35 +0000319 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000320
321 // We want to legalize GlobalAddress and ConstantPool nodes into the
Scott Michel8efdca42007-12-04 22:23:35 +0000322 // appropriate instructions to materialize the address.
Scott Michel33d73eb2008-11-21 02:56:16 +0000323 for (unsigned sctype = (unsigned) MVT::i8; sctype < (unsigned) MVT::f128;
Scott Michelf9f42e62008-01-29 02:16:57 +0000324 ++sctype) {
Duncan Sands92c43912008-06-06 12:08:01 +0000325 MVT VT = (MVT::SimpleValueType)sctype;
326
327 setOperationAction(ISD::GlobalAddress, VT, Custom);
328 setOperationAction(ISD::ConstantPool, VT, Custom);
329 setOperationAction(ISD::JumpTable, VT, Custom);
Scott Michelf9f42e62008-01-29 02:16:57 +0000330 }
Scott Michel8efdca42007-12-04 22:23:35 +0000331
332 // RET must be custom lowered, to meet ABI requirements
333 setOperationAction(ISD::RET, MVT::Other, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000334
Scott Michel8efdca42007-12-04 22:23:35 +0000335 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
336 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000337
Scott Michel8efdca42007-12-04 22:23:35 +0000338 // Use the default implementation.
339 setOperationAction(ISD::VAARG , MVT::Other, Expand);
340 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
341 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000342 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
Scott Michel8efdca42007-12-04 22:23:35 +0000343 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
344 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
345 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Expand);
346
347 // Cell SPU has instructions for converting between i64 and fp.
348 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
349 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000350
Scott Michel8efdca42007-12-04 22:23:35 +0000351 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
352 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
353
354 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
355 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
356
357 // First set operation action for all vector types to expand. Then we
358 // will selectively turn on ones that can be effectively codegen'd.
359 addRegisterClass(MVT::v16i8, SPU::VECREGRegisterClass);
360 addRegisterClass(MVT::v8i16, SPU::VECREGRegisterClass);
361 addRegisterClass(MVT::v4i32, SPU::VECREGRegisterClass);
362 addRegisterClass(MVT::v2i64, SPU::VECREGRegisterClass);
363 addRegisterClass(MVT::v4f32, SPU::VECREGRegisterClass);
364 addRegisterClass(MVT::v2f64, SPU::VECREGRegisterClass);
365
Duncan Sands92c43912008-06-06 12:08:01 +0000366 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
367 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
368 MVT VT = (MVT::SimpleValueType)i;
Scott Michel8efdca42007-12-04 22:23:35 +0000369
Duncan Sands92c43912008-06-06 12:08:01 +0000370 // add/sub are legal for all supported vector VT's.
371 setOperationAction(ISD::ADD , VT, Legal);
372 setOperationAction(ISD::SUB , VT, Legal);
373 // mul has to be custom lowered.
374 setOperationAction(ISD::MUL , VT, Custom);
375
376 setOperationAction(ISD::AND , VT, Legal);
377 setOperationAction(ISD::OR , VT, Legal);
378 setOperationAction(ISD::XOR , VT, Legal);
379 setOperationAction(ISD::LOAD , VT, Legal);
380 setOperationAction(ISD::SELECT, VT, Legal);
381 setOperationAction(ISD::STORE, VT, Legal);
Scott Michel4ec722e2008-07-16 17:17:29 +0000382
Scott Michel8efdca42007-12-04 22:23:35 +0000383 // These operations need to be expanded:
Duncan Sands92c43912008-06-06 12:08:01 +0000384 setOperationAction(ISD::SDIV, VT, Expand);
385 setOperationAction(ISD::SREM, VT, Expand);
386 setOperationAction(ISD::UDIV, VT, Expand);
387 setOperationAction(ISD::UREM, VT, Expand);
388 setOperationAction(ISD::FDIV, VT, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000389
390 // Custom lower build_vector, constant pool spills, insert and
391 // extract vector elements:
Duncan Sands92c43912008-06-06 12:08:01 +0000392 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
393 setOperationAction(ISD::ConstantPool, VT, Custom);
394 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
395 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
396 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
397 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000398 }
399
400 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
401 setOperationAction(ISD::AND, MVT::v16i8, Custom);
402 setOperationAction(ISD::OR, MVT::v16i8, Custom);
403 setOperationAction(ISD::XOR, MVT::v16i8, Custom);
404 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000405
Scott Michel8efdca42007-12-04 22:23:35 +0000406 setShiftAmountType(MVT::i32);
Duncan Sands8cf4a822008-11-23 15:47:28 +0000407 setBooleanContents(ZeroOrOneBooleanContent);
Scott Michel4ec722e2008-07-16 17:17:29 +0000408
Scott Michel8efdca42007-12-04 22:23:35 +0000409 setStackPointerRegisterToSaveRestore(SPU::R1);
Scott Michel4ec722e2008-07-16 17:17:29 +0000410
Scott Michel8efdca42007-12-04 22:23:35 +0000411 // We have target-specific dag combine patterns for the following nodes:
Scott Michelf9f42e62008-01-29 02:16:57 +0000412 setTargetDAGCombine(ISD::ADD);
Scott Michel97872d32008-02-23 18:41:37 +0000413 setTargetDAGCombine(ISD::ZERO_EXTEND);
414 setTargetDAGCombine(ISD::SIGN_EXTEND);
415 setTargetDAGCombine(ISD::ANY_EXTEND);
Scott Michel4ec722e2008-07-16 17:17:29 +0000416
Scott Michel8efdca42007-12-04 22:23:35 +0000417 computeRegisterProperties();
Scott Michel56a125e2008-11-22 23:50:42 +0000418
Scott Michel2c261072008-12-09 03:37:19 +0000419 // Set pre-RA register scheduler default to BURR, which produces slightly
420 // better code than the default (could also be TDRR, but TargetLowering.h
421 // needs a mod to support that model):
422 setSchedulingPreference(SchedulingForRegPressure);
Scott Michel8efdca42007-12-04 22:23:35 +0000423}
424
425const char *
426SPUTargetLowering::getTargetNodeName(unsigned Opcode) const
427{
428 if (node_names.empty()) {
429 node_names[(unsigned) SPUISD::RET_FLAG] = "SPUISD::RET_FLAG";
430 node_names[(unsigned) SPUISD::Hi] = "SPUISD::Hi";
431 node_names[(unsigned) SPUISD::Lo] = "SPUISD::Lo";
432 node_names[(unsigned) SPUISD::PCRelAddr] = "SPUISD::PCRelAddr";
Scott Micheldbac4cf2008-01-11 02:53:15 +0000433 node_names[(unsigned) SPUISD::AFormAddr] = "SPUISD::AFormAddr";
Scott Michelf9f42e62008-01-29 02:16:57 +0000434 node_names[(unsigned) SPUISD::IndirectAddr] = "SPUISD::IndirectAddr";
Scott Michel8efdca42007-12-04 22:23:35 +0000435 node_names[(unsigned) SPUISD::LDRESULT] = "SPUISD::LDRESULT";
436 node_names[(unsigned) SPUISD::CALL] = "SPUISD::CALL";
437 node_names[(unsigned) SPUISD::SHUFB] = "SPUISD::SHUFB";
Scott Michel56a125e2008-11-22 23:50:42 +0000438 node_names[(unsigned) SPUISD::SHUFFLE_MASK] = "SPUISD::SHUFFLE_MASK";
Scott Michel8efdca42007-12-04 22:23:35 +0000439 node_names[(unsigned) SPUISD::CNTB] = "SPUISD::CNTB";
440 node_names[(unsigned) SPUISD::PROMOTE_SCALAR] = "SPUISD::PROMOTE_SCALAR";
Scott Michelc630c412008-11-24 17:11:17 +0000441 node_names[(unsigned) SPUISD::VEC2PREFSLOT] = "SPUISD::VEC2PREFSLOT";
Scott Michel8efdca42007-12-04 22:23:35 +0000442 node_names[(unsigned) SPUISD::MPY] = "SPUISD::MPY";
443 node_names[(unsigned) SPUISD::MPYU] = "SPUISD::MPYU";
444 node_names[(unsigned) SPUISD::MPYH] = "SPUISD::MPYH";
445 node_names[(unsigned) SPUISD::MPYHH] = "SPUISD::MPYHH";
Scott Michel97872d32008-02-23 18:41:37 +0000446 node_names[(unsigned) SPUISD::SHLQUAD_L_BITS] = "SPUISD::SHLQUAD_L_BITS";
447 node_names[(unsigned) SPUISD::SHLQUAD_L_BYTES] = "SPUISD::SHLQUAD_L_BYTES";
Scott Michel8efdca42007-12-04 22:23:35 +0000448 node_names[(unsigned) SPUISD::VEC_SHL] = "SPUISD::VEC_SHL";
449 node_names[(unsigned) SPUISD::VEC_SRL] = "SPUISD::VEC_SRL";
450 node_names[(unsigned) SPUISD::VEC_SRA] = "SPUISD::VEC_SRA";
451 node_names[(unsigned) SPUISD::VEC_ROTL] = "SPUISD::VEC_ROTL";
452 node_names[(unsigned) SPUISD::VEC_ROTR] = "SPUISD::VEC_ROTR";
Scott Michel97872d32008-02-23 18:41:37 +0000453 node_names[(unsigned) SPUISD::ROTQUAD_RZ_BYTES] =
454 "SPUISD::ROTQUAD_RZ_BYTES";
455 node_names[(unsigned) SPUISD::ROTQUAD_RZ_BITS] =
456 "SPUISD::ROTQUAD_RZ_BITS";
Scott Michel8efdca42007-12-04 22:23:35 +0000457 node_names[(unsigned) SPUISD::ROTBYTES_LEFT] = "SPUISD::ROTBYTES_LEFT";
Scott Michel67224b22008-06-02 22:18:03 +0000458 node_names[(unsigned) SPUISD::ROTBYTES_LEFT_BITS] =
459 "SPUISD::ROTBYTES_LEFT_BITS";
460 node_names[(unsigned) SPUISD::SELECT_MASK] = "SPUISD::SELECT_MASK";
Scott Michel8efdca42007-12-04 22:23:35 +0000461 node_names[(unsigned) SPUISD::SELB] = "SPUISD::SELB";
Scott Michel67224b22008-06-02 22:18:03 +0000462 node_names[(unsigned) SPUISD::ADD_EXTENDED] = "SPUISD::ADD_EXTENDED";
463 node_names[(unsigned) SPUISD::CARRY_GENERATE] = "SPUISD::CARRY_GENERATE";
464 node_names[(unsigned) SPUISD::SUB_EXTENDED] = "SPUISD::SUB_EXTENDED";
465 node_names[(unsigned) SPUISD::BORROW_GENERATE] = "SPUISD::BORROW_GENERATE";
Scott Michel8efdca42007-12-04 22:23:35 +0000466 node_names[(unsigned) SPUISD::FPInterp] = "SPUISD::FPInterp";
467 node_names[(unsigned) SPUISD::FPRecipEst] = "SPUISD::FPRecipEst";
468 node_names[(unsigned) SPUISD::SEXT32TO64] = "SPUISD::SEXT32TO64";
469 }
470
471 std::map<unsigned, const char *>::iterator i = node_names.find(Opcode);
472
473 return ((i != node_names.end()) ? i->second : 0);
474}
475
Dan Gohman8181bd12008-07-27 21:46:04 +0000476MVT SPUTargetLowering::getSetCCResultType(const SDValue &Op) const {
Duncan Sands92c43912008-06-06 12:08:01 +0000477 MVT VT = Op.getValueType();
Scott Michela313fb02008-10-30 01:51:48 +0000478 return (VT.isInteger() ? VT : MVT(MVT::i32));
Scott Michel53ab7792008-03-10 16:58:52 +0000479}
480
Scott Michel8efdca42007-12-04 22:23:35 +0000481//===----------------------------------------------------------------------===//
482// Calling convention code:
483//===----------------------------------------------------------------------===//
484
485#include "SPUGenCallingConv.inc"
486
487//===----------------------------------------------------------------------===//
488// LowerOperation implementation
489//===----------------------------------------------------------------------===//
490
Scott Micheldbac4cf2008-01-11 02:53:15 +0000491/// Aligned load common code for CellSPU
492/*!
493 \param[in] Op The SelectionDAG load or store operand
494 \param[in] DAG The selection DAG
495 \param[in] ST CellSPU subtarget information structure
496 \param[in,out] alignment Caller initializes this to the load or store node's
497 value from getAlignment(), may be updated while generating the aligned load
498 \param[in,out] alignOffs Aligned offset; set by AlignedLoad to the aligned
499 offset (divisible by 16, modulo 16 == 0)
500 \param[in,out] prefSlotOffs Preferred slot offset; set by AlignedLoad to the
501 offset of the preferred slot (modulo 16 != 0)
502 \param[in,out] VT Caller initializes this value type to the the load or store
503 node's loaded or stored value type; may be updated if an i1-extended load or
504 store.
505 \param[out] was16aligned true if the base pointer had 16-byte alignment,
506 otherwise false. Can help to determine if the chunk needs to be rotated.
507
508 Both load and store lowering load a block of data aligned on a 16-byte
509 boundary. This is the common aligned load code shared between both.
510 */
Dan Gohman8181bd12008-07-27 21:46:04 +0000511static SDValue
512AlignedLoad(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST,
Scott Micheldbac4cf2008-01-11 02:53:15 +0000513 LSBaseSDNode *LSN,
514 unsigned &alignment, int &alignOffs, int &prefSlotOffs,
Duncan Sands92c43912008-06-06 12:08:01 +0000515 MVT &VT, bool &was16aligned)
Scott Micheldbac4cf2008-01-11 02:53:15 +0000516{
Duncan Sands92c43912008-06-06 12:08:01 +0000517 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Micheldbac4cf2008-01-11 02:53:15 +0000518 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +0000519 SDValue basePtr = LSN->getBasePtr();
520 SDValue chain = LSN->getChain();
Scott Micheldbac4cf2008-01-11 02:53:15 +0000521
522 if (basePtr.getOpcode() == ISD::ADD) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000523 SDValue Op1 = basePtr.getNode()->getOperand(1);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000524
Gabor Greife9f7f582008-08-31 15:37:04 +0000525 if (Op1.getOpcode() == ISD::Constant
526 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel394e26d2008-01-17 20:38:41 +0000527 const ConstantSDNode *CN = cast<ConstantSDNode>(basePtr.getOperand(1));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000528
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000529 alignOffs = (int) CN->getZExtValue();
Scott Micheldbac4cf2008-01-11 02:53:15 +0000530 prefSlotOffs = (int) (alignOffs & 0xf);
531
532 // Adjust the rotation amount to ensure that the final result ends up in
533 // the preferred slot:
534 prefSlotOffs -= vtm->prefslot_byte;
535 basePtr = basePtr.getOperand(0);
536
Scott Michel394e26d2008-01-17 20:38:41 +0000537 // Loading from memory, can we adjust alignment?
538 if (basePtr.getOpcode() == SPUISD::AFormAddr) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000539 SDValue APtr = basePtr.getOperand(0);
Scott Michel394e26d2008-01-17 20:38:41 +0000540 if (APtr.getOpcode() == ISD::TargetGlobalAddress) {
541 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(APtr);
542 alignment = GSDN->getGlobal()->getAlignment();
543 }
Scott Micheldbac4cf2008-01-11 02:53:15 +0000544 }
545 } else {
546 alignOffs = 0;
547 prefSlotOffs = -vtm->prefslot_byte;
548 }
Scott Michelbc5fbc12008-04-30 00:30:08 +0000549 } else if (basePtr.getOpcode() == ISD::FrameIndex) {
550 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(basePtr);
551 alignOffs = int(FIN->getIndex() * SPUFrameInfo::stackSlotSize());
552 prefSlotOffs = (int) (alignOffs & 0xf);
553 prefSlotOffs -= vtm->prefslot_byte;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000554 } else {
555 alignOffs = 0;
556 prefSlotOffs = -vtm->prefslot_byte;
557 }
558
559 if (alignment == 16) {
560 // Realign the base pointer as a D-Form address:
561 if (!isMemoryOperand(basePtr) || (alignOffs & ~0xf) != 0) {
Scott Michel394e26d2008-01-17 20:38:41 +0000562 basePtr = DAG.getNode(ISD::ADD, PtrVT,
563 basePtr,
Scott Michel5a6f17b2008-01-30 02:55:46 +0000564 DAG.getConstant((alignOffs & ~0xf), PtrVT));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000565 }
566
567 // Emit the vector load:
568 was16aligned = true;
569 return DAG.getLoad(MVT::v16i8, chain, basePtr,
570 LSN->getSrcValue(), LSN->getSrcValueOffset(),
571 LSN->isVolatile(), 16);
572 }
573
574 // Unaligned load or we're using the "large memory" model, which means that
575 // we have to be very pessimistic:
Scott Michel394e26d2008-01-17 20:38:41 +0000576 if (isMemoryOperand(basePtr) || isIndirectOperand(basePtr)) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000577 basePtr = DAG.getNode(SPUISD::IndirectAddr, PtrVT, basePtr,
578 DAG.getConstant(0, PtrVT));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000579 }
580
581 // Add the offset
Scott Michelf9f42e62008-01-29 02:16:57 +0000582 basePtr = DAG.getNode(ISD::ADD, PtrVT, basePtr,
Scott Michel5a6f17b2008-01-30 02:55:46 +0000583 DAG.getConstant((alignOffs & ~0xf), PtrVT));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000584 was16aligned = false;
585 return DAG.getLoad(MVT::v16i8, chain, basePtr,
586 LSN->getSrcValue(), LSN->getSrcValueOffset(),
587 LSN->isVolatile(), 16);
588}
589
Scott Michel8efdca42007-12-04 22:23:35 +0000590/// Custom lower loads for CellSPU
591/*!
592 All CellSPU loads and stores are aligned to 16-byte boundaries, so for elements
593 within a 16-byte block, we have to rotate to extract the requested element.
Scott Michel6ccefab2008-12-04 03:02:42 +0000594
595 For extending loads, we also want to ensure that the following sequence is
596 emitted, e.g. for MVT::f32 extending load to MVT::f64:
597
598\verbatim
599%1 v16i8,ch = load
600%2 v16i8,ch = rotate %1
601%3 v4f8, ch = bitconvert %2
602%4 f32 = vec2perfslot %3
603%5 f64 = fp_extend %4
604\endverbatim
605*/
Dan Gohman8181bd12008-07-27 21:46:04 +0000606static SDValue
607LowerLOAD(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel8efdca42007-12-04 22:23:35 +0000608 LoadSDNode *LN = cast<LoadSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000609 SDValue the_chain = LN->getChain();
Scott Michel6ccefab2008-12-04 03:02:42 +0000610 MVT InVT = LN->getMemoryVT();
611 MVT OutVT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +0000612 ISD::LoadExtType ExtType = LN->getExtensionType();
613 unsigned alignment = LN->getAlignment();
Dan Gohman8181bd12008-07-27 21:46:04 +0000614 SDValue Ops[8];
Scott Michel8efdca42007-12-04 22:23:35 +0000615
Scott Michel8efdca42007-12-04 22:23:35 +0000616 switch (LN->getAddressingMode()) {
617 case ISD::UNINDEXED: {
Scott Micheldbac4cf2008-01-11 02:53:15 +0000618 int offset, rotamt;
619 bool was16aligned;
Dan Gohman8181bd12008-07-27 21:46:04 +0000620 SDValue result =
Scott Michel6ccefab2008-12-04 03:02:42 +0000621 AlignedLoad(Op, DAG, ST, LN,alignment, offset, rotamt, InVT,
622 was16aligned);
Scott Michel8efdca42007-12-04 22:23:35 +0000623
Gabor Greif1c80d112008-08-28 21:40:38 +0000624 if (result.getNode() == 0)
Scott Michel8efdca42007-12-04 22:23:35 +0000625 return result;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000626
627 the_chain = result.getValue(1);
628 // Rotate the chunk if necessary
629 if (rotamt < 0)
630 rotamt += 16;
Scott Michelabc58242008-01-11 21:01:19 +0000631 if (rotamt != 0 || !was16aligned) {
Scott Micheldbac4cf2008-01-11 02:53:15 +0000632 SDVTList vecvts = DAG.getVTList(MVT::v16i8, MVT::Other);
633
Scott Michel6ccefab2008-12-04 03:02:42 +0000634 Ops[0] = result;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000635 if (was16aligned) {
Scott Michel6ccefab2008-12-04 03:02:42 +0000636 Ops[1] = DAG.getConstant(rotamt, MVT::i16);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000637 } else {
Duncan Sands92c43912008-06-06 12:08:01 +0000638 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Micheldbac4cf2008-01-11 02:53:15 +0000639 LoadSDNode *LN1 = cast<LoadSDNode>(result);
Scott Michel6ccefab2008-12-04 03:02:42 +0000640 Ops[1] = DAG.getNode(ISD::ADD, PtrVT, LN1->getBasePtr(),
Scott Michel5a6f17b2008-01-30 02:55:46 +0000641 DAG.getConstant(rotamt, PtrVT));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000642 }
643
Scott Michel6ccefab2008-12-04 03:02:42 +0000644 result = DAG.getNode(SPUISD::ROTBYTES_LEFT, MVT::v16i8, Ops, 2);
Scott Michel8efdca42007-12-04 22:23:35 +0000645 }
Scott Micheldbac4cf2008-01-11 02:53:15 +0000646
Scott Michel6ccefab2008-12-04 03:02:42 +0000647 // Convert the loaded v16i8 vector to the appropriate vector type
648 // specified by the operand:
649 MVT vecVT = MVT::getVectorVT(InVT, (128 / InVT.getSizeInBits()));
650 result = DAG.getNode(SPUISD::VEC2PREFSLOT, InVT,
651 DAG.getNode(ISD::BIT_CONVERT, vecVT, result));
Scott Michel4ec722e2008-07-16 17:17:29 +0000652
Scott Michel6ccefab2008-12-04 03:02:42 +0000653 // Handle extending loads by extending the scalar result:
654 if (ExtType == ISD::SEXTLOAD) {
655 result = DAG.getNode(ISD::SIGN_EXTEND, OutVT, result);
656 } else if (ExtType == ISD::ZEXTLOAD) {
657 result = DAG.getNode(ISD::ZERO_EXTEND, OutVT, result);
658 } else if (ExtType == ISD::EXTLOAD) {
659 unsigned NewOpc = ISD::ANY_EXTEND;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000660
Scott Michel6ccefab2008-12-04 03:02:42 +0000661 if (OutVT.isFloatingPoint())
662 NewOpc = ISD::FP_EXTEND;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000663
Scott Michel6ccefab2008-12-04 03:02:42 +0000664 result = DAG.getNode(NewOpc, OutVT, result);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000665 }
666
Scott Michel6ccefab2008-12-04 03:02:42 +0000667 SDVTList retvts = DAG.getVTList(OutVT, MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +0000668 SDValue retops[2] = {
Scott Michel394e26d2008-01-17 20:38:41 +0000669 result,
Scott Michel5a6f17b2008-01-30 02:55:46 +0000670 the_chain
Scott Michel394e26d2008-01-17 20:38:41 +0000671 };
Scott Micheldbac4cf2008-01-11 02:53:15 +0000672
Scott Michel394e26d2008-01-17 20:38:41 +0000673 result = DAG.getNode(SPUISD::LDRESULT, retvts,
674 retops, sizeof(retops) / sizeof(retops[0]));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000675 return result;
Scott Michel8efdca42007-12-04 22:23:35 +0000676 }
677 case ISD::PRE_INC:
678 case ISD::PRE_DEC:
679 case ISD::POST_INC:
680 case ISD::POST_DEC:
681 case ISD::LAST_INDEXED_MODE:
682 cerr << "LowerLOAD: Got a LoadSDNode with an addr mode other than "
683 "UNINDEXED\n";
684 cerr << (unsigned) LN->getAddressingMode() << "\n";
685 abort();
686 /*NOTREACHED*/
687 }
688
Dan Gohman8181bd12008-07-27 21:46:04 +0000689 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000690}
691
692/// Custom lower stores for CellSPU
693/*!
694 All CellSPU stores are aligned to 16-byte boundaries, so for elements
695 within a 16-byte block, we have to generate a shuffle to insert the
696 requested element into its place, then store the resulting block.
697 */
Dan Gohman8181bd12008-07-27 21:46:04 +0000698static SDValue
699LowerSTORE(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel8efdca42007-12-04 22:23:35 +0000700 StoreSDNode *SN = cast<StoreSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000701 SDValue Value = SN->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000702 MVT VT = Value.getValueType();
703 MVT StVT = (!SN->isTruncatingStore() ? VT : SN->getMemoryVT());
704 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Micheldbac4cf2008-01-11 02:53:15 +0000705 unsigned alignment = SN->getAlignment();
Scott Michel8efdca42007-12-04 22:23:35 +0000706
707 switch (SN->getAddressingMode()) {
708 case ISD::UNINDEXED: {
Scott Micheldbac4cf2008-01-11 02:53:15 +0000709 int chunk_offset, slot_offset;
710 bool was16aligned;
Scott Michel8efdca42007-12-04 22:23:35 +0000711
Scott Michel33d73eb2008-11-21 02:56:16 +0000712 // The vector type we really want to load from the 16-byte chunk.
Scott Michele1006032008-11-19 17:45:08 +0000713 MVT vecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits())),
714 stVecVT = MVT::getVectorVT(StVT, (128 / StVT.getSizeInBits()));
Scott Michel8efdca42007-12-04 22:23:35 +0000715
Dan Gohman8181bd12008-07-27 21:46:04 +0000716 SDValue alignLoadVec =
Scott Micheldbac4cf2008-01-11 02:53:15 +0000717 AlignedLoad(Op, DAG, ST, SN, alignment,
718 chunk_offset, slot_offset, VT, was16aligned);
Scott Michel8efdca42007-12-04 22:23:35 +0000719
Gabor Greif1c80d112008-08-28 21:40:38 +0000720 if (alignLoadVec.getNode() == 0)
Scott Micheldbac4cf2008-01-11 02:53:15 +0000721 return alignLoadVec;
Scott Michel8efdca42007-12-04 22:23:35 +0000722
Scott Micheldbac4cf2008-01-11 02:53:15 +0000723 LoadSDNode *LN = cast<LoadSDNode>(alignLoadVec);
Dan Gohman8181bd12008-07-27 21:46:04 +0000724 SDValue basePtr = LN->getBasePtr();
725 SDValue the_chain = alignLoadVec.getValue(1);
726 SDValue theValue = SN->getValue();
727 SDValue result;
Scott Michel8efdca42007-12-04 22:23:35 +0000728
729 if (StVT != VT
Scott Michel5a6f17b2008-01-30 02:55:46 +0000730 && (theValue.getOpcode() == ISD::AssertZext
731 || theValue.getOpcode() == ISD::AssertSext)) {
Scott Michel8efdca42007-12-04 22:23:35 +0000732 // Drill down and get the value for zero- and sign-extended
733 // quantities
Scott Michel4ec722e2008-07-16 17:17:29 +0000734 theValue = theValue.getOperand(0);
Scott Michel8efdca42007-12-04 22:23:35 +0000735 }
736
Scott Micheldbac4cf2008-01-11 02:53:15 +0000737 chunk_offset &= 0xf;
Scott Michel8efdca42007-12-04 22:23:35 +0000738
Dan Gohman8181bd12008-07-27 21:46:04 +0000739 SDValue insertEltOffs = DAG.getConstant(chunk_offset, PtrVT);
740 SDValue insertEltPtr;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000741
742 // If the base pointer is already a D-form address, then just create
743 // a new D-form address with a slot offset and the orignal base pointer.
744 // Otherwise generate a D-form address with the slot offset relative
745 // to the stack pointer, which is always aligned.
Scott Michelabc58242008-01-11 21:01:19 +0000746 DEBUG(cerr << "CellSPU LowerSTORE: basePtr = ");
Gabor Greif1c80d112008-08-28 21:40:38 +0000747 DEBUG(basePtr.getNode()->dump(&DAG));
Scott Michelabc58242008-01-11 21:01:19 +0000748 DEBUG(cerr << "\n");
749
Scott Michelf9f42e62008-01-29 02:16:57 +0000750 if (basePtr.getOpcode() == SPUISD::IndirectAddr ||
751 (basePtr.getOpcode() == ISD::ADD
752 && basePtr.getOperand(0).getOpcode() == SPUISD::IndirectAddr)) {
Scott Michelabc58242008-01-11 21:01:19 +0000753 insertEltPtr = basePtr;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000754 } else {
Scott Michelf9f42e62008-01-29 02:16:57 +0000755 insertEltPtr = DAG.getNode(ISD::ADD, PtrVT, basePtr, insertEltOffs);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000756 }
757
Scott Michelf65c8f02008-11-19 15:24:16 +0000758 SDValue insertEltOp =
Scott Michel0718cd82008-12-01 17:56:02 +0000759 DAG.getNode(SPUISD::SHUFFLE_MASK, vecVT, insertEltPtr);
Scott Michele1006032008-11-19 17:45:08 +0000760 SDValue vectorizeOp =
761 DAG.getNode(ISD::SCALAR_TO_VECTOR, vecVT, theValue);
Scott Michelf65c8f02008-11-19 15:24:16 +0000762
Scott Michel0718cd82008-12-01 17:56:02 +0000763 result = DAG.getNode(SPUISD::SHUFB, vecVT,
764 vectorizeOp, alignLoadVec,
765 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, insertEltOp));
Scott Michel8efdca42007-12-04 22:23:35 +0000766
Scott Micheldbac4cf2008-01-11 02:53:15 +0000767 result = DAG.getStore(the_chain, result, basePtr,
Scott Michel8efdca42007-12-04 22:23:35 +0000768 LN->getSrcValue(), LN->getSrcValueOffset(),
769 LN->isVolatile(), LN->getAlignment());
770
Scott Michel8c2746e2008-12-04 17:16:59 +0000771#if 0 && !defined(NDEBUG)
Scott Michelf65c8f02008-11-19 15:24:16 +0000772 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
773 const SDValue &currentRoot = DAG.getRoot();
774
775 DAG.setRoot(result);
776 cerr << "------- CellSPU:LowerStore result:\n";
777 DAG.dump();
778 cerr << "-------\n";
779 DAG.setRoot(currentRoot);
780 }
781#endif
Scott Michelec8c82e2008-12-02 19:53:53 +0000782
Scott Michel8efdca42007-12-04 22:23:35 +0000783 return result;
784 /*UNREACHED*/
785 }
786 case ISD::PRE_INC:
787 case ISD::PRE_DEC:
788 case ISD::POST_INC:
789 case ISD::POST_DEC:
790 case ISD::LAST_INDEXED_MODE:
791 cerr << "LowerLOAD: Got a LoadSDNode with an addr mode other than "
792 "UNINDEXED\n";
793 cerr << (unsigned) SN->getAddressingMode() << "\n";
794 abort();
795 /*NOTREACHED*/
796 }
797
Dan Gohman8181bd12008-07-27 21:46:04 +0000798 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000799}
800
801/// Generate the address of a constant pool entry.
Dan Gohman8181bd12008-07-27 21:46:04 +0000802static SDValue
803LowerConstantPool(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Duncan Sands92c43912008-06-06 12:08:01 +0000804 MVT PtrVT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +0000805 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
806 Constant *C = CP->getConstVal();
Dan Gohman8181bd12008-07-27 21:46:04 +0000807 SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
808 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000809 const TargetMachine &TM = DAG.getTarget();
Scott Michel8efdca42007-12-04 22:23:35 +0000810
811 if (TM.getRelocationModel() == Reloc::Static) {
812 if (!ST->usingLargeMem()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000813 // Just return the SDValue with the constant pool address in it.
Scott Michel394e26d2008-01-17 20:38:41 +0000814 return DAG.getNode(SPUISD::AFormAddr, PtrVT, CPI, Zero);
Scott Michel8efdca42007-12-04 22:23:35 +0000815 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000816 SDValue Hi = DAG.getNode(SPUISD::Hi, PtrVT, CPI, Zero);
817 SDValue Lo = DAG.getNode(SPUISD::Lo, PtrVT, CPI, Zero);
Scott Michel97872d32008-02-23 18:41:37 +0000818 return DAG.getNode(SPUISD::IndirectAddr, PtrVT, Hi, Lo);
Scott Michel8efdca42007-12-04 22:23:35 +0000819 }
820 }
821
822 assert(0 &&
Gabor Greife9f7f582008-08-31 15:37:04 +0000823 "LowerConstantPool: Relocation model other than static"
824 " not supported.");
Dan Gohman8181bd12008-07-27 21:46:04 +0000825 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000826}
827
Dan Gohman8181bd12008-07-27 21:46:04 +0000828static SDValue
829LowerJumpTable(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Duncan Sands92c43912008-06-06 12:08:01 +0000830 MVT PtrVT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +0000831 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000832 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
833 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Michel8efdca42007-12-04 22:23:35 +0000834 const TargetMachine &TM = DAG.getTarget();
835
836 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michel97872d32008-02-23 18:41:37 +0000837 if (!ST->usingLargeMem()) {
838 return DAG.getNode(SPUISD::AFormAddr, PtrVT, JTI, Zero);
839 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000840 SDValue Hi = DAG.getNode(SPUISD::Hi, PtrVT, JTI, Zero);
841 SDValue Lo = DAG.getNode(SPUISD::Lo, PtrVT, JTI, Zero);
Scott Michel97872d32008-02-23 18:41:37 +0000842 return DAG.getNode(SPUISD::IndirectAddr, PtrVT, Hi, Lo);
843 }
Scott Michel8efdca42007-12-04 22:23:35 +0000844 }
845
846 assert(0 &&
847 "LowerJumpTable: Relocation model other than static not supported.");
Dan Gohman8181bd12008-07-27 21:46:04 +0000848 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000849}
850
Dan Gohman8181bd12008-07-27 21:46:04 +0000851static SDValue
852LowerGlobalAddress(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Duncan Sands92c43912008-06-06 12:08:01 +0000853 MVT PtrVT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +0000854 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
855 GlobalValue *GV = GSDN->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +0000856 SDValue GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
Scott Michel8efdca42007-12-04 22:23:35 +0000857 const TargetMachine &TM = DAG.getTarget();
Dan Gohman8181bd12008-07-27 21:46:04 +0000858 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Michel4ec722e2008-07-16 17:17:29 +0000859
Scott Michel8efdca42007-12-04 22:23:35 +0000860 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michelf9f42e62008-01-29 02:16:57 +0000861 if (!ST->usingLargeMem()) {
862 return DAG.getNode(SPUISD::AFormAddr, PtrVT, GA, Zero);
863 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000864 SDValue Hi = DAG.getNode(SPUISD::Hi, PtrVT, GA, Zero);
865 SDValue Lo = DAG.getNode(SPUISD::Lo, PtrVT, GA, Zero);
Scott Michelf9f42e62008-01-29 02:16:57 +0000866 return DAG.getNode(SPUISD::IndirectAddr, PtrVT, Hi, Lo);
867 }
Scott Michel8efdca42007-12-04 22:23:35 +0000868 } else {
869 cerr << "LowerGlobalAddress: Relocation model other than static not "
Scott Michel5a6f17b2008-01-30 02:55:46 +0000870 << "supported.\n";
Scott Michel8efdca42007-12-04 22:23:35 +0000871 abort();
872 /*NOTREACHED*/
873 }
874
Dan Gohman8181bd12008-07-27 21:46:04 +0000875 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000876}
877
878//! Custom lower i64 integer constants
879/*!
880 This code inserts all of the necessary juggling that needs to occur to load
881 a 64-bit constant into a register.
882 */
Dan Gohman8181bd12008-07-27 21:46:04 +0000883static SDValue
884LowerConstant(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +0000885 MVT VT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +0000886
887 if (VT == MVT::i64) {
Scott Michel0718cd82008-12-01 17:56:02 +0000888 ConstantSDNode *CN = cast<ConstantSDNode>(Op.getNode());
889 SDValue T = DAG.getConstant(CN->getZExtValue(), VT);
Scott Michelc630c412008-11-24 17:11:17 +0000890 return DAG.getNode(SPUISD::VEC2PREFSLOT, VT,
Scott Michel5a6f17b2008-01-30 02:55:46 +0000891 DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i64, T, T));
Scott Michel8efdca42007-12-04 22:23:35 +0000892 } else {
893 cerr << "LowerConstant: unhandled constant type "
Duncan Sands92c43912008-06-06 12:08:01 +0000894 << VT.getMVTString()
Scott Michel5a6f17b2008-01-30 02:55:46 +0000895 << "\n";
Scott Michel8efdca42007-12-04 22:23:35 +0000896 abort();
897 /*NOTREACHED*/
898 }
899
Dan Gohman8181bd12008-07-27 21:46:04 +0000900 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000901}
902
Nate Begeman78125042008-02-14 18:43:04 +0000903//! Custom lower double precision floating point constants
Dan Gohman8181bd12008-07-27 21:46:04 +0000904static SDValue
905LowerConstantFP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +0000906 MVT VT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +0000907
Nate Begeman78125042008-02-14 18:43:04 +0000908 if (VT == MVT::f64) {
Scott Michel0718cd82008-12-01 17:56:02 +0000909 ConstantFPSDNode *FP = cast<ConstantFPSDNode>(Op.getNode());
910
911 assert((FP != 0) &&
912 "LowerConstantFP: Node is not ConstantFPSDNode");
913
Scott Michel11e88bb2007-12-19 20:15:47 +0000914 uint64_t dbits = DoubleToBits(FP->getValueAPF().convertToDouble());
Scott Michel0718cd82008-12-01 17:56:02 +0000915 SDValue T = DAG.getConstant(dbits, MVT::i64);
916 SDValue Tvec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i64, T, T);
917 return DAG.getNode(SPUISD::VEC2PREFSLOT, VT,
918 DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Tvec));
Scott Michel8efdca42007-12-04 22:23:35 +0000919 }
920
Dan Gohman8181bd12008-07-27 21:46:04 +0000921 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000922}
923
Scott Michel33d73eb2008-11-21 02:56:16 +0000924//! Lower MVT::i8 brcond to a promoted type (MVT::i32, MVT::i16)
Dan Gohman8181bd12008-07-27 21:46:04 +0000925static SDValue
926LowerBRCOND(SDValue Op, SelectionDAG &DAG)
Scott Michel394e26d2008-01-17 20:38:41 +0000927{
Dan Gohman8181bd12008-07-27 21:46:04 +0000928 SDValue Cond = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +0000929 MVT CondVT = Cond.getValueType();
930 MVT CondNVT;
Scott Michel394e26d2008-01-17 20:38:41 +0000931
Scott Michel33d73eb2008-11-21 02:56:16 +0000932 if (CondVT == MVT::i8) {
933 CondNVT = MVT::i16;
Scott Michel394e26d2008-01-17 20:38:41 +0000934 return DAG.getNode(ISD::BRCOND, Op.getValueType(),
935 Op.getOperand(0),
936 DAG.getNode(ISD::ZERO_EXTEND, CondNVT, Op.getOperand(1)),
937 Op.getOperand(2));
938 } else
Dan Gohman8181bd12008-07-27 21:46:04 +0000939 return SDValue(); // Unchanged
Scott Michel394e26d2008-01-17 20:38:41 +0000940}
941
Dan Gohman8181bd12008-07-27 21:46:04 +0000942static SDValue
943LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG, int &VarArgsFrameIndex)
Scott Michel8efdca42007-12-04 22:23:35 +0000944{
945 MachineFunction &MF = DAG.getMachineFunction();
946 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattner1b989192007-12-31 04:13:23 +0000947 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michela313fb02008-10-30 01:51:48 +0000948 SmallVector<SDValue, 48> ArgValues;
Dan Gohman8181bd12008-07-27 21:46:04 +0000949 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000950 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Scott Michel8efdca42007-12-04 22:23:35 +0000951
952 const unsigned *ArgRegs = SPURegisterInfo::getArgRegs();
953 const unsigned NumArgRegs = SPURegisterInfo::getNumArgRegs();
Scott Michel4ec722e2008-07-16 17:17:29 +0000954
Scott Michel8efdca42007-12-04 22:23:35 +0000955 unsigned ArgOffset = SPUFrameInfo::minStackSize();
956 unsigned ArgRegIdx = 0;
957 unsigned StackSlotSize = SPUFrameInfo::stackSlotSize();
Scott Michel4ec722e2008-07-16 17:17:29 +0000958
Duncan Sands92c43912008-06-06 12:08:01 +0000959 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel4ec722e2008-07-16 17:17:29 +0000960
Scott Michel8efdca42007-12-04 22:23:35 +0000961 // Add DAG nodes to load the arguments or copy them out of registers.
Gabor Greife9f7f582008-08-31 15:37:04 +0000962 for (unsigned ArgNo = 0, e = Op.getNode()->getNumValues() - 1;
963 ArgNo != e; ++ArgNo) {
Duncan Sands92c43912008-06-06 12:08:01 +0000964 MVT ObjectVT = Op.getValue(ArgNo).getValueType();
965 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Scott Michela313fb02008-10-30 01:51:48 +0000966 SDValue ArgVal;
Scott Michel8efdca42007-12-04 22:23:35 +0000967
Scott Michela313fb02008-10-30 01:51:48 +0000968 if (ArgRegIdx < NumArgRegs) {
969 const TargetRegisterClass *ArgRegClass;
Scott Michel4ec722e2008-07-16 17:17:29 +0000970
Scott Michela313fb02008-10-30 01:51:48 +0000971 switch (ObjectVT.getSimpleVT()) {
972 default: {
Scott Michel33d73eb2008-11-21 02:56:16 +0000973 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
974 << ObjectVT.getMVTString()
975 << "\n";
976 abort();
Scott Michela313fb02008-10-30 01:51:48 +0000977 }
978 case MVT::i8:
Scott Michel33d73eb2008-11-21 02:56:16 +0000979 ArgRegClass = &SPU::R8CRegClass;
980 break;
Scott Michela313fb02008-10-30 01:51:48 +0000981 case MVT::i16:
Scott Michel33d73eb2008-11-21 02:56:16 +0000982 ArgRegClass = &SPU::R16CRegClass;
983 break;
Scott Michela313fb02008-10-30 01:51:48 +0000984 case MVT::i32:
Scott Michel33d73eb2008-11-21 02:56:16 +0000985 ArgRegClass = &SPU::R32CRegClass;
986 break;
Scott Michela313fb02008-10-30 01:51:48 +0000987 case MVT::i64:
Scott Michel33d73eb2008-11-21 02:56:16 +0000988 ArgRegClass = &SPU::R64CRegClass;
989 break;
Scott Michela313fb02008-10-30 01:51:48 +0000990 case MVT::f32:
Scott Michel33d73eb2008-11-21 02:56:16 +0000991 ArgRegClass = &SPU::R32FPRegClass;
992 break;
Scott Michela313fb02008-10-30 01:51:48 +0000993 case MVT::f64:
Scott Michel33d73eb2008-11-21 02:56:16 +0000994 ArgRegClass = &SPU::R64FPRegClass;
995 break;
Scott Michela313fb02008-10-30 01:51:48 +0000996 case MVT::v2f64:
997 case MVT::v4f32:
998 case MVT::v2i64:
999 case MVT::v4i32:
1000 case MVT::v8i16:
1001 case MVT::v16i8:
Scott Michel33d73eb2008-11-21 02:56:16 +00001002 ArgRegClass = &SPU::VECREGRegClass;
1003 break;
Scott Michela313fb02008-10-30 01:51:48 +00001004 }
1005
1006 unsigned VReg = RegInfo.createVirtualRegister(ArgRegClass);
1007 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
1008 ArgVal = DAG.getCopyFromReg(Root, VReg, ObjectVT);
1009 ++ArgRegIdx;
1010 } else {
1011 // We need to load the argument to a virtual register if we determined
1012 // above that we ran out of physical registers of the appropriate type
1013 // or we're forced to do vararg
Chris Lattner60069452008-02-13 07:35:30 +00001014 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Dan Gohman8181bd12008-07-27 21:46:04 +00001015 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner60069452008-02-13 07:35:30 +00001016 ArgVal = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
Scott Michel8efdca42007-12-04 22:23:35 +00001017 ArgOffset += StackSlotSize;
1018 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001019
Scott Michel8efdca42007-12-04 22:23:35 +00001020 ArgValues.push_back(ArgVal);
Scott Michela313fb02008-10-30 01:51:48 +00001021 // Update the chain
1022 Root = ArgVal.getOperand(0);
Scott Michel8efdca42007-12-04 22:23:35 +00001023 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001024
Scott Michela313fb02008-10-30 01:51:48 +00001025 // vararg handling:
Scott Michel8efdca42007-12-04 22:23:35 +00001026 if (isVarArg) {
Scott Michela313fb02008-10-30 01:51:48 +00001027 // unsigned int ptr_size = PtrVT.getSizeInBits() / 8;
1028 // We will spill (79-3)+1 registers to the stack
1029 SmallVector<SDValue, 79-3+1> MemOps;
1030
1031 // Create the frame slot
1032
Scott Michel8efdca42007-12-04 22:23:35 +00001033 for (; ArgRegIdx != NumArgRegs; ++ArgRegIdx) {
Scott Michela313fb02008-10-30 01:51:48 +00001034 VarArgsFrameIndex = MFI->CreateFixedObject(StackSlotSize, ArgOffset);
1035 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
1036 SDValue ArgVal = DAG.getRegister(ArgRegs[ArgRegIdx], MVT::v16i8);
1037 SDValue Store = DAG.getStore(Root, ArgVal, FIN, NULL, 0);
1038 Root = Store.getOperand(0);
Scott Michel8efdca42007-12-04 22:23:35 +00001039 MemOps.push_back(Store);
Scott Michela313fb02008-10-30 01:51:48 +00001040
1041 // Increment address by stack slot size for the next stored argument
1042 ArgOffset += StackSlotSize;
Scott Michel8efdca42007-12-04 22:23:35 +00001043 }
1044 if (!MemOps.empty())
Scott Michela313fb02008-10-30 01:51:48 +00001045 Root = DAG.getNode(ISD::TokenFactor,MVT::Other,&MemOps[0],MemOps.size());
Scott Michel8efdca42007-12-04 22:23:35 +00001046 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001047
Scott Michel8efdca42007-12-04 22:23:35 +00001048 ArgValues.push_back(Root);
Scott Michel4ec722e2008-07-16 17:17:29 +00001049
Scott Michel8efdca42007-12-04 22:23:35 +00001050 // Return the new list of results.
Duncan Sands42d7bb82008-12-01 11:41:29 +00001051 return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
1052 &ArgValues[0], ArgValues.size());
Scott Michel8efdca42007-12-04 22:23:35 +00001053}
1054
1055/// isLSAAddress - Return the immediate to use if the specified
1056/// value is representable as a LSA address.
Dan Gohman8181bd12008-07-27 21:46:04 +00001057static SDNode *isLSAAddress(SDValue Op, SelectionDAG &DAG) {
Scott Michel5974f432008-11-11 03:06:06 +00001058 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Scott Michel8efdca42007-12-04 22:23:35 +00001059 if (!C) return 0;
Scott Michel4ec722e2008-07-16 17:17:29 +00001060
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001061 int Addr = C->getZExtValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001062 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
1063 (Addr << 14 >> 14) != Addr)
1064 return 0; // Top 14 bits have to be sext of immediate.
Scott Michel4ec722e2008-07-16 17:17:29 +00001065
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001066 return DAG.getConstant((int)C->getZExtValue() >> 2, MVT::i32).getNode();
Scott Michel8efdca42007-12-04 22:23:35 +00001067}
1068
1069static
Dan Gohman8181bd12008-07-27 21:46:04 +00001070SDValue
1071LowerCALL(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001072 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1073 SDValue Chain = TheCall->getChain();
Dan Gohman705e3f72008-09-13 01:54:27 +00001074 SDValue Callee = TheCall->getCallee();
1075 unsigned NumOps = TheCall->getNumArgs();
Scott Michel8efdca42007-12-04 22:23:35 +00001076 unsigned StackSlotSize = SPUFrameInfo::stackSlotSize();
1077 const unsigned *ArgRegs = SPURegisterInfo::getArgRegs();
1078 const unsigned NumArgRegs = SPURegisterInfo::getNumArgRegs();
1079
1080 // Handy pointer type
Duncan Sands92c43912008-06-06 12:08:01 +00001081 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel4ec722e2008-07-16 17:17:29 +00001082
Scott Michel8efdca42007-12-04 22:23:35 +00001083 // Accumulate how many bytes are to be pushed on the stack, including the
1084 // linkage area, and parameter passing area. According to the SPU ABI,
1085 // we minimally need space for [LR] and [SP]
1086 unsigned NumStackBytes = SPUFrameInfo::minStackSize();
Scott Michel4ec722e2008-07-16 17:17:29 +00001087
Scott Michel8efdca42007-12-04 22:23:35 +00001088 // Set up a copy of the stack pointer for use loading and storing any
1089 // arguments that may not fit in the registers available for argument
1090 // passing.
Dan Gohman8181bd12008-07-27 21:46:04 +00001091 SDValue StackPtr = DAG.getRegister(SPU::R1, MVT::i32);
Scott Michel4ec722e2008-07-16 17:17:29 +00001092
Scott Michel8efdca42007-12-04 22:23:35 +00001093 // Figure out which arguments are going to go in registers, and which in
1094 // memory.
1095 unsigned ArgOffset = SPUFrameInfo::minStackSize(); // Just below [LR]
1096 unsigned ArgRegIdx = 0;
1097
1098 // Keep track of registers passing arguments
Dan Gohman8181bd12008-07-27 21:46:04 +00001099 std::vector<std::pair<unsigned, SDValue> > RegsToPass;
Scott Michel8efdca42007-12-04 22:23:35 +00001100 // And the arguments passed on the stack
Dan Gohman8181bd12008-07-27 21:46:04 +00001101 SmallVector<SDValue, 8> MemOpChains;
Scott Michel8efdca42007-12-04 22:23:35 +00001102
1103 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001104 SDValue Arg = TheCall->getArg(i);
Scott Michel4ec722e2008-07-16 17:17:29 +00001105
Scott Michel8efdca42007-12-04 22:23:35 +00001106 // PtrOff will be used to store the current argument to the stack if a
1107 // register cannot be found for it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001108 SDValue PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Scott Michel8efdca42007-12-04 22:23:35 +00001109 PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr, PtrOff);
1110
Duncan Sands92c43912008-06-06 12:08:01 +00001111 switch (Arg.getValueType().getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001112 default: assert(0 && "Unexpected ValueType for argument!");
1113 case MVT::i32:
1114 case MVT::i64:
1115 case MVT::i128:
1116 if (ArgRegIdx != NumArgRegs) {
1117 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1118 } else {
1119 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Scott Michel5a6f17b2008-01-30 02:55:46 +00001120 ArgOffset += StackSlotSize;
Scott Michel8efdca42007-12-04 22:23:35 +00001121 }
1122 break;
1123 case MVT::f32:
1124 case MVT::f64:
1125 if (ArgRegIdx != NumArgRegs) {
1126 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1127 } else {
1128 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Scott Michel5a6f17b2008-01-30 02:55:46 +00001129 ArgOffset += StackSlotSize;
Scott Michel8efdca42007-12-04 22:23:35 +00001130 }
1131 break;
Scott Michele2641a12008-12-04 21:01:44 +00001132 case MVT::v2i64:
1133 case MVT::v2f64:
Scott Michel8efdca42007-12-04 22:23:35 +00001134 case MVT::v4f32:
1135 case MVT::v4i32:
1136 case MVT::v8i16:
1137 case MVT::v16i8:
1138 if (ArgRegIdx != NumArgRegs) {
1139 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1140 } else {
1141 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Scott Michel5a6f17b2008-01-30 02:55:46 +00001142 ArgOffset += StackSlotSize;
Scott Michel8efdca42007-12-04 22:23:35 +00001143 }
1144 break;
1145 }
1146 }
1147
1148 // Update number of stack bytes actually used, insert a call sequence start
1149 NumStackBytes = (ArgOffset - SPUFrameInfo::minStackSize());
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001150 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumStackBytes,
1151 true));
Scott Michel8efdca42007-12-04 22:23:35 +00001152
1153 if (!MemOpChains.empty()) {
1154 // Adjust the stack pointer for the stack arguments.
1155 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1156 &MemOpChains[0], MemOpChains.size());
1157 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001158
Scott Michel8efdca42007-12-04 22:23:35 +00001159 // Build a sequence of copy-to-reg nodes chained together with token chain
1160 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman8181bd12008-07-27 21:46:04 +00001161 SDValue InFlag;
Scott Michel8efdca42007-12-04 22:23:35 +00001162 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1163 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1164 InFlag);
1165 InFlag = Chain.getValue(1);
1166 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001167
Dan Gohman8181bd12008-07-27 21:46:04 +00001168 SmallVector<SDValue, 8> Ops;
Scott Michel8efdca42007-12-04 22:23:35 +00001169 unsigned CallOpc = SPUISD::CALL;
Scott Michel4ec722e2008-07-16 17:17:29 +00001170
Bill Wendlingfef06052008-09-16 21:48:12 +00001171 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1172 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1173 // node so that legalize doesn't hack it.
Scott Michel5974f432008-11-11 03:06:06 +00001174 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Scott Michel8efdca42007-12-04 22:23:35 +00001175 GlobalValue *GV = G->getGlobal();
Duncan Sands92c43912008-06-06 12:08:01 +00001176 MVT CalleeVT = Callee.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00001177 SDValue Zero = DAG.getConstant(0, PtrVT);
1178 SDValue GA = DAG.getTargetGlobalAddress(GV, CalleeVT);
Scott Michel8efdca42007-12-04 22:23:35 +00001179
Scott Micheldbac4cf2008-01-11 02:53:15 +00001180 if (!ST->usingLargeMem()) {
1181 // Turn calls to targets that are defined (i.e., have bodies) into BRSL
1182 // style calls, otherwise, external symbols are BRASL calls. This assumes
1183 // that declared/defined symbols are in the same compilation unit and can
1184 // be reached through PC-relative jumps.
1185 //
1186 // NOTE:
1187 // This may be an unsafe assumption for JIT and really large compilation
1188 // units.
1189 if (GV->isDeclaration()) {
1190 Callee = DAG.getNode(SPUISD::AFormAddr, CalleeVT, GA, Zero);
1191 } else {
1192 Callee = DAG.getNode(SPUISD::PCRelAddr, CalleeVT, GA, Zero);
1193 }
Scott Michel8efdca42007-12-04 22:23:35 +00001194 } else {
Scott Micheldbac4cf2008-01-11 02:53:15 +00001195 // "Large memory" mode: Turn all calls into indirect calls with a X-form
1196 // address pairs:
Scott Michelf9f42e62008-01-29 02:16:57 +00001197 Callee = DAG.getNode(SPUISD::IndirectAddr, PtrVT, GA, Zero);
Scott Michel8efdca42007-12-04 22:23:35 +00001198 }
Scott Michel5974f432008-11-11 03:06:06 +00001199 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Bill Wendlingfef06052008-09-16 21:48:12 +00001200 Callee = DAG.getExternalSymbol(S->getSymbol(), Callee.getValueType());
Scott Micheldbac4cf2008-01-11 02:53:15 +00001201 else if (SDNode *Dest = isLSAAddress(Callee, DAG)) {
Scott Michel8efdca42007-12-04 22:23:35 +00001202 // If this is an absolute destination address that appears to be a legal
1203 // local store address, use the munged value.
Dan Gohman8181bd12008-07-27 21:46:04 +00001204 Callee = SDValue(Dest, 0);
Scott Micheldbac4cf2008-01-11 02:53:15 +00001205 }
Scott Michel8efdca42007-12-04 22:23:35 +00001206
1207 Ops.push_back(Chain);
1208 Ops.push_back(Callee);
Scott Michel4ec722e2008-07-16 17:17:29 +00001209
Scott Michel8efdca42007-12-04 22:23:35 +00001210 // Add argument registers to the end of the list so that they are known live
1211 // into the call.
1212 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Scott Michel4ec722e2008-07-16 17:17:29 +00001213 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Scott Michel8efdca42007-12-04 22:23:35 +00001214 RegsToPass[i].second.getValueType()));
Scott Michel4ec722e2008-07-16 17:17:29 +00001215
Gabor Greif1c80d112008-08-28 21:40:38 +00001216 if (InFlag.getNode())
Scott Michel8efdca42007-12-04 22:23:35 +00001217 Ops.push_back(InFlag);
Duncan Sands698842f2008-07-02 17:40:58 +00001218 // Returns a chain and a flag for retval copy to use.
1219 Chain = DAG.getNode(CallOpc, DAG.getVTList(MVT::Other, MVT::Flag),
1220 &Ops[0], Ops.size());
Scott Michel8efdca42007-12-04 22:23:35 +00001221 InFlag = Chain.getValue(1);
1222
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001223 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumStackBytes, true),
1224 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman705e3f72008-09-13 01:54:27 +00001225 if (TheCall->getValueType(0) != MVT::Other)
Evan Cheng07322bb2008-02-05 22:44:06 +00001226 InFlag = Chain.getValue(1);
1227
Dan Gohman8181bd12008-07-27 21:46:04 +00001228 SDValue ResultVals[3];
Scott Michel8efdca42007-12-04 22:23:35 +00001229 unsigned NumResults = 0;
Scott Michel4ec722e2008-07-16 17:17:29 +00001230
Scott Michel8efdca42007-12-04 22:23:35 +00001231 // If the call has results, copy the values out of the ret val registers.
Dan Gohman705e3f72008-09-13 01:54:27 +00001232 switch (TheCall->getValueType(0).getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001233 default: assert(0 && "Unexpected ret value!");
1234 case MVT::Other: break;
1235 case MVT::i32:
Dan Gohman705e3f72008-09-13 01:54:27 +00001236 if (TheCall->getValueType(1) == MVT::i32) {
Scott Michel8efdca42007-12-04 22:23:35 +00001237 Chain = DAG.getCopyFromReg(Chain, SPU::R4, MVT::i32, InFlag).getValue(1);
1238 ResultVals[0] = Chain.getValue(0);
1239 Chain = DAG.getCopyFromReg(Chain, SPU::R3, MVT::i32,
1240 Chain.getValue(2)).getValue(1);
1241 ResultVals[1] = Chain.getValue(0);
1242 NumResults = 2;
Scott Michel8efdca42007-12-04 22:23:35 +00001243 } else {
1244 Chain = DAG.getCopyFromReg(Chain, SPU::R3, MVT::i32, InFlag).getValue(1);
1245 ResultVals[0] = Chain.getValue(0);
1246 NumResults = 1;
1247 }
Scott Michel8efdca42007-12-04 22:23:35 +00001248 break;
1249 case MVT::i64:
1250 Chain = DAG.getCopyFromReg(Chain, SPU::R3, MVT::i64, InFlag).getValue(1);
1251 ResultVals[0] = Chain.getValue(0);
1252 NumResults = 1;
Scott Michel8efdca42007-12-04 22:23:35 +00001253 break;
1254 case MVT::f32:
1255 case MVT::f64:
Dan Gohman705e3f72008-09-13 01:54:27 +00001256 Chain = DAG.getCopyFromReg(Chain, SPU::R3, TheCall->getValueType(0),
Scott Michel8efdca42007-12-04 22:23:35 +00001257 InFlag).getValue(1);
1258 ResultVals[0] = Chain.getValue(0);
1259 NumResults = 1;
Scott Michel8efdca42007-12-04 22:23:35 +00001260 break;
1261 case MVT::v2f64:
Scott Michele2641a12008-12-04 21:01:44 +00001262 case MVT::v2i64:
Scott Michel8efdca42007-12-04 22:23:35 +00001263 case MVT::v4f32:
1264 case MVT::v4i32:
1265 case MVT::v8i16:
1266 case MVT::v16i8:
Dan Gohman705e3f72008-09-13 01:54:27 +00001267 Chain = DAG.getCopyFromReg(Chain, SPU::R3, TheCall->getValueType(0),
Scott Michel8efdca42007-12-04 22:23:35 +00001268 InFlag).getValue(1);
1269 ResultVals[0] = Chain.getValue(0);
1270 NumResults = 1;
Scott Michel8efdca42007-12-04 22:23:35 +00001271 break;
1272 }
Duncan Sands698842f2008-07-02 17:40:58 +00001273
Scott Michel8efdca42007-12-04 22:23:35 +00001274 // If the function returns void, just return the chain.
1275 if (NumResults == 0)
1276 return Chain;
Scott Michel4ec722e2008-07-16 17:17:29 +00001277
Scott Michel8efdca42007-12-04 22:23:35 +00001278 // Otherwise, merge everything together with a MERGE_VALUES node.
1279 ResultVals[NumResults++] = Chain;
Dan Gohman8181bd12008-07-27 21:46:04 +00001280 SDValue Res = DAG.getMergeValues(ResultVals, NumResults);
Gabor Greif46bf5472008-08-26 22:36:50 +00001281 return Res.getValue(Op.getResNo());
Scott Michel8efdca42007-12-04 22:23:35 +00001282}
1283
Dan Gohman8181bd12008-07-27 21:46:04 +00001284static SDValue
1285LowerRET(SDValue Op, SelectionDAG &DAG, TargetMachine &TM) {
Scott Michel8efdca42007-12-04 22:23:35 +00001286 SmallVector<CCValAssign, 16> RVLocs;
1287 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
1288 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
1289 CCState CCInfo(CC, isVarArg, TM, RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001290 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SPU);
Scott Michel4ec722e2008-07-16 17:17:29 +00001291
Scott Michel8efdca42007-12-04 22:23:35 +00001292 // If this is the first return lowered for this function, add the regs to the
1293 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +00001294 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001295 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner1b989192007-12-31 04:13:23 +00001296 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Scott Michel8efdca42007-12-04 22:23:35 +00001297 }
1298
Dan Gohman8181bd12008-07-27 21:46:04 +00001299 SDValue Chain = Op.getOperand(0);
1300 SDValue Flag;
Scott Michel4ec722e2008-07-16 17:17:29 +00001301
Scott Michel8efdca42007-12-04 22:23:35 +00001302 // Copy the result values into the output registers.
1303 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1304 CCValAssign &VA = RVLocs[i];
1305 assert(VA.isRegLoc() && "Can only return in registers!");
1306 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
1307 Flag = Chain.getValue(1);
1308 }
1309
Gabor Greif1c80d112008-08-28 21:40:38 +00001310 if (Flag.getNode())
Scott Michel8efdca42007-12-04 22:23:35 +00001311 return DAG.getNode(SPUISD::RET_FLAG, MVT::Other, Chain, Flag);
1312 else
1313 return DAG.getNode(SPUISD::RET_FLAG, MVT::Other, Chain);
1314}
1315
1316
1317//===----------------------------------------------------------------------===//
1318// Vector related lowering:
1319//===----------------------------------------------------------------------===//
1320
1321static ConstantSDNode *
1322getVecImm(SDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001323 SDValue OpVal(0, 0);
Scott Michel4ec722e2008-07-16 17:17:29 +00001324
Scott Michel8efdca42007-12-04 22:23:35 +00001325 // Check to see if this buildvec has a single non-undef value in its elements.
1326 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1327 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greif1c80d112008-08-28 21:40:38 +00001328 if (OpVal.getNode() == 0)
Scott Michel8efdca42007-12-04 22:23:35 +00001329 OpVal = N->getOperand(i);
1330 else if (OpVal != N->getOperand(i))
1331 return 0;
1332 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001333
Gabor Greif1c80d112008-08-28 21:40:38 +00001334 if (OpVal.getNode() != 0) {
Scott Michel5974f432008-11-11 03:06:06 +00001335 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Scott Michel8efdca42007-12-04 22:23:35 +00001336 return CN;
1337 }
1338 }
1339
1340 return 0; // All UNDEF: use implicit def.; not Constant node
1341}
1342
1343/// get_vec_i18imm - Test if this vector is a vector filled with the same value
1344/// and the value fits into an unsigned 18-bit constant, and if so, return the
1345/// constant
Dan Gohman8181bd12008-07-27 21:46:04 +00001346SDValue SPU::get_vec_u18imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00001347 MVT ValueType) {
Scott Michel8efdca42007-12-04 22:23:35 +00001348 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001349 uint64_t Value = CN->getZExtValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001350 if (ValueType == MVT::i64) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001351 uint64_t UValue = CN->getZExtValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001352 uint32_t upper = uint32_t(UValue >> 32);
1353 uint32_t lower = uint32_t(UValue);
1354 if (upper != lower)
Dan Gohman8181bd12008-07-27 21:46:04 +00001355 return SDValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001356 Value = Value >> 32;
1357 }
Scott Michel8efdca42007-12-04 22:23:35 +00001358 if (Value <= 0x3ffff)
Dan Gohman57d5d5d2008-11-05 02:06:09 +00001359 return DAG.getTargetConstant(Value, ValueType);
Scott Michel8efdca42007-12-04 22:23:35 +00001360 }
1361
Dan Gohman8181bd12008-07-27 21:46:04 +00001362 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001363}
1364
1365/// get_vec_i16imm - Test if this vector is a vector filled with the same value
1366/// and the value fits into a signed 16-bit constant, and if so, return the
1367/// constant
Dan Gohman8181bd12008-07-27 21:46:04 +00001368SDValue SPU::get_vec_i16imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00001369 MVT ValueType) {
Scott Michel8efdca42007-12-04 22:23:35 +00001370 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohman40686732008-09-26 21:54:37 +00001371 int64_t Value = CN->getSExtValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001372 if (ValueType == MVT::i64) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001373 uint64_t UValue = CN->getZExtValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001374 uint32_t upper = uint32_t(UValue >> 32);
1375 uint32_t lower = uint32_t(UValue);
1376 if (upper != lower)
Dan Gohman8181bd12008-07-27 21:46:04 +00001377 return SDValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001378 Value = Value >> 32;
1379 }
Scott Michel6baba072008-03-05 23:02:02 +00001380 if (Value >= -(1 << 15) && Value <= ((1 << 15) - 1)) {
Dan Gohman57d5d5d2008-11-05 02:06:09 +00001381 return DAG.getTargetConstant(Value, ValueType);
Scott Michel8efdca42007-12-04 22:23:35 +00001382 }
1383 }
1384
Dan Gohman8181bd12008-07-27 21:46:04 +00001385 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001386}
1387
1388/// get_vec_i10imm - Test if this vector is a vector filled with the same value
1389/// and the value fits into a signed 10-bit constant, and if so, return the
1390/// constant
Dan Gohman8181bd12008-07-27 21:46:04 +00001391SDValue SPU::get_vec_i10imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00001392 MVT ValueType) {
Scott Michel8efdca42007-12-04 22:23:35 +00001393 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohman40686732008-09-26 21:54:37 +00001394 int64_t Value = CN->getSExtValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001395 if (ValueType == MVT::i64) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001396 uint64_t UValue = CN->getZExtValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001397 uint32_t upper = uint32_t(UValue >> 32);
1398 uint32_t lower = uint32_t(UValue);
1399 if (upper != lower)
Dan Gohman8181bd12008-07-27 21:46:04 +00001400 return SDValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001401 Value = Value >> 32;
1402 }
Scott Michel6baba072008-03-05 23:02:02 +00001403 if (isS10Constant(Value))
Dan Gohman57d5d5d2008-11-05 02:06:09 +00001404 return DAG.getTargetConstant(Value, ValueType);
Scott Michel8efdca42007-12-04 22:23:35 +00001405 }
1406
Dan Gohman8181bd12008-07-27 21:46:04 +00001407 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001408}
1409
1410/// get_vec_i8imm - Test if this vector is a vector filled with the same value
1411/// and the value fits into a signed 8-bit constant, and if so, return the
1412/// constant.
1413///
1414/// @note: The incoming vector is v16i8 because that's the only way we can load
1415/// constant vectors. Thus, we test to see if the upper and lower bytes are the
1416/// same value.
Dan Gohman8181bd12008-07-27 21:46:04 +00001417SDValue SPU::get_vec_i8imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00001418 MVT ValueType) {
Scott Michel8efdca42007-12-04 22:23:35 +00001419 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001420 int Value = (int) CN->getZExtValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001421 if (ValueType == MVT::i16
Scott Michel5a6f17b2008-01-30 02:55:46 +00001422 && Value <= 0xffff /* truncated from uint64_t */
1423 && ((short) Value >> 8) == ((short) Value & 0xff))
Dan Gohman57d5d5d2008-11-05 02:06:09 +00001424 return DAG.getTargetConstant(Value & 0xff, ValueType);
Scott Michel8efdca42007-12-04 22:23:35 +00001425 else if (ValueType == MVT::i8
Scott Michel5a6f17b2008-01-30 02:55:46 +00001426 && (Value & 0xff) == Value)
Dan Gohman57d5d5d2008-11-05 02:06:09 +00001427 return DAG.getTargetConstant(Value, ValueType);
Scott Michel8efdca42007-12-04 22:23:35 +00001428 }
1429
Dan Gohman8181bd12008-07-27 21:46:04 +00001430 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001431}
1432
1433/// get_ILHUvec_imm - Test if this vector is a vector filled with the same value
1434/// and the value fits into a signed 16-bit constant, and if so, return the
1435/// constant
Dan Gohman8181bd12008-07-27 21:46:04 +00001436SDValue SPU::get_ILHUvec_imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00001437 MVT ValueType) {
Scott Michel8efdca42007-12-04 22:23:35 +00001438 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001439 uint64_t Value = CN->getZExtValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001440 if ((ValueType == MVT::i32
Scott Michel5a6f17b2008-01-30 02:55:46 +00001441 && ((unsigned) Value & 0xffff0000) == (unsigned) Value)
1442 || (ValueType == MVT::i64 && (Value & 0xffff0000) == Value))
Dan Gohman57d5d5d2008-11-05 02:06:09 +00001443 return DAG.getTargetConstant(Value >> 16, ValueType);
Scott Michel8efdca42007-12-04 22:23:35 +00001444 }
1445
Dan Gohman8181bd12008-07-27 21:46:04 +00001446 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001447}
1448
1449/// get_v4i32_imm - Catch-all for general 32-bit constant vectors
Dan Gohman8181bd12008-07-27 21:46:04 +00001450SDValue SPU::get_v4i32_imm(SDNode *N, SelectionDAG &DAG) {
Scott Michel8efdca42007-12-04 22:23:35 +00001451 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohman57d5d5d2008-11-05 02:06:09 +00001452 return DAG.getTargetConstant((unsigned) CN->getZExtValue(), MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00001453 }
1454
Dan Gohman8181bd12008-07-27 21:46:04 +00001455 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001456}
1457
1458/// get_v4i32_imm - Catch-all for general 64-bit constant vectors
Dan Gohman8181bd12008-07-27 21:46:04 +00001459SDValue SPU::get_v2i64_imm(SDNode *N, SelectionDAG &DAG) {
Scott Michel8efdca42007-12-04 22:23:35 +00001460 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohman57d5d5d2008-11-05 02:06:09 +00001461 return DAG.getTargetConstant((unsigned) CN->getZExtValue(), MVT::i64);
Scott Michel8efdca42007-12-04 22:23:35 +00001462 }
1463
Dan Gohman8181bd12008-07-27 21:46:04 +00001464 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001465}
1466
1467// If this is a vector of constants or undefs, get the bits. A bit in
Scott Michel4ec722e2008-07-16 17:17:29 +00001468// UndefBits is set if the corresponding element of the vector is an
Scott Michel8efdca42007-12-04 22:23:35 +00001469// ISD::UNDEF value. For undefs, the corresponding VectorBits values are
1470// zero. Return true if this is not an array of constants, false if it is.
1471//
1472static bool GetConstantBuildVectorBits(SDNode *BV, uint64_t VectorBits[2],
1473 uint64_t UndefBits[2]) {
1474 // Start with zero'd results.
1475 VectorBits[0] = VectorBits[1] = UndefBits[0] = UndefBits[1] = 0;
Scott Michel4ec722e2008-07-16 17:17:29 +00001476
Duncan Sands92c43912008-06-06 12:08:01 +00001477 unsigned EltBitSize = BV->getOperand(0).getValueType().getSizeInBits();
Scott Michel8efdca42007-12-04 22:23:35 +00001478 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001479 SDValue OpVal = BV->getOperand(i);
Scott Michel4ec722e2008-07-16 17:17:29 +00001480
Scott Michel8efdca42007-12-04 22:23:35 +00001481 unsigned PartNo = i >= e/2; // In the upper 128 bits?
1482 unsigned SlotNo = e/2 - (i & (e/2-1))-1; // Which subpiece of the uint64_t.
1483
1484 uint64_t EltBits = 0;
1485 if (OpVal.getOpcode() == ISD::UNDEF) {
1486 uint64_t EltUndefBits = ~0ULL >> (64-EltBitSize);
1487 UndefBits[PartNo] |= EltUndefBits << (SlotNo*EltBitSize);
1488 continue;
Scott Michel5974f432008-11-11 03:06:06 +00001489 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001490 EltBits = CN->getZExtValue() & (~0ULL >> (64-EltBitSize));
Scott Michel5974f432008-11-11 03:06:06 +00001491 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Scott Michel8efdca42007-12-04 22:23:35 +00001492 const APFloat &apf = CN->getValueAPF();
1493 EltBits = (CN->getValueType(0) == MVT::f32
Scott Michel5a6f17b2008-01-30 02:55:46 +00001494 ? FloatToBits(apf.convertToFloat())
1495 : DoubleToBits(apf.convertToDouble()));
Scott Michel8efdca42007-12-04 22:23:35 +00001496 } else {
1497 // Nonconstant element.
1498 return true;
1499 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001500
Scott Michel8efdca42007-12-04 22:23:35 +00001501 VectorBits[PartNo] |= EltBits << (SlotNo*EltBitSize);
1502 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001503
1504 //printf("%llx %llx %llx %llx\n",
Scott Michel8efdca42007-12-04 22:23:35 +00001505 // VectorBits[0], VectorBits[1], UndefBits[0], UndefBits[1]);
1506 return false;
1507}
1508
1509/// If this is a splat (repetition) of a value across the whole vector, return
1510/// the smallest size that splats it. For example, "0x01010101010101..." is a
Scott Michel4ec722e2008-07-16 17:17:29 +00001511/// splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
Scott Michel8efdca42007-12-04 22:23:35 +00001512/// SplatSize = 1 byte.
Scott Michel4ec722e2008-07-16 17:17:29 +00001513static bool isConstantSplat(const uint64_t Bits128[2],
Scott Michel8efdca42007-12-04 22:23:35 +00001514 const uint64_t Undef128[2],
Scott Michel5a6f17b2008-01-30 02:55:46 +00001515 int MinSplatBits,
Scott Michel8efdca42007-12-04 22:23:35 +00001516 uint64_t &SplatBits, uint64_t &SplatUndef,
1517 int &SplatSize) {
1518 // Don't let undefs prevent splats from matching. See if the top 64-bits are
1519 // the same as the lower 64-bits, ignoring undefs.
1520 uint64_t Bits64 = Bits128[0] | Bits128[1];
1521 uint64_t Undef64 = Undef128[0] & Undef128[1];
1522 uint32_t Bits32 = uint32_t(Bits64) | uint32_t(Bits64 >> 32);
1523 uint32_t Undef32 = uint32_t(Undef64) & uint32_t(Undef64 >> 32);
1524 uint16_t Bits16 = uint16_t(Bits32) | uint16_t(Bits32 >> 16);
1525 uint16_t Undef16 = uint16_t(Undef32) & uint16_t(Undef32 >> 16);
1526
1527 if ((Bits128[0] & ~Undef128[1]) == (Bits128[1] & ~Undef128[0])) {
1528 if (MinSplatBits < 64) {
Scott Michel4ec722e2008-07-16 17:17:29 +00001529
Scott Michel8efdca42007-12-04 22:23:35 +00001530 // Check that the top 32-bits are the same as the lower 32-bits, ignoring
1531 // undefs.
1532 if ((Bits64 & (~Undef64 >> 32)) == ((Bits64 >> 32) & ~Undef64)) {
Scott Michel5a6f17b2008-01-30 02:55:46 +00001533 if (MinSplatBits < 32) {
Scott Michel8efdca42007-12-04 22:23:35 +00001534
Scott Michel5a6f17b2008-01-30 02:55:46 +00001535 // If the top 16-bits are different than the lower 16-bits, ignoring
1536 // undefs, we have an i32 splat.
1537 if ((Bits32 & (~Undef32 >> 16)) == ((Bits32 >> 16) & ~Undef32)) {
1538 if (MinSplatBits < 16) {
1539 // If the top 8-bits are different than the lower 8-bits, ignoring
1540 // undefs, we have an i16 splat.
Gabor Greife9f7f582008-08-31 15:37:04 +00001541 if ((Bits16 & (uint16_t(~Undef16) >> 8))
1542 == ((Bits16 >> 8) & ~Undef16)) {
Scott Michel5a6f17b2008-01-30 02:55:46 +00001543 // Otherwise, we have an 8-bit splat.
1544 SplatBits = uint8_t(Bits16) | uint8_t(Bits16 >> 8);
1545 SplatUndef = uint8_t(Undef16) & uint8_t(Undef16 >> 8);
1546 SplatSize = 1;
1547 return true;
1548 }
1549 } else {
1550 SplatBits = Bits16;
1551 SplatUndef = Undef16;
1552 SplatSize = 2;
1553 return true;
1554 }
1555 }
1556 } else {
1557 SplatBits = Bits32;
1558 SplatUndef = Undef32;
1559 SplatSize = 4;
1560 return true;
1561 }
Scott Michel8efdca42007-12-04 22:23:35 +00001562 }
1563 } else {
1564 SplatBits = Bits128[0];
1565 SplatUndef = Undef128[0];
1566 SplatSize = 8;
1567 return true;
1568 }
1569 }
1570
1571 return false; // Can't be a splat if two pieces don't match.
1572}
1573
1574// If this is a case we can't handle, return null and let the default
1575// expansion code take care of it. If we CAN select this case, and if it
1576// selects to a single instruction, return Op. Otherwise, if we can codegen
1577// this case more efficiently than a constant pool load, lower it to the
1578// sequence of ops that should be used.
Dan Gohman8181bd12008-07-27 21:46:04 +00001579static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00001580 MVT VT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +00001581 // If this is a vector of constants or undefs, get the bits. A bit in
Scott Michel4ec722e2008-07-16 17:17:29 +00001582 // UndefBits is set if the corresponding element of the vector is an
Scott Michel8efdca42007-12-04 22:23:35 +00001583 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
Scott Michel4ec722e2008-07-16 17:17:29 +00001584 // zero.
Scott Michel8efdca42007-12-04 22:23:35 +00001585 uint64_t VectorBits[2];
1586 uint64_t UndefBits[2];
1587 uint64_t SplatBits, SplatUndef;
1588 int SplatSize;
Gabor Greif1c80d112008-08-28 21:40:38 +00001589 if (GetConstantBuildVectorBits(Op.getNode(), VectorBits, UndefBits)
Scott Michel8efdca42007-12-04 22:23:35 +00001590 || !isConstantSplat(VectorBits, UndefBits,
Duncan Sands92c43912008-06-06 12:08:01 +00001591 VT.getVectorElementType().getSizeInBits(),
Scott Michel8efdca42007-12-04 22:23:35 +00001592 SplatBits, SplatUndef, SplatSize))
Dan Gohman8181bd12008-07-27 21:46:04 +00001593 return SDValue(); // Not a constant vector, not a splat.
Scott Michel4ec722e2008-07-16 17:17:29 +00001594
Duncan Sands92c43912008-06-06 12:08:01 +00001595 switch (VT.getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001596 default:
1597 case MVT::v4f32: {
1598 uint32_t Value32 = SplatBits;
1599 assert(SplatSize == 4
Scott Michel5a6f17b2008-01-30 02:55:46 +00001600 && "LowerBUILD_VECTOR: Unexpected floating point vector element.");
Scott Michel8efdca42007-12-04 22:23:35 +00001601 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
Dan Gohman8181bd12008-07-27 21:46:04 +00001602 SDValue T = DAG.getConstant(Value32, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00001603 return DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001604 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, T, T, T, T));
Scott Michel8efdca42007-12-04 22:23:35 +00001605 break;
1606 }
1607 case MVT::v2f64: {
1608 uint64_t f64val = SplatBits;
1609 assert(SplatSize == 8
Scott Michelc630c412008-11-24 17:11:17 +00001610 && "LowerBUILD_VECTOR: 64-bit float vector size > 8 bytes.");
Scott Michel8efdca42007-12-04 22:23:35 +00001611 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
Dan Gohman8181bd12008-07-27 21:46:04 +00001612 SDValue T = DAG.getConstant(f64val, MVT::i64);
Scott Michel8efdca42007-12-04 22:23:35 +00001613 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001614 DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i64, T, T));
Scott Michel8efdca42007-12-04 22:23:35 +00001615 break;
1616 }
1617 case MVT::v16i8: {
1618 // 8-bit constants have to be expanded to 16-bits
1619 unsigned short Value16 = SplatBits | (SplatBits << 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00001620 SDValue Ops[8];
Scott Michel8efdca42007-12-04 22:23:35 +00001621 for (int i = 0; i < 8; ++i)
1622 Ops[i] = DAG.getConstant(Value16, MVT::i16);
1623 return DAG.getNode(ISD::BIT_CONVERT, VT,
1624 DAG.getNode(ISD::BUILD_VECTOR, MVT::v8i16, Ops, 8));
1625 }
1626 case MVT::v8i16: {
1627 unsigned short Value16;
Scott Michel4ec722e2008-07-16 17:17:29 +00001628 if (SplatSize == 2)
Scott Michel8efdca42007-12-04 22:23:35 +00001629 Value16 = (unsigned short) (SplatBits & 0xffff);
1630 else
1631 Value16 = (unsigned short) (SplatBits | (SplatBits << 8));
Dan Gohman8181bd12008-07-27 21:46:04 +00001632 SDValue T = DAG.getConstant(Value16, VT.getVectorElementType());
1633 SDValue Ops[8];
Scott Michel8efdca42007-12-04 22:23:35 +00001634 for (int i = 0; i < 8; ++i) Ops[i] = T;
1635 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops, 8);
1636 }
1637 case MVT::v4i32: {
1638 unsigned int Value = SplatBits;
Dan Gohman8181bd12008-07-27 21:46:04 +00001639 SDValue T = DAG.getConstant(Value, VT.getVectorElementType());
Scott Michel8efdca42007-12-04 22:23:35 +00001640 return DAG.getNode(ISD::BUILD_VECTOR, VT, T, T, T, T);
1641 }
1642 case MVT::v2i64: {
1643 uint64_t val = SplatBits;
1644 uint32_t upper = uint32_t(val >> 32);
1645 uint32_t lower = uint32_t(val);
1646
Scott Michelbcc7b672008-03-06 04:02:54 +00001647 if (upper == lower) {
1648 // Magic constant that can be matched by IL, ILA, et. al.
Dan Gohman8181bd12008-07-27 21:46:04 +00001649 SDValue Val = DAG.getTargetConstant(val, MVT::i64);
Scott Michelbcc7b672008-03-06 04:02:54 +00001650 return DAG.getNode(ISD::BUILD_VECTOR, VT, Val, Val);
Scott Michel6baba072008-03-05 23:02:02 +00001651 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001652 SDValue LO32;
1653 SDValue HI32;
1654 SmallVector<SDValue, 16> ShufBytes;
1655 SDValue Result;
Scott Michel8efdca42007-12-04 22:23:35 +00001656 bool upper_special, lower_special;
1657
1658 // NOTE: This code creates common-case shuffle masks that can be easily
1659 // detected as common expressions. It is not attempting to create highly
1660 // specialized masks to replace any and all 0's, 0xff's and 0x80's.
1661
1662 // Detect if the upper or lower half is a special shuffle mask pattern:
Scott Michelc630c412008-11-24 17:11:17 +00001663 upper_special = (upper == 0||upper == 0xffffffff||upper == 0x80000000);
1664 lower_special = (lower == 0||lower == 0xffffffff||lower == 0x80000000);
Scott Michel8efdca42007-12-04 22:23:35 +00001665
1666 // Create lower vector if not a special pattern
1667 if (!lower_special) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001668 SDValue LO32C = DAG.getConstant(lower, MVT::i32);
Scott Michel5a6f17b2008-01-30 02:55:46 +00001669 LO32 = DAG.getNode(ISD::BIT_CONVERT, VT,
1670 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1671 LO32C, LO32C, LO32C, LO32C));
Scott Michel8efdca42007-12-04 22:23:35 +00001672 }
1673
1674 // Create upper vector if not a special pattern
1675 if (!upper_special) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001676 SDValue HI32C = DAG.getConstant(upper, MVT::i32);
Scott Michel5a6f17b2008-01-30 02:55:46 +00001677 HI32 = DAG.getNode(ISD::BIT_CONVERT, VT,
1678 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1679 HI32C, HI32C, HI32C, HI32C));
Scott Michel8efdca42007-12-04 22:23:35 +00001680 }
1681
1682 // If either upper or lower are special, then the two input operands are
1683 // the same (basically, one of them is a "don't care")
1684 if (lower_special)
Scott Michel5a6f17b2008-01-30 02:55:46 +00001685 LO32 = HI32;
Scott Michel8efdca42007-12-04 22:23:35 +00001686 if (upper_special)
Scott Michel5a6f17b2008-01-30 02:55:46 +00001687 HI32 = LO32;
Scott Michel8efdca42007-12-04 22:23:35 +00001688 if (lower_special && upper_special) {
Scott Michel5a6f17b2008-01-30 02:55:46 +00001689 // Unhappy situation... both upper and lower are special, so punt with
1690 // a target constant:
Dan Gohman8181bd12008-07-27 21:46:04 +00001691 SDValue Zero = DAG.getConstant(0, MVT::i32);
Scott Michel5a6f17b2008-01-30 02:55:46 +00001692 HI32 = LO32 = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Zero, Zero,
Scott Michel8efdca42007-12-04 22:23:35 +00001693 Zero, Zero);
1694 }
1695
1696 for (int i = 0; i < 4; ++i) {
Scott Michel67224b22008-06-02 22:18:03 +00001697 uint64_t val = 0;
Scott Michel5a6f17b2008-01-30 02:55:46 +00001698 for (int j = 0; j < 4; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001699 SDValue V;
Scott Michel5a6f17b2008-01-30 02:55:46 +00001700 bool process_upper, process_lower;
Scott Michel67224b22008-06-02 22:18:03 +00001701 val <<= 8;
Scott Michel5a6f17b2008-01-30 02:55:46 +00001702 process_upper = (upper_special && (i & 1) == 0);
1703 process_lower = (lower_special && (i & 1) == 1);
Scott Michel8efdca42007-12-04 22:23:35 +00001704
Scott Michel5a6f17b2008-01-30 02:55:46 +00001705 if (process_upper || process_lower) {
1706 if ((process_upper && upper == 0)
1707 || (process_lower && lower == 0))
Scott Michel67224b22008-06-02 22:18:03 +00001708 val |= 0x80;
Scott Michel5a6f17b2008-01-30 02:55:46 +00001709 else if ((process_upper && upper == 0xffffffff)
1710 || (process_lower && lower == 0xffffffff))
Scott Michel67224b22008-06-02 22:18:03 +00001711 val |= 0xc0;
Scott Michel5a6f17b2008-01-30 02:55:46 +00001712 else if ((process_upper && upper == 0x80000000)
1713 || (process_lower && lower == 0x80000000))
Scott Michel67224b22008-06-02 22:18:03 +00001714 val |= (j == 0 ? 0xe0 : 0x80);
Scott Michel5a6f17b2008-01-30 02:55:46 +00001715 } else
Scott Michel67224b22008-06-02 22:18:03 +00001716 val |= i * 4 + j + ((i & 1) * 16);
Scott Michel5a6f17b2008-01-30 02:55:46 +00001717 }
Scott Michel67224b22008-06-02 22:18:03 +00001718
1719 ShufBytes.push_back(DAG.getConstant(val, MVT::i32));
Scott Michel8efdca42007-12-04 22:23:35 +00001720 }
1721
1722 return DAG.getNode(SPUISD::SHUFB, VT, HI32, LO32,
Scott Michel67224b22008-06-02 22:18:03 +00001723 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001724 &ShufBytes[0], ShufBytes.size()));
Scott Michel8efdca42007-12-04 22:23:35 +00001725 }
1726 }
1727 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001728
Dan Gohman8181bd12008-07-27 21:46:04 +00001729 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001730}
1731
1732/// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3) to something on
1733/// which the Cell can operate. The code inspects V3 to ascertain whether the
1734/// permutation vector, V3, is monotonically increasing with one "exception"
1735/// element, e.g., (0, 1, _, 3). If this is the case, then generate a
Scott Michel56a125e2008-11-22 23:50:42 +00001736/// SHUFFLE_MASK synthetic instruction. Otherwise, spill V3 to the constant pool.
Scott Michel8efdca42007-12-04 22:23:35 +00001737/// In either case, the net result is going to eventually invoke SHUFB to
1738/// permute/shuffle the bytes from V1 and V2.
1739/// \note
Scott Michel56a125e2008-11-22 23:50:42 +00001740/// SHUFFLE_MASK is eventually selected as one of the C*D instructions, generate
Scott Michel8efdca42007-12-04 22:23:35 +00001741/// control word for byte/halfword/word insertion. This takes care of a single
1742/// element move from V2 into V1.
1743/// \note
1744/// SPUISD::SHUFB is eventually selected as Cell's <i>shufb</i> instructions.
Dan Gohman8181bd12008-07-27 21:46:04 +00001745static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
1746 SDValue V1 = Op.getOperand(0);
1747 SDValue V2 = Op.getOperand(1);
1748 SDValue PermMask = Op.getOperand(2);
Scott Michel4ec722e2008-07-16 17:17:29 +00001749
Scott Michel8efdca42007-12-04 22:23:35 +00001750 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michel4ec722e2008-07-16 17:17:29 +00001751
Scott Michel8efdca42007-12-04 22:23:35 +00001752 // If we have a single element being moved from V1 to V2, this can be handled
1753 // using the C*[DX] compute mask instructions, but the vector elements have
1754 // to be monotonically increasing with one exception element.
Scott Michele2641a12008-12-04 21:01:44 +00001755 MVT VecVT = V1.getValueType();
1756 MVT EltVT = VecVT.getVectorElementType();
Scott Michel8efdca42007-12-04 22:23:35 +00001757 unsigned EltsFromV2 = 0;
1758 unsigned V2Elt = 0;
1759 unsigned V2EltIdx0 = 0;
1760 unsigned CurrElt = 0;
Scott Michele2641a12008-12-04 21:01:44 +00001761 unsigned MaxElts = VecVT.getVectorNumElements();
1762 unsigned PrevElt = 0;
1763 unsigned V0Elt = 0;
Scott Michel8efdca42007-12-04 22:23:35 +00001764 bool monotonic = true;
Scott Michele2641a12008-12-04 21:01:44 +00001765 bool rotate = true;
1766
1767 if (EltVT == MVT::i8) {
Scott Michel8efdca42007-12-04 22:23:35 +00001768 V2EltIdx0 = 16;
Scott Michele2641a12008-12-04 21:01:44 +00001769 } else if (EltVT == MVT::i16) {
Scott Michel8efdca42007-12-04 22:23:35 +00001770 V2EltIdx0 = 8;
Scott Michele2641a12008-12-04 21:01:44 +00001771 } else if (EltVT == MVT::i32 || EltVT == MVT::f32) {
Scott Michel8efdca42007-12-04 22:23:35 +00001772 V2EltIdx0 = 4;
Scott Michele2641a12008-12-04 21:01:44 +00001773 } else if (EltVT == MVT::i64 || EltVT == MVT::f64) {
1774 V2EltIdx0 = 2;
1775 } else
Scott Michel8efdca42007-12-04 22:23:35 +00001776 assert(0 && "Unhandled vector type in LowerVECTOR_SHUFFLE");
1777
Scott Michele2641a12008-12-04 21:01:44 +00001778 for (unsigned i = 0; i != PermMask.getNumOperands(); ++i) {
1779 if (PermMask.getOperand(i).getOpcode() != ISD::UNDEF) {
1780 unsigned SrcElt = cast<ConstantSDNode > (PermMask.getOperand(i))->getZExtValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001781
Scott Michele2641a12008-12-04 21:01:44 +00001782 if (monotonic) {
1783 if (SrcElt >= V2EltIdx0) {
1784 if (1 >= (++EltsFromV2)) {
1785 V2Elt = (V2EltIdx0 - SrcElt) << 2;
1786 }
1787 } else if (CurrElt != SrcElt) {
1788 monotonic = false;
1789 }
1790
1791 ++CurrElt;
1792 }
1793
1794 if (rotate) {
1795 if (PrevElt > 0 && SrcElt < MaxElts) {
1796 if ((PrevElt == SrcElt - 1)
1797 || (PrevElt == MaxElts - 1 && SrcElt == 0)) {
1798 PrevElt = SrcElt;
1799 if (SrcElt == 0)
1800 V0Elt = i;
1801 } else {
1802 rotate = false;
1803 }
1804 } else if (PrevElt == 0) {
1805 // First time through, need to keep track of previous element
1806 PrevElt = SrcElt;
1807 } else {
1808 // This isn't a rotation, takes elements from vector 2
1809 rotate = false;
1810 }
1811 }
Scott Michel8efdca42007-12-04 22:23:35 +00001812 }
Scott Michel8efdca42007-12-04 22:23:35 +00001813 }
1814
1815 if (EltsFromV2 == 1 && monotonic) {
1816 // Compute mask and shuffle
1817 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +00001818 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1819 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
Duncan Sands92c43912008-06-06 12:08:01 +00001820 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel8efdca42007-12-04 22:23:35 +00001821 // Initialize temporary register to 0
Dan Gohman8181bd12008-07-27 21:46:04 +00001822 SDValue InitTempReg =
Scott Michel8efdca42007-12-04 22:23:35 +00001823 DAG.getCopyToReg(DAG.getEntryNode(), VReg, DAG.getConstant(0, PtrVT));
Scott Michel56a125e2008-11-22 23:50:42 +00001824 // Copy register's contents as index in SHUFFLE_MASK:
Dan Gohman8181bd12008-07-27 21:46:04 +00001825 SDValue ShufMaskOp =
Scott Michel0718cd82008-12-01 17:56:02 +00001826 DAG.getNode(SPUISD::SHUFFLE_MASK, MVT::v4i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001827 DAG.getTargetConstant(V2Elt, MVT::i32),
1828 DAG.getCopyFromReg(InitTempReg, VReg, PtrVT));
Scott Michel8efdca42007-12-04 22:23:35 +00001829 // Use shuffle mask in SHUFB synthetic instruction:
1830 return DAG.getNode(SPUISD::SHUFB, V1.getValueType(), V2, V1, ShufMaskOp);
Scott Michele2641a12008-12-04 21:01:44 +00001831 } else if (rotate) {
1832 int rotamt = (MaxElts - V0Elt) * EltVT.getSizeInBits()/8;
1833
1834 return DAG.getNode(SPUISD::ROTBYTES_LEFT, V1.getValueType(),
1835 V1, DAG.getConstant(rotamt, MVT::i16));
Scott Michel8efdca42007-12-04 22:23:35 +00001836 } else {
Gabor Greife9f7f582008-08-31 15:37:04 +00001837 // Convert the SHUFFLE_VECTOR mask's input element units to the
1838 // actual bytes.
Duncan Sands92c43912008-06-06 12:08:01 +00001839 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michel4ec722e2008-07-16 17:17:29 +00001840
Dan Gohman8181bd12008-07-27 21:46:04 +00001841 SmallVector<SDValue, 16> ResultMask;
Scott Michel8efdca42007-12-04 22:23:35 +00001842 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
1843 unsigned SrcElt;
1844 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
Scott Michel5a6f17b2008-01-30 02:55:46 +00001845 SrcElt = 0;
Scott Michel4ec722e2008-07-16 17:17:29 +00001846 else
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001847 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getZExtValue();
Scott Michel4ec722e2008-07-16 17:17:29 +00001848
Scott Michel97872d32008-02-23 18:41:37 +00001849 for (unsigned j = 0; j < BytesPerElement; ++j) {
Scott Michel5a6f17b2008-01-30 02:55:46 +00001850 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
1851 MVT::i8));
Scott Michel8efdca42007-12-04 22:23:35 +00001852 }
1853 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001854
Dan Gohman8181bd12008-07-27 21:46:04 +00001855 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8,
Scott Michel0718cd82008-12-01 17:56:02 +00001856 &ResultMask[0], ResultMask.size());
Scott Michel8efdca42007-12-04 22:23:35 +00001857 return DAG.getNode(SPUISD::SHUFB, V1.getValueType(), V1, V2, VPermMask);
1858 }
1859}
1860
Dan Gohman8181bd12008-07-27 21:46:04 +00001861static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
1862 SDValue Op0 = Op.getOperand(0); // Op0 = the scalar
Scott Michel8efdca42007-12-04 22:23:35 +00001863
Gabor Greif1c80d112008-08-28 21:40:38 +00001864 if (Op0.getNode()->getOpcode() == ISD::Constant) {
Scott Michel8efdca42007-12-04 22:23:35 +00001865 // For a constant, build the appropriate constant vector, which will
1866 // eventually simplify to a vector register load.
1867
Gabor Greif1c80d112008-08-28 21:40:38 +00001868 ConstantSDNode *CN = cast<ConstantSDNode>(Op0.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00001869 SmallVector<SDValue, 16> ConstVecValues;
Duncan Sands92c43912008-06-06 12:08:01 +00001870 MVT VT;
Scott Michel8efdca42007-12-04 22:23:35 +00001871 size_t n_copies;
1872
1873 // Create a constant vector:
Duncan Sands92c43912008-06-06 12:08:01 +00001874 switch (Op.getValueType().getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001875 default: assert(0 && "Unexpected constant value type in "
Scott Michel5a6f17b2008-01-30 02:55:46 +00001876 "LowerSCALAR_TO_VECTOR");
Scott Michel8efdca42007-12-04 22:23:35 +00001877 case MVT::v16i8: n_copies = 16; VT = MVT::i8; break;
1878 case MVT::v8i16: n_copies = 8; VT = MVT::i16; break;
1879 case MVT::v4i32: n_copies = 4; VT = MVT::i32; break;
1880 case MVT::v4f32: n_copies = 4; VT = MVT::f32; break;
1881 case MVT::v2i64: n_copies = 2; VT = MVT::i64; break;
1882 case MVT::v2f64: n_copies = 2; VT = MVT::f64; break;
1883 }
1884
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001885 SDValue CValue = DAG.getConstant(CN->getZExtValue(), VT);
Scott Michel8efdca42007-12-04 22:23:35 +00001886 for (size_t j = 0; j < n_copies; ++j)
1887 ConstVecValues.push_back(CValue);
1888
1889 return DAG.getNode(ISD::BUILD_VECTOR, Op.getValueType(),
Scott Michel5a6f17b2008-01-30 02:55:46 +00001890 &ConstVecValues[0], ConstVecValues.size());
Scott Michel8efdca42007-12-04 22:23:35 +00001891 } else {
1892 // Otherwise, copy the value from one register to another:
Duncan Sands92c43912008-06-06 12:08:01 +00001893 switch (Op0.getValueType().getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001894 default: assert(0 && "Unexpected value type in LowerSCALAR_TO_VECTOR");
1895 case MVT::i8:
1896 case MVT::i16:
1897 case MVT::i32:
1898 case MVT::i64:
1899 case MVT::f32:
1900 case MVT::f64:
1901 return DAG.getNode(SPUISD::PROMOTE_SCALAR, Op.getValueType(), Op0, Op0);
1902 }
1903 }
1904
Dan Gohman8181bd12008-07-27 21:46:04 +00001905 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001906}
1907
Dan Gohman8181bd12008-07-27 21:46:04 +00001908static SDValue LowerVectorMUL(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00001909 switch (Op.getValueType().getSimpleVT()) {
1910 default:
1911 cerr << "CellSPU: Unknown vector multiplication, got "
1912 << Op.getValueType().getMVTString()
1913 << "\n";
1914 abort();
1915 /*NOTREACHED*/
1916
Scott Michel8efdca42007-12-04 22:23:35 +00001917 case MVT::v4i32: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001918 SDValue rA = Op.getOperand(0);
1919 SDValue rB = Op.getOperand(1);
1920 SDValue HiProd1 = DAG.getNode(SPUISD::MPYH, MVT::v4i32, rA, rB);
1921 SDValue HiProd2 = DAG.getNode(SPUISD::MPYH, MVT::v4i32, rB, rA);
1922 SDValue LoProd = DAG.getNode(SPUISD::MPYU, MVT::v4i32, rA, rB);
1923 SDValue Residual1 = DAG.getNode(ISD::ADD, MVT::v4i32, LoProd, HiProd1);
Scott Michel8efdca42007-12-04 22:23:35 +00001924
1925 return DAG.getNode(ISD::ADD, MVT::v4i32, Residual1, HiProd2);
1926 break;
1927 }
1928
1929 // Multiply two v8i16 vectors (pipeline friendly version):
1930 // a) multiply lower halves, mask off upper 16-bit of 32-bit product
1931 // b) multiply upper halves, rotate left by 16 bits (inserts 16 lower zeroes)
1932 // c) Use SELB to select upper and lower halves from the intermediate results
1933 //
Scott Michel67224b22008-06-02 22:18:03 +00001934 // NOTE: We really want to move the SELECT_MASK to earlier to actually get the
Scott Michel8efdca42007-12-04 22:23:35 +00001935 // dual-issue. This code does manage to do this, even if it's a little on
1936 // the wacky side
1937 case MVT::v8i16: {
1938 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +00001939 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001940 SDValue Chain = Op.getOperand(0);
1941 SDValue rA = Op.getOperand(0);
1942 SDValue rB = Op.getOperand(1);
Chris Lattner1b989192007-12-31 04:13:23 +00001943 unsigned FSMBIreg = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
1944 unsigned HiProdReg = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
Scott Michel8efdca42007-12-04 22:23:35 +00001945
Dan Gohman8181bd12008-07-27 21:46:04 +00001946 SDValue FSMBOp =
Scott Michel8efdca42007-12-04 22:23:35 +00001947 DAG.getCopyToReg(Chain, FSMBIreg,
Scott Michel67224b22008-06-02 22:18:03 +00001948 DAG.getNode(SPUISD::SELECT_MASK, MVT::v8i16,
Scott Michelbc5fbc12008-04-30 00:30:08 +00001949 DAG.getConstant(0xcccc, MVT::i16)));
Scott Michel8efdca42007-12-04 22:23:35 +00001950
Dan Gohman8181bd12008-07-27 21:46:04 +00001951 SDValue HHProd =
Scott Michel8efdca42007-12-04 22:23:35 +00001952 DAG.getCopyToReg(FSMBOp, HiProdReg,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001953 DAG.getNode(SPUISD::MPYHH, MVT::v8i16, rA, rB));
Scott Michel8efdca42007-12-04 22:23:35 +00001954
Dan Gohman8181bd12008-07-27 21:46:04 +00001955 SDValue HHProd_v4i32 =
Scott Michel8efdca42007-12-04 22:23:35 +00001956 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001957 DAG.getCopyFromReg(HHProd, HiProdReg, MVT::v4i32));
Scott Michel8efdca42007-12-04 22:23:35 +00001958
1959 return DAG.getNode(SPUISD::SELB, MVT::v8i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001960 DAG.getNode(SPUISD::MPY, MVT::v8i16, rA, rB),
1961 DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
1962 DAG.getNode(SPUISD::VEC_SHL, MVT::v4i32,
1963 HHProd_v4i32,
1964 DAG.getConstant(16, MVT::i16))),
1965 DAG.getCopyFromReg(FSMBOp, FSMBIreg, MVT::v4i32));
Scott Michel8efdca42007-12-04 22:23:35 +00001966 }
1967
1968 // This M00sE is N@stI! (apologies to Monty Python)
1969 //
1970 // SPU doesn't know how to do any 8-bit multiplication, so the solution
1971 // is to break it all apart, sign extend, and reassemble the various
1972 // intermediate products.
1973 case MVT::v16i8: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001974 SDValue rA = Op.getOperand(0);
1975 SDValue rB = Op.getOperand(1);
1976 SDValue c8 = DAG.getConstant(8, MVT::i32);
1977 SDValue c16 = DAG.getConstant(16, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00001978
Dan Gohman8181bd12008-07-27 21:46:04 +00001979 SDValue LLProd =
Scott Michel8efdca42007-12-04 22:23:35 +00001980 DAG.getNode(SPUISD::MPY, MVT::v8i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001981 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rA),
1982 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rB));
Scott Michel8efdca42007-12-04 22:23:35 +00001983
Dan Gohman8181bd12008-07-27 21:46:04 +00001984 SDValue rALH = DAG.getNode(SPUISD::VEC_SRA, MVT::v8i16, rA, c8);
Scott Michel8efdca42007-12-04 22:23:35 +00001985
Dan Gohman8181bd12008-07-27 21:46:04 +00001986 SDValue rBLH = DAG.getNode(SPUISD::VEC_SRA, MVT::v8i16, rB, c8);
Scott Michel8efdca42007-12-04 22:23:35 +00001987
Dan Gohman8181bd12008-07-27 21:46:04 +00001988 SDValue LHProd =
Scott Michel8efdca42007-12-04 22:23:35 +00001989 DAG.getNode(SPUISD::VEC_SHL, MVT::v8i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001990 DAG.getNode(SPUISD::MPY, MVT::v8i16, rALH, rBLH), c8);
Scott Michel8efdca42007-12-04 22:23:35 +00001991
Dan Gohman8181bd12008-07-27 21:46:04 +00001992 SDValue FSMBmask = DAG.getNode(SPUISD::SELECT_MASK, MVT::v8i16,
Scott Michelbc5fbc12008-04-30 00:30:08 +00001993 DAG.getConstant(0x2222, MVT::i16));
Scott Michel8efdca42007-12-04 22:23:35 +00001994
Dan Gohman8181bd12008-07-27 21:46:04 +00001995 SDValue LoProdParts =
Scott Michel97872d32008-02-23 18:41:37 +00001996 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
1997 DAG.getNode(SPUISD::SELB, MVT::v8i16,
1998 LLProd, LHProd, FSMBmask));
Scott Michel8efdca42007-12-04 22:23:35 +00001999
Dan Gohman8181bd12008-07-27 21:46:04 +00002000 SDValue LoProdMask = DAG.getConstant(0xffff, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00002001
Dan Gohman8181bd12008-07-27 21:46:04 +00002002 SDValue LoProd =
Scott Michel8efdca42007-12-04 22:23:35 +00002003 DAG.getNode(ISD::AND, MVT::v4i32,
Scott Michel97872d32008-02-23 18:41:37 +00002004 LoProdParts,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002005 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
2006 LoProdMask, LoProdMask,
2007 LoProdMask, LoProdMask));
Scott Michel8efdca42007-12-04 22:23:35 +00002008
Dan Gohman8181bd12008-07-27 21:46:04 +00002009 SDValue rAH =
Scott Michel8efdca42007-12-04 22:23:35 +00002010 DAG.getNode(SPUISD::VEC_SRA, MVT::v4i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002011 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, rA), c16);
Scott Michel8efdca42007-12-04 22:23:35 +00002012
Dan Gohman8181bd12008-07-27 21:46:04 +00002013 SDValue rBH =
Scott Michel8efdca42007-12-04 22:23:35 +00002014 DAG.getNode(SPUISD::VEC_SRA, MVT::v4i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002015 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, rB), c16);
Scott Michel8efdca42007-12-04 22:23:35 +00002016
Dan Gohman8181bd12008-07-27 21:46:04 +00002017 SDValue HLProd =
Scott Michel8efdca42007-12-04 22:23:35 +00002018 DAG.getNode(SPUISD::MPY, MVT::v8i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002019 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rAH),
2020 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rBH));
Scott Michel8efdca42007-12-04 22:23:35 +00002021
Dan Gohman8181bd12008-07-27 21:46:04 +00002022 SDValue HHProd_1 =
Scott Michel8efdca42007-12-04 22:23:35 +00002023 DAG.getNode(SPUISD::MPY, MVT::v8i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002024 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16,
Gabor Greife9f7f582008-08-31 15:37:04 +00002025 DAG.getNode(SPUISD::VEC_SRA,
2026 MVT::v4i32, rAH, c8)),
Scott Michel5a6f17b2008-01-30 02:55:46 +00002027 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16,
Gabor Greife9f7f582008-08-31 15:37:04 +00002028 DAG.getNode(SPUISD::VEC_SRA,
2029 MVT::v4i32, rBH, c8)));
Scott Michel8efdca42007-12-04 22:23:35 +00002030
Dan Gohman8181bd12008-07-27 21:46:04 +00002031 SDValue HHProd =
Scott Michel97872d32008-02-23 18:41:37 +00002032 DAG.getNode(SPUISD::SELB, MVT::v8i16,
2033 HLProd,
2034 DAG.getNode(SPUISD::VEC_SHL, MVT::v8i16, HHProd_1, c8),
2035 FSMBmask);
Scott Michel8efdca42007-12-04 22:23:35 +00002036
Dan Gohman8181bd12008-07-27 21:46:04 +00002037 SDValue HiProd =
Scott Michel97872d32008-02-23 18:41:37 +00002038 DAG.getNode(SPUISD::VEC_SHL, MVT::v4i32, HHProd, c16);
Scott Michel8efdca42007-12-04 22:23:35 +00002039
2040 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002041 DAG.getNode(ISD::OR, MVT::v4i32,
2042 LoProd, HiProd));
Scott Michel8efdca42007-12-04 22:23:35 +00002043 }
Scott Michel8efdca42007-12-04 22:23:35 +00002044 }
2045
Dan Gohman8181bd12008-07-27 21:46:04 +00002046 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00002047}
2048
Dan Gohman8181bd12008-07-27 21:46:04 +00002049static SDValue LowerFDIVf32(SDValue Op, SelectionDAG &DAG) {
Scott Michel8efdca42007-12-04 22:23:35 +00002050 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +00002051 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel8efdca42007-12-04 22:23:35 +00002052
Dan Gohman8181bd12008-07-27 21:46:04 +00002053 SDValue A = Op.getOperand(0);
2054 SDValue B = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00002055 MVT VT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +00002056
2057 unsigned VRegBR, VRegC;
2058
2059 if (VT == MVT::f32) {
Chris Lattner1b989192007-12-31 04:13:23 +00002060 VRegBR = RegInfo.createVirtualRegister(&SPU::R32FPRegClass);
2061 VRegC = RegInfo.createVirtualRegister(&SPU::R32FPRegClass);
Scott Michel8efdca42007-12-04 22:23:35 +00002062 } else {
Chris Lattner1b989192007-12-31 04:13:23 +00002063 VRegBR = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
2064 VRegC = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
Scott Michel8efdca42007-12-04 22:23:35 +00002065 }
2066 // TODO: make sure we're feeding FPInterp the right arguments
2067 // Right now: fi B, frest(B)
2068
2069 // Computes BRcpl =
2070 // (Floating Interpolate (FP Reciprocal Estimate B))
Dan Gohman8181bd12008-07-27 21:46:04 +00002071 SDValue BRcpl =
Scott Michel4ec722e2008-07-16 17:17:29 +00002072 DAG.getCopyToReg(DAG.getEntryNode(), VRegBR,
2073 DAG.getNode(SPUISD::FPInterp, VT, B,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002074 DAG.getNode(SPUISD::FPRecipEst, VT, B)));
Scott Michel4ec722e2008-07-16 17:17:29 +00002075
Scott Michel8efdca42007-12-04 22:23:35 +00002076 // Computes A * BRcpl and stores in a temporary register
Dan Gohman8181bd12008-07-27 21:46:04 +00002077 SDValue AxBRcpl =
Scott Michel8efdca42007-12-04 22:23:35 +00002078 DAG.getCopyToReg(BRcpl, VRegC,
Scott Michel4ec722e2008-07-16 17:17:29 +00002079 DAG.getNode(ISD::FMUL, VT, A,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002080 DAG.getCopyFromReg(BRcpl, VRegBR, VT)));
Scott Michel8efdca42007-12-04 22:23:35 +00002081 // What's the Chain variable do? It's magic!
2082 // TODO: set Chain = Op(0).getEntryNode()
Scott Michel4ec722e2008-07-16 17:17:29 +00002083
2084 return DAG.getNode(ISD::FADD, VT,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002085 DAG.getCopyFromReg(AxBRcpl, VRegC, VT),
Scott Michel4ec722e2008-07-16 17:17:29 +00002086 DAG.getNode(ISD::FMUL, VT,
2087 DAG.getCopyFromReg(AxBRcpl, VRegBR, VT),
Scott Michel5a6f17b2008-01-30 02:55:46 +00002088 DAG.getNode(ISD::FSUB, VT, A,
Scott Michel4ec722e2008-07-16 17:17:29 +00002089 DAG.getNode(ISD::FMUL, VT, B,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002090 DAG.getCopyFromReg(AxBRcpl, VRegC, VT)))));
Scott Michel8efdca42007-12-04 22:23:35 +00002091}
2092
Dan Gohman8181bd12008-07-27 21:46:04 +00002093static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002094 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002095 SDValue N = Op.getOperand(0);
2096 SDValue Elt = Op.getOperand(1);
Scott Michel56a125e2008-11-22 23:50:42 +00002097 SDValue retval;
Scott Michel8efdca42007-12-04 22:23:35 +00002098
Scott Michel56a125e2008-11-22 23:50:42 +00002099 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
2100 // Constant argument:
2101 int EltNo = (int) C->getZExtValue();
Scott Michel8efdca42007-12-04 22:23:35 +00002102
Scott Michel56a125e2008-11-22 23:50:42 +00002103 // sanity checks:
2104 if (VT == MVT::i8 && EltNo >= 16)
2105 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i8 extraction slot > 15");
2106 else if (VT == MVT::i16 && EltNo >= 8)
2107 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i16 extraction slot > 7");
2108 else if (VT == MVT::i32 && EltNo >= 4)
2109 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i32 extraction slot > 4");
2110 else if (VT == MVT::i64 && EltNo >= 2)
2111 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i64 extraction slot > 2");
Scott Michel8efdca42007-12-04 22:23:35 +00002112
Scott Michel56a125e2008-11-22 23:50:42 +00002113 if (EltNo == 0 && (VT == MVT::i32 || VT == MVT::i64)) {
2114 // i32 and i64: Element 0 is the preferred slot
Scott Michelc630c412008-11-24 17:11:17 +00002115 return DAG.getNode(SPUISD::VEC2PREFSLOT, VT, N);
Scott Michel56a125e2008-11-22 23:50:42 +00002116 }
Scott Michel8efdca42007-12-04 22:23:35 +00002117
Scott Michel56a125e2008-11-22 23:50:42 +00002118 // Need to generate shuffle mask and extract:
2119 int prefslot_begin = -1, prefslot_end = -1;
2120 int elt_byte = EltNo * VT.getSizeInBits() / 8;
2121
2122 switch (VT.getSimpleVT()) {
2123 default:
2124 assert(false && "Invalid value type!");
2125 case MVT::i8: {
2126 prefslot_begin = prefslot_end = 3;
2127 break;
2128 }
2129 case MVT::i16: {
2130 prefslot_begin = 2; prefslot_end = 3;
2131 break;
2132 }
2133 case MVT::i32:
2134 case MVT::f32: {
2135 prefslot_begin = 0; prefslot_end = 3;
2136 break;
2137 }
2138 case MVT::i64:
2139 case MVT::f64: {
2140 prefslot_begin = 0; prefslot_end = 7;
2141 break;
2142 }
2143 }
2144
2145 assert(prefslot_begin != -1 && prefslot_end != -1 &&
2146 "LowerEXTRACT_VECTOR_ELT: preferred slots uninitialized");
2147
2148 unsigned int ShufBytes[16];
2149 for (int i = 0; i < 16; ++i) {
2150 // zero fill uppper part of preferred slot, don't care about the
2151 // other slots:
2152 unsigned int mask_val;
2153 if (i <= prefslot_end) {
2154 mask_val =
2155 ((i < prefslot_begin)
2156 ? 0x80
2157 : elt_byte + (i - prefslot_begin));
2158
2159 ShufBytes[i] = mask_val;
2160 } else
2161 ShufBytes[i] = ShufBytes[i % (prefslot_end + 1)];
2162 }
2163
2164 SDValue ShufMask[4];
2165 for (unsigned i = 0; i < sizeof(ShufMask)/sizeof(ShufMask[0]); ++i) {
Scott Michele2641a12008-12-04 21:01:44 +00002166 unsigned bidx = i * 4;
Scott Michel56a125e2008-11-22 23:50:42 +00002167 unsigned int bits = ((ShufBytes[bidx] << 24) |
2168 (ShufBytes[bidx+1] << 16) |
2169 (ShufBytes[bidx+2] << 8) |
2170 ShufBytes[bidx+3]);
2171 ShufMask[i] = DAG.getConstant(bits, MVT::i32);
2172 }
2173
2174 SDValue ShufMaskVec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
2175 &ShufMask[0],
2176 sizeof(ShufMask) / sizeof(ShufMask[0]));
2177
Scott Michelc630c412008-11-24 17:11:17 +00002178 retval = DAG.getNode(SPUISD::VEC2PREFSLOT, VT,
Scott Michel56a125e2008-11-22 23:50:42 +00002179 DAG.getNode(SPUISD::SHUFB, N.getValueType(),
2180 N, N, ShufMaskVec));
2181 } else {
2182 // Variable index: Rotate the requested element into slot 0, then replicate
2183 // slot 0 across the vector
2184 MVT VecVT = N.getValueType();
2185 if (!VecVT.isSimple() || !VecVT.isVector() || !VecVT.is128BitVector()) {
2186 cerr << "LowerEXTRACT_VECTOR_ELT: Must have a simple, 128-bit vector type!\n";
2187 abort();
2188 }
2189
2190 // Make life easier by making sure the index is zero-extended to i32
2191 if (Elt.getValueType() != MVT::i32)
2192 Elt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Elt);
2193
2194 // Scale the index to a bit/byte shift quantity
2195 APInt scaleFactor =
Scott Michelc630c412008-11-24 17:11:17 +00002196 APInt(32, uint64_t(16 / N.getValueType().getVectorNumElements()), false);
2197 unsigned scaleShift = scaleFactor.logBase2();
Scott Michel56a125e2008-11-22 23:50:42 +00002198 SDValue vecShift;
Scott Michel56a125e2008-11-22 23:50:42 +00002199
Scott Michelc630c412008-11-24 17:11:17 +00002200 if (scaleShift > 0) {
2201 // Scale the shift factor:
Scott Michel56a125e2008-11-22 23:50:42 +00002202 Elt = DAG.getNode(ISD::SHL, MVT::i32, Elt,
Scott Michel0718cd82008-12-01 17:56:02 +00002203 DAG.getConstant(scaleShift, MVT::i32));
Scott Michel56a125e2008-11-22 23:50:42 +00002204 }
2205
Scott Michelc630c412008-11-24 17:11:17 +00002206 vecShift = DAG.getNode(SPUISD::SHLQUAD_L_BYTES, VecVT, N, Elt);
2207
2208 // Replicate the bytes starting at byte 0 across the entire vector (for
2209 // consistency with the notion of a unified register set)
Scott Michel56a125e2008-11-22 23:50:42 +00002210 SDValue replicate;
2211
2212 switch (VT.getSimpleVT()) {
2213 default:
2214 cerr << "LowerEXTRACT_VECTOR_ELT(varable): Unhandled vector type\n";
2215 abort();
2216 /*NOTREACHED*/
2217 case MVT::i8: {
Scott Michelc630c412008-11-24 17:11:17 +00002218 SDValue factor = DAG.getConstant(0x00000000, MVT::i32);
Scott Michel56a125e2008-11-22 23:50:42 +00002219 replicate = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, factor, factor,
2220 factor, factor);
2221 break;
2222 }
2223 case MVT::i16: {
Scott Michelc630c412008-11-24 17:11:17 +00002224 SDValue factor = DAG.getConstant(0x00010001, MVT::i32);
Scott Michel56a125e2008-11-22 23:50:42 +00002225 replicate = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, factor, factor,
2226 factor, factor);
2227 break;
2228 }
2229 case MVT::i32:
2230 case MVT::f32: {
2231 SDValue factor = DAG.getConstant(0x00010203, MVT::i32);
2232 replicate = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, factor, factor,
2233 factor, factor);
2234 break;
2235 }
2236 case MVT::i64:
2237 case MVT::f64: {
2238 SDValue loFactor = DAG.getConstant(0x00010203, MVT::i32);
2239 SDValue hiFactor = DAG.getConstant(0x04050607, MVT::i32);
2240 replicate = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, loFactor, hiFactor,
2241 loFactor, hiFactor);
2242 break;
2243 }
2244 }
2245
Scott Michelc630c412008-11-24 17:11:17 +00002246 retval = DAG.getNode(SPUISD::VEC2PREFSLOT, VT,
Scott Michel0718cd82008-12-01 17:56:02 +00002247 DAG.getNode(SPUISD::SHUFB, VecVT,
2248 vecShift, vecShift, replicate));
Scott Michel8efdca42007-12-04 22:23:35 +00002249 }
2250
Scott Michel56a125e2008-11-22 23:50:42 +00002251 return retval;
Scott Michel8efdca42007-12-04 22:23:35 +00002252}
2253
Dan Gohman8181bd12008-07-27 21:46:04 +00002254static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
2255 SDValue VecOp = Op.getOperand(0);
2256 SDValue ValOp = Op.getOperand(1);
2257 SDValue IdxOp = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00002258 MVT VT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +00002259
2260 ConstantSDNode *CN = cast<ConstantSDNode>(IdxOp);
2261 assert(CN != 0 && "LowerINSERT_VECTOR_ELT: Index is not constant!");
2262
Duncan Sands92c43912008-06-06 12:08:01 +00002263 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel0718cd82008-12-01 17:56:02 +00002264 // Use $sp ($1) because it's always 16-byte aligned and it's available:
2265 SDValue Pointer = DAG.getNode(SPUISD::IndirectAddr, PtrVT,
2266 DAG.getRegister(SPU::R1, PtrVT),
2267 DAG.getConstant(CN->getSExtValue(), PtrVT));
2268 SDValue ShufMask = DAG.getNode(SPUISD::SHUFFLE_MASK, VT, Pointer);
Scott Michel8efdca42007-12-04 22:23:35 +00002269
Dan Gohman8181bd12008-07-27 21:46:04 +00002270 SDValue result =
Scott Michel8efdca42007-12-04 22:23:35 +00002271 DAG.getNode(SPUISD::SHUFB, VT,
2272 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, ValOp),
Scott Michel0718cd82008-12-01 17:56:02 +00002273 VecOp,
2274 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, ShufMask));
Scott Michel8efdca42007-12-04 22:23:35 +00002275
2276 return result;
2277}
2278
Dan Gohman8181bd12008-07-27 21:46:04 +00002279static SDValue LowerI8Math(SDValue Op, SelectionDAG &DAG, unsigned Opc)
Scott Michel97872d32008-02-23 18:41:37 +00002280{
Dan Gohman8181bd12008-07-27 21:46:04 +00002281 SDValue N0 = Op.getOperand(0); // Everything has at least one operand
Scott Michel8efdca42007-12-04 22:23:35 +00002282
2283 assert(Op.getValueType() == MVT::i8);
2284 switch (Opc) {
2285 default:
2286 assert(0 && "Unhandled i8 math operator");
2287 /*NOTREACHED*/
2288 break;
2289 case ISD::SUB: {
2290 // 8-bit subtraction: Promote the arguments up to 16-bits and truncate
2291 // the result:
Dan Gohman8181bd12008-07-27 21:46:04 +00002292 SDValue N1 = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002293 N0 = (N0.getOpcode() != ISD::Constant
2294 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N0)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002295 : DAG.getConstant(cast<ConstantSDNode>(N0)->getZExtValue(),
2296 MVT::i16));
Scott Michel8efdca42007-12-04 22:23:35 +00002297 N1 = (N1.getOpcode() != ISD::Constant
2298 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N1)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002299 : DAG.getConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
2300 MVT::i16));
Scott Michel4ec722e2008-07-16 17:17:29 +00002301 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
Scott Michel8efdca42007-12-04 22:23:35 +00002302 DAG.getNode(Opc, MVT::i16, N0, N1));
Scott Michel4ec722e2008-07-16 17:17:29 +00002303 }
Scott Michel8efdca42007-12-04 22:23:35 +00002304 case ISD::ROTR:
2305 case ISD::ROTL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002306 SDValue N1 = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002307 unsigned N1Opc;
2308 N0 = (N0.getOpcode() != ISD::Constant
2309 ? DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, N0)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002310 : DAG.getConstant(cast<ConstantSDNode>(N0)->getZExtValue(),
2311 MVT::i16));
Duncan Sands7aef60d2008-10-30 19:24:28 +00002312 N1Opc = N1.getValueType().bitsLT(MVT::i32)
Gabor Greife9f7f582008-08-31 15:37:04 +00002313 ? ISD::ZERO_EXTEND
2314 : ISD::TRUNCATE;
Scott Michel8efdca42007-12-04 22:23:35 +00002315 N1 = (N1.getOpcode() != ISD::Constant
Duncan Sands7aef60d2008-10-30 19:24:28 +00002316 ? DAG.getNode(N1Opc, MVT::i32, N1)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002317 : DAG.getConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
Duncan Sands7aef60d2008-10-30 19:24:28 +00002318 MVT::i32));
Dan Gohman8181bd12008-07-27 21:46:04 +00002319 SDValue ExpandArg =
Scott Michel8efdca42007-12-04 22:23:35 +00002320 DAG.getNode(ISD::OR, MVT::i16, N0,
2321 DAG.getNode(ISD::SHL, MVT::i16,
Duncan Sands7aef60d2008-10-30 19:24:28 +00002322 N0, DAG.getConstant(8, MVT::i32)));
Scott Michel4ec722e2008-07-16 17:17:29 +00002323 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
Scott Michel8efdca42007-12-04 22:23:35 +00002324 DAG.getNode(Opc, MVT::i16, ExpandArg, N1));
2325 }
2326 case ISD::SRL:
2327 case ISD::SHL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002328 SDValue N1 = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002329 unsigned N1Opc;
2330 N0 = (N0.getOpcode() != ISD::Constant
2331 ? DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, N0)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002332 : DAG.getConstant(cast<ConstantSDNode>(N0)->getZExtValue(),
2333 MVT::i16));
Gabor Greife9f7f582008-08-31 15:37:04 +00002334 N1Opc = N1.getValueType().bitsLT(MVT::i16)
2335 ? ISD::ZERO_EXTEND
2336 : ISD::TRUNCATE;
Scott Michel8efdca42007-12-04 22:23:35 +00002337 N1 = (N1.getOpcode() != ISD::Constant
2338 ? DAG.getNode(N1Opc, MVT::i16, N1)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002339 : DAG.getConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
2340 MVT::i16));
Scott Michel4ec722e2008-07-16 17:17:29 +00002341 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
Scott Michel8efdca42007-12-04 22:23:35 +00002342 DAG.getNode(Opc, MVT::i16, N0, N1));
2343 }
2344 case ISD::SRA: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002345 SDValue N1 = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002346 unsigned N1Opc;
2347 N0 = (N0.getOpcode() != ISD::Constant
2348 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N0)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002349 : DAG.getConstant(cast<ConstantSDNode>(N0)->getZExtValue(),
2350 MVT::i16));
Gabor Greife9f7f582008-08-31 15:37:04 +00002351 N1Opc = N1.getValueType().bitsLT(MVT::i16)
2352 ? ISD::SIGN_EXTEND
2353 : ISD::TRUNCATE;
Scott Michel8efdca42007-12-04 22:23:35 +00002354 N1 = (N1.getOpcode() != ISD::Constant
2355 ? DAG.getNode(N1Opc, MVT::i16, N1)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002356 : DAG.getConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
2357 MVT::i16));
Scott Michel4ec722e2008-07-16 17:17:29 +00002358 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
Scott Michel8efdca42007-12-04 22:23:35 +00002359 DAG.getNode(Opc, MVT::i16, N0, N1));
2360 }
2361 case ISD::MUL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002362 SDValue N1 = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002363 unsigned N1Opc;
2364 N0 = (N0.getOpcode() != ISD::Constant
2365 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N0)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002366 : DAG.getConstant(cast<ConstantSDNode>(N0)->getZExtValue(),
2367 MVT::i16));
Duncan Sandsec142ee2008-06-08 20:54:56 +00002368 N1Opc = N1.getValueType().bitsLT(MVT::i16) ? ISD::SIGN_EXTEND : ISD::TRUNCATE;
Scott Michel8efdca42007-12-04 22:23:35 +00002369 N1 = (N1.getOpcode() != ISD::Constant
2370 ? DAG.getNode(N1Opc, MVT::i16, N1)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002371 : DAG.getConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
2372 MVT::i16));
Scott Michel4ec722e2008-07-16 17:17:29 +00002373 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
Scott Michel8efdca42007-12-04 22:23:35 +00002374 DAG.getNode(Opc, MVT::i16, N0, N1));
2375 break;
2376 }
2377 }
2378
Dan Gohman8181bd12008-07-27 21:46:04 +00002379 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00002380}
2381
Dan Gohman8181bd12008-07-27 21:46:04 +00002382static SDValue LowerI64Math(SDValue Op, SelectionDAG &DAG, unsigned Opc)
Scott Michel97872d32008-02-23 18:41:37 +00002383{
Duncan Sands92c43912008-06-06 12:08:01 +00002384 MVT VT = Op.getValueType();
2385 MVT VecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits()));
Scott Michel97872d32008-02-23 18:41:37 +00002386
Dan Gohman8181bd12008-07-27 21:46:04 +00002387 SDValue Op0 = Op.getOperand(0);
Scott Michel97872d32008-02-23 18:41:37 +00002388
2389 switch (Opc) {
2390 case ISD::ZERO_EXTEND:
2391 case ISD::SIGN_EXTEND:
2392 case ISD::ANY_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00002393 MVT Op0VT = Op0.getValueType();
2394 MVT Op0VecVT = MVT::getVectorVT(Op0VT, (128 / Op0VT.getSizeInBits()));
Scott Michel97872d32008-02-23 18:41:37 +00002395
2396 assert(Op0VT == MVT::i32
2397 && "CellSPU: Zero/sign extending something other than i32");
2398
Scott Michelc630c412008-11-24 17:11:17 +00002399 DEBUG(cerr << "CellSPU.LowerI64Math: lowering zero/sign/any extend\n");
2400
Dan Gohman8181bd12008-07-27 21:46:04 +00002401 SDValue PromoteScalar =
Scott Michelc630c412008-11-24 17:11:17 +00002402 DAG.getNode(SPUISD::PROMOTE_SCALAR, Op0VecVT, Op0);
Scott Michel97872d32008-02-23 18:41:37 +00002403
Scott Michelc630c412008-11-24 17:11:17 +00002404 if (Opc != ISD::SIGN_EXTEND) {
Scott Michelf2df6cb2008-11-24 18:20:46 +00002405 // Use a shuffle to zero extend the i32 to i64 directly:
2406 SDValue shufMask =
2407 DAG.getNode(ISD::BUILD_VECTOR, Op0VecVT,
2408 DAG.getConstant(0x80808080, MVT::i32),
2409 DAG.getConstant(0x00010203, MVT::i32),
2410 DAG.getConstant(0x80808080, MVT::i32),
2411 DAG.getConstant(0x08090a0b, MVT::i32));
2412 SDValue zextShuffle =
2413 DAG.getNode(SPUISD::SHUFB, Op0VecVT,
2414 PromoteScalar, PromoteScalar, shufMask);
2415
Scott Michelc630c412008-11-24 17:11:17 +00002416 return DAG.getNode(SPUISD::VEC2PREFSLOT, VT,
Scott Michelf2df6cb2008-11-24 18:20:46 +00002417 DAG.getNode(ISD::BIT_CONVERT, VecVT, zextShuffle));
Scott Michelc630c412008-11-24 17:11:17 +00002418 } else {
2419 // SPU has no "rotate quadword and replicate bit 0" (i.e. rotate/shift
2420 // right and propagate the sign bit) instruction.
Scott Michelf2df6cb2008-11-24 18:20:46 +00002421 SDValue RotQuad =
2422 DAG.getNode(SPUISD::ROTQUAD_RZ_BYTES, Op0VecVT,
2423 PromoteScalar, DAG.getConstant(4, MVT::i32));
Scott Michelc630c412008-11-24 17:11:17 +00002424 SDValue SignQuad =
2425 DAG.getNode(SPUISD::VEC_SRA, Op0VecVT,
2426 PromoteScalar, DAG.getConstant(32, MVT::i32));
2427 SDValue SelMask =
2428 DAG.getNode(SPUISD::SELECT_MASK, Op0VecVT,
2429 DAG.getConstant(0xf0f0, MVT::i16));
2430 SDValue CombineQuad =
2431 DAG.getNode(SPUISD::SELB, Op0VecVT,
2432 SignQuad, RotQuad, SelMask);
2433
2434 return DAG.getNode(SPUISD::VEC2PREFSLOT, VT,
2435 DAG.getNode(ISD::BIT_CONVERT, VecVT, CombineQuad));
2436 }
Scott Michel97872d32008-02-23 18:41:37 +00002437 }
2438
Scott Michel67224b22008-06-02 22:18:03 +00002439 case ISD::ADD: {
2440 // Turn operands into vectors to satisfy type checking (shufb works on
2441 // vectors)
Dan Gohman8181bd12008-07-27 21:46:04 +00002442 SDValue Op0 =
Scott Michel67224b22008-06-02 22:18:03 +00002443 DAG.getNode(SPUISD::PROMOTE_SCALAR, MVT::v2i64, Op.getOperand(0));
Dan Gohman8181bd12008-07-27 21:46:04 +00002444 SDValue Op1 =
Scott Michel67224b22008-06-02 22:18:03 +00002445 DAG.getNode(SPUISD::PROMOTE_SCALAR, MVT::v2i64, Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00002446 SmallVector<SDValue, 16> ShufBytes;
Scott Michel67224b22008-06-02 22:18:03 +00002447
2448 // Create the shuffle mask for "rotating" the borrow up one register slot
2449 // once the borrow is generated.
2450 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
2451 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
2452 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
2453 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
2454
Dan Gohman8181bd12008-07-27 21:46:04 +00002455 SDValue CarryGen =
Scott Michel67224b22008-06-02 22:18:03 +00002456 DAG.getNode(SPUISD::CARRY_GENERATE, MVT::v2i64, Op0, Op1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002457 SDValue ShiftedCarry =
Scott Michel67224b22008-06-02 22:18:03 +00002458 DAG.getNode(SPUISD::SHUFB, MVT::v2i64,
2459 CarryGen, CarryGen,
2460 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
2461 &ShufBytes[0], ShufBytes.size()));
2462
Scott Michelc630c412008-11-24 17:11:17 +00002463 return DAG.getNode(SPUISD::VEC2PREFSLOT, MVT::i64,
Scott Michel67224b22008-06-02 22:18:03 +00002464 DAG.getNode(SPUISD::ADD_EXTENDED, MVT::v2i64,
2465 Op0, Op1, ShiftedCarry));
2466 }
2467
2468 case ISD::SUB: {
2469 // Turn operands into vectors to satisfy type checking (shufb works on
2470 // vectors)
Dan Gohman8181bd12008-07-27 21:46:04 +00002471 SDValue Op0 =
Scott Michel67224b22008-06-02 22:18:03 +00002472 DAG.getNode(SPUISD::PROMOTE_SCALAR, MVT::v2i64, Op.getOperand(0));
Dan Gohman8181bd12008-07-27 21:46:04 +00002473 SDValue Op1 =
Scott Michel67224b22008-06-02 22:18:03 +00002474 DAG.getNode(SPUISD::PROMOTE_SCALAR, MVT::v2i64, Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00002475 SmallVector<SDValue, 16> ShufBytes;
Scott Michel67224b22008-06-02 22:18:03 +00002476
2477 // Create the shuffle mask for "rotating" the borrow up one register slot
2478 // once the borrow is generated.
2479 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
2480 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
2481 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
2482 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
2483
Dan Gohman8181bd12008-07-27 21:46:04 +00002484 SDValue BorrowGen =
Scott Michel67224b22008-06-02 22:18:03 +00002485 DAG.getNode(SPUISD::BORROW_GENERATE, MVT::v2i64, Op0, Op1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002486 SDValue ShiftedBorrow =
Scott Michel67224b22008-06-02 22:18:03 +00002487 DAG.getNode(SPUISD::SHUFB, MVT::v2i64,
2488 BorrowGen, BorrowGen,
2489 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
2490 &ShufBytes[0], ShufBytes.size()));
2491
Scott Michelc630c412008-11-24 17:11:17 +00002492 return DAG.getNode(SPUISD::VEC2PREFSLOT, MVT::i64,
Scott Michel67224b22008-06-02 22:18:03 +00002493 DAG.getNode(SPUISD::SUB_EXTENDED, MVT::v2i64,
2494 Op0, Op1, ShiftedBorrow));
2495 }
2496
Scott Michel97872d32008-02-23 18:41:37 +00002497 case ISD::SHL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002498 SDValue ShiftAmt = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00002499 MVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002500 SDValue Op0Vec = DAG.getNode(SPUISD::PROMOTE_SCALAR, VecVT, Op0);
2501 SDValue MaskLower =
Scott Michel97872d32008-02-23 18:41:37 +00002502 DAG.getNode(SPUISD::SELB, VecVT,
2503 Op0Vec,
2504 DAG.getConstant(0, VecVT),
Scott Michel67224b22008-06-02 22:18:03 +00002505 DAG.getNode(SPUISD::SELECT_MASK, VecVT,
Scott Michel97872d32008-02-23 18:41:37 +00002506 DAG.getConstant(0xff00ULL, MVT::i16)));
Dan Gohman8181bd12008-07-27 21:46:04 +00002507 SDValue ShiftAmtBytes =
Scott Michel97872d32008-02-23 18:41:37 +00002508 DAG.getNode(ISD::SRL, ShiftAmtVT,
2509 ShiftAmt,
2510 DAG.getConstant(3, ShiftAmtVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002511 SDValue ShiftAmtBits =
Scott Michel97872d32008-02-23 18:41:37 +00002512 DAG.getNode(ISD::AND, ShiftAmtVT,
2513 ShiftAmt,
2514 DAG.getConstant(7, ShiftAmtVT));
2515
Scott Michelc630c412008-11-24 17:11:17 +00002516 return DAG.getNode(SPUISD::VEC2PREFSLOT, VT,
Scott Michel97872d32008-02-23 18:41:37 +00002517 DAG.getNode(SPUISD::SHLQUAD_L_BITS, VecVT,
2518 DAG.getNode(SPUISD::SHLQUAD_L_BYTES, VecVT,
2519 MaskLower, ShiftAmtBytes),
2520 ShiftAmtBits));
2521 }
2522
2523 case ISD::SRL: {
Duncan Sands92c43912008-06-06 12:08:01 +00002524 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002525 SDValue ShiftAmt = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00002526 MVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002527 SDValue ShiftAmtBytes =
Scott Michel97872d32008-02-23 18:41:37 +00002528 DAG.getNode(ISD::SRL, ShiftAmtVT,
2529 ShiftAmt,
2530 DAG.getConstant(3, ShiftAmtVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002531 SDValue ShiftAmtBits =
Scott Michel97872d32008-02-23 18:41:37 +00002532 DAG.getNode(ISD::AND, ShiftAmtVT,
2533 ShiftAmt,
2534 DAG.getConstant(7, ShiftAmtVT));
2535
2536 return DAG.getNode(SPUISD::ROTQUAD_RZ_BITS, VT,
2537 DAG.getNode(SPUISD::ROTQUAD_RZ_BYTES, VT,
2538 Op0, ShiftAmtBytes),
2539 ShiftAmtBits);
2540 }
Scott Michel67224b22008-06-02 22:18:03 +00002541
2542 case ISD::SRA: {
2543 // Promote Op0 to vector
Dan Gohman8181bd12008-07-27 21:46:04 +00002544 SDValue Op0 =
Scott Michel67224b22008-06-02 22:18:03 +00002545 DAG.getNode(SPUISD::PROMOTE_SCALAR, MVT::v2i64, Op.getOperand(0));
Dan Gohman8181bd12008-07-27 21:46:04 +00002546 SDValue ShiftAmt = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00002547 MVT ShiftVT = ShiftAmt.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00002548
2549 // Negate variable shift amounts
2550 if (!isa<ConstantSDNode>(ShiftAmt)) {
2551 ShiftAmt = DAG.getNode(ISD::SUB, ShiftVT,
2552 DAG.getConstant(0, ShiftVT), ShiftAmt);
2553 }
2554
Dan Gohman8181bd12008-07-27 21:46:04 +00002555 SDValue UpperHalfSign =
Scott Michelc630c412008-11-24 17:11:17 +00002556 DAG.getNode(SPUISD::VEC2PREFSLOT, MVT::i32,
Scott Michel67224b22008-06-02 22:18:03 +00002557 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
2558 DAG.getNode(SPUISD::VEC_SRA, MVT::v2i64,
2559 Op0, DAG.getConstant(31, MVT::i32))));
Dan Gohman8181bd12008-07-27 21:46:04 +00002560 SDValue UpperHalfSignMask =
Scott Michel67224b22008-06-02 22:18:03 +00002561 DAG.getNode(SPUISD::SELECT_MASK, MVT::v2i64, UpperHalfSign);
Dan Gohman8181bd12008-07-27 21:46:04 +00002562 SDValue UpperLowerMask =
Scott Michel67224b22008-06-02 22:18:03 +00002563 DAG.getNode(SPUISD::SELECT_MASK, MVT::v2i64,
2564 DAG.getConstant(0xff00, MVT::i16));
Dan Gohman8181bd12008-07-27 21:46:04 +00002565 SDValue UpperLowerSelect =
Scott Michel67224b22008-06-02 22:18:03 +00002566 DAG.getNode(SPUISD::SELB, MVT::v2i64,
2567 UpperHalfSignMask, Op0, UpperLowerMask);
Dan Gohman8181bd12008-07-27 21:46:04 +00002568 SDValue RotateLeftBytes =
Scott Michel67224b22008-06-02 22:18:03 +00002569 DAG.getNode(SPUISD::ROTBYTES_LEFT_BITS, MVT::v2i64,
2570 UpperLowerSelect, ShiftAmt);
Dan Gohman8181bd12008-07-27 21:46:04 +00002571 SDValue RotateLeftBits =
Scott Michel67224b22008-06-02 22:18:03 +00002572 DAG.getNode(SPUISD::ROTBYTES_LEFT, MVT::v2i64,
2573 RotateLeftBytes, ShiftAmt);
2574
Scott Michelc630c412008-11-24 17:11:17 +00002575 return DAG.getNode(SPUISD::VEC2PREFSLOT, MVT::i64,
Scott Michel67224b22008-06-02 22:18:03 +00002576 RotateLeftBits);
2577 }
Scott Michel97872d32008-02-23 18:41:37 +00002578 }
2579
Dan Gohman8181bd12008-07-27 21:46:04 +00002580 return SDValue();
Scott Michel97872d32008-02-23 18:41:37 +00002581}
2582
Scott Michel8efdca42007-12-04 22:23:35 +00002583//! Lower byte immediate operations for v16i8 vectors:
Dan Gohman8181bd12008-07-27 21:46:04 +00002584static SDValue
2585LowerByteImmed(SDValue Op, SelectionDAG &DAG) {
2586 SDValue ConstVec;
2587 SDValue Arg;
Duncan Sands92c43912008-06-06 12:08:01 +00002588 MVT VT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +00002589
2590 ConstVec = Op.getOperand(0);
2591 Arg = Op.getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002592 if (ConstVec.getNode()->getOpcode() != ISD::BUILD_VECTOR) {
2593 if (ConstVec.getNode()->getOpcode() == ISD::BIT_CONVERT) {
Scott Michel8efdca42007-12-04 22:23:35 +00002594 ConstVec = ConstVec.getOperand(0);
2595 } else {
2596 ConstVec = Op.getOperand(1);
2597 Arg = Op.getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +00002598 if (ConstVec.getNode()->getOpcode() == ISD::BIT_CONVERT) {
Scott Michel5a6f17b2008-01-30 02:55:46 +00002599 ConstVec = ConstVec.getOperand(0);
Scott Michel8efdca42007-12-04 22:23:35 +00002600 }
2601 }
2602 }
2603
Gabor Greif1c80d112008-08-28 21:40:38 +00002604 if (ConstVec.getNode()->getOpcode() == ISD::BUILD_VECTOR) {
Scott Michel8efdca42007-12-04 22:23:35 +00002605 uint64_t VectorBits[2];
2606 uint64_t UndefBits[2];
2607 uint64_t SplatBits, SplatUndef;
2608 int SplatSize;
2609
Gabor Greif1c80d112008-08-28 21:40:38 +00002610 if (!GetConstantBuildVectorBits(ConstVec.getNode(), VectorBits, UndefBits)
Scott Michel5a6f17b2008-01-30 02:55:46 +00002611 && isConstantSplat(VectorBits, UndefBits,
Duncan Sands92c43912008-06-06 12:08:01 +00002612 VT.getVectorElementType().getSizeInBits(),
Scott Michel5a6f17b2008-01-30 02:55:46 +00002613 SplatBits, SplatUndef, SplatSize)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002614 SDValue tcVec[16];
2615 SDValue tc = DAG.getTargetConstant(SplatBits & 0xff, MVT::i8);
Scott Michel8efdca42007-12-04 22:23:35 +00002616 const size_t tcVecSize = sizeof(tcVec) / sizeof(tcVec[0]);
2617
2618 // Turn the BUILD_VECTOR into a set of target constants:
2619 for (size_t i = 0; i < tcVecSize; ++i)
Scott Michel5a6f17b2008-01-30 02:55:46 +00002620 tcVec[i] = tc;
Scott Michel8efdca42007-12-04 22:23:35 +00002621
Gabor Greif1c80d112008-08-28 21:40:38 +00002622 return DAG.getNode(Op.getNode()->getOpcode(), VT, Arg,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002623 DAG.getNode(ISD::BUILD_VECTOR, VT, tcVec, tcVecSize));
Scott Michel8efdca42007-12-04 22:23:35 +00002624 }
2625 }
Nate Begeman7569e762008-07-29 19:07:27 +00002626 // These operations (AND, OR, XOR) are legal, they just couldn't be custom
2627 // lowered. Return the operation, rather than a null SDValue.
2628 return Op;
Scott Michel8efdca42007-12-04 22:23:35 +00002629}
2630
2631//! Lower i32 multiplication
Dan Gohman8181bd12008-07-27 21:46:04 +00002632static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG, MVT VT,
Scott Michel8efdca42007-12-04 22:23:35 +00002633 unsigned Opc) {
Duncan Sands92c43912008-06-06 12:08:01 +00002634 switch (VT.getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00002635 default:
2636 cerr << "CellSPU: Unknown LowerMUL value type, got "
Duncan Sands92c43912008-06-06 12:08:01 +00002637 << Op.getValueType().getMVTString()
Scott Michel5a6f17b2008-01-30 02:55:46 +00002638 << "\n";
Scott Michel8efdca42007-12-04 22:23:35 +00002639 abort();
2640 /*NOTREACHED*/
2641
2642 case MVT::i32: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002643 SDValue rA = Op.getOperand(0);
2644 SDValue rB = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002645
2646 return DAG.getNode(ISD::ADD, MVT::i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002647 DAG.getNode(ISD::ADD, MVT::i32,
2648 DAG.getNode(SPUISD::MPYH, MVT::i32, rA, rB),
2649 DAG.getNode(SPUISD::MPYH, MVT::i32, rB, rA)),
2650 DAG.getNode(SPUISD::MPYU, MVT::i32, rA, rB));
Scott Michel8efdca42007-12-04 22:23:35 +00002651 }
2652 }
2653
Dan Gohman8181bd12008-07-27 21:46:04 +00002654 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00002655}
2656
2657//! Custom lowering for CTPOP (count population)
2658/*!
2659 Custom lowering code that counts the number ones in the input
2660 operand. SPU has such an instruction, but it counts the number of
2661 ones per byte, which then have to be accumulated.
2662*/
Dan Gohman8181bd12008-07-27 21:46:04 +00002663static SDValue LowerCTPOP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002664 MVT VT = Op.getValueType();
2665 MVT vecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits()));
Scott Michel8efdca42007-12-04 22:23:35 +00002666
Duncan Sands92c43912008-06-06 12:08:01 +00002667 switch (VT.getSimpleVT()) {
2668 default:
2669 assert(false && "Invalid value type!");
Scott Michel8efdca42007-12-04 22:23:35 +00002670 case MVT::i8: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002671 SDValue N = Op.getOperand(0);
2672 SDValue Elt0 = DAG.getConstant(0, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00002673
Dan Gohman8181bd12008-07-27 21:46:04 +00002674 SDValue Promote = DAG.getNode(SPUISD::PROMOTE_SCALAR, vecVT, N, N);
2675 SDValue CNTB = DAG.getNode(SPUISD::CNTB, vecVT, Promote);
Scott Michel8efdca42007-12-04 22:23:35 +00002676
2677 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i8, CNTB, Elt0);
2678 }
2679
2680 case MVT::i16: {
2681 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +00002682 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel8efdca42007-12-04 22:23:35 +00002683
Chris Lattner1b989192007-12-31 04:13:23 +00002684 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R16CRegClass);
Scott Michel8efdca42007-12-04 22:23:35 +00002685
Dan Gohman8181bd12008-07-27 21:46:04 +00002686 SDValue N = Op.getOperand(0);
2687 SDValue Elt0 = DAG.getConstant(0, MVT::i16);
2688 SDValue Mask0 = DAG.getConstant(0x0f, MVT::i16);
Duncan Sands7aef60d2008-10-30 19:24:28 +00002689 SDValue Shift1 = DAG.getConstant(8, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00002690
Dan Gohman8181bd12008-07-27 21:46:04 +00002691 SDValue Promote = DAG.getNode(SPUISD::PROMOTE_SCALAR, vecVT, N, N);
2692 SDValue CNTB = DAG.getNode(SPUISD::CNTB, vecVT, Promote);
Scott Michel8efdca42007-12-04 22:23:35 +00002693
2694 // CNTB_result becomes the chain to which all of the virtual registers
2695 // CNTB_reg, SUM1_reg become associated:
Dan Gohman8181bd12008-07-27 21:46:04 +00002696 SDValue CNTB_result =
Scott Michel8efdca42007-12-04 22:23:35 +00002697 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, CNTB, Elt0);
Scott Michel4ec722e2008-07-16 17:17:29 +00002698
Dan Gohman8181bd12008-07-27 21:46:04 +00002699 SDValue CNTB_rescopy =
Scott Michel8efdca42007-12-04 22:23:35 +00002700 DAG.getCopyToReg(CNTB_result, CNTB_reg, CNTB_result);
2701
Dan Gohman8181bd12008-07-27 21:46:04 +00002702 SDValue Tmp1 = DAG.getCopyFromReg(CNTB_rescopy, CNTB_reg, MVT::i16);
Scott Michel8efdca42007-12-04 22:23:35 +00002703
2704 return DAG.getNode(ISD::AND, MVT::i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002705 DAG.getNode(ISD::ADD, MVT::i16,
2706 DAG.getNode(ISD::SRL, MVT::i16,
2707 Tmp1, Shift1),
2708 Tmp1),
2709 Mask0);
Scott Michel8efdca42007-12-04 22:23:35 +00002710 }
2711
2712 case MVT::i32: {
2713 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +00002714 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel8efdca42007-12-04 22:23:35 +00002715
Chris Lattner1b989192007-12-31 04:13:23 +00002716 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
2717 unsigned SUM1_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
Scott Michel8efdca42007-12-04 22:23:35 +00002718
Dan Gohman8181bd12008-07-27 21:46:04 +00002719 SDValue N = Op.getOperand(0);
2720 SDValue Elt0 = DAG.getConstant(0, MVT::i32);
2721 SDValue Mask0 = DAG.getConstant(0xff, MVT::i32);
2722 SDValue Shift1 = DAG.getConstant(16, MVT::i32);
2723 SDValue Shift2 = DAG.getConstant(8, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00002724
Dan Gohman8181bd12008-07-27 21:46:04 +00002725 SDValue Promote = DAG.getNode(SPUISD::PROMOTE_SCALAR, vecVT, N, N);
2726 SDValue CNTB = DAG.getNode(SPUISD::CNTB, vecVT, Promote);
Scott Michel8efdca42007-12-04 22:23:35 +00002727
2728 // CNTB_result becomes the chain to which all of the virtual registers
2729 // CNTB_reg, SUM1_reg become associated:
Dan Gohman8181bd12008-07-27 21:46:04 +00002730 SDValue CNTB_result =
Scott Michel8efdca42007-12-04 22:23:35 +00002731 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, CNTB, Elt0);
Scott Michel4ec722e2008-07-16 17:17:29 +00002732
Dan Gohman8181bd12008-07-27 21:46:04 +00002733 SDValue CNTB_rescopy =
Scott Michel8efdca42007-12-04 22:23:35 +00002734 DAG.getCopyToReg(CNTB_result, CNTB_reg, CNTB_result);
2735
Dan Gohman8181bd12008-07-27 21:46:04 +00002736 SDValue Comp1 =
Scott Michel8efdca42007-12-04 22:23:35 +00002737 DAG.getNode(ISD::SRL, MVT::i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002738 DAG.getCopyFromReg(CNTB_rescopy, CNTB_reg, MVT::i32), Shift1);
Scott Michel8efdca42007-12-04 22:23:35 +00002739
Dan Gohman8181bd12008-07-27 21:46:04 +00002740 SDValue Sum1 =
Scott Michel8efdca42007-12-04 22:23:35 +00002741 DAG.getNode(ISD::ADD, MVT::i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002742 Comp1, DAG.getCopyFromReg(CNTB_rescopy, CNTB_reg, MVT::i32));
Scott Michel8efdca42007-12-04 22:23:35 +00002743
Dan Gohman8181bd12008-07-27 21:46:04 +00002744 SDValue Sum1_rescopy =
Scott Michel8efdca42007-12-04 22:23:35 +00002745 DAG.getCopyToReg(CNTB_result, SUM1_reg, Sum1);
2746
Dan Gohman8181bd12008-07-27 21:46:04 +00002747 SDValue Comp2 =
Scott Michel8efdca42007-12-04 22:23:35 +00002748 DAG.getNode(ISD::SRL, MVT::i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002749 DAG.getCopyFromReg(Sum1_rescopy, SUM1_reg, MVT::i32),
2750 Shift2);
Dan Gohman8181bd12008-07-27 21:46:04 +00002751 SDValue Sum2 =
Scott Michel8efdca42007-12-04 22:23:35 +00002752 DAG.getNode(ISD::ADD, MVT::i32, Comp2,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002753 DAG.getCopyFromReg(Sum1_rescopy, SUM1_reg, MVT::i32));
Scott Michel8efdca42007-12-04 22:23:35 +00002754
2755 return DAG.getNode(ISD::AND, MVT::i32, Sum2, Mask0);
2756 }
2757
2758 case MVT::i64:
2759 break;
2760 }
2761
Dan Gohman8181bd12008-07-27 21:46:04 +00002762 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00002763}
2764
Scott Michel56a125e2008-11-22 23:50:42 +00002765//! Lower ISD::SELECT_CC
2766/*!
2767 ISD::SELECT_CC can (generally) be implemented directly on the SPU using the
2768 SELB instruction.
2769
2770 \note Need to revisit this in the future: if the code path through the true
2771 and false value computations is longer than the latency of a branch (6
2772 cycles), then it would be more advantageous to branch and insert a new basic
2773 block and branch on the condition. However, this code does not make that
2774 assumption, given the simplisitc uses so far.
2775 */
2776
2777static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
2778 MVT VT = Op.getValueType();
2779 SDValue lhs = Op.getOperand(0);
2780 SDValue rhs = Op.getOperand(1);
2781 SDValue trueval = Op.getOperand(2);
2782 SDValue falseval = Op.getOperand(3);
2783 SDValue condition = Op.getOperand(4);
2784
2785 // Note: Really should be ISD::SELECT instead of SPUISD::SELB, but LLVM's
2786 // legalizer insists on combining SETCC/SELECT into SELECT_CC, so we end up
2787 // with another "cannot select select_cc" assert:
2788
2789 SDValue compare = DAG.getNode(ISD::SETCC, VT, lhs, rhs, condition);
2790 return DAG.getNode(SPUISD::SELB, VT, trueval, falseval, compare);
2791}
2792
Scott Michelec8c82e2008-12-02 19:53:53 +00002793//! Custom lower ISD::TRUNCATE
2794static SDValue LowerTRUNCATE(SDValue Op, SelectionDAG &DAG)
2795{
2796 MVT VT = Op.getValueType();
2797 MVT::SimpleValueType simpleVT = VT.getSimpleVT();
2798 MVT VecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits()));
2799
2800 SDValue Op0 = Op.getOperand(0);
2801 MVT Op0VT = Op0.getValueType();
2802 MVT Op0VecVT = MVT::getVectorVT(Op0VT, (128 / Op0VT.getSizeInBits()));
2803
2804 SDValue PromoteScalar = DAG.getNode(SPUISD::PROMOTE_SCALAR, Op0VecVT, Op0);
2805
2806 unsigned maskLow;
2807 unsigned maskHigh;
2808
2809 // Create shuffle mask
2810 switch (Op0VT.getSimpleVT()) {
2811 case MVT::i128:
2812 switch (simpleVT) {
2813 case MVT::i64:
2814 // least significant doubleword of quadword
2815 maskHigh = 0x08090a0b;
2816 maskLow = 0x0c0d0e0f;
2817 break;
2818 case MVT::i32:
2819 // least significant word of quadword
2820 maskHigh = maskLow = 0x0c0d0e0f;
2821 break;
2822 case MVT::i16:
2823 // least significant halfword of quadword
2824 maskHigh = maskLow = 0x0e0f0e0f;
2825 break;
2826 case MVT::i8:
2827 // least significant byte of quadword
2828 maskHigh = maskLow = 0x0f0f0f0f;
2829 break;
2830 default:
2831 cerr << "Truncation to illegal type!";
2832 abort();
2833 }
2834 break;
2835 case MVT::i64:
2836 switch (simpleVT) {
2837 case MVT::i32:
2838 // least significant word of doubleword
2839 maskHigh = maskLow = 0x04050607;
2840 break;
2841 case MVT::i16:
2842 // least significant halfword of doubleword
2843 maskHigh = maskLow = 0x06070607;
2844 break;
2845 case MVT::i8:
2846 // least significant byte of doubleword
2847 maskHigh = maskLow = 0x07070707;
2848 break;
2849 default:
2850 cerr << "Truncation to illegal type!";
2851 abort();
2852 }
2853 break;
2854 case MVT::i32:
2855 case MVT::i16:
2856 switch (simpleVT) {
2857 case MVT::i16:
2858 // least significant halfword of word
2859 maskHigh = maskLow = 0x02030203;
2860 break;
2861 case MVT::i8:
2862 // least significant byte of word/halfword
2863 maskHigh = maskLow = 0x03030303;
2864 break;
2865 default:
2866 cerr << "Truncation to illegal type!";
2867 abort();
2868 }
2869 break;
2870 default:
2871 cerr << "Trying to lower truncation from illegal type!";
2872 abort();
2873 }
2874
2875 // Use a shuffle to perform the truncation
2876 SDValue shufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
2877 DAG.getConstant(maskHigh, MVT::i32),
2878 DAG.getConstant(maskLow, MVT::i32),
2879 DAG.getConstant(maskHigh, MVT::i32),
2880 DAG.getConstant(maskLow, MVT::i32));
2881
2882 SDValue truncShuffle = DAG.getNode(SPUISD::SHUFB, Op0VecVT,
2883 PromoteScalar, PromoteScalar, shufMask);
2884
2885 return DAG.getNode(SPUISD::VEC2PREFSLOT, VT,
2886 DAG.getNode(ISD::BIT_CONVERT, VecVT, truncShuffle));
2887}
2888
Scott Michel56a125e2008-11-22 23:50:42 +00002889//! Custom (target-specific) lowering entry point
2890/*!
2891 This is where LLVM's DAG selection process calls to do target-specific
2892 lowering of nodes.
2893 */
Dan Gohman8181bd12008-07-27 21:46:04 +00002894SDValue
2895SPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
Scott Michel8efdca42007-12-04 22:23:35 +00002896{
Scott Michel97872d32008-02-23 18:41:37 +00002897 unsigned Opc = (unsigned) Op.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00002898 MVT VT = Op.getValueType();
Scott Michel97872d32008-02-23 18:41:37 +00002899
2900 switch (Opc) {
Scott Michel8efdca42007-12-04 22:23:35 +00002901 default: {
2902 cerr << "SPUTargetLowering::LowerOperation(): need to lower this!\n";
Scott Michel97872d32008-02-23 18:41:37 +00002903 cerr << "Op.getOpcode() = " << Opc << "\n";
Gabor Greif1c80d112008-08-28 21:40:38 +00002904 cerr << "*Op.getNode():\n";
2905 Op.getNode()->dump();
Scott Michel8efdca42007-12-04 22:23:35 +00002906 abort();
2907 }
2908 case ISD::LOAD:
Scott Michelec8c82e2008-12-02 19:53:53 +00002909 case ISD::EXTLOAD:
Scott Michel8efdca42007-12-04 22:23:35 +00002910 case ISD::SEXTLOAD:
2911 case ISD::ZEXTLOAD:
2912 return LowerLOAD(Op, DAG, SPUTM.getSubtargetImpl());
2913 case ISD::STORE:
2914 return LowerSTORE(Op, DAG, SPUTM.getSubtargetImpl());
2915 case ISD::ConstantPool:
2916 return LowerConstantPool(Op, DAG, SPUTM.getSubtargetImpl());
2917 case ISD::GlobalAddress:
2918 return LowerGlobalAddress(Op, DAG, SPUTM.getSubtargetImpl());
2919 case ISD::JumpTable:
2920 return LowerJumpTable(Op, DAG, SPUTM.getSubtargetImpl());
2921 case ISD::Constant:
2922 return LowerConstant(Op, DAG);
2923 case ISD::ConstantFP:
2924 return LowerConstantFP(Op, DAG);
Scott Michel394e26d2008-01-17 20:38:41 +00002925 case ISD::BRCOND:
2926 return LowerBRCOND(Op, DAG);
Scott Michel8efdca42007-12-04 22:23:35 +00002927 case ISD::FORMAL_ARGUMENTS:
Scott Michel394e26d2008-01-17 20:38:41 +00002928 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Scott Michel8efdca42007-12-04 22:23:35 +00002929 case ISD::CALL:
Scott Micheldbac4cf2008-01-11 02:53:15 +00002930 return LowerCALL(Op, DAG, SPUTM.getSubtargetImpl());
Scott Michel8efdca42007-12-04 22:23:35 +00002931 case ISD::RET:
2932 return LowerRET(Op, DAG, getTargetMachine());
2933
Scott Michel97872d32008-02-23 18:41:37 +00002934
2935 // i8, i64 math ops:
2936 case ISD::ZERO_EXTEND:
2937 case ISD::SIGN_EXTEND:
2938 case ISD::ANY_EXTEND:
Scott Michel67224b22008-06-02 22:18:03 +00002939 case ISD::ADD:
Scott Michel8efdca42007-12-04 22:23:35 +00002940 case ISD::SUB:
2941 case ISD::ROTR:
2942 case ISD::ROTL:
2943 case ISD::SRL:
2944 case ISD::SHL:
Scott Michel67224b22008-06-02 22:18:03 +00002945 case ISD::SRA: {
Scott Michel97872d32008-02-23 18:41:37 +00002946 if (VT == MVT::i8)
2947 return LowerI8Math(Op, DAG, Opc);
2948 else if (VT == MVT::i64)
2949 return LowerI64Math(Op, DAG, Opc);
2950 break;
Scott Michel67224b22008-06-02 22:18:03 +00002951 }
Scott Michel8efdca42007-12-04 22:23:35 +00002952
2953 // Vector-related lowering.
2954 case ISD::BUILD_VECTOR:
2955 return LowerBUILD_VECTOR(Op, DAG);
2956 case ISD::SCALAR_TO_VECTOR:
2957 return LowerSCALAR_TO_VECTOR(Op, DAG);
2958 case ISD::VECTOR_SHUFFLE:
2959 return LowerVECTOR_SHUFFLE(Op, DAG);
2960 case ISD::EXTRACT_VECTOR_ELT:
2961 return LowerEXTRACT_VECTOR_ELT(Op, DAG);
2962 case ISD::INSERT_VECTOR_ELT:
2963 return LowerINSERT_VECTOR_ELT(Op, DAG);
2964
2965 // Look for ANDBI, ORBI and XORBI opportunities and lower appropriately:
2966 case ISD::AND:
2967 case ISD::OR:
2968 case ISD::XOR:
2969 return LowerByteImmed(Op, DAG);
2970
2971 // Vector and i8 multiply:
2972 case ISD::MUL:
Duncan Sands92c43912008-06-06 12:08:01 +00002973 if (VT.isVector())
Scott Michel8efdca42007-12-04 22:23:35 +00002974 return LowerVectorMUL(Op, DAG);
Scott Michel97872d32008-02-23 18:41:37 +00002975 else if (VT == MVT::i8)
2976 return LowerI8Math(Op, DAG, Opc);
Scott Michel8efdca42007-12-04 22:23:35 +00002977 else
Scott Michel97872d32008-02-23 18:41:37 +00002978 return LowerMUL(Op, DAG, VT, Opc);
Scott Michel8efdca42007-12-04 22:23:35 +00002979
2980 case ISD::FDIV:
Scott Michel97872d32008-02-23 18:41:37 +00002981 if (VT == MVT::f32 || VT == MVT::v4f32)
Scott Michel8efdca42007-12-04 22:23:35 +00002982 return LowerFDIVf32(Op, DAG);
Scott Michel56a125e2008-11-22 23:50:42 +00002983#if 0
2984 // This is probably a libcall
2985 else if (Op.getValueType() == MVT::f64)
2986 return LowerFDIVf64(Op, DAG);
2987#endif
Scott Michel8efdca42007-12-04 22:23:35 +00002988 else
2989 assert(0 && "Calling FDIV on unsupported MVT");
2990
2991 case ISD::CTPOP:
2992 return LowerCTPOP(Op, DAG);
Scott Michel56a125e2008-11-22 23:50:42 +00002993
2994 case ISD::SELECT_CC:
2995 return LowerSELECT_CC(Op, DAG);
Scott Michelec8c82e2008-12-02 19:53:53 +00002996
2997 case ISD::TRUNCATE:
2998 return LowerTRUNCATE(Op, DAG);
Scott Michel8efdca42007-12-04 22:23:35 +00002999 }
3000
Dan Gohman8181bd12008-07-27 21:46:04 +00003001 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00003002}
3003
Duncan Sands7d9834b2008-12-01 11:39:25 +00003004void SPUTargetLowering::ReplaceNodeResults(SDNode *N,
3005 SmallVectorImpl<SDValue>&Results,
3006 SelectionDAG &DAG)
Scott Michel6e2d68b2008-11-10 23:43:06 +00003007{
3008#if 0
3009 unsigned Opc = (unsigned) N->getOpcode();
3010 MVT OpVT = N->getValueType(0);
3011
3012 switch (Opc) {
3013 default: {
3014 cerr << "SPUTargetLowering::ReplaceNodeResults(): need to fix this!\n";
3015 cerr << "Op.getOpcode() = " << Opc << "\n";
3016 cerr << "*Op.getNode():\n";
3017 N->dump();
3018 abort();
3019 /*NOTREACHED*/
3020 }
3021 }
3022#endif
3023
3024 /* Otherwise, return unchanged */
Scott Michel6e2d68b2008-11-10 23:43:06 +00003025}
3026
Scott Michel8efdca42007-12-04 22:23:35 +00003027//===----------------------------------------------------------------------===//
Scott Michel8efdca42007-12-04 22:23:35 +00003028// Target Optimization Hooks
3029//===----------------------------------------------------------------------===//
3030
Dan Gohman8181bd12008-07-27 21:46:04 +00003031SDValue
Scott Michel8efdca42007-12-04 22:23:35 +00003032SPUTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const
3033{
3034#if 0
3035 TargetMachine &TM = getTargetMachine();
Scott Michelf9f42e62008-01-29 02:16:57 +00003036#endif
3037 const SPUSubtarget *ST = SPUTM.getSubtargetImpl();
Scott Michel8efdca42007-12-04 22:23:35 +00003038 SelectionDAG &DAG = DCI.DAG;
Scott Michel0718cd82008-12-01 17:56:02 +00003039 SDValue Op0 = N->getOperand(0); // everything has at least one operand
3040 MVT NodeVT = N->getValueType(0); // The node's value type
3041 MVT Op0VT = Op0.getValueType(); // The first operand's result
3042 SDValue Result; // Initially, empty result
Scott Michel8efdca42007-12-04 22:23:35 +00003043
3044 switch (N->getOpcode()) {
3045 default: break;
Scott Michelf9f42e62008-01-29 02:16:57 +00003046 case ISD::ADD: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003047 SDValue Op1 = N->getOperand(1);
Scott Michelf9f42e62008-01-29 02:16:57 +00003048
Scott Michel67224b22008-06-02 22:18:03 +00003049 if (isa<ConstantSDNode>(Op1) && Op0.getOpcode() == SPUISD::IndirectAddr) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003050 SDValue Op01 = Op0.getOperand(1);
Scott Michelf9f42e62008-01-29 02:16:57 +00003051 if (Op01.getOpcode() == ISD::Constant
3052 || Op01.getOpcode() == ISD::TargetConstant) {
3053 // (add <const>, (SPUindirect <arg>, <const>)) ->
3054 // (SPUindirect <arg>, <const + const>)
3055 ConstantSDNode *CN0 = cast<ConstantSDNode>(Op1);
3056 ConstantSDNode *CN1 = cast<ConstantSDNode>(Op01);
Dan Gohman8181bd12008-07-27 21:46:04 +00003057 SDValue combinedConst =
Scott Michel0718cd82008-12-01 17:56:02 +00003058 DAG.getConstant(CN0->getZExtValue() + CN1->getZExtValue(), Op0VT);
Scott Michelf9f42e62008-01-29 02:16:57 +00003059
Scott Michel8c2746e2008-12-04 17:16:59 +00003060#if !defined(NDEBUG)
Scott Michel6ccefab2008-12-04 03:02:42 +00003061 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
3062 cerr << "\n"
3063 << "Replace: (add " << CN0->getZExtValue() << ", "
3064 << "(SPUindirect <arg>, " << CN1->getZExtValue() << "))\n"
3065 << "With: (SPUindirect <arg>, "
3066 << CN0->getZExtValue() + CN1->getZExtValue() << ")\n";
3067 }
3068#endif
3069
Scott Michel0718cd82008-12-01 17:56:02 +00003070 return DAG.getNode(SPUISD::IndirectAddr, Op0VT,
Scott Michelf9f42e62008-01-29 02:16:57 +00003071 Op0.getOperand(0), combinedConst);
3072 }
Scott Michel67224b22008-06-02 22:18:03 +00003073 } else if (isa<ConstantSDNode>(Op0)
Scott Michelf9f42e62008-01-29 02:16:57 +00003074 && Op1.getOpcode() == SPUISD::IndirectAddr) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003075 SDValue Op11 = Op1.getOperand(1);
Scott Michelf9f42e62008-01-29 02:16:57 +00003076 if (Op11.getOpcode() == ISD::Constant
3077 || Op11.getOpcode() == ISD::TargetConstant) {
3078 // (add (SPUindirect <arg>, <const>), <const>) ->
3079 // (SPUindirect <arg>, <const + const>)
3080 ConstantSDNode *CN0 = cast<ConstantSDNode>(Op0);
3081 ConstantSDNode *CN1 = cast<ConstantSDNode>(Op11);
Dan Gohman8181bd12008-07-27 21:46:04 +00003082 SDValue combinedConst =
Scott Michel0718cd82008-12-01 17:56:02 +00003083 DAG.getConstant(CN0->getZExtValue() + CN1->getZExtValue(), Op0VT);
Scott Michelf9f42e62008-01-29 02:16:57 +00003084
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003085 DEBUG(cerr << "Replace: (add " << CN0->getZExtValue() << ", "
3086 << "(SPUindirect <arg>, " << CN1->getZExtValue() << "))\n");
Scott Michelf9f42e62008-01-29 02:16:57 +00003087 DEBUG(cerr << "With: (SPUindirect <arg>, "
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003088 << CN0->getZExtValue() + CN1->getZExtValue() << ")\n");
Scott Michelf9f42e62008-01-29 02:16:57 +00003089
3090 return DAG.getNode(SPUISD::IndirectAddr, Op1.getValueType(),
3091 Op1.getOperand(0), combinedConst);
3092 }
3093 }
Scott Michel97872d32008-02-23 18:41:37 +00003094 break;
3095 }
3096 case ISD::SIGN_EXTEND:
3097 case ISD::ZERO_EXTEND:
3098 case ISD::ANY_EXTEND: {
Scott Michel0718cd82008-12-01 17:56:02 +00003099 if (Op0.getOpcode() == SPUISD::VEC2PREFSLOT && NodeVT == Op0VT) {
Scott Michel97872d32008-02-23 18:41:37 +00003100 // (any_extend (SPUextract_elt0 <arg>)) ->
3101 // (SPUextract_elt0 <arg>)
3102 // Types must match, however...
Scott Michel8c2746e2008-12-04 17:16:59 +00003103#if !defined(NDEBUG)
3104 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Scott Michel6ccefab2008-12-04 03:02:42 +00003105 cerr << "\nReplace: ";
3106 N->dump(&DAG);
3107 cerr << "\nWith: ";
3108 Op0.getNode()->dump(&DAG);
3109 cerr << "\n";
Scott Michel8c2746e2008-12-04 17:16:59 +00003110 }
Scott Michel6ccefab2008-12-04 03:02:42 +00003111#endif
Scott Michel97872d32008-02-23 18:41:37 +00003112
3113 return Op0;
3114 }
3115 break;
3116 }
3117 case SPUISD::IndirectAddr: {
3118 if (!ST->usingLargeMem() && Op0.getOpcode() == SPUISD::AFormAddr) {
3119 ConstantSDNode *CN = cast<ConstantSDNode>(N->getOperand(1));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003120 if (CN->getZExtValue() == 0) {
Scott Michel97872d32008-02-23 18:41:37 +00003121 // (SPUindirect (SPUaform <addr>, 0), 0) ->
3122 // (SPUaform <addr>, 0)
3123
3124 DEBUG(cerr << "Replace: ");
3125 DEBUG(N->dump(&DAG));
3126 DEBUG(cerr << "\nWith: ");
Gabor Greif1c80d112008-08-28 21:40:38 +00003127 DEBUG(Op0.getNode()->dump(&DAG));
Scott Michel97872d32008-02-23 18:41:37 +00003128 DEBUG(cerr << "\n");
3129
3130 return Op0;
3131 }
3132 }
3133 break;
3134 }
3135 case SPUISD::SHLQUAD_L_BITS:
3136 case SPUISD::SHLQUAD_L_BYTES:
3137 case SPUISD::VEC_SHL:
3138 case SPUISD::VEC_SRL:
3139 case SPUISD::VEC_SRA:
3140 case SPUISD::ROTQUAD_RZ_BYTES:
3141 case SPUISD::ROTQUAD_RZ_BITS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003142 SDValue Op1 = N->getOperand(1);
Scott Michel97872d32008-02-23 18:41:37 +00003143
3144 if (isa<ConstantSDNode>(Op1)) {
3145 // Kill degenerate vector shifts:
3146 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003147 if (CN->getZExtValue() == 0) {
Scott Michel97872d32008-02-23 18:41:37 +00003148 Result = Op0;
3149 }
3150 }
3151 break;
3152 }
3153 case SPUISD::PROMOTE_SCALAR: {
3154 switch (Op0.getOpcode()) {
3155 default:
3156 break;
3157 case ISD::ANY_EXTEND:
3158 case ISD::ZERO_EXTEND:
3159 case ISD::SIGN_EXTEND: {
Scott Michel0718cd82008-12-01 17:56:02 +00003160 // (SPUpromote_scalar (any|zero|sign_extend (SPUvec2prefslot <arg>))) ->
Scott Michel97872d32008-02-23 18:41:37 +00003161 // <arg>
3162 // but only if the SPUpromote_scalar and <arg> types match.
Dan Gohman8181bd12008-07-27 21:46:04 +00003163 SDValue Op00 = Op0.getOperand(0);
Scott Michelc630c412008-11-24 17:11:17 +00003164 if (Op00.getOpcode() == SPUISD::VEC2PREFSLOT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003165 SDValue Op000 = Op00.getOperand(0);
Scott Michel0718cd82008-12-01 17:56:02 +00003166 if (Op000.getValueType() == NodeVT) {
Scott Michel97872d32008-02-23 18:41:37 +00003167 Result = Op000;
3168 }
3169 }
3170 break;
3171 }
Scott Michelc630c412008-11-24 17:11:17 +00003172 case SPUISD::VEC2PREFSLOT: {
Scott Michel0718cd82008-12-01 17:56:02 +00003173 // (SPUpromote_scalar (SPUvec2prefslot <arg>)) ->
Scott Michel97872d32008-02-23 18:41:37 +00003174 // <arg>
3175 Result = Op0.getOperand(0);
3176 break;
Scott Michel4ec722e2008-07-16 17:17:29 +00003177 }
Scott Michel97872d32008-02-23 18:41:37 +00003178 }
3179 break;
Scott Michelf9f42e62008-01-29 02:16:57 +00003180 }
3181 }
Scott Michel394e26d2008-01-17 20:38:41 +00003182 // Otherwise, return unchanged.
Scott Michel0718cd82008-12-01 17:56:02 +00003183#ifndef NDEBUG
Gabor Greif1c80d112008-08-28 21:40:38 +00003184 if (Result.getNode()) {
Scott Michel97872d32008-02-23 18:41:37 +00003185 DEBUG(cerr << "\nReplace.SPU: ");
3186 DEBUG(N->dump(&DAG));
3187 DEBUG(cerr << "\nWith: ");
Gabor Greif1c80d112008-08-28 21:40:38 +00003188 DEBUG(Result.getNode()->dump(&DAG));
Scott Michel97872d32008-02-23 18:41:37 +00003189 DEBUG(cerr << "\n");
3190 }
3191#endif
3192
3193 return Result;
Scott Michel8efdca42007-12-04 22:23:35 +00003194}
3195
3196//===----------------------------------------------------------------------===//
3197// Inline Assembly Support
3198//===----------------------------------------------------------------------===//
3199
3200/// getConstraintType - Given a constraint letter, return the type of
3201/// constraint it is for this target.
Scott Michel4ec722e2008-07-16 17:17:29 +00003202SPUTargetLowering::ConstraintType
Scott Michel8efdca42007-12-04 22:23:35 +00003203SPUTargetLowering::getConstraintType(const std::string &ConstraintLetter) const {
3204 if (ConstraintLetter.size() == 1) {
3205 switch (ConstraintLetter[0]) {
3206 default: break;
3207 case 'b':
3208 case 'r':
3209 case 'f':
3210 case 'v':
3211 case 'y':
3212 return C_RegisterClass;
Scott Michel4ec722e2008-07-16 17:17:29 +00003213 }
Scott Michel8efdca42007-12-04 22:23:35 +00003214 }
3215 return TargetLowering::getConstraintType(ConstraintLetter);
3216}
3217
Scott Michel4ec722e2008-07-16 17:17:29 +00003218std::pair<unsigned, const TargetRegisterClass*>
Scott Michel8efdca42007-12-04 22:23:35 +00003219SPUTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00003220 MVT VT) const
Scott Michel8efdca42007-12-04 22:23:35 +00003221{
3222 if (Constraint.size() == 1) {
3223 // GCC RS6000 Constraint Letters
3224 switch (Constraint[0]) {
3225 case 'b': // R1-R31
3226 case 'r': // R0-R31
3227 if (VT == MVT::i64)
3228 return std::make_pair(0U, SPU::R64CRegisterClass);
3229 return std::make_pair(0U, SPU::R32CRegisterClass);
3230 case 'f':
3231 if (VT == MVT::f32)
3232 return std::make_pair(0U, SPU::R32FPRegisterClass);
3233 else if (VT == MVT::f64)
3234 return std::make_pair(0U, SPU::R64FPRegisterClass);
3235 break;
Scott Michel4ec722e2008-07-16 17:17:29 +00003236 case 'v':
Scott Michel8efdca42007-12-04 22:23:35 +00003237 return std::make_pair(0U, SPU::GPRCRegisterClass);
3238 }
3239 }
Scott Michel4ec722e2008-07-16 17:17:29 +00003240
Scott Michel8efdca42007-12-04 22:23:35 +00003241 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3242}
3243
Scott Michel97872d32008-02-23 18:41:37 +00003244//! Compute used/known bits for a SPU operand
Scott Michel8efdca42007-12-04 22:23:35 +00003245void
Dan Gohman8181bd12008-07-27 21:46:04 +00003246SPUTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00003247 const APInt &Mask,
Scott Michel4ec722e2008-07-16 17:17:29 +00003248 APInt &KnownZero,
Dan Gohman229fa052008-02-13 00:35:47 +00003249 APInt &KnownOne,
Scott Michel5a6f17b2008-01-30 02:55:46 +00003250 const SelectionDAG &DAG,
3251 unsigned Depth ) const {
Scott Michelbc5fbc12008-04-30 00:30:08 +00003252#if 0
Scott Michel97872d32008-02-23 18:41:37 +00003253 const uint64_t uint64_sizebits = sizeof(uint64_t) * 8;
Scott Michelbc5fbc12008-04-30 00:30:08 +00003254#endif
Scott Michel97872d32008-02-23 18:41:37 +00003255
3256 switch (Op.getOpcode()) {
3257 default:
3258 // KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
3259 break;
3260
3261#if 0
3262 case CALL:
3263 case SHUFB:
Scott Michel56a125e2008-11-22 23:50:42 +00003264 case SHUFFLE_MASK:
Scott Michel97872d32008-02-23 18:41:37 +00003265 case CNTB:
3266#endif
3267
3268 case SPUISD::PROMOTE_SCALAR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003269 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003270 MVT Op0VT = Op0.getValueType();
3271 unsigned Op0VTBits = Op0VT.getSizeInBits();
3272 uint64_t InMask = Op0VT.getIntegerVTBitMask();
Scott Michelbc5fbc12008-04-30 00:30:08 +00003273 KnownZero |= APInt(Op0VTBits, ~InMask, false);
3274 KnownOne |= APInt(Op0VTBits, InMask, false);
Scott Michel97872d32008-02-23 18:41:37 +00003275 break;
3276 }
Scott Michel4ec722e2008-07-16 17:17:29 +00003277
Scott Michel97872d32008-02-23 18:41:37 +00003278 case SPUISD::LDRESULT:
Scott Michel6ccefab2008-12-04 03:02:42 +00003279 case SPUISD::VEC2PREFSLOT: {
Duncan Sands92c43912008-06-06 12:08:01 +00003280 MVT OpVT = Op.getValueType();
3281 unsigned OpVTBits = OpVT.getSizeInBits();
3282 uint64_t InMask = OpVT.getIntegerVTBitMask();
Scott Michelbc5fbc12008-04-30 00:30:08 +00003283 KnownZero |= APInt(OpVTBits, ~InMask, false);
3284 KnownOne |= APInt(OpVTBits, InMask, false);
Scott Michel97872d32008-02-23 18:41:37 +00003285 break;
3286 }
3287
3288#if 0
Scott Michel97872d32008-02-23 18:41:37 +00003289 case MPY:
3290 case MPYU:
3291 case MPYH:
3292 case MPYHH:
Scott Michelbc5fbc12008-04-30 00:30:08 +00003293 case SPUISD::SHLQUAD_L_BITS:
3294 case SPUISD::SHLQUAD_L_BYTES:
3295 case SPUISD::VEC_SHL:
3296 case SPUISD::VEC_SRL:
3297 case SPUISD::VEC_SRA:
3298 case SPUISD::VEC_ROTL:
3299 case SPUISD::VEC_ROTR:
3300 case SPUISD::ROTQUAD_RZ_BYTES:
3301 case SPUISD::ROTQUAD_RZ_BITS:
Scott Michelbc5fbc12008-04-30 00:30:08 +00003302 case SPUISD::ROTBYTES_LEFT:
Scott Michel67224b22008-06-02 22:18:03 +00003303 case SPUISD::SELECT_MASK:
3304 case SPUISD::SELB:
3305 case SPUISD::FPInterp:
3306 case SPUISD::FPRecipEst:
3307 case SPUISD::SEXT32TO64:
Scott Michel97872d32008-02-23 18:41:37 +00003308#endif
3309 }
Scott Michel8efdca42007-12-04 22:23:35 +00003310}
3311
Scott Michelbc5fbc12008-04-30 00:30:08 +00003312// LowerAsmOperandForConstraint
3313void
Dan Gohman8181bd12008-07-27 21:46:04 +00003314SPUTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Scott Michelbc5fbc12008-04-30 00:30:08 +00003315 char ConstraintLetter,
Evan Cheng7f250d62008-09-24 00:05:32 +00003316 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00003317 std::vector<SDValue> &Ops,
Scott Michelbc5fbc12008-04-30 00:30:08 +00003318 SelectionDAG &DAG) const {
3319 // Default, for the time being, to the base class handler
Evan Cheng7f250d62008-09-24 00:05:32 +00003320 TargetLowering::LowerAsmOperandForConstraint(Op, ConstraintLetter, hasMemory,
3321 Ops, DAG);
Scott Michelbc5fbc12008-04-30 00:30:08 +00003322}
3323
Scott Michel8efdca42007-12-04 22:23:35 +00003324/// isLegalAddressImmediate - Return true if the integer value can be used
3325/// as the offset of the target addressing mode.
Gabor Greife9f7f582008-08-31 15:37:04 +00003326bool SPUTargetLowering::isLegalAddressImmediate(int64_t V,
3327 const Type *Ty) const {
Scott Michel8efdca42007-12-04 22:23:35 +00003328 // SPU's addresses are 256K:
3329 return (V > -(1 << 18) && V < (1 << 18) - 1);
3330}
3331
3332bool SPUTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Scott Michel4ec722e2008-07-16 17:17:29 +00003333 return false;
Scott Michel8efdca42007-12-04 22:23:35 +00003334}
Dan Gohman36322c72008-10-18 02:06:02 +00003335
3336bool
3337SPUTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3338 // The SPU target isn't yet aware of offsets.
3339 return false;
3340}