blob: 72fad2c13beb16630eb775685b434603a47037d0 [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"
31
32#include <map>
33
34using namespace llvm;
35
36// Used in getTargetNodeName() below
37namespace {
38 std::map<unsigned, const char *> node_names;
39
Duncan Sands92c43912008-06-06 12:08:01 +000040 //! MVT mapping to useful data for Cell SPU
Scott Michel8efdca42007-12-04 22:23:35 +000041 struct valtype_map_s {
Duncan Sands92c43912008-06-06 12:08:01 +000042 const MVT valtype;
Scott Michel5a6f17b2008-01-30 02:55:46 +000043 const int prefslot_byte;
Scott Michel8efdca42007-12-04 22:23:35 +000044 };
Scott Michel4ec722e2008-07-16 17:17:29 +000045
Scott Michel8efdca42007-12-04 22:23:35 +000046 const valtype_map_s valtype_map[] = {
47 { MVT::i1, 3 },
48 { MVT::i8, 3 },
49 { MVT::i16, 2 },
50 { MVT::i32, 0 },
51 { MVT::f32, 0 },
52 { MVT::i64, 0 },
53 { MVT::f64, 0 },
54 { MVT::i128, 0 }
55 };
56
57 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
58
Duncan Sands92c43912008-06-06 12:08:01 +000059 const valtype_map_s *getValueTypeMapEntry(MVT VT) {
Scott Michel8efdca42007-12-04 22:23:35 +000060 const valtype_map_s *retval = 0;
61
62 for (size_t i = 0; i < n_valtype_map; ++i) {
63 if (valtype_map[i].valtype == VT) {
Scott Michel5a6f17b2008-01-30 02:55:46 +000064 retval = valtype_map + i;
65 break;
Scott Michel8efdca42007-12-04 22:23:35 +000066 }
67 }
68
69#ifndef NDEBUG
70 if (retval == 0) {
71 cerr << "getValueTypeMapEntry returns NULL for "
Duncan Sands92c43912008-06-06 12:08:01 +000072 << VT.getMVTString()
Scott Michel5a6f17b2008-01-30 02:55:46 +000073 << "\n";
Scott Michel8efdca42007-12-04 22:23:35 +000074 abort();
75 }
76#endif
77
78 return retval;
79 }
80
81 //! Predicate that returns true if operand is a memory target
82 /*!
83 \arg Op Operand to test
84 \return true if the operand is a memory target (i.e., global
Scott Micheldbac4cf2008-01-11 02:53:15 +000085 address, external symbol, constant pool) or an A-form
Scott Michel8efdca42007-12-04 22:23:35 +000086 address.
87 */
Dan Gohman8181bd12008-07-27 21:46:04 +000088 bool isMemoryOperand(const SDValue &Op)
Scott Michel8efdca42007-12-04 22:23:35 +000089 {
90 const unsigned Opc = Op.getOpcode();
91 return (Opc == ISD::GlobalAddress
92 || Opc == ISD::GlobalTLSAddress
Scott Michel8efdca42007-12-04 22:23:35 +000093 || Opc == ISD::JumpTable
94 || Opc == ISD::ConstantPool
95 || Opc == ISD::ExternalSymbol
96 || Opc == ISD::TargetGlobalAddress
97 || Opc == ISD::TargetGlobalTLSAddress
Scott Michel8efdca42007-12-04 22:23:35 +000098 || Opc == ISD::TargetJumpTable
99 || Opc == ISD::TargetConstantPool
100 || Opc == ISD::TargetExternalSymbol
Scott Micheldbac4cf2008-01-11 02:53:15 +0000101 || Opc == SPUISD::AFormAddr);
Scott Michel8efdca42007-12-04 22:23:35 +0000102 }
Scott Michel394e26d2008-01-17 20:38:41 +0000103
104 //! Predicate that returns true if the operand is an indirect target
Dan Gohman8181bd12008-07-27 21:46:04 +0000105 bool isIndirectOperand(const SDValue &Op)
Scott Michel394e26d2008-01-17 20:38:41 +0000106 {
107 const unsigned Opc = Op.getOpcode();
108 return (Opc == ISD::Register
Scott Michel5a6f17b2008-01-30 02:55:46 +0000109 || Opc == SPUISD::LDRESULT);
Scott Michel394e26d2008-01-17 20:38:41 +0000110 }
Scott Michel8efdca42007-12-04 22:23:35 +0000111}
112
113SPUTargetLowering::SPUTargetLowering(SPUTargetMachine &TM)
114 : TargetLowering(TM),
115 SPUTM(TM)
116{
117 // Fold away setcc operations if possible.
118 setPow2DivIsCheap();
119
120 // Use _setjmp/_longjmp instead of setjmp/longjmp.
121 setUseUnderscoreSetJmp(true);
122 setUseUnderscoreLongJmp(true);
Scott Michel4ec722e2008-07-16 17:17:29 +0000123
Scott Michel8efdca42007-12-04 22:23:35 +0000124 // Set up the SPU's register classes:
Scott Michel438be252007-12-17 22:32:34 +0000125 addRegisterClass(MVT::i8, SPU::R8CRegisterClass);
126 addRegisterClass(MVT::i16, SPU::R16CRegisterClass);
127 addRegisterClass(MVT::i32, SPU::R32CRegisterClass);
128 addRegisterClass(MVT::i64, SPU::R64CRegisterClass);
129 addRegisterClass(MVT::f32, SPU::R32FPRegisterClass);
130 addRegisterClass(MVT::f64, SPU::R64FPRegisterClass);
Scott Michel8efdca42007-12-04 22:23:35 +0000131 addRegisterClass(MVT::i128, SPU::GPRCRegisterClass);
Scott Michel4ec722e2008-07-16 17:17:29 +0000132
Scott Michel8efdca42007-12-04 22:23:35 +0000133 // SPU has no sign or zero extended loads for i1, i8, i16:
Scott Michel394e26d2008-01-17 20:38:41 +0000134 setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote);
Scott Michel8efdca42007-12-04 22:23:35 +0000135 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
136 setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote);
Chris Lattner3bc08502008-01-17 19:59:44 +0000137 setTruncStoreAction(MVT::i8, MVT::i1, Custom);
138 setTruncStoreAction(MVT::i16, MVT::i1, Custom);
139 setTruncStoreAction(MVT::i32, MVT::i1, Custom);
140 setTruncStoreAction(MVT::i64, MVT::i1, Custom);
141 setTruncStoreAction(MVT::i128, MVT::i1, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000142
143 setLoadXAction(ISD::EXTLOAD, MVT::i8, Custom);
144 setLoadXAction(ISD::SEXTLOAD, MVT::i8, Custom);
145 setLoadXAction(ISD::ZEXTLOAD, MVT::i8, Custom);
Chris Lattner3bc08502008-01-17 19:59:44 +0000146 setTruncStoreAction(MVT::i8 , MVT::i8, Custom);
147 setTruncStoreAction(MVT::i16 , MVT::i8, Custom);
148 setTruncStoreAction(MVT::i32 , MVT::i8, Custom);
149 setTruncStoreAction(MVT::i64 , MVT::i8, Custom);
150 setTruncStoreAction(MVT::i128, MVT::i8, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000151
Scott Michel8efdca42007-12-04 22:23:35 +0000152 setLoadXAction(ISD::EXTLOAD, MVT::i16, Custom);
153 setLoadXAction(ISD::SEXTLOAD, MVT::i16, Custom);
154 setLoadXAction(ISD::ZEXTLOAD, MVT::i16, Custom);
155
156 // SPU constant load actions are custom lowered:
157 setOperationAction(ISD::Constant, MVT::i64, Custom);
Nate Begeman78125042008-02-14 18:43:04 +0000158 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
Scott Michel8efdca42007-12-04 22:23:35 +0000159 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
160
161 // SPU's loads and stores have to be custom lowered:
162 for (unsigned sctype = (unsigned) MVT::i1; sctype < (unsigned) MVT::f128;
163 ++sctype) {
Duncan Sands92c43912008-06-06 12:08:01 +0000164 MVT VT = (MVT::SimpleValueType)sctype;
165
166 setOperationAction(ISD::LOAD, VT, Custom);
167 setOperationAction(ISD::STORE, VT, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000168 }
169
Scott Michel394e26d2008-01-17 20:38:41 +0000170 // Custom lower BRCOND for i1, i8 to "promote" the result to
171 // i32 and i16, respectively.
172 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 Michel4ec722e2008-07-16 17:17:29 +0000177 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Scott Michel8efdca42007-12-04 22:23:35 +0000178
179 // SPU has no intrinsics for these particular operations:
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000180 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
181
Scott Michel8efdca42007-12-04 22:23:35 +0000182 // PowerPC has no SREM/UREM instructions
183 setOperationAction(ISD::SREM, MVT::i32, Expand);
184 setOperationAction(ISD::UREM, MVT::i32, Expand);
185 setOperationAction(ISD::SREM, MVT::i64, Expand);
186 setOperationAction(ISD::UREM, MVT::i64, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000187
Scott Michel8efdca42007-12-04 22:23:35 +0000188 // We don't support sin/cos/sqrt/fmod
189 setOperationAction(ISD::FSIN , MVT::f64, Expand);
190 setOperationAction(ISD::FCOS , MVT::f64, Expand);
191 setOperationAction(ISD::FREM , MVT::f64, Expand);
192 setOperationAction(ISD::FSIN , MVT::f32, Expand);
193 setOperationAction(ISD::FCOS , MVT::f32, Expand);
194 setOperationAction(ISD::FREM , MVT::f32, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000195
Scott Michel8efdca42007-12-04 22:23:35 +0000196 // If we're enabling GP optimizations, use hardware square root
197 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
198 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000199
Scott Michel8efdca42007-12-04 22:23:35 +0000200 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
201 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
202
203 // SPU can do rotate right and left, so legalize it... but customize for i8
204 // because instructions don't exist.
Bill Wendling965299c2008-08-31 02:59:23 +0000205
206 // FIXME: Change from "expand" to appropriate type once ROTR is supported in
207 // .td files.
208 setOperationAction(ISD::ROTR, MVT::i32, Expand /*Legal*/);
209 setOperationAction(ISD::ROTR, MVT::i16, Expand /*Legal*/);
210 setOperationAction(ISD::ROTR, MVT::i8, Expand /*Custom*/);
211
Scott Michel8efdca42007-12-04 22:23:35 +0000212 setOperationAction(ISD::ROTL, MVT::i32, Legal);
213 setOperationAction(ISD::ROTL, MVT::i16, Legal);
214 setOperationAction(ISD::ROTL, MVT::i8, Custom);
215 // SPU has no native version of shift left/right for i8
216 setOperationAction(ISD::SHL, MVT::i8, Custom);
217 setOperationAction(ISD::SRL, MVT::i8, Custom);
218 setOperationAction(ISD::SRA, MVT::i8, Custom);
Scott Michel97872d32008-02-23 18:41:37 +0000219 // And SPU needs custom lowering for shift left/right for i64
220 setOperationAction(ISD::SHL, MVT::i64, Custom);
221 setOperationAction(ISD::SRL, MVT::i64, Custom);
222 setOperationAction(ISD::SRA, MVT::i64, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000223
Scott Michel4ec722e2008-07-16 17:17:29 +0000224 // Custom lower i8, i32 and i64 multiplications
225 setOperationAction(ISD::MUL, MVT::i8, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000226 setOperationAction(ISD::MUL, MVT::i32, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000227 setOperationAction(ISD::MUL, MVT::i64, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000228
Scott Michel67224b22008-06-02 22:18:03 +0000229 // Need to custom handle (some) common i8, i64 math ops
230 setOperationAction(ISD::ADD, MVT::i64, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000231 setOperationAction(ISD::SUB, MVT::i8, Custom);
Scott Michel67224b22008-06-02 22:18:03 +0000232 setOperationAction(ISD::SUB, MVT::i64, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000233
Scott Michel8efdca42007-12-04 22:23:35 +0000234 // SPU does not have BSWAP. It does have i32 support CTLZ.
235 // CTPOP has to be custom lowered.
236 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
237 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
238
239 setOperationAction(ISD::CTPOP, MVT::i8, Custom);
240 setOperationAction(ISD::CTPOP, MVT::i16, Custom);
241 setOperationAction(ISD::CTPOP, MVT::i32, Custom);
242 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
243
244 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
245 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
246
247 setOperationAction(ISD::CTLZ , MVT::i32, Legal);
Scott Michel4ec722e2008-07-16 17:17:29 +0000248
Scott Michel67224b22008-06-02 22:18:03 +0000249 // SPU has a version of select that implements (a&~c)|(b&c), just like
Scott Michel978b96f2008-03-10 23:49:09 +0000250 // select ought to work:
Scott Michel53ab7792008-03-10 16:58:52 +0000251 setOperationAction(ISD::SELECT, MVT::i1, Promote);
252 setOperationAction(ISD::SELECT, MVT::i8, Legal);
Scott Michel6baba072008-03-05 23:02:02 +0000253 setOperationAction(ISD::SELECT, MVT::i16, Legal);
254 setOperationAction(ISD::SELECT, MVT::i32, Legal);
Scott Michel8efdca42007-12-04 22:23:35 +0000255 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Scott Michel8efdca42007-12-04 22:23:35 +0000256
Scott Michel53ab7792008-03-10 16:58:52 +0000257 setOperationAction(ISD::SETCC, MVT::i1, Promote);
258 setOperationAction(ISD::SETCC, MVT::i8, Legal);
259 setOperationAction(ISD::SETCC, MVT::i16, Legal);
260 setOperationAction(ISD::SETCC, MVT::i32, Legal);
261 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Scott Michel6baba072008-03-05 23:02:02 +0000262
Scott Michel97872d32008-02-23 18:41:37 +0000263 // Zero extension and sign extension for i64 have to be
264 // custom legalized
265 setOperationAction(ISD::ZERO_EXTEND, MVT::i64, Custom);
266 setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom);
267 setOperationAction(ISD::ANY_EXTEND, MVT::i64, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000268
Scott Michel8efdca42007-12-04 22:23:35 +0000269 // SPU has a legal FP -> signed INT instruction
270 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Legal);
271 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
272 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Legal);
273 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
274
275 // FDIV on SPU requires custom lowering
276 setOperationAction(ISD::FDIV, MVT::f32, Custom);
277 //setOperationAction(ISD::FDIV, MVT::f64, Custom);
278
279 // SPU has [U|S]INT_TO_FP
280 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Legal);
281 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
282 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
283 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Legal);
284 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
285 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
286 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
287 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
288
Scott Michel754d8662007-12-20 00:44:13 +0000289 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Legal);
290 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Legal);
291 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Legal);
292 setOperationAction(ISD::BIT_CONVERT, MVT::f64, Legal);
Scott Michel8efdca42007-12-04 22:23:35 +0000293
294 // We cannot sextinreg(i1). Expand to shifts.
295 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000296
Scott Michel8efdca42007-12-04 22:23:35 +0000297 // Support label based line numbers.
Dan Gohman472d12c2008-06-30 20:59:49 +0000298 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Scott Michel8efdca42007-12-04 22:23:35 +0000299 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000300
301 // We want to legalize GlobalAddress and ConstantPool nodes into the
Scott Michel8efdca42007-12-04 22:23:35 +0000302 // appropriate instructions to materialize the address.
Scott Michelf9f42e62008-01-29 02:16:57 +0000303 for (unsigned sctype = (unsigned) MVT::i1; sctype < (unsigned) MVT::f128;
304 ++sctype) {
Duncan Sands92c43912008-06-06 12:08:01 +0000305 MVT VT = (MVT::SimpleValueType)sctype;
306
307 setOperationAction(ISD::GlobalAddress, VT, Custom);
308 setOperationAction(ISD::ConstantPool, VT, Custom);
309 setOperationAction(ISD::JumpTable, VT, Custom);
Scott Michelf9f42e62008-01-29 02:16:57 +0000310 }
Scott Michel8efdca42007-12-04 22:23:35 +0000311
312 // RET must be custom lowered, to meet ABI requirements
313 setOperationAction(ISD::RET, MVT::Other, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000314
Scott Michel8efdca42007-12-04 22:23:35 +0000315 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
316 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000317
Scott Michel8efdca42007-12-04 22:23:35 +0000318 // Use the default implementation.
319 setOperationAction(ISD::VAARG , MVT::Other, Expand);
320 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
321 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Scott Michel4ec722e2008-07-16 17:17:29 +0000322 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
Scott Michel8efdca42007-12-04 22:23:35 +0000323 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
324 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
325 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Expand);
326
327 // Cell SPU has instructions for converting between i64 and fp.
328 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
329 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Scott Michel4ec722e2008-07-16 17:17:29 +0000330
Scott Michel8efdca42007-12-04 22:23:35 +0000331 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
332 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
333
334 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
335 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
336
337 // First set operation action for all vector types to expand. Then we
338 // will selectively turn on ones that can be effectively codegen'd.
339 addRegisterClass(MVT::v16i8, SPU::VECREGRegisterClass);
340 addRegisterClass(MVT::v8i16, SPU::VECREGRegisterClass);
341 addRegisterClass(MVT::v4i32, SPU::VECREGRegisterClass);
342 addRegisterClass(MVT::v2i64, SPU::VECREGRegisterClass);
343 addRegisterClass(MVT::v4f32, SPU::VECREGRegisterClass);
344 addRegisterClass(MVT::v2f64, SPU::VECREGRegisterClass);
345
Duncan Sands92c43912008-06-06 12:08:01 +0000346 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
347 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
348 MVT VT = (MVT::SimpleValueType)i;
Scott Michel8efdca42007-12-04 22:23:35 +0000349
Duncan Sands92c43912008-06-06 12:08:01 +0000350 // add/sub are legal for all supported vector VT's.
351 setOperationAction(ISD::ADD , VT, Legal);
352 setOperationAction(ISD::SUB , VT, Legal);
353 // mul has to be custom lowered.
354 setOperationAction(ISD::MUL , VT, Custom);
355
356 setOperationAction(ISD::AND , VT, Legal);
357 setOperationAction(ISD::OR , VT, Legal);
358 setOperationAction(ISD::XOR , VT, Legal);
359 setOperationAction(ISD::LOAD , VT, Legal);
360 setOperationAction(ISD::SELECT, VT, Legal);
361 setOperationAction(ISD::STORE, VT, Legal);
Scott Michel4ec722e2008-07-16 17:17:29 +0000362
Scott Michel8efdca42007-12-04 22:23:35 +0000363 // These operations need to be expanded:
Duncan Sands92c43912008-06-06 12:08:01 +0000364 setOperationAction(ISD::SDIV, VT, Expand);
365 setOperationAction(ISD::SREM, VT, Expand);
366 setOperationAction(ISD::UDIV, VT, Expand);
367 setOperationAction(ISD::UREM, VT, Expand);
368 setOperationAction(ISD::FDIV, VT, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000369
370 // Custom lower build_vector, constant pool spills, insert and
371 // extract vector elements:
Duncan Sands92c43912008-06-06 12:08:01 +0000372 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
373 setOperationAction(ISD::ConstantPool, VT, Custom);
374 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
375 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
376 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
377 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
Scott Michel8efdca42007-12-04 22:23:35 +0000378 }
379
380 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
381 setOperationAction(ISD::AND, MVT::v16i8, Custom);
382 setOperationAction(ISD::OR, MVT::v16i8, Custom);
383 setOperationAction(ISD::XOR, MVT::v16i8, Custom);
384 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000385
Scott Michel8efdca42007-12-04 22:23:35 +0000386 setShiftAmountType(MVT::i32);
387 setSetCCResultContents(ZeroOrOneSetCCResult);
Scott Michel4ec722e2008-07-16 17:17:29 +0000388
Scott Michel8efdca42007-12-04 22:23:35 +0000389 setStackPointerRegisterToSaveRestore(SPU::R1);
Scott Michel4ec722e2008-07-16 17:17:29 +0000390
Scott Michel8efdca42007-12-04 22:23:35 +0000391 // We have target-specific dag combine patterns for the following nodes:
Scott Michelf9f42e62008-01-29 02:16:57 +0000392 setTargetDAGCombine(ISD::ADD);
Scott Michel97872d32008-02-23 18:41:37 +0000393 setTargetDAGCombine(ISD::ZERO_EXTEND);
394 setTargetDAGCombine(ISD::SIGN_EXTEND);
395 setTargetDAGCombine(ISD::ANY_EXTEND);
Scott Michel4ec722e2008-07-16 17:17:29 +0000396
Scott Michel8efdca42007-12-04 22:23:35 +0000397 computeRegisterProperties();
398}
399
400const char *
401SPUTargetLowering::getTargetNodeName(unsigned Opcode) const
402{
403 if (node_names.empty()) {
404 node_names[(unsigned) SPUISD::RET_FLAG] = "SPUISD::RET_FLAG";
405 node_names[(unsigned) SPUISD::Hi] = "SPUISD::Hi";
406 node_names[(unsigned) SPUISD::Lo] = "SPUISD::Lo";
407 node_names[(unsigned) SPUISD::PCRelAddr] = "SPUISD::PCRelAddr";
Scott Micheldbac4cf2008-01-11 02:53:15 +0000408 node_names[(unsigned) SPUISD::AFormAddr] = "SPUISD::AFormAddr";
Scott Michelf9f42e62008-01-29 02:16:57 +0000409 node_names[(unsigned) SPUISD::IndirectAddr] = "SPUISD::IndirectAddr";
Scott Michel8efdca42007-12-04 22:23:35 +0000410 node_names[(unsigned) SPUISD::LDRESULT] = "SPUISD::LDRESULT";
411 node_names[(unsigned) SPUISD::CALL] = "SPUISD::CALL";
412 node_names[(unsigned) SPUISD::SHUFB] = "SPUISD::SHUFB";
413 node_names[(unsigned) SPUISD::INSERT_MASK] = "SPUISD::INSERT_MASK";
414 node_names[(unsigned) SPUISD::CNTB] = "SPUISD::CNTB";
415 node_names[(unsigned) SPUISD::PROMOTE_SCALAR] = "SPUISD::PROMOTE_SCALAR";
416 node_names[(unsigned) SPUISD::EXTRACT_ELT0] = "SPUISD::EXTRACT_ELT0";
417 node_names[(unsigned) SPUISD::EXTRACT_ELT0_CHAINED] = "SPUISD::EXTRACT_ELT0_CHAINED";
418 node_names[(unsigned) SPUISD::EXTRACT_I1_ZEXT] = "SPUISD::EXTRACT_I1_ZEXT";
419 node_names[(unsigned) SPUISD::EXTRACT_I1_SEXT] = "SPUISD::EXTRACT_I1_SEXT";
420 node_names[(unsigned) SPUISD::EXTRACT_I8_ZEXT] = "SPUISD::EXTRACT_I8_ZEXT";
421 node_names[(unsigned) SPUISD::EXTRACT_I8_SEXT] = "SPUISD::EXTRACT_I8_SEXT";
422 node_names[(unsigned) SPUISD::MPY] = "SPUISD::MPY";
423 node_names[(unsigned) SPUISD::MPYU] = "SPUISD::MPYU";
424 node_names[(unsigned) SPUISD::MPYH] = "SPUISD::MPYH";
425 node_names[(unsigned) SPUISD::MPYHH] = "SPUISD::MPYHH";
Scott Michel97872d32008-02-23 18:41:37 +0000426 node_names[(unsigned) SPUISD::SHLQUAD_L_BITS] = "SPUISD::SHLQUAD_L_BITS";
427 node_names[(unsigned) SPUISD::SHLQUAD_L_BYTES] = "SPUISD::SHLQUAD_L_BYTES";
Scott Michel8efdca42007-12-04 22:23:35 +0000428 node_names[(unsigned) SPUISD::VEC_SHL] = "SPUISD::VEC_SHL";
429 node_names[(unsigned) SPUISD::VEC_SRL] = "SPUISD::VEC_SRL";
430 node_names[(unsigned) SPUISD::VEC_SRA] = "SPUISD::VEC_SRA";
431 node_names[(unsigned) SPUISD::VEC_ROTL] = "SPUISD::VEC_ROTL";
432 node_names[(unsigned) SPUISD::VEC_ROTR] = "SPUISD::VEC_ROTR";
Scott Michel97872d32008-02-23 18:41:37 +0000433 node_names[(unsigned) SPUISD::ROTQUAD_RZ_BYTES] =
434 "SPUISD::ROTQUAD_RZ_BYTES";
435 node_names[(unsigned) SPUISD::ROTQUAD_RZ_BITS] =
436 "SPUISD::ROTQUAD_RZ_BITS";
Scott Michel8efdca42007-12-04 22:23:35 +0000437 node_names[(unsigned) SPUISD::ROTBYTES_RIGHT_S] =
438 "SPUISD::ROTBYTES_RIGHT_S";
439 node_names[(unsigned) SPUISD::ROTBYTES_LEFT] = "SPUISD::ROTBYTES_LEFT";
440 node_names[(unsigned) SPUISD::ROTBYTES_LEFT_CHAINED] =
441 "SPUISD::ROTBYTES_LEFT_CHAINED";
Scott Michel67224b22008-06-02 22:18:03 +0000442 node_names[(unsigned) SPUISD::ROTBYTES_LEFT_BITS] =
443 "SPUISD::ROTBYTES_LEFT_BITS";
444 node_names[(unsigned) SPUISD::SELECT_MASK] = "SPUISD::SELECT_MASK";
Scott Michel8efdca42007-12-04 22:23:35 +0000445 node_names[(unsigned) SPUISD::SELB] = "SPUISD::SELB";
Scott Michel67224b22008-06-02 22:18:03 +0000446 node_names[(unsigned) SPUISD::ADD_EXTENDED] = "SPUISD::ADD_EXTENDED";
447 node_names[(unsigned) SPUISD::CARRY_GENERATE] = "SPUISD::CARRY_GENERATE";
448 node_names[(unsigned) SPUISD::SUB_EXTENDED] = "SPUISD::SUB_EXTENDED";
449 node_names[(unsigned) SPUISD::BORROW_GENERATE] = "SPUISD::BORROW_GENERATE";
Scott Michel8efdca42007-12-04 22:23:35 +0000450 node_names[(unsigned) SPUISD::FPInterp] = "SPUISD::FPInterp";
451 node_names[(unsigned) SPUISD::FPRecipEst] = "SPUISD::FPRecipEst";
452 node_names[(unsigned) SPUISD::SEXT32TO64] = "SPUISD::SEXT32TO64";
453 }
454
455 std::map<unsigned, const char *>::iterator i = node_names.find(Opcode);
456
457 return ((i != node_names.end()) ? i->second : 0);
458}
459
Dan Gohman8181bd12008-07-27 21:46:04 +0000460MVT SPUTargetLowering::getSetCCResultType(const SDValue &Op) const {
Duncan Sands92c43912008-06-06 12:08:01 +0000461 MVT VT = Op.getValueType();
462 if (VT.isInteger())
Scott Michel978b96f2008-03-10 23:49:09 +0000463 return VT;
464 else
465 return MVT::i32;
Scott Michel53ab7792008-03-10 16:58:52 +0000466}
467
Scott Michel8efdca42007-12-04 22:23:35 +0000468//===----------------------------------------------------------------------===//
469// Calling convention code:
470//===----------------------------------------------------------------------===//
471
472#include "SPUGenCallingConv.inc"
473
474//===----------------------------------------------------------------------===//
475// LowerOperation implementation
476//===----------------------------------------------------------------------===//
477
Scott Micheldbac4cf2008-01-11 02:53:15 +0000478/// Aligned load common code for CellSPU
479/*!
480 \param[in] Op The SelectionDAG load or store operand
481 \param[in] DAG The selection DAG
482 \param[in] ST CellSPU subtarget information structure
483 \param[in,out] alignment Caller initializes this to the load or store node's
484 value from getAlignment(), may be updated while generating the aligned load
485 \param[in,out] alignOffs Aligned offset; set by AlignedLoad to the aligned
486 offset (divisible by 16, modulo 16 == 0)
487 \param[in,out] prefSlotOffs Preferred slot offset; set by AlignedLoad to the
488 offset of the preferred slot (modulo 16 != 0)
489 \param[in,out] VT Caller initializes this value type to the the load or store
490 node's loaded or stored value type; may be updated if an i1-extended load or
491 store.
492 \param[out] was16aligned true if the base pointer had 16-byte alignment,
493 otherwise false. Can help to determine if the chunk needs to be rotated.
494
495 Both load and store lowering load a block of data aligned on a 16-byte
496 boundary. This is the common aligned load code shared between both.
497 */
Dan Gohman8181bd12008-07-27 21:46:04 +0000498static SDValue
499AlignedLoad(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST,
Scott Micheldbac4cf2008-01-11 02:53:15 +0000500 LSBaseSDNode *LSN,
501 unsigned &alignment, int &alignOffs, int &prefSlotOffs,
Duncan Sands92c43912008-06-06 12:08:01 +0000502 MVT &VT, bool &was16aligned)
Scott Micheldbac4cf2008-01-11 02:53:15 +0000503{
Duncan Sands92c43912008-06-06 12:08:01 +0000504 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Micheldbac4cf2008-01-11 02:53:15 +0000505 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +0000506 SDValue basePtr = LSN->getBasePtr();
507 SDValue chain = LSN->getChain();
Scott Micheldbac4cf2008-01-11 02:53:15 +0000508
509 if (basePtr.getOpcode() == ISD::ADD) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000510 SDValue Op1 = basePtr.getNode()->getOperand(1);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000511
512 if (Op1.getOpcode() == ISD::Constant || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel394e26d2008-01-17 20:38:41 +0000513 const ConstantSDNode *CN = cast<ConstantSDNode>(basePtr.getOperand(1));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000514
515 alignOffs = (int) CN->getValue();
516 prefSlotOffs = (int) (alignOffs & 0xf);
517
518 // Adjust the rotation amount to ensure that the final result ends up in
519 // the preferred slot:
520 prefSlotOffs -= vtm->prefslot_byte;
521 basePtr = basePtr.getOperand(0);
522
Scott Michel394e26d2008-01-17 20:38:41 +0000523 // Loading from memory, can we adjust alignment?
524 if (basePtr.getOpcode() == SPUISD::AFormAddr) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000525 SDValue APtr = basePtr.getOperand(0);
Scott Michel394e26d2008-01-17 20:38:41 +0000526 if (APtr.getOpcode() == ISD::TargetGlobalAddress) {
527 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(APtr);
528 alignment = GSDN->getGlobal()->getAlignment();
529 }
Scott Micheldbac4cf2008-01-11 02:53:15 +0000530 }
531 } else {
532 alignOffs = 0;
533 prefSlotOffs = -vtm->prefslot_byte;
534 }
Scott Michelbc5fbc12008-04-30 00:30:08 +0000535 } else if (basePtr.getOpcode() == ISD::FrameIndex) {
536 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(basePtr);
537 alignOffs = int(FIN->getIndex() * SPUFrameInfo::stackSlotSize());
538 prefSlotOffs = (int) (alignOffs & 0xf);
539 prefSlotOffs -= vtm->prefslot_byte;
540 basePtr = DAG.getRegister(SPU::R1, VT);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000541 } else {
542 alignOffs = 0;
543 prefSlotOffs = -vtm->prefslot_byte;
544 }
545
546 if (alignment == 16) {
547 // Realign the base pointer as a D-Form address:
548 if (!isMemoryOperand(basePtr) || (alignOffs & ~0xf) != 0) {
Scott Michel394e26d2008-01-17 20:38:41 +0000549 basePtr = DAG.getNode(ISD::ADD, PtrVT,
550 basePtr,
Scott Michel5a6f17b2008-01-30 02:55:46 +0000551 DAG.getConstant((alignOffs & ~0xf), PtrVT));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000552 }
553
554 // Emit the vector load:
555 was16aligned = true;
556 return DAG.getLoad(MVT::v16i8, chain, basePtr,
557 LSN->getSrcValue(), LSN->getSrcValueOffset(),
558 LSN->isVolatile(), 16);
559 }
560
561 // Unaligned load or we're using the "large memory" model, which means that
562 // we have to be very pessimistic:
Scott Michel394e26d2008-01-17 20:38:41 +0000563 if (isMemoryOperand(basePtr) || isIndirectOperand(basePtr)) {
Scott Michelf9f42e62008-01-29 02:16:57 +0000564 basePtr = DAG.getNode(SPUISD::IndirectAddr, PtrVT, basePtr, DAG.getConstant(0, PtrVT));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000565 }
566
567 // Add the offset
Scott Michelf9f42e62008-01-29 02:16:57 +0000568 basePtr = DAG.getNode(ISD::ADD, PtrVT, basePtr,
Scott Michel5a6f17b2008-01-30 02:55:46 +0000569 DAG.getConstant((alignOffs & ~0xf), PtrVT));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000570 was16aligned = false;
571 return DAG.getLoad(MVT::v16i8, chain, basePtr,
572 LSN->getSrcValue(), LSN->getSrcValueOffset(),
573 LSN->isVolatile(), 16);
574}
575
Scott Michel8efdca42007-12-04 22:23:35 +0000576/// Custom lower loads for CellSPU
577/*!
578 All CellSPU loads and stores are aligned to 16-byte boundaries, so for elements
579 within a 16-byte block, we have to rotate to extract the requested element.
580 */
Dan Gohman8181bd12008-07-27 21:46:04 +0000581static SDValue
582LowerLOAD(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel8efdca42007-12-04 22:23:35 +0000583 LoadSDNode *LN = cast<LoadSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000584 SDValue the_chain = LN->getChain();
Duncan Sands92c43912008-06-06 12:08:01 +0000585 MVT VT = LN->getMemoryVT();
Gabor Greif1c80d112008-08-28 21:40:38 +0000586 MVT OpVT = Op.getNode()->getValueType(0);
Scott Michel8efdca42007-12-04 22:23:35 +0000587 ISD::LoadExtType ExtType = LN->getExtensionType();
588 unsigned alignment = LN->getAlignment();
Dan Gohman8181bd12008-07-27 21:46:04 +0000589 SDValue Ops[8];
Scott Michel8efdca42007-12-04 22:23:35 +0000590
Scott Michel8efdca42007-12-04 22:23:35 +0000591 switch (LN->getAddressingMode()) {
592 case ISD::UNINDEXED: {
Scott Micheldbac4cf2008-01-11 02:53:15 +0000593 int offset, rotamt;
594 bool was16aligned;
Dan Gohman8181bd12008-07-27 21:46:04 +0000595 SDValue result =
Scott Micheldbac4cf2008-01-11 02:53:15 +0000596 AlignedLoad(Op, DAG, ST, LN,alignment, offset, rotamt, VT, was16aligned);
Scott Michel8efdca42007-12-04 22:23:35 +0000597
Gabor Greif1c80d112008-08-28 21:40:38 +0000598 if (result.getNode() == 0)
Scott Michel8efdca42007-12-04 22:23:35 +0000599 return result;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000600
601 the_chain = result.getValue(1);
602 // Rotate the chunk if necessary
603 if (rotamt < 0)
604 rotamt += 16;
Scott Michelabc58242008-01-11 21:01:19 +0000605 if (rotamt != 0 || !was16aligned) {
Scott Micheldbac4cf2008-01-11 02:53:15 +0000606 SDVTList vecvts = DAG.getVTList(MVT::v16i8, MVT::Other);
607
Scott Michel394e26d2008-01-17 20:38:41 +0000608 Ops[0] = the_chain;
609 Ops[1] = result;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000610 if (was16aligned) {
Scott Micheldbac4cf2008-01-11 02:53:15 +0000611 Ops[2] = DAG.getConstant(rotamt, MVT::i16);
612 } else {
Duncan Sands92c43912008-06-06 12:08:01 +0000613 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Micheldbac4cf2008-01-11 02:53:15 +0000614 LoadSDNode *LN1 = cast<LoadSDNode>(result);
Scott Michelabc58242008-01-11 21:01:19 +0000615 Ops[2] = DAG.getNode(ISD::ADD, PtrVT, LN1->getBasePtr(),
Scott Michel5a6f17b2008-01-30 02:55:46 +0000616 DAG.getConstant(rotamt, PtrVT));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000617 }
618
619 result = DAG.getNode(SPUISD::ROTBYTES_LEFT_CHAINED, vecvts, Ops, 3);
620 the_chain = result.getValue(1);
Scott Michel8efdca42007-12-04 22:23:35 +0000621 }
Scott Micheldbac4cf2008-01-11 02:53:15 +0000622
623 if (VT == OpVT || ExtType == ISD::EXTLOAD) {
624 SDVTList scalarvts;
Duncan Sands92c43912008-06-06 12:08:01 +0000625 MVT vecVT = MVT::v16i8;
Scott Michel4ec722e2008-07-16 17:17:29 +0000626
Scott Micheldbac4cf2008-01-11 02:53:15 +0000627 // Convert the loaded v16i8 vector to the appropriate vector type
628 // specified by the operand:
629 if (OpVT == VT) {
630 if (VT != MVT::i1)
Duncan Sands92c43912008-06-06 12:08:01 +0000631 vecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits()));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000632 } else
Duncan Sands92c43912008-06-06 12:08:01 +0000633 vecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000634
635 Ops[0] = the_chain;
636 Ops[1] = DAG.getNode(ISD::BIT_CONVERT, vecVT, result);
637 scalarvts = DAG.getVTList((OpVT == VT ? VT : OpVT), MVT::Other);
638 result = DAG.getNode(SPUISD::EXTRACT_ELT0_CHAINED, scalarvts, Ops, 2);
639 the_chain = result.getValue(1);
640 } else {
641 // Handle the sign and zero-extending loads for i1 and i8:
642 unsigned NewOpC;
643
644 if (ExtType == ISD::SEXTLOAD) {
645 NewOpC = (OpVT == MVT::i1
646 ? SPUISD::EXTRACT_I1_SEXT
647 : SPUISD::EXTRACT_I8_SEXT);
648 } else {
649 assert(ExtType == ISD::ZEXTLOAD);
650 NewOpC = (OpVT == MVT::i1
651 ? SPUISD::EXTRACT_I1_ZEXT
652 : SPUISD::EXTRACT_I8_ZEXT);
653 }
654
655 result = DAG.getNode(NewOpC, OpVT, result);
656 }
657
658 SDVTList retvts = DAG.getVTList(OpVT, MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +0000659 SDValue retops[2] = {
Scott Michel394e26d2008-01-17 20:38:41 +0000660 result,
Scott Michel5a6f17b2008-01-30 02:55:46 +0000661 the_chain
Scott Michel394e26d2008-01-17 20:38:41 +0000662 };
Scott Micheldbac4cf2008-01-11 02:53:15 +0000663
Scott Michel394e26d2008-01-17 20:38:41 +0000664 result = DAG.getNode(SPUISD::LDRESULT, retvts,
665 retops, sizeof(retops) / sizeof(retops[0]));
Scott Micheldbac4cf2008-01-11 02:53:15 +0000666 return result;
Scott Michel8efdca42007-12-04 22:23:35 +0000667 }
668 case ISD::PRE_INC:
669 case ISD::PRE_DEC:
670 case ISD::POST_INC:
671 case ISD::POST_DEC:
672 case ISD::LAST_INDEXED_MODE:
673 cerr << "LowerLOAD: Got a LoadSDNode with an addr mode other than "
674 "UNINDEXED\n";
675 cerr << (unsigned) LN->getAddressingMode() << "\n";
676 abort();
677 /*NOTREACHED*/
678 }
679
Dan Gohman8181bd12008-07-27 21:46:04 +0000680 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000681}
682
683/// Custom lower stores for CellSPU
684/*!
685 All CellSPU stores are aligned to 16-byte boundaries, so for elements
686 within a 16-byte block, we have to generate a shuffle to insert the
687 requested element into its place, then store the resulting block.
688 */
Dan Gohman8181bd12008-07-27 21:46:04 +0000689static SDValue
690LowerSTORE(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel8efdca42007-12-04 22:23:35 +0000691 StoreSDNode *SN = cast<StoreSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000692 SDValue Value = SN->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000693 MVT VT = Value.getValueType();
694 MVT StVT = (!SN->isTruncatingStore() ? VT : SN->getMemoryVT());
695 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Micheldbac4cf2008-01-11 02:53:15 +0000696 unsigned alignment = SN->getAlignment();
Scott Michel8efdca42007-12-04 22:23:35 +0000697
698 switch (SN->getAddressingMode()) {
699 case ISD::UNINDEXED: {
Scott Micheldbac4cf2008-01-11 02:53:15 +0000700 int chunk_offset, slot_offset;
701 bool was16aligned;
Scott Michel8efdca42007-12-04 22:23:35 +0000702
703 // The vector type we really want to load from the 16-byte chunk, except
704 // in the case of MVT::i1, which has to be v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +0000705 MVT vecVT, stVecVT = MVT::v16i8;
Scott Michel4ec722e2008-07-16 17:17:29 +0000706
Scott Michel8efdca42007-12-04 22:23:35 +0000707 if (StVT != MVT::i1)
Duncan Sands92c43912008-06-06 12:08:01 +0000708 stVecVT = MVT::getVectorVT(StVT, (128 / StVT.getSizeInBits()));
709 vecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits()));
Scott Michel8efdca42007-12-04 22:23:35 +0000710
Dan Gohman8181bd12008-07-27 21:46:04 +0000711 SDValue alignLoadVec =
Scott Micheldbac4cf2008-01-11 02:53:15 +0000712 AlignedLoad(Op, DAG, ST, SN, alignment,
713 chunk_offset, slot_offset, VT, was16aligned);
Scott Michel8efdca42007-12-04 22:23:35 +0000714
Gabor Greif1c80d112008-08-28 21:40:38 +0000715 if (alignLoadVec.getNode() == 0)
Scott Micheldbac4cf2008-01-11 02:53:15 +0000716 return alignLoadVec;
Scott Michel8efdca42007-12-04 22:23:35 +0000717
Scott Micheldbac4cf2008-01-11 02:53:15 +0000718 LoadSDNode *LN = cast<LoadSDNode>(alignLoadVec);
Dan Gohman8181bd12008-07-27 21:46:04 +0000719 SDValue basePtr = LN->getBasePtr();
720 SDValue the_chain = alignLoadVec.getValue(1);
721 SDValue theValue = SN->getValue();
722 SDValue result;
Scott Michel8efdca42007-12-04 22:23:35 +0000723
724 if (StVT != VT
Scott Michel5a6f17b2008-01-30 02:55:46 +0000725 && (theValue.getOpcode() == ISD::AssertZext
726 || theValue.getOpcode() == ISD::AssertSext)) {
Scott Michel8efdca42007-12-04 22:23:35 +0000727 // Drill down and get the value for zero- and sign-extended
728 // quantities
Scott Michel4ec722e2008-07-16 17:17:29 +0000729 theValue = theValue.getOperand(0);
Scott Michel8efdca42007-12-04 22:23:35 +0000730 }
731
Scott Micheldbac4cf2008-01-11 02:53:15 +0000732 chunk_offset &= 0xf;
Scott Michel8efdca42007-12-04 22:23:35 +0000733
Dan Gohman8181bd12008-07-27 21:46:04 +0000734 SDValue insertEltOffs = DAG.getConstant(chunk_offset, PtrVT);
735 SDValue insertEltPtr;
736 SDValue insertEltOp;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000737
738 // If the base pointer is already a D-form address, then just create
739 // a new D-form address with a slot offset and the orignal base pointer.
740 // Otherwise generate a D-form address with the slot offset relative
741 // to the stack pointer, which is always aligned.
Scott Michelabc58242008-01-11 21:01:19 +0000742 DEBUG(cerr << "CellSPU LowerSTORE: basePtr = ");
Gabor Greif1c80d112008-08-28 21:40:38 +0000743 DEBUG(basePtr.getNode()->dump(&DAG));
Scott Michelabc58242008-01-11 21:01:19 +0000744 DEBUG(cerr << "\n");
745
Scott Michelf9f42e62008-01-29 02:16:57 +0000746 if (basePtr.getOpcode() == SPUISD::IndirectAddr ||
747 (basePtr.getOpcode() == ISD::ADD
748 && basePtr.getOperand(0).getOpcode() == SPUISD::IndirectAddr)) {
Scott Michelabc58242008-01-11 21:01:19 +0000749 insertEltPtr = basePtr;
Scott Micheldbac4cf2008-01-11 02:53:15 +0000750 } else {
Scott Michelf9f42e62008-01-29 02:16:57 +0000751 insertEltPtr = DAG.getNode(ISD::ADD, PtrVT, basePtr, insertEltOffs);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000752 }
753
754 insertEltOp = DAG.getNode(SPUISD::INSERT_MASK, stVecVT, insertEltPtr);
Scott Michel8efdca42007-12-04 22:23:35 +0000755 result = DAG.getNode(SPUISD::SHUFB, vecVT,
Scott Michel5a6f17b2008-01-30 02:55:46 +0000756 DAG.getNode(ISD::SCALAR_TO_VECTOR, vecVT, theValue),
757 alignLoadVec,
758 DAG.getNode(ISD::BIT_CONVERT, vecVT, insertEltOp));
Scott Michel8efdca42007-12-04 22:23:35 +0000759
Scott Micheldbac4cf2008-01-11 02:53:15 +0000760 result = DAG.getStore(the_chain, result, basePtr,
Scott Michel8efdca42007-12-04 22:23:35 +0000761 LN->getSrcValue(), LN->getSrcValueOffset(),
762 LN->isVolatile(), LN->getAlignment());
763
764 return result;
765 /*UNREACHED*/
766 }
767 case ISD::PRE_INC:
768 case ISD::PRE_DEC:
769 case ISD::POST_INC:
770 case ISD::POST_DEC:
771 case ISD::LAST_INDEXED_MODE:
772 cerr << "LowerLOAD: Got a LoadSDNode with an addr mode other than "
773 "UNINDEXED\n";
774 cerr << (unsigned) SN->getAddressingMode() << "\n";
775 abort();
776 /*NOTREACHED*/
777 }
778
Dan Gohman8181bd12008-07-27 21:46:04 +0000779 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000780}
781
782/// Generate the address of a constant pool entry.
Dan Gohman8181bd12008-07-27 21:46:04 +0000783static SDValue
784LowerConstantPool(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Duncan Sands92c43912008-06-06 12:08:01 +0000785 MVT PtrVT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +0000786 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
787 Constant *C = CP->getConstVal();
Dan Gohman8181bd12008-07-27 21:46:04 +0000788 SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
789 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Micheldbac4cf2008-01-11 02:53:15 +0000790 const TargetMachine &TM = DAG.getTarget();
Scott Michel8efdca42007-12-04 22:23:35 +0000791
792 if (TM.getRelocationModel() == Reloc::Static) {
793 if (!ST->usingLargeMem()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000794 // Just return the SDValue with the constant pool address in it.
Scott Michel394e26d2008-01-17 20:38:41 +0000795 return DAG.getNode(SPUISD::AFormAddr, PtrVT, CPI, Zero);
Scott Michel8efdca42007-12-04 22:23:35 +0000796 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000797 SDValue Hi = DAG.getNode(SPUISD::Hi, PtrVT, CPI, Zero);
798 SDValue Lo = DAG.getNode(SPUISD::Lo, PtrVT, CPI, Zero);
Scott Michel97872d32008-02-23 18:41:37 +0000799 return DAG.getNode(SPUISD::IndirectAddr, PtrVT, Hi, Lo);
Scott Michel8efdca42007-12-04 22:23:35 +0000800 }
801 }
802
803 assert(0 &&
804 "LowerConstantPool: Relocation model other than static not supported.");
Dan Gohman8181bd12008-07-27 21:46:04 +0000805 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000806}
807
Dan Gohman8181bd12008-07-27 21:46:04 +0000808static SDValue
809LowerJumpTable(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Duncan Sands92c43912008-06-06 12:08:01 +0000810 MVT PtrVT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +0000811 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000812 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
813 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Michel8efdca42007-12-04 22:23:35 +0000814 const TargetMachine &TM = DAG.getTarget();
815
816 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michel97872d32008-02-23 18:41:37 +0000817 if (!ST->usingLargeMem()) {
818 return DAG.getNode(SPUISD::AFormAddr, PtrVT, JTI, Zero);
819 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000820 SDValue Hi = DAG.getNode(SPUISD::Hi, PtrVT, JTI, Zero);
821 SDValue Lo = DAG.getNode(SPUISD::Lo, PtrVT, JTI, Zero);
Scott Michel97872d32008-02-23 18:41:37 +0000822 return DAG.getNode(SPUISD::IndirectAddr, PtrVT, Hi, Lo);
823 }
Scott Michel8efdca42007-12-04 22:23:35 +0000824 }
825
826 assert(0 &&
827 "LowerJumpTable: Relocation model other than static not supported.");
Dan Gohman8181bd12008-07-27 21:46:04 +0000828 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000829}
830
Dan Gohman8181bd12008-07-27 21:46:04 +0000831static SDValue
832LowerGlobalAddress(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Duncan Sands92c43912008-06-06 12:08:01 +0000833 MVT PtrVT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +0000834 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
835 GlobalValue *GV = GSDN->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +0000836 SDValue GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
Scott Michel8efdca42007-12-04 22:23:35 +0000837 const TargetMachine &TM = DAG.getTarget();
Dan Gohman8181bd12008-07-27 21:46:04 +0000838 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Michel4ec722e2008-07-16 17:17:29 +0000839
Scott Michel8efdca42007-12-04 22:23:35 +0000840 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michelf9f42e62008-01-29 02:16:57 +0000841 if (!ST->usingLargeMem()) {
842 return DAG.getNode(SPUISD::AFormAddr, PtrVT, GA, Zero);
843 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000844 SDValue Hi = DAG.getNode(SPUISD::Hi, PtrVT, GA, Zero);
845 SDValue Lo = DAG.getNode(SPUISD::Lo, PtrVT, GA, Zero);
Scott Michelf9f42e62008-01-29 02:16:57 +0000846 return DAG.getNode(SPUISD::IndirectAddr, PtrVT, Hi, Lo);
847 }
Scott Michel8efdca42007-12-04 22:23:35 +0000848 } else {
849 cerr << "LowerGlobalAddress: Relocation model other than static not "
Scott Michel5a6f17b2008-01-30 02:55:46 +0000850 << "supported.\n";
Scott Michel8efdca42007-12-04 22:23:35 +0000851 abort();
852 /*NOTREACHED*/
853 }
854
Dan Gohman8181bd12008-07-27 21:46:04 +0000855 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000856}
857
858//! Custom lower i64 integer constants
859/*!
860 This code inserts all of the necessary juggling that needs to occur to load
861 a 64-bit constant into a register.
862 */
Dan Gohman8181bd12008-07-27 21:46:04 +0000863static SDValue
864LowerConstant(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +0000865 MVT VT = Op.getValueType();
Gabor Greif1c80d112008-08-28 21:40:38 +0000866 ConstantSDNode *CN = cast<ConstantSDNode>(Op.getNode());
Scott Michel8efdca42007-12-04 22:23:35 +0000867
868 if (VT == MVT::i64) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000869 SDValue T = DAG.getConstant(CN->getValue(), MVT::i64);
Scott Michel8efdca42007-12-04 22:23:35 +0000870 return DAG.getNode(SPUISD::EXTRACT_ELT0, VT,
Scott Michel5a6f17b2008-01-30 02:55:46 +0000871 DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i64, T, T));
Scott Michel8efdca42007-12-04 22:23:35 +0000872 } else {
873 cerr << "LowerConstant: unhandled constant type "
Duncan Sands92c43912008-06-06 12:08:01 +0000874 << VT.getMVTString()
Scott Michel5a6f17b2008-01-30 02:55:46 +0000875 << "\n";
Scott Michel8efdca42007-12-04 22:23:35 +0000876 abort();
877 /*NOTREACHED*/
878 }
879
Dan Gohman8181bd12008-07-27 21:46:04 +0000880 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000881}
882
Nate Begeman78125042008-02-14 18:43:04 +0000883//! Custom lower double precision floating point constants
Dan Gohman8181bd12008-07-27 21:46:04 +0000884static SDValue
885LowerConstantFP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +0000886 MVT VT = Op.getValueType();
Gabor Greif1c80d112008-08-28 21:40:38 +0000887 ConstantFPSDNode *FP = cast<ConstantFPSDNode>(Op.getNode());
Scott Michel8efdca42007-12-04 22:23:35 +0000888
889 assert((FP != 0) &&
Scott Michel5a6f17b2008-01-30 02:55:46 +0000890 "LowerConstantFP: Node is not ConstantFPSDNode");
Scott Michel8efdca42007-12-04 22:23:35 +0000891
Nate Begeman78125042008-02-14 18:43:04 +0000892 if (VT == MVT::f64) {
Scott Michel11e88bb2007-12-19 20:15:47 +0000893 uint64_t dbits = DoubleToBits(FP->getValueAPF().convertToDouble());
Scott Michel8efdca42007-12-04 22:23:35 +0000894 return DAG.getNode(ISD::BIT_CONVERT, VT,
Scott Michel5a6f17b2008-01-30 02:55:46 +0000895 LowerConstant(DAG.getConstant(dbits, MVT::i64), DAG));
Scott Michel8efdca42007-12-04 22:23:35 +0000896 }
897
Dan Gohman8181bd12008-07-27 21:46:04 +0000898 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +0000899}
900
Scott Michel394e26d2008-01-17 20:38:41 +0000901//! Lower MVT::i1, MVT::i8 brcond to a promoted type (MVT::i32, MVT::i16)
Dan Gohman8181bd12008-07-27 21:46:04 +0000902static SDValue
903LowerBRCOND(SDValue Op, SelectionDAG &DAG)
Scott Michel394e26d2008-01-17 20:38:41 +0000904{
Dan Gohman8181bd12008-07-27 21:46:04 +0000905 SDValue Cond = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +0000906 MVT CondVT = Cond.getValueType();
907 MVT CondNVT;
Scott Michel394e26d2008-01-17 20:38:41 +0000908
909 if (CondVT == MVT::i1 || CondVT == MVT::i8) {
910 CondNVT = (CondVT == MVT::i1 ? MVT::i32 : MVT::i16);
911 return DAG.getNode(ISD::BRCOND, Op.getValueType(),
912 Op.getOperand(0),
913 DAG.getNode(ISD::ZERO_EXTEND, CondNVT, Op.getOperand(1)),
914 Op.getOperand(2));
915 } else
Dan Gohman8181bd12008-07-27 21:46:04 +0000916 return SDValue(); // Unchanged
Scott Michel394e26d2008-01-17 20:38:41 +0000917}
918
Dan Gohman8181bd12008-07-27 21:46:04 +0000919static SDValue
920LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG, int &VarArgsFrameIndex)
Scott Michel8efdca42007-12-04 22:23:35 +0000921{
922 MachineFunction &MF = DAG.getMachineFunction();
923 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattner1b989192007-12-31 04:13:23 +0000924 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +0000925 SmallVector<SDValue, 8> ArgValues;
926 SDValue Root = Op.getOperand(0);
Scott Michel8efdca42007-12-04 22:23:35 +0000927 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
928
929 const unsigned *ArgRegs = SPURegisterInfo::getArgRegs();
930 const unsigned NumArgRegs = SPURegisterInfo::getNumArgRegs();
Scott Michel4ec722e2008-07-16 17:17:29 +0000931
Scott Michel8efdca42007-12-04 22:23:35 +0000932 unsigned ArgOffset = SPUFrameInfo::minStackSize();
933 unsigned ArgRegIdx = 0;
934 unsigned StackSlotSize = SPUFrameInfo::stackSlotSize();
Scott Michel4ec722e2008-07-16 17:17:29 +0000935
Duncan Sands92c43912008-06-06 12:08:01 +0000936 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel4ec722e2008-07-16 17:17:29 +0000937
Scott Michel8efdca42007-12-04 22:23:35 +0000938 // Add DAG nodes to load the arguments or copy them out of registers.
Gabor Greif1c80d112008-08-28 21:40:38 +0000939 for (unsigned ArgNo = 0, e = Op.getNode()->getNumValues()-1; ArgNo != e; ++ArgNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000940 SDValue ArgVal;
Scott Michel8efdca42007-12-04 22:23:35 +0000941 bool needsLoad = false;
Duncan Sands92c43912008-06-06 12:08:01 +0000942 MVT ObjectVT = Op.getValue(ArgNo).getValueType();
943 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Scott Michel8efdca42007-12-04 22:23:35 +0000944
Duncan Sands92c43912008-06-06 12:08:01 +0000945 switch (ObjectVT.getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +0000946 default: {
947 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
Duncan Sands92c43912008-06-06 12:08:01 +0000948 << ObjectVT.getMVTString()
Scott Michel8efdca42007-12-04 22:23:35 +0000949 << "\n";
950 abort();
951 }
952 case MVT::i8:
953 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner1b989192007-12-31 04:13:23 +0000954 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R8CRegClass);
955 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel8efdca42007-12-04 22:23:35 +0000956 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i8);
957 ++ArgRegIdx;
958 } else {
959 needsLoad = true;
960 }
961 break;
962 case MVT::i16:
963 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner1b989192007-12-31 04:13:23 +0000964 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R16CRegClass);
965 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel8efdca42007-12-04 22:23:35 +0000966 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i16);
967 ++ArgRegIdx;
968 } else {
969 needsLoad = true;
970 }
971 break;
972 case MVT::i32:
973 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner1b989192007-12-31 04:13:23 +0000974 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
975 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel8efdca42007-12-04 22:23:35 +0000976 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i32);
977 ++ArgRegIdx;
978 } else {
979 needsLoad = true;
980 }
981 break;
982 case MVT::i64:
983 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner1b989192007-12-31 04:13:23 +0000984 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R64CRegClass);
985 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel8efdca42007-12-04 22:23:35 +0000986 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i64);
987 ++ArgRegIdx;
988 } else {
989 needsLoad = true;
990 }
991 break;
992 case MVT::f32:
993 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner1b989192007-12-31 04:13:23 +0000994 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32FPRegClass);
995 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel8efdca42007-12-04 22:23:35 +0000996 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::f32);
997 ++ArgRegIdx;
998 } else {
999 needsLoad = true;
1000 }
1001 break;
1002 case MVT::f64:
1003 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner1b989192007-12-31 04:13:23 +00001004 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R64FPRegClass);
1005 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel8efdca42007-12-04 22:23:35 +00001006 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::f64);
1007 ++ArgRegIdx;
1008 } else {
1009 needsLoad = true;
1010 }
1011 break;
1012 case MVT::v2f64:
1013 case MVT::v4f32:
Scott Michel6baba072008-03-05 23:02:02 +00001014 case MVT::v2i64:
Scott Michel8efdca42007-12-04 22:23:35 +00001015 case MVT::v4i32:
1016 case MVT::v8i16:
1017 case MVT::v16i8:
1018 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner1b989192007-12-31 04:13:23 +00001019 unsigned VReg = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
1020 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel8efdca42007-12-04 22:23:35 +00001021 ArgVal = DAG.getCopyFromReg(Root, VReg, ObjectVT);
1022 ++ArgRegIdx;
1023 } else {
1024 needsLoad = true;
1025 }
1026 break;
1027 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001028
Scott Michel8efdca42007-12-04 22:23:35 +00001029 // We need to load the argument to a virtual register if we determined above
1030 // that we ran out of physical registers of the appropriate type
1031 if (needsLoad) {
Chris Lattner60069452008-02-13 07:35:30 +00001032 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Dan Gohman8181bd12008-07-27 21:46:04 +00001033 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner60069452008-02-13 07:35:30 +00001034 ArgVal = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
Scott Michel8efdca42007-12-04 22:23:35 +00001035 ArgOffset += StackSlotSize;
1036 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001037
Scott Michel8efdca42007-12-04 22:23:35 +00001038 ArgValues.push_back(ArgVal);
1039 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001040
Scott Michel8efdca42007-12-04 22:23:35 +00001041 // If the function takes variable number of arguments, make a frame index for
1042 // the start of the first vararg value... for expansion of llvm.va_start.
1043 if (isVarArg) {
Duncan Sands92c43912008-06-06 12:08:01 +00001044 VarArgsFrameIndex = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Scott Michel8efdca42007-12-04 22:23:35 +00001045 ArgOffset);
Dan Gohman8181bd12008-07-27 21:46:04 +00001046 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Scott Michel8efdca42007-12-04 22:23:35 +00001047 // If this function is vararg, store any remaining integer argument regs to
1048 // their spots on the stack so that they may be loaded by deferencing the
1049 // result of va_next.
Dan Gohman8181bd12008-07-27 21:46:04 +00001050 SmallVector<SDValue, 8> MemOps;
Scott Michel8efdca42007-12-04 22:23:35 +00001051 for (; ArgRegIdx != NumArgRegs; ++ArgRegIdx) {
Chris Lattner1b989192007-12-31 04:13:23 +00001052 unsigned VReg = RegInfo.createVirtualRegister(&SPU::GPRCRegClass);
1053 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Dan Gohman8181bd12008-07-27 21:46:04 +00001054 SDValue Val = DAG.getCopyFromReg(Root, VReg, PtrVT);
1055 SDValue Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Scott Michel8efdca42007-12-04 22:23:35 +00001056 MemOps.push_back(Store);
1057 // Increment the address by four for the next argument to store
Dan Gohman8181bd12008-07-27 21:46:04 +00001058 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Scott Michel8efdca42007-12-04 22:23:35 +00001059 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
1060 }
1061 if (!MemOps.empty())
1062 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
1063 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001064
Scott Michel8efdca42007-12-04 22:23:35 +00001065 ArgValues.push_back(Root);
Scott Michel4ec722e2008-07-16 17:17:29 +00001066
Scott Michel8efdca42007-12-04 22:23:35 +00001067 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001068 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Duncan Sandsf19591c2008-06-30 10:19:09 +00001069 ArgValues.size());
Scott Michel8efdca42007-12-04 22:23:35 +00001070}
1071
1072/// isLSAAddress - Return the immediate to use if the specified
1073/// value is representable as a LSA address.
Dan Gohman8181bd12008-07-27 21:46:04 +00001074static SDNode *isLSAAddress(SDValue Op, SelectionDAG &DAG) {
Scott Michel8efdca42007-12-04 22:23:35 +00001075 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1076 if (!C) return 0;
Scott Michel4ec722e2008-07-16 17:17:29 +00001077
Scott Michel8efdca42007-12-04 22:23:35 +00001078 int Addr = C->getValue();
1079 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
1080 (Addr << 14 >> 14) != Addr)
1081 return 0; // Top 14 bits have to be sext of immediate.
Scott Michel4ec722e2008-07-16 17:17:29 +00001082
Gabor Greif1c80d112008-08-28 21:40:38 +00001083 return DAG.getConstant((int)C->getValue() >> 2, MVT::i32).getNode();
Scott Michel8efdca42007-12-04 22:23:35 +00001084}
1085
1086static
Dan Gohman8181bd12008-07-27 21:46:04 +00001087SDValue
1088LowerCALL(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
1089 SDValue Chain = Op.getOperand(0);
Scott Michel8efdca42007-12-04 22:23:35 +00001090#if 0
1091 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1092 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1093#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00001094 SDValue Callee = Op.getOperand(4);
Scott Michel8efdca42007-12-04 22:23:35 +00001095 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1096 unsigned StackSlotSize = SPUFrameInfo::stackSlotSize();
1097 const unsigned *ArgRegs = SPURegisterInfo::getArgRegs();
1098 const unsigned NumArgRegs = SPURegisterInfo::getNumArgRegs();
1099
1100 // Handy pointer type
Duncan Sands92c43912008-06-06 12:08:01 +00001101 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel4ec722e2008-07-16 17:17:29 +00001102
Scott Michel8efdca42007-12-04 22:23:35 +00001103 // Accumulate how many bytes are to be pushed on the stack, including the
1104 // linkage area, and parameter passing area. According to the SPU ABI,
1105 // we minimally need space for [LR] and [SP]
1106 unsigned NumStackBytes = SPUFrameInfo::minStackSize();
Scott Michel4ec722e2008-07-16 17:17:29 +00001107
Scott Michel8efdca42007-12-04 22:23:35 +00001108 // Set up a copy of the stack pointer for use loading and storing any
1109 // arguments that may not fit in the registers available for argument
1110 // passing.
Dan Gohman8181bd12008-07-27 21:46:04 +00001111 SDValue StackPtr = DAG.getRegister(SPU::R1, MVT::i32);
Scott Michel4ec722e2008-07-16 17:17:29 +00001112
Scott Michel8efdca42007-12-04 22:23:35 +00001113 // Figure out which arguments are going to go in registers, and which in
1114 // memory.
1115 unsigned ArgOffset = SPUFrameInfo::minStackSize(); // Just below [LR]
1116 unsigned ArgRegIdx = 0;
1117
1118 // Keep track of registers passing arguments
Dan Gohman8181bd12008-07-27 21:46:04 +00001119 std::vector<std::pair<unsigned, SDValue> > RegsToPass;
Scott Michel8efdca42007-12-04 22:23:35 +00001120 // And the arguments passed on the stack
Dan Gohman8181bd12008-07-27 21:46:04 +00001121 SmallVector<SDValue, 8> MemOpChains;
Scott Michel8efdca42007-12-04 22:23:35 +00001122
1123 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001124 SDValue Arg = Op.getOperand(5+2*i);
Scott Michel4ec722e2008-07-16 17:17:29 +00001125
Scott Michel8efdca42007-12-04 22:23:35 +00001126 // PtrOff will be used to store the current argument to the stack if a
1127 // register cannot be found for it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001128 SDValue PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Scott Michel8efdca42007-12-04 22:23:35 +00001129 PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr, PtrOff);
1130
Duncan Sands92c43912008-06-06 12:08:01 +00001131 switch (Arg.getValueType().getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001132 default: assert(0 && "Unexpected ValueType for argument!");
1133 case MVT::i32:
1134 case MVT::i64:
1135 case MVT::i128:
1136 if (ArgRegIdx != NumArgRegs) {
1137 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1138 } else {
1139 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Scott Michel5a6f17b2008-01-30 02:55:46 +00001140 ArgOffset += StackSlotSize;
Scott Michel8efdca42007-12-04 22:23:35 +00001141 }
1142 break;
1143 case MVT::f32:
1144 case MVT::f64:
1145 if (ArgRegIdx != NumArgRegs) {
1146 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1147 } else {
1148 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Scott Michel5a6f17b2008-01-30 02:55:46 +00001149 ArgOffset += StackSlotSize;
Scott Michel8efdca42007-12-04 22:23:35 +00001150 }
1151 break;
1152 case MVT::v4f32:
1153 case MVT::v4i32:
1154 case MVT::v8i16:
1155 case MVT::v16i8:
1156 if (ArgRegIdx != NumArgRegs) {
1157 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1158 } else {
1159 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Scott Michel5a6f17b2008-01-30 02:55:46 +00001160 ArgOffset += StackSlotSize;
Scott Michel8efdca42007-12-04 22:23:35 +00001161 }
1162 break;
1163 }
1164 }
1165
1166 // Update number of stack bytes actually used, insert a call sequence start
1167 NumStackBytes = (ArgOffset - SPUFrameInfo::minStackSize());
1168 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumStackBytes, PtrVT));
1169
1170 if (!MemOpChains.empty()) {
1171 // Adjust the stack pointer for the stack arguments.
1172 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1173 &MemOpChains[0], MemOpChains.size());
1174 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001175
Scott Michel8efdca42007-12-04 22:23:35 +00001176 // Build a sequence of copy-to-reg nodes chained together with token chain
1177 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman8181bd12008-07-27 21:46:04 +00001178 SDValue InFlag;
Scott Michel8efdca42007-12-04 22:23:35 +00001179 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1180 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1181 InFlag);
1182 InFlag = Chain.getValue(1);
1183 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001184
Dan Gohman8181bd12008-07-27 21:46:04 +00001185 SmallVector<SDValue, 8> Ops;
Scott Michel8efdca42007-12-04 22:23:35 +00001186 unsigned CallOpc = SPUISD::CALL;
Scott Michel4ec722e2008-07-16 17:17:29 +00001187
Scott Michel8efdca42007-12-04 22:23:35 +00001188 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1189 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1190 // node so that legalize doesn't hack it.
1191 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1192 GlobalValue *GV = G->getGlobal();
Duncan Sands92c43912008-06-06 12:08:01 +00001193 MVT CalleeVT = Callee.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00001194 SDValue Zero = DAG.getConstant(0, PtrVT);
1195 SDValue GA = DAG.getTargetGlobalAddress(GV, CalleeVT);
Scott Michel8efdca42007-12-04 22:23:35 +00001196
Scott Micheldbac4cf2008-01-11 02:53:15 +00001197 if (!ST->usingLargeMem()) {
1198 // Turn calls to targets that are defined (i.e., have bodies) into BRSL
1199 // style calls, otherwise, external symbols are BRASL calls. This assumes
1200 // that declared/defined symbols are in the same compilation unit and can
1201 // be reached through PC-relative jumps.
1202 //
1203 // NOTE:
1204 // This may be an unsafe assumption for JIT and really large compilation
1205 // units.
1206 if (GV->isDeclaration()) {
1207 Callee = DAG.getNode(SPUISD::AFormAddr, CalleeVT, GA, Zero);
1208 } else {
1209 Callee = DAG.getNode(SPUISD::PCRelAddr, CalleeVT, GA, Zero);
1210 }
Scott Michel8efdca42007-12-04 22:23:35 +00001211 } else {
Scott Micheldbac4cf2008-01-11 02:53:15 +00001212 // "Large memory" mode: Turn all calls into indirect calls with a X-form
1213 // address pairs:
Scott Michelf9f42e62008-01-29 02:16:57 +00001214 Callee = DAG.getNode(SPUISD::IndirectAddr, PtrVT, GA, Zero);
Scott Michel8efdca42007-12-04 22:23:35 +00001215 }
1216 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1217 Callee = DAG.getExternalSymbol(S->getSymbol(), Callee.getValueType());
Scott Micheldbac4cf2008-01-11 02:53:15 +00001218 else if (SDNode *Dest = isLSAAddress(Callee, DAG)) {
Scott Michel8efdca42007-12-04 22:23:35 +00001219 // If this is an absolute destination address that appears to be a legal
1220 // local store address, use the munged value.
Dan Gohman8181bd12008-07-27 21:46:04 +00001221 Callee = SDValue(Dest, 0);
Scott Micheldbac4cf2008-01-11 02:53:15 +00001222 }
Scott Michel8efdca42007-12-04 22:23:35 +00001223
1224 Ops.push_back(Chain);
1225 Ops.push_back(Callee);
Scott Michel4ec722e2008-07-16 17:17:29 +00001226
Scott Michel8efdca42007-12-04 22:23:35 +00001227 // Add argument registers to the end of the list so that they are known live
1228 // into the call.
1229 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Scott Michel4ec722e2008-07-16 17:17:29 +00001230 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Scott Michel8efdca42007-12-04 22:23:35 +00001231 RegsToPass[i].second.getValueType()));
Scott Michel4ec722e2008-07-16 17:17:29 +00001232
Gabor Greif1c80d112008-08-28 21:40:38 +00001233 if (InFlag.getNode())
Scott Michel8efdca42007-12-04 22:23:35 +00001234 Ops.push_back(InFlag);
Duncan Sands698842f2008-07-02 17:40:58 +00001235 // Returns a chain and a flag for retval copy to use.
1236 Chain = DAG.getNode(CallOpc, DAG.getVTList(MVT::Other, MVT::Flag),
1237 &Ops[0], Ops.size());
Scott Michel8efdca42007-12-04 22:23:35 +00001238 InFlag = Chain.getValue(1);
1239
Evan Cheng07322bb2008-02-05 22:44:06 +00001240 Chain = DAG.getCALLSEQ_END(Chain,
1241 DAG.getConstant(NumStackBytes, PtrVT),
1242 DAG.getConstant(0, PtrVT),
1243 InFlag);
Gabor Greif1c80d112008-08-28 21:40:38 +00001244 if (Op.getNode()->getValueType(0) != MVT::Other)
Evan Cheng07322bb2008-02-05 22:44:06 +00001245 InFlag = Chain.getValue(1);
1246
Dan Gohman8181bd12008-07-27 21:46:04 +00001247 SDValue ResultVals[3];
Scott Michel8efdca42007-12-04 22:23:35 +00001248 unsigned NumResults = 0;
Scott Michel4ec722e2008-07-16 17:17:29 +00001249
Scott Michel8efdca42007-12-04 22:23:35 +00001250 // If the call has results, copy the values out of the ret val registers.
Gabor Greif1c80d112008-08-28 21:40:38 +00001251 switch (Op.getNode()->getValueType(0).getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001252 default: assert(0 && "Unexpected ret value!");
1253 case MVT::Other: break;
1254 case MVT::i32:
Gabor Greif1c80d112008-08-28 21:40:38 +00001255 if (Op.getNode()->getValueType(1) == MVT::i32) {
Scott Michel8efdca42007-12-04 22:23:35 +00001256 Chain = DAG.getCopyFromReg(Chain, SPU::R4, MVT::i32, InFlag).getValue(1);
1257 ResultVals[0] = Chain.getValue(0);
1258 Chain = DAG.getCopyFromReg(Chain, SPU::R3, MVT::i32,
1259 Chain.getValue(2)).getValue(1);
1260 ResultVals[1] = Chain.getValue(0);
1261 NumResults = 2;
Scott Michel8efdca42007-12-04 22:23:35 +00001262 } else {
1263 Chain = DAG.getCopyFromReg(Chain, SPU::R3, MVT::i32, InFlag).getValue(1);
1264 ResultVals[0] = Chain.getValue(0);
1265 NumResults = 1;
1266 }
Scott Michel8efdca42007-12-04 22:23:35 +00001267 break;
1268 case MVT::i64:
1269 Chain = DAG.getCopyFromReg(Chain, SPU::R3, MVT::i64, InFlag).getValue(1);
1270 ResultVals[0] = Chain.getValue(0);
1271 NumResults = 1;
Scott Michel8efdca42007-12-04 22:23:35 +00001272 break;
1273 case MVT::f32:
1274 case MVT::f64:
Gabor Greif1c80d112008-08-28 21:40:38 +00001275 Chain = DAG.getCopyFromReg(Chain, SPU::R3, Op.getNode()->getValueType(0),
Scott Michel8efdca42007-12-04 22:23:35 +00001276 InFlag).getValue(1);
1277 ResultVals[0] = Chain.getValue(0);
1278 NumResults = 1;
Scott Michel8efdca42007-12-04 22:23:35 +00001279 break;
1280 case MVT::v2f64:
1281 case MVT::v4f32:
1282 case MVT::v4i32:
1283 case MVT::v8i16:
1284 case MVT::v16i8:
Gabor Greif1c80d112008-08-28 21:40:38 +00001285 Chain = DAG.getCopyFromReg(Chain, SPU::R3, Op.getNode()->getValueType(0),
Scott Michel8efdca42007-12-04 22:23:35 +00001286 InFlag).getValue(1);
1287 ResultVals[0] = Chain.getValue(0);
1288 NumResults = 1;
Scott Michel8efdca42007-12-04 22:23:35 +00001289 break;
1290 }
Duncan Sands698842f2008-07-02 17:40:58 +00001291
Scott Michel8efdca42007-12-04 22:23:35 +00001292 // If the function returns void, just return the chain.
1293 if (NumResults == 0)
1294 return Chain;
Scott Michel4ec722e2008-07-16 17:17:29 +00001295
Scott Michel8efdca42007-12-04 22:23:35 +00001296 // Otherwise, merge everything together with a MERGE_VALUES node.
1297 ResultVals[NumResults++] = Chain;
Dan Gohman8181bd12008-07-27 21:46:04 +00001298 SDValue Res = DAG.getMergeValues(ResultVals, NumResults);
Gabor Greif46bf5472008-08-26 22:36:50 +00001299 return Res.getValue(Op.getResNo());
Scott Michel8efdca42007-12-04 22:23:35 +00001300}
1301
Dan Gohman8181bd12008-07-27 21:46:04 +00001302static SDValue
1303LowerRET(SDValue Op, SelectionDAG &DAG, TargetMachine &TM) {
Scott Michel8efdca42007-12-04 22:23:35 +00001304 SmallVector<CCValAssign, 16> RVLocs;
1305 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
1306 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
1307 CCState CCInfo(CC, isVarArg, TM, RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001308 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SPU);
Scott Michel4ec722e2008-07-16 17:17:29 +00001309
Scott Michel8efdca42007-12-04 22:23:35 +00001310 // If this is the first return lowered for this function, add the regs to the
1311 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +00001312 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001313 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner1b989192007-12-31 04:13:23 +00001314 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Scott Michel8efdca42007-12-04 22:23:35 +00001315 }
1316
Dan Gohman8181bd12008-07-27 21:46:04 +00001317 SDValue Chain = Op.getOperand(0);
1318 SDValue Flag;
Scott Michel4ec722e2008-07-16 17:17:29 +00001319
Scott Michel8efdca42007-12-04 22:23:35 +00001320 // Copy the result values into the output registers.
1321 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1322 CCValAssign &VA = RVLocs[i];
1323 assert(VA.isRegLoc() && "Can only return in registers!");
1324 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
1325 Flag = Chain.getValue(1);
1326 }
1327
Gabor Greif1c80d112008-08-28 21:40:38 +00001328 if (Flag.getNode())
Scott Michel8efdca42007-12-04 22:23:35 +00001329 return DAG.getNode(SPUISD::RET_FLAG, MVT::Other, Chain, Flag);
1330 else
1331 return DAG.getNode(SPUISD::RET_FLAG, MVT::Other, Chain);
1332}
1333
1334
1335//===----------------------------------------------------------------------===//
1336// Vector related lowering:
1337//===----------------------------------------------------------------------===//
1338
1339static ConstantSDNode *
1340getVecImm(SDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001341 SDValue OpVal(0, 0);
Scott Michel4ec722e2008-07-16 17:17:29 +00001342
Scott Michel8efdca42007-12-04 22:23:35 +00001343 // Check to see if this buildvec has a single non-undef value in its elements.
1344 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1345 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greif1c80d112008-08-28 21:40:38 +00001346 if (OpVal.getNode() == 0)
Scott Michel8efdca42007-12-04 22:23:35 +00001347 OpVal = N->getOperand(i);
1348 else if (OpVal != N->getOperand(i))
1349 return 0;
1350 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001351
Gabor Greif1c80d112008-08-28 21:40:38 +00001352 if (OpVal.getNode() != 0) {
Scott Michel8efdca42007-12-04 22:23:35 +00001353 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
1354 return CN;
1355 }
1356 }
1357
1358 return 0; // All UNDEF: use implicit def.; not Constant node
1359}
1360
1361/// get_vec_i18imm - Test if this vector is a vector filled with the same value
1362/// and the value fits into an unsigned 18-bit constant, and if so, return the
1363/// constant
Dan Gohman8181bd12008-07-27 21:46:04 +00001364SDValue SPU::get_vec_u18imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00001365 MVT ValueType) {
Scott Michel8efdca42007-12-04 22:23:35 +00001366 if (ConstantSDNode *CN = getVecImm(N)) {
1367 uint64_t Value = CN->getValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001368 if (ValueType == MVT::i64) {
1369 uint64_t UValue = CN->getValue();
1370 uint32_t upper = uint32_t(UValue >> 32);
1371 uint32_t lower = uint32_t(UValue);
1372 if (upper != lower)
Dan Gohman8181bd12008-07-27 21:46:04 +00001373 return SDValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001374 Value = Value >> 32;
1375 }
Scott Michel8efdca42007-12-04 22:23:35 +00001376 if (Value <= 0x3ffff)
1377 return DAG.getConstant(Value, ValueType);
1378 }
1379
Dan Gohman8181bd12008-07-27 21:46:04 +00001380 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001381}
1382
1383/// get_vec_i16imm - Test if this vector is a vector filled with the same value
1384/// and the value fits into a signed 16-bit constant, and if so, return the
1385/// constant
Dan Gohman8181bd12008-07-27 21:46:04 +00001386SDValue SPU::get_vec_i16imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00001387 MVT ValueType) {
Scott Michel8efdca42007-12-04 22:23:35 +00001388 if (ConstantSDNode *CN = getVecImm(N)) {
Scott Michel6baba072008-03-05 23:02:02 +00001389 int64_t Value = CN->getSignExtended();
Scott Michelbcc7b672008-03-06 04:02:54 +00001390 if (ValueType == MVT::i64) {
1391 uint64_t UValue = CN->getValue();
1392 uint32_t upper = uint32_t(UValue >> 32);
1393 uint32_t lower = uint32_t(UValue);
1394 if (upper != lower)
Dan Gohman8181bd12008-07-27 21:46:04 +00001395 return SDValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001396 Value = Value >> 32;
1397 }
Scott Michel6baba072008-03-05 23:02:02 +00001398 if (Value >= -(1 << 15) && Value <= ((1 << 15) - 1)) {
1399 return DAG.getConstant(Value, ValueType);
Scott Michel8efdca42007-12-04 22:23:35 +00001400 }
1401 }
1402
Dan Gohman8181bd12008-07-27 21:46:04 +00001403 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001404}
1405
1406/// get_vec_i10imm - Test if this vector is a vector filled with the same value
1407/// and the value fits into a signed 10-bit constant, and if so, return the
1408/// constant
Dan Gohman8181bd12008-07-27 21:46:04 +00001409SDValue SPU::get_vec_i10imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00001410 MVT ValueType) {
Scott Michel8efdca42007-12-04 22:23:35 +00001411 if (ConstantSDNode *CN = getVecImm(N)) {
Scott Michel6baba072008-03-05 23:02:02 +00001412 int64_t Value = CN->getSignExtended();
Scott Michelbcc7b672008-03-06 04:02:54 +00001413 if (ValueType == MVT::i64) {
1414 uint64_t UValue = CN->getValue();
1415 uint32_t upper = uint32_t(UValue >> 32);
1416 uint32_t lower = uint32_t(UValue);
1417 if (upper != lower)
Dan Gohman8181bd12008-07-27 21:46:04 +00001418 return SDValue();
Scott Michelbcc7b672008-03-06 04:02:54 +00001419 Value = Value >> 32;
1420 }
Scott Michel6baba072008-03-05 23:02:02 +00001421 if (isS10Constant(Value))
Scott Michel8efdca42007-12-04 22:23:35 +00001422 return DAG.getConstant(Value, ValueType);
1423 }
1424
Dan Gohman8181bd12008-07-27 21:46:04 +00001425 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001426}
1427
1428/// get_vec_i8imm - Test if this vector is a vector filled with the same value
1429/// and the value fits into a signed 8-bit constant, and if so, return the
1430/// constant.
1431///
1432/// @note: The incoming vector is v16i8 because that's the only way we can load
1433/// constant vectors. Thus, we test to see if the upper and lower bytes are the
1434/// same value.
Dan Gohman8181bd12008-07-27 21:46:04 +00001435SDValue SPU::get_vec_i8imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00001436 MVT ValueType) {
Scott Michel8efdca42007-12-04 22:23:35 +00001437 if (ConstantSDNode *CN = getVecImm(N)) {
1438 int Value = (int) CN->getValue();
1439 if (ValueType == MVT::i16
Scott Michel5a6f17b2008-01-30 02:55:46 +00001440 && Value <= 0xffff /* truncated from uint64_t */
1441 && ((short) Value >> 8) == ((short) Value & 0xff))
Scott Michel8efdca42007-12-04 22:23:35 +00001442 return DAG.getConstant(Value & 0xff, ValueType);
1443 else if (ValueType == MVT::i8
Scott Michel5a6f17b2008-01-30 02:55:46 +00001444 && (Value & 0xff) == Value)
Scott Michel8efdca42007-12-04 22:23:35 +00001445 return DAG.getConstant(Value, ValueType);
1446 }
1447
Dan Gohman8181bd12008-07-27 21:46:04 +00001448 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001449}
1450
1451/// get_ILHUvec_imm - Test if this vector is a vector filled with the same value
1452/// and the value fits into a signed 16-bit constant, and if so, return the
1453/// constant
Dan Gohman8181bd12008-07-27 21:46:04 +00001454SDValue SPU::get_ILHUvec_imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00001455 MVT ValueType) {
Scott Michel8efdca42007-12-04 22:23:35 +00001456 if (ConstantSDNode *CN = getVecImm(N)) {
1457 uint64_t Value = CN->getValue();
1458 if ((ValueType == MVT::i32
Scott Michel5a6f17b2008-01-30 02:55:46 +00001459 && ((unsigned) Value & 0xffff0000) == (unsigned) Value)
1460 || (ValueType == MVT::i64 && (Value & 0xffff0000) == Value))
Scott Michel8efdca42007-12-04 22:23:35 +00001461 return DAG.getConstant(Value >> 16, ValueType);
1462 }
1463
Dan Gohman8181bd12008-07-27 21:46:04 +00001464 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001465}
1466
1467/// get_v4i32_imm - Catch-all for general 32-bit constant vectors
Dan Gohman8181bd12008-07-27 21:46:04 +00001468SDValue SPU::get_v4i32_imm(SDNode *N, SelectionDAG &DAG) {
Scott Michel8efdca42007-12-04 22:23:35 +00001469 if (ConstantSDNode *CN = getVecImm(N)) {
1470 return DAG.getConstant((unsigned) CN->getValue(), MVT::i32);
1471 }
1472
Dan Gohman8181bd12008-07-27 21:46:04 +00001473 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001474}
1475
1476/// get_v4i32_imm - Catch-all for general 64-bit constant vectors
Dan Gohman8181bd12008-07-27 21:46:04 +00001477SDValue SPU::get_v2i64_imm(SDNode *N, SelectionDAG &DAG) {
Scott Michel8efdca42007-12-04 22:23:35 +00001478 if (ConstantSDNode *CN = getVecImm(N)) {
1479 return DAG.getConstant((unsigned) CN->getValue(), MVT::i64);
1480 }
1481
Dan Gohman8181bd12008-07-27 21:46:04 +00001482 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001483}
1484
1485// If this is a vector of constants or undefs, get the bits. A bit in
Scott Michel4ec722e2008-07-16 17:17:29 +00001486// UndefBits is set if the corresponding element of the vector is an
Scott Michel8efdca42007-12-04 22:23:35 +00001487// ISD::UNDEF value. For undefs, the corresponding VectorBits values are
1488// zero. Return true if this is not an array of constants, false if it is.
1489//
1490static bool GetConstantBuildVectorBits(SDNode *BV, uint64_t VectorBits[2],
1491 uint64_t UndefBits[2]) {
1492 // Start with zero'd results.
1493 VectorBits[0] = VectorBits[1] = UndefBits[0] = UndefBits[1] = 0;
Scott Michel4ec722e2008-07-16 17:17:29 +00001494
Duncan Sands92c43912008-06-06 12:08:01 +00001495 unsigned EltBitSize = BV->getOperand(0).getValueType().getSizeInBits();
Scott Michel8efdca42007-12-04 22:23:35 +00001496 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001497 SDValue OpVal = BV->getOperand(i);
Scott Michel4ec722e2008-07-16 17:17:29 +00001498
Scott Michel8efdca42007-12-04 22:23:35 +00001499 unsigned PartNo = i >= e/2; // In the upper 128 bits?
1500 unsigned SlotNo = e/2 - (i & (e/2-1))-1; // Which subpiece of the uint64_t.
1501
1502 uint64_t EltBits = 0;
1503 if (OpVal.getOpcode() == ISD::UNDEF) {
1504 uint64_t EltUndefBits = ~0ULL >> (64-EltBitSize);
1505 UndefBits[PartNo] |= EltUndefBits << (SlotNo*EltBitSize);
1506 continue;
1507 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
1508 EltBits = CN->getValue() & (~0ULL >> (64-EltBitSize));
1509 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
1510 const APFloat &apf = CN->getValueAPF();
1511 EltBits = (CN->getValueType(0) == MVT::f32
Scott Michel5a6f17b2008-01-30 02:55:46 +00001512 ? FloatToBits(apf.convertToFloat())
1513 : DoubleToBits(apf.convertToDouble()));
Scott Michel8efdca42007-12-04 22:23:35 +00001514 } else {
1515 // Nonconstant element.
1516 return true;
1517 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001518
Scott Michel8efdca42007-12-04 22:23:35 +00001519 VectorBits[PartNo] |= EltBits << (SlotNo*EltBitSize);
1520 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001521
1522 //printf("%llx %llx %llx %llx\n",
Scott Michel8efdca42007-12-04 22:23:35 +00001523 // VectorBits[0], VectorBits[1], UndefBits[0], UndefBits[1]);
1524 return false;
1525}
1526
1527/// If this is a splat (repetition) of a value across the whole vector, return
1528/// the smallest size that splats it. For example, "0x01010101010101..." is a
Scott Michel4ec722e2008-07-16 17:17:29 +00001529/// splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
Scott Michel8efdca42007-12-04 22:23:35 +00001530/// SplatSize = 1 byte.
Scott Michel4ec722e2008-07-16 17:17:29 +00001531static bool isConstantSplat(const uint64_t Bits128[2],
Scott Michel8efdca42007-12-04 22:23:35 +00001532 const uint64_t Undef128[2],
Scott Michel5a6f17b2008-01-30 02:55:46 +00001533 int MinSplatBits,
Scott Michel8efdca42007-12-04 22:23:35 +00001534 uint64_t &SplatBits, uint64_t &SplatUndef,
1535 int &SplatSize) {
1536 // Don't let undefs prevent splats from matching. See if the top 64-bits are
1537 // the same as the lower 64-bits, ignoring undefs.
1538 uint64_t Bits64 = Bits128[0] | Bits128[1];
1539 uint64_t Undef64 = Undef128[0] & Undef128[1];
1540 uint32_t Bits32 = uint32_t(Bits64) | uint32_t(Bits64 >> 32);
1541 uint32_t Undef32 = uint32_t(Undef64) & uint32_t(Undef64 >> 32);
1542 uint16_t Bits16 = uint16_t(Bits32) | uint16_t(Bits32 >> 16);
1543 uint16_t Undef16 = uint16_t(Undef32) & uint16_t(Undef32 >> 16);
1544
1545 if ((Bits128[0] & ~Undef128[1]) == (Bits128[1] & ~Undef128[0])) {
1546 if (MinSplatBits < 64) {
Scott Michel4ec722e2008-07-16 17:17:29 +00001547
Scott Michel8efdca42007-12-04 22:23:35 +00001548 // Check that the top 32-bits are the same as the lower 32-bits, ignoring
1549 // undefs.
1550 if ((Bits64 & (~Undef64 >> 32)) == ((Bits64 >> 32) & ~Undef64)) {
Scott Michel5a6f17b2008-01-30 02:55:46 +00001551 if (MinSplatBits < 32) {
Scott Michel8efdca42007-12-04 22:23:35 +00001552
Scott Michel5a6f17b2008-01-30 02:55:46 +00001553 // If the top 16-bits are different than the lower 16-bits, ignoring
1554 // undefs, we have an i32 splat.
1555 if ((Bits32 & (~Undef32 >> 16)) == ((Bits32 >> 16) & ~Undef32)) {
1556 if (MinSplatBits < 16) {
1557 // If the top 8-bits are different than the lower 8-bits, ignoring
1558 // undefs, we have an i16 splat.
1559 if ((Bits16 & (uint16_t(~Undef16) >> 8)) == ((Bits16 >> 8) & ~Undef16)) {
1560 // Otherwise, we have an 8-bit splat.
1561 SplatBits = uint8_t(Bits16) | uint8_t(Bits16 >> 8);
1562 SplatUndef = uint8_t(Undef16) & uint8_t(Undef16 >> 8);
1563 SplatSize = 1;
1564 return true;
1565 }
1566 } else {
1567 SplatBits = Bits16;
1568 SplatUndef = Undef16;
1569 SplatSize = 2;
1570 return true;
1571 }
1572 }
1573 } else {
1574 SplatBits = Bits32;
1575 SplatUndef = Undef32;
1576 SplatSize = 4;
1577 return true;
1578 }
Scott Michel8efdca42007-12-04 22:23:35 +00001579 }
1580 } else {
1581 SplatBits = Bits128[0];
1582 SplatUndef = Undef128[0];
1583 SplatSize = 8;
1584 return true;
1585 }
1586 }
1587
1588 return false; // Can't be a splat if two pieces don't match.
1589}
1590
1591// If this is a case we can't handle, return null and let the default
1592// expansion code take care of it. If we CAN select this case, and if it
1593// selects to a single instruction, return Op. Otherwise, if we can codegen
1594// this case more efficiently than a constant pool load, lower it to the
1595// sequence of ops that should be used.
Dan Gohman8181bd12008-07-27 21:46:04 +00001596static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00001597 MVT VT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +00001598 // If this is a vector of constants or undefs, get the bits. A bit in
Scott Michel4ec722e2008-07-16 17:17:29 +00001599 // UndefBits is set if the corresponding element of the vector is an
Scott Michel8efdca42007-12-04 22:23:35 +00001600 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
Scott Michel4ec722e2008-07-16 17:17:29 +00001601 // zero.
Scott Michel8efdca42007-12-04 22:23:35 +00001602 uint64_t VectorBits[2];
1603 uint64_t UndefBits[2];
1604 uint64_t SplatBits, SplatUndef;
1605 int SplatSize;
Gabor Greif1c80d112008-08-28 21:40:38 +00001606 if (GetConstantBuildVectorBits(Op.getNode(), VectorBits, UndefBits)
Scott Michel8efdca42007-12-04 22:23:35 +00001607 || !isConstantSplat(VectorBits, UndefBits,
Duncan Sands92c43912008-06-06 12:08:01 +00001608 VT.getVectorElementType().getSizeInBits(),
Scott Michel8efdca42007-12-04 22:23:35 +00001609 SplatBits, SplatUndef, SplatSize))
Dan Gohman8181bd12008-07-27 21:46:04 +00001610 return SDValue(); // Not a constant vector, not a splat.
Scott Michel4ec722e2008-07-16 17:17:29 +00001611
Duncan Sands92c43912008-06-06 12:08:01 +00001612 switch (VT.getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001613 default:
1614 case MVT::v4f32: {
1615 uint32_t Value32 = SplatBits;
1616 assert(SplatSize == 4
Scott Michel5a6f17b2008-01-30 02:55:46 +00001617 && "LowerBUILD_VECTOR: Unexpected floating point vector element.");
Scott Michel8efdca42007-12-04 22:23:35 +00001618 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
Dan Gohman8181bd12008-07-27 21:46:04 +00001619 SDValue T = DAG.getConstant(Value32, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00001620 return DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001621 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, T, T, T, T));
Scott Michel8efdca42007-12-04 22:23:35 +00001622 break;
1623 }
1624 case MVT::v2f64: {
1625 uint64_t f64val = SplatBits;
1626 assert(SplatSize == 8
Scott Michel5a6f17b2008-01-30 02:55:46 +00001627 && "LowerBUILD_VECTOR: 64-bit float vector element: unexpected size.");
Scott Michel8efdca42007-12-04 22:23:35 +00001628 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
Dan Gohman8181bd12008-07-27 21:46:04 +00001629 SDValue T = DAG.getConstant(f64val, MVT::i64);
Scott Michel8efdca42007-12-04 22:23:35 +00001630 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001631 DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i64, T, T));
Scott Michel8efdca42007-12-04 22:23:35 +00001632 break;
1633 }
1634 case MVT::v16i8: {
1635 // 8-bit constants have to be expanded to 16-bits
1636 unsigned short Value16 = SplatBits | (SplatBits << 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00001637 SDValue Ops[8];
Scott Michel8efdca42007-12-04 22:23:35 +00001638 for (int i = 0; i < 8; ++i)
1639 Ops[i] = DAG.getConstant(Value16, MVT::i16);
1640 return DAG.getNode(ISD::BIT_CONVERT, VT,
1641 DAG.getNode(ISD::BUILD_VECTOR, MVT::v8i16, Ops, 8));
1642 }
1643 case MVT::v8i16: {
1644 unsigned short Value16;
Scott Michel4ec722e2008-07-16 17:17:29 +00001645 if (SplatSize == 2)
Scott Michel8efdca42007-12-04 22:23:35 +00001646 Value16 = (unsigned short) (SplatBits & 0xffff);
1647 else
1648 Value16 = (unsigned short) (SplatBits | (SplatBits << 8));
Dan Gohman8181bd12008-07-27 21:46:04 +00001649 SDValue T = DAG.getConstant(Value16, VT.getVectorElementType());
1650 SDValue Ops[8];
Scott Michel8efdca42007-12-04 22:23:35 +00001651 for (int i = 0; i < 8; ++i) Ops[i] = T;
1652 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops, 8);
1653 }
1654 case MVT::v4i32: {
1655 unsigned int Value = SplatBits;
Dan Gohman8181bd12008-07-27 21:46:04 +00001656 SDValue T = DAG.getConstant(Value, VT.getVectorElementType());
Scott Michel8efdca42007-12-04 22:23:35 +00001657 return DAG.getNode(ISD::BUILD_VECTOR, VT, T, T, T, T);
1658 }
1659 case MVT::v2i64: {
1660 uint64_t val = SplatBits;
1661 uint32_t upper = uint32_t(val >> 32);
1662 uint32_t lower = uint32_t(val);
1663
Scott Michelbcc7b672008-03-06 04:02:54 +00001664 if (upper == lower) {
1665 // Magic constant that can be matched by IL, ILA, et. al.
Dan Gohman8181bd12008-07-27 21:46:04 +00001666 SDValue Val = DAG.getTargetConstant(val, MVT::i64);
Scott Michelbcc7b672008-03-06 04:02:54 +00001667 return DAG.getNode(ISD::BUILD_VECTOR, VT, Val, Val);
Scott Michel6baba072008-03-05 23:02:02 +00001668 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001669 SDValue LO32;
1670 SDValue HI32;
1671 SmallVector<SDValue, 16> ShufBytes;
1672 SDValue Result;
Scott Michel8efdca42007-12-04 22:23:35 +00001673 bool upper_special, lower_special;
1674
1675 // NOTE: This code creates common-case shuffle masks that can be easily
1676 // detected as common expressions. It is not attempting to create highly
1677 // specialized masks to replace any and all 0's, 0xff's and 0x80's.
1678
1679 // Detect if the upper or lower half is a special shuffle mask pattern:
1680 upper_special = (upper == 0 || upper == 0xffffffff || upper == 0x80000000);
1681 lower_special = (lower == 0 || lower == 0xffffffff || lower == 0x80000000);
1682
1683 // Create lower vector if not a special pattern
1684 if (!lower_special) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001685 SDValue LO32C = DAG.getConstant(lower, MVT::i32);
Scott Michel5a6f17b2008-01-30 02:55:46 +00001686 LO32 = DAG.getNode(ISD::BIT_CONVERT, VT,
1687 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1688 LO32C, LO32C, LO32C, LO32C));
Scott Michel8efdca42007-12-04 22:23:35 +00001689 }
1690
1691 // Create upper vector if not a special pattern
1692 if (!upper_special) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001693 SDValue HI32C = DAG.getConstant(upper, MVT::i32);
Scott Michel5a6f17b2008-01-30 02:55:46 +00001694 HI32 = DAG.getNode(ISD::BIT_CONVERT, VT,
1695 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1696 HI32C, HI32C, HI32C, HI32C));
Scott Michel8efdca42007-12-04 22:23:35 +00001697 }
1698
1699 // If either upper or lower are special, then the two input operands are
1700 // the same (basically, one of them is a "don't care")
1701 if (lower_special)
Scott Michel5a6f17b2008-01-30 02:55:46 +00001702 LO32 = HI32;
Scott Michel8efdca42007-12-04 22:23:35 +00001703 if (upper_special)
Scott Michel5a6f17b2008-01-30 02:55:46 +00001704 HI32 = LO32;
Scott Michel8efdca42007-12-04 22:23:35 +00001705 if (lower_special && upper_special) {
Scott Michel5a6f17b2008-01-30 02:55:46 +00001706 // Unhappy situation... both upper and lower are special, so punt with
1707 // a target constant:
Dan Gohman8181bd12008-07-27 21:46:04 +00001708 SDValue Zero = DAG.getConstant(0, MVT::i32);
Scott Michel5a6f17b2008-01-30 02:55:46 +00001709 HI32 = LO32 = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Zero, Zero,
Scott Michel8efdca42007-12-04 22:23:35 +00001710 Zero, Zero);
1711 }
1712
1713 for (int i = 0; i < 4; ++i) {
Scott Michel67224b22008-06-02 22:18:03 +00001714 uint64_t val = 0;
Scott Michel5a6f17b2008-01-30 02:55:46 +00001715 for (int j = 0; j < 4; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001716 SDValue V;
Scott Michel5a6f17b2008-01-30 02:55:46 +00001717 bool process_upper, process_lower;
Scott Michel67224b22008-06-02 22:18:03 +00001718 val <<= 8;
Scott Michel5a6f17b2008-01-30 02:55:46 +00001719 process_upper = (upper_special && (i & 1) == 0);
1720 process_lower = (lower_special && (i & 1) == 1);
Scott Michel8efdca42007-12-04 22:23:35 +00001721
Scott Michel5a6f17b2008-01-30 02:55:46 +00001722 if (process_upper || process_lower) {
1723 if ((process_upper && upper == 0)
1724 || (process_lower && lower == 0))
Scott Michel67224b22008-06-02 22:18:03 +00001725 val |= 0x80;
Scott Michel5a6f17b2008-01-30 02:55:46 +00001726 else if ((process_upper && upper == 0xffffffff)
1727 || (process_lower && lower == 0xffffffff))
Scott Michel67224b22008-06-02 22:18:03 +00001728 val |= 0xc0;
Scott Michel5a6f17b2008-01-30 02:55:46 +00001729 else if ((process_upper && upper == 0x80000000)
1730 || (process_lower && lower == 0x80000000))
Scott Michel67224b22008-06-02 22:18:03 +00001731 val |= (j == 0 ? 0xe0 : 0x80);
Scott Michel5a6f17b2008-01-30 02:55:46 +00001732 } else
Scott Michel67224b22008-06-02 22:18:03 +00001733 val |= i * 4 + j + ((i & 1) * 16);
Scott Michel5a6f17b2008-01-30 02:55:46 +00001734 }
Scott Michel67224b22008-06-02 22:18:03 +00001735
1736 ShufBytes.push_back(DAG.getConstant(val, MVT::i32));
Scott Michel8efdca42007-12-04 22:23:35 +00001737 }
1738
1739 return DAG.getNode(SPUISD::SHUFB, VT, HI32, LO32,
Scott Michel67224b22008-06-02 22:18:03 +00001740 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001741 &ShufBytes[0], ShufBytes.size()));
Scott Michel8efdca42007-12-04 22:23:35 +00001742 }
1743 }
1744 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001745
Dan Gohman8181bd12008-07-27 21:46:04 +00001746 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001747}
1748
1749/// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3) to something on
1750/// which the Cell can operate. The code inspects V3 to ascertain whether the
1751/// permutation vector, V3, is monotonically increasing with one "exception"
1752/// element, e.g., (0, 1, _, 3). If this is the case, then generate a
1753/// INSERT_MASK synthetic instruction. Otherwise, spill V3 to the constant pool.
1754/// In either case, the net result is going to eventually invoke SHUFB to
1755/// permute/shuffle the bytes from V1 and V2.
1756/// \note
1757/// INSERT_MASK is eventually selected as one of the C*D instructions, generate
1758/// control word for byte/halfword/word insertion. This takes care of a single
1759/// element move from V2 into V1.
1760/// \note
1761/// SPUISD::SHUFB is eventually selected as Cell's <i>shufb</i> instructions.
Dan Gohman8181bd12008-07-27 21:46:04 +00001762static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
1763 SDValue V1 = Op.getOperand(0);
1764 SDValue V2 = Op.getOperand(1);
1765 SDValue PermMask = Op.getOperand(2);
Scott Michel4ec722e2008-07-16 17:17:29 +00001766
Scott Michel8efdca42007-12-04 22:23:35 +00001767 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michel4ec722e2008-07-16 17:17:29 +00001768
Scott Michel8efdca42007-12-04 22:23:35 +00001769 // If we have a single element being moved from V1 to V2, this can be handled
1770 // using the C*[DX] compute mask instructions, but the vector elements have
1771 // to be monotonically increasing with one exception element.
Duncan Sands92c43912008-06-06 12:08:01 +00001772 MVT EltVT = V1.getValueType().getVectorElementType();
Scott Michel8efdca42007-12-04 22:23:35 +00001773 unsigned EltsFromV2 = 0;
1774 unsigned V2Elt = 0;
1775 unsigned V2EltIdx0 = 0;
1776 unsigned CurrElt = 0;
1777 bool monotonic = true;
1778 if (EltVT == MVT::i8)
1779 V2EltIdx0 = 16;
1780 else if (EltVT == MVT::i16)
1781 V2EltIdx0 = 8;
1782 else if (EltVT == MVT::i32)
1783 V2EltIdx0 = 4;
1784 else
1785 assert(0 && "Unhandled vector type in LowerVECTOR_SHUFFLE");
1786
1787 for (unsigned i = 0, e = PermMask.getNumOperands();
1788 EltsFromV2 <= 1 && monotonic && i != e;
1789 ++i) {
1790 unsigned SrcElt;
1791 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
1792 SrcElt = 0;
Scott Michel4ec722e2008-07-16 17:17:29 +00001793 else
Scott Michel8efdca42007-12-04 22:23:35 +00001794 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
1795
1796 if (SrcElt >= V2EltIdx0) {
1797 ++EltsFromV2;
1798 V2Elt = (V2EltIdx0 - SrcElt) << 2;
1799 } else if (CurrElt != SrcElt) {
1800 monotonic = false;
1801 }
1802
1803 ++CurrElt;
1804 }
1805
1806 if (EltsFromV2 == 1 && monotonic) {
1807 // Compute mask and shuffle
1808 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +00001809 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1810 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
Duncan Sands92c43912008-06-06 12:08:01 +00001811 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel8efdca42007-12-04 22:23:35 +00001812 // Initialize temporary register to 0
Dan Gohman8181bd12008-07-27 21:46:04 +00001813 SDValue InitTempReg =
Scott Michel8efdca42007-12-04 22:23:35 +00001814 DAG.getCopyToReg(DAG.getEntryNode(), VReg, DAG.getConstant(0, PtrVT));
1815 // Copy register's contents as index in INSERT_MASK:
Dan Gohman8181bd12008-07-27 21:46:04 +00001816 SDValue ShufMaskOp =
Scott Michel8efdca42007-12-04 22:23:35 +00001817 DAG.getNode(SPUISD::INSERT_MASK, V1.getValueType(),
Scott Michel5a6f17b2008-01-30 02:55:46 +00001818 DAG.getTargetConstant(V2Elt, MVT::i32),
1819 DAG.getCopyFromReg(InitTempReg, VReg, PtrVT));
Scott Michel8efdca42007-12-04 22:23:35 +00001820 // Use shuffle mask in SHUFB synthetic instruction:
1821 return DAG.getNode(SPUISD::SHUFB, V1.getValueType(), V2, V1, ShufMaskOp);
1822 } else {
1823 // Convert the SHUFFLE_VECTOR mask's input element units to the actual bytes.
Duncan Sands92c43912008-06-06 12:08:01 +00001824 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michel4ec722e2008-07-16 17:17:29 +00001825
Dan Gohman8181bd12008-07-27 21:46:04 +00001826 SmallVector<SDValue, 16> ResultMask;
Scott Michel8efdca42007-12-04 22:23:35 +00001827 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
1828 unsigned SrcElt;
1829 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
Scott Michel5a6f17b2008-01-30 02:55:46 +00001830 SrcElt = 0;
Scott Michel4ec722e2008-07-16 17:17:29 +00001831 else
Scott Michel5a6f17b2008-01-30 02:55:46 +00001832 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
Scott Michel4ec722e2008-07-16 17:17:29 +00001833
Scott Michel97872d32008-02-23 18:41:37 +00001834 for (unsigned j = 0; j < BytesPerElement; ++j) {
Scott Michel5a6f17b2008-01-30 02:55:46 +00001835 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
1836 MVT::i8));
Scott Michel8efdca42007-12-04 22:23:35 +00001837 }
1838 }
Scott Michel4ec722e2008-07-16 17:17:29 +00001839
Dan Gohman8181bd12008-07-27 21:46:04 +00001840 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001841 &ResultMask[0], ResultMask.size());
Scott Michel8efdca42007-12-04 22:23:35 +00001842 return DAG.getNode(SPUISD::SHUFB, V1.getValueType(), V1, V2, VPermMask);
1843 }
1844}
1845
Dan Gohman8181bd12008-07-27 21:46:04 +00001846static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
1847 SDValue Op0 = Op.getOperand(0); // Op0 = the scalar
Scott Michel8efdca42007-12-04 22:23:35 +00001848
Gabor Greif1c80d112008-08-28 21:40:38 +00001849 if (Op0.getNode()->getOpcode() == ISD::Constant) {
Scott Michel8efdca42007-12-04 22:23:35 +00001850 // For a constant, build the appropriate constant vector, which will
1851 // eventually simplify to a vector register load.
1852
Gabor Greif1c80d112008-08-28 21:40:38 +00001853 ConstantSDNode *CN = cast<ConstantSDNode>(Op0.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00001854 SmallVector<SDValue, 16> ConstVecValues;
Duncan Sands92c43912008-06-06 12:08:01 +00001855 MVT VT;
Scott Michel8efdca42007-12-04 22:23:35 +00001856 size_t n_copies;
1857
1858 // Create a constant vector:
Duncan Sands92c43912008-06-06 12:08:01 +00001859 switch (Op.getValueType().getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001860 default: assert(0 && "Unexpected constant value type in "
Scott Michel5a6f17b2008-01-30 02:55:46 +00001861 "LowerSCALAR_TO_VECTOR");
Scott Michel8efdca42007-12-04 22:23:35 +00001862 case MVT::v16i8: n_copies = 16; VT = MVT::i8; break;
1863 case MVT::v8i16: n_copies = 8; VT = MVT::i16; break;
1864 case MVT::v4i32: n_copies = 4; VT = MVT::i32; break;
1865 case MVT::v4f32: n_copies = 4; VT = MVT::f32; break;
1866 case MVT::v2i64: n_copies = 2; VT = MVT::i64; break;
1867 case MVT::v2f64: n_copies = 2; VT = MVT::f64; break;
1868 }
1869
Dan Gohman8181bd12008-07-27 21:46:04 +00001870 SDValue CValue = DAG.getConstant(CN->getValue(), VT);
Scott Michel8efdca42007-12-04 22:23:35 +00001871 for (size_t j = 0; j < n_copies; ++j)
1872 ConstVecValues.push_back(CValue);
1873
1874 return DAG.getNode(ISD::BUILD_VECTOR, Op.getValueType(),
Scott Michel5a6f17b2008-01-30 02:55:46 +00001875 &ConstVecValues[0], ConstVecValues.size());
Scott Michel8efdca42007-12-04 22:23:35 +00001876 } else {
1877 // Otherwise, copy the value from one register to another:
Duncan Sands92c43912008-06-06 12:08:01 +00001878 switch (Op0.getValueType().getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00001879 default: assert(0 && "Unexpected value type in LowerSCALAR_TO_VECTOR");
1880 case MVT::i8:
1881 case MVT::i16:
1882 case MVT::i32:
1883 case MVT::i64:
1884 case MVT::f32:
1885 case MVT::f64:
1886 return DAG.getNode(SPUISD::PROMOTE_SCALAR, Op.getValueType(), Op0, Op0);
1887 }
1888 }
1889
Dan Gohman8181bd12008-07-27 21:46:04 +00001890 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00001891}
1892
Dan Gohman8181bd12008-07-27 21:46:04 +00001893static SDValue LowerVectorMUL(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00001894 switch (Op.getValueType().getSimpleVT()) {
1895 default:
1896 cerr << "CellSPU: Unknown vector multiplication, got "
1897 << Op.getValueType().getMVTString()
1898 << "\n";
1899 abort();
1900 /*NOTREACHED*/
1901
Scott Michel8efdca42007-12-04 22:23:35 +00001902 case MVT::v4i32: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001903 SDValue rA = Op.getOperand(0);
1904 SDValue rB = Op.getOperand(1);
1905 SDValue HiProd1 = DAG.getNode(SPUISD::MPYH, MVT::v4i32, rA, rB);
1906 SDValue HiProd2 = DAG.getNode(SPUISD::MPYH, MVT::v4i32, rB, rA);
1907 SDValue LoProd = DAG.getNode(SPUISD::MPYU, MVT::v4i32, rA, rB);
1908 SDValue Residual1 = DAG.getNode(ISD::ADD, MVT::v4i32, LoProd, HiProd1);
Scott Michel8efdca42007-12-04 22:23:35 +00001909
1910 return DAG.getNode(ISD::ADD, MVT::v4i32, Residual1, HiProd2);
1911 break;
1912 }
1913
1914 // Multiply two v8i16 vectors (pipeline friendly version):
1915 // a) multiply lower halves, mask off upper 16-bit of 32-bit product
1916 // b) multiply upper halves, rotate left by 16 bits (inserts 16 lower zeroes)
1917 // c) Use SELB to select upper and lower halves from the intermediate results
1918 //
Scott Michel67224b22008-06-02 22:18:03 +00001919 // NOTE: We really want to move the SELECT_MASK to earlier to actually get the
Scott Michel8efdca42007-12-04 22:23:35 +00001920 // dual-issue. This code does manage to do this, even if it's a little on
1921 // the wacky side
1922 case MVT::v8i16: {
1923 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +00001924 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001925 SDValue Chain = Op.getOperand(0);
1926 SDValue rA = Op.getOperand(0);
1927 SDValue rB = Op.getOperand(1);
Chris Lattner1b989192007-12-31 04:13:23 +00001928 unsigned FSMBIreg = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
1929 unsigned HiProdReg = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
Scott Michel8efdca42007-12-04 22:23:35 +00001930
Dan Gohman8181bd12008-07-27 21:46:04 +00001931 SDValue FSMBOp =
Scott Michel8efdca42007-12-04 22:23:35 +00001932 DAG.getCopyToReg(Chain, FSMBIreg,
Scott Michel67224b22008-06-02 22:18:03 +00001933 DAG.getNode(SPUISD::SELECT_MASK, MVT::v8i16,
Scott Michelbc5fbc12008-04-30 00:30:08 +00001934 DAG.getConstant(0xcccc, MVT::i16)));
Scott Michel8efdca42007-12-04 22:23:35 +00001935
Dan Gohman8181bd12008-07-27 21:46:04 +00001936 SDValue HHProd =
Scott Michel8efdca42007-12-04 22:23:35 +00001937 DAG.getCopyToReg(FSMBOp, HiProdReg,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001938 DAG.getNode(SPUISD::MPYHH, MVT::v8i16, rA, rB));
Scott Michel8efdca42007-12-04 22:23:35 +00001939
Dan Gohman8181bd12008-07-27 21:46:04 +00001940 SDValue HHProd_v4i32 =
Scott Michel8efdca42007-12-04 22:23:35 +00001941 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001942 DAG.getCopyFromReg(HHProd, HiProdReg, MVT::v4i32));
Scott Michel8efdca42007-12-04 22:23:35 +00001943
1944 return DAG.getNode(SPUISD::SELB, MVT::v8i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001945 DAG.getNode(SPUISD::MPY, MVT::v8i16, rA, rB),
1946 DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
1947 DAG.getNode(SPUISD::VEC_SHL, MVT::v4i32,
1948 HHProd_v4i32,
1949 DAG.getConstant(16, MVT::i16))),
1950 DAG.getCopyFromReg(FSMBOp, FSMBIreg, MVT::v4i32));
Scott Michel8efdca42007-12-04 22:23:35 +00001951 }
1952
1953 // This M00sE is N@stI! (apologies to Monty Python)
1954 //
1955 // SPU doesn't know how to do any 8-bit multiplication, so the solution
1956 // is to break it all apart, sign extend, and reassemble the various
1957 // intermediate products.
1958 case MVT::v16i8: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001959 SDValue rA = Op.getOperand(0);
1960 SDValue rB = Op.getOperand(1);
1961 SDValue c8 = DAG.getConstant(8, MVT::i32);
1962 SDValue c16 = DAG.getConstant(16, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00001963
Dan Gohman8181bd12008-07-27 21:46:04 +00001964 SDValue LLProd =
Scott Michel8efdca42007-12-04 22:23:35 +00001965 DAG.getNode(SPUISD::MPY, MVT::v8i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001966 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rA),
1967 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rB));
Scott Michel8efdca42007-12-04 22:23:35 +00001968
Dan Gohman8181bd12008-07-27 21:46:04 +00001969 SDValue rALH = DAG.getNode(SPUISD::VEC_SRA, MVT::v8i16, rA, c8);
Scott Michel8efdca42007-12-04 22:23:35 +00001970
Dan Gohman8181bd12008-07-27 21:46:04 +00001971 SDValue rBLH = DAG.getNode(SPUISD::VEC_SRA, MVT::v8i16, rB, c8);
Scott Michel8efdca42007-12-04 22:23:35 +00001972
Dan Gohman8181bd12008-07-27 21:46:04 +00001973 SDValue LHProd =
Scott Michel8efdca42007-12-04 22:23:35 +00001974 DAG.getNode(SPUISD::VEC_SHL, MVT::v8i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001975 DAG.getNode(SPUISD::MPY, MVT::v8i16, rALH, rBLH), c8);
Scott Michel8efdca42007-12-04 22:23:35 +00001976
Dan Gohman8181bd12008-07-27 21:46:04 +00001977 SDValue FSMBmask = DAG.getNode(SPUISD::SELECT_MASK, MVT::v8i16,
Scott Michelbc5fbc12008-04-30 00:30:08 +00001978 DAG.getConstant(0x2222, MVT::i16));
Scott Michel8efdca42007-12-04 22:23:35 +00001979
Dan Gohman8181bd12008-07-27 21:46:04 +00001980 SDValue LoProdParts =
Scott Michel97872d32008-02-23 18:41:37 +00001981 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
1982 DAG.getNode(SPUISD::SELB, MVT::v8i16,
1983 LLProd, LHProd, FSMBmask));
Scott Michel8efdca42007-12-04 22:23:35 +00001984
Dan Gohman8181bd12008-07-27 21:46:04 +00001985 SDValue LoProdMask = DAG.getConstant(0xffff, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00001986
Dan Gohman8181bd12008-07-27 21:46:04 +00001987 SDValue LoProd =
Scott Michel8efdca42007-12-04 22:23:35 +00001988 DAG.getNode(ISD::AND, MVT::v4i32,
Scott Michel97872d32008-02-23 18:41:37 +00001989 LoProdParts,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001990 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1991 LoProdMask, LoProdMask,
1992 LoProdMask, LoProdMask));
Scott Michel8efdca42007-12-04 22:23:35 +00001993
Dan Gohman8181bd12008-07-27 21:46:04 +00001994 SDValue rAH =
Scott Michel8efdca42007-12-04 22:23:35 +00001995 DAG.getNode(SPUISD::VEC_SRA, MVT::v4i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00001996 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, rA), c16);
Scott Michel8efdca42007-12-04 22:23:35 +00001997
Dan Gohman8181bd12008-07-27 21:46:04 +00001998 SDValue rBH =
Scott Michel8efdca42007-12-04 22:23:35 +00001999 DAG.getNode(SPUISD::VEC_SRA, MVT::v4i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002000 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, rB), c16);
Scott Michel8efdca42007-12-04 22:23:35 +00002001
Dan Gohman8181bd12008-07-27 21:46:04 +00002002 SDValue HLProd =
Scott Michel8efdca42007-12-04 22:23:35 +00002003 DAG.getNode(SPUISD::MPY, MVT::v8i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002004 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rAH),
2005 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rBH));
Scott Michel8efdca42007-12-04 22:23:35 +00002006
Dan Gohman8181bd12008-07-27 21:46:04 +00002007 SDValue HHProd_1 =
Scott Michel8efdca42007-12-04 22:23:35 +00002008 DAG.getNode(SPUISD::MPY, MVT::v8i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002009 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16,
2010 DAG.getNode(SPUISD::VEC_SRA, MVT::v4i32, rAH, c8)),
2011 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16,
2012 DAG.getNode(SPUISD::VEC_SRA, MVT::v4i32, rBH, c8)));
Scott Michel8efdca42007-12-04 22:23:35 +00002013
Dan Gohman8181bd12008-07-27 21:46:04 +00002014 SDValue HHProd =
Scott Michel97872d32008-02-23 18:41:37 +00002015 DAG.getNode(SPUISD::SELB, MVT::v8i16,
2016 HLProd,
2017 DAG.getNode(SPUISD::VEC_SHL, MVT::v8i16, HHProd_1, c8),
2018 FSMBmask);
Scott Michel8efdca42007-12-04 22:23:35 +00002019
Dan Gohman8181bd12008-07-27 21:46:04 +00002020 SDValue HiProd =
Scott Michel97872d32008-02-23 18:41:37 +00002021 DAG.getNode(SPUISD::VEC_SHL, MVT::v4i32, HHProd, c16);
Scott Michel8efdca42007-12-04 22:23:35 +00002022
2023 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002024 DAG.getNode(ISD::OR, MVT::v4i32,
2025 LoProd, HiProd));
Scott Michel8efdca42007-12-04 22:23:35 +00002026 }
Scott Michel8efdca42007-12-04 22:23:35 +00002027 }
2028
Dan Gohman8181bd12008-07-27 21:46:04 +00002029 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00002030}
2031
Dan Gohman8181bd12008-07-27 21:46:04 +00002032static SDValue LowerFDIVf32(SDValue Op, SelectionDAG &DAG) {
Scott Michel8efdca42007-12-04 22:23:35 +00002033 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +00002034 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel8efdca42007-12-04 22:23:35 +00002035
Dan Gohman8181bd12008-07-27 21:46:04 +00002036 SDValue A = Op.getOperand(0);
2037 SDValue B = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00002038 MVT VT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +00002039
2040 unsigned VRegBR, VRegC;
2041
2042 if (VT == MVT::f32) {
Chris Lattner1b989192007-12-31 04:13:23 +00002043 VRegBR = RegInfo.createVirtualRegister(&SPU::R32FPRegClass);
2044 VRegC = RegInfo.createVirtualRegister(&SPU::R32FPRegClass);
Scott Michel8efdca42007-12-04 22:23:35 +00002045 } else {
Chris Lattner1b989192007-12-31 04:13:23 +00002046 VRegBR = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
2047 VRegC = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
Scott Michel8efdca42007-12-04 22:23:35 +00002048 }
2049 // TODO: make sure we're feeding FPInterp the right arguments
2050 // Right now: fi B, frest(B)
2051
2052 // Computes BRcpl =
2053 // (Floating Interpolate (FP Reciprocal Estimate B))
Dan Gohman8181bd12008-07-27 21:46:04 +00002054 SDValue BRcpl =
Scott Michel4ec722e2008-07-16 17:17:29 +00002055 DAG.getCopyToReg(DAG.getEntryNode(), VRegBR,
2056 DAG.getNode(SPUISD::FPInterp, VT, B,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002057 DAG.getNode(SPUISD::FPRecipEst, VT, B)));
Scott Michel4ec722e2008-07-16 17:17:29 +00002058
Scott Michel8efdca42007-12-04 22:23:35 +00002059 // Computes A * BRcpl and stores in a temporary register
Dan Gohman8181bd12008-07-27 21:46:04 +00002060 SDValue AxBRcpl =
Scott Michel8efdca42007-12-04 22:23:35 +00002061 DAG.getCopyToReg(BRcpl, VRegC,
Scott Michel4ec722e2008-07-16 17:17:29 +00002062 DAG.getNode(ISD::FMUL, VT, A,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002063 DAG.getCopyFromReg(BRcpl, VRegBR, VT)));
Scott Michel8efdca42007-12-04 22:23:35 +00002064 // What's the Chain variable do? It's magic!
2065 // TODO: set Chain = Op(0).getEntryNode()
Scott Michel4ec722e2008-07-16 17:17:29 +00002066
2067 return DAG.getNode(ISD::FADD, VT,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002068 DAG.getCopyFromReg(AxBRcpl, VRegC, VT),
Scott Michel4ec722e2008-07-16 17:17:29 +00002069 DAG.getNode(ISD::FMUL, VT,
2070 DAG.getCopyFromReg(AxBRcpl, VRegBR, VT),
Scott Michel5a6f17b2008-01-30 02:55:46 +00002071 DAG.getNode(ISD::FSUB, VT, A,
Scott Michel4ec722e2008-07-16 17:17:29 +00002072 DAG.getNode(ISD::FMUL, VT, B,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002073 DAG.getCopyFromReg(AxBRcpl, VRegC, VT)))));
Scott Michel8efdca42007-12-04 22:23:35 +00002074}
2075
Dan Gohman8181bd12008-07-27 21:46:04 +00002076static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002077 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002078 SDValue N = Op.getOperand(0);
2079 SDValue Elt = Op.getOperand(1);
2080 SDValue ShufMask[16];
Scott Michel8efdca42007-12-04 22:23:35 +00002081 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt);
2082
2083 assert(C != 0 && "LowerEXTRACT_VECTOR_ELT expecting constant SDNode");
2084
2085 int EltNo = (int) C->getValue();
2086
2087 // sanity checks:
2088 if (VT == MVT::i8 && EltNo >= 16)
2089 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i8 extraction slot > 15");
2090 else if (VT == MVT::i16 && EltNo >= 8)
2091 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i16 extraction slot > 7");
2092 else if (VT == MVT::i32 && EltNo >= 4)
2093 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i32 extraction slot > 4");
2094 else if (VT == MVT::i64 && EltNo >= 2)
2095 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i64 extraction slot > 2");
2096
2097 if (EltNo == 0 && (VT == MVT::i32 || VT == MVT::i64)) {
2098 // i32 and i64: Element 0 is the preferred slot
2099 return DAG.getNode(SPUISD::EXTRACT_ELT0, VT, N);
2100 }
2101
2102 // Need to generate shuffle mask and extract:
Scott Michel4c07cbd2007-12-19 21:17:42 +00002103 int prefslot_begin = -1, prefslot_end = -1;
Duncan Sands92c43912008-06-06 12:08:01 +00002104 int elt_byte = EltNo * VT.getSizeInBits() / 8;
Scott Michel8efdca42007-12-04 22:23:35 +00002105
Duncan Sands92c43912008-06-06 12:08:01 +00002106 switch (VT.getSimpleVT()) {
2107 default:
2108 assert(false && "Invalid value type!");
Scott Michel8efdca42007-12-04 22:23:35 +00002109 case MVT::i8: {
2110 prefslot_begin = prefslot_end = 3;
2111 break;
2112 }
2113 case MVT::i16: {
2114 prefslot_begin = 2; prefslot_end = 3;
2115 break;
2116 }
2117 case MVT::i32: {
2118 prefslot_begin = 0; prefslot_end = 3;
2119 break;
2120 }
2121 case MVT::i64: {
2122 prefslot_begin = 0; prefslot_end = 7;
2123 break;
2124 }
2125 }
2126
Scott Michel4c07cbd2007-12-19 21:17:42 +00002127 assert(prefslot_begin != -1 && prefslot_end != -1 &&
Scott Michel5a6f17b2008-01-30 02:55:46 +00002128 "LowerEXTRACT_VECTOR_ELT: preferred slots uninitialized");
Scott Michel4c07cbd2007-12-19 21:17:42 +00002129
Scott Michel8efdca42007-12-04 22:23:35 +00002130 for (int i = 0; i < 16; ++i) {
2131 // zero fill uppper part of preferred slot, don't care about the
2132 // other slots:
2133 unsigned int mask_val;
2134
2135 if (i <= prefslot_end) {
2136 mask_val =
Scott Michel5a6f17b2008-01-30 02:55:46 +00002137 ((i < prefslot_begin)
2138 ? 0x80
2139 : elt_byte + (i - prefslot_begin));
Scott Michel8efdca42007-12-04 22:23:35 +00002140
Scott Michel4c07cbd2007-12-19 21:17:42 +00002141 ShufMask[i] = DAG.getConstant(mask_val, MVT::i8);
Scott Michel4ec722e2008-07-16 17:17:29 +00002142 } else
Scott Michel8efdca42007-12-04 22:23:35 +00002143 ShufMask[i] = ShufMask[i % (prefslot_end + 1)];
2144 }
2145
Dan Gohman8181bd12008-07-27 21:46:04 +00002146 SDValue ShufMaskVec =
Scott Michel8efdca42007-12-04 22:23:35 +00002147 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002148 &ShufMask[0],
2149 sizeof(ShufMask) / sizeof(ShufMask[0]));
Scott Michel8efdca42007-12-04 22:23:35 +00002150
2151 return DAG.getNode(SPUISD::EXTRACT_ELT0, VT,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002152 DAG.getNode(SPUISD::SHUFB, N.getValueType(),
2153 N, N, ShufMaskVec));
Scott Michel4ec722e2008-07-16 17:17:29 +00002154
Scott Michel8efdca42007-12-04 22:23:35 +00002155}
2156
Dan Gohman8181bd12008-07-27 21:46:04 +00002157static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
2158 SDValue VecOp = Op.getOperand(0);
2159 SDValue ValOp = Op.getOperand(1);
2160 SDValue IdxOp = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00002161 MVT VT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +00002162
2163 ConstantSDNode *CN = cast<ConstantSDNode>(IdxOp);
2164 assert(CN != 0 && "LowerINSERT_VECTOR_ELT: Index is not constant!");
2165
Duncan Sands92c43912008-06-06 12:08:01 +00002166 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel8efdca42007-12-04 22:23:35 +00002167 // Use $2 because it's always 16-byte aligned and it's available:
Dan Gohman8181bd12008-07-27 21:46:04 +00002168 SDValue PtrBase = DAG.getRegister(SPU::R2, PtrVT);
Scott Michel8efdca42007-12-04 22:23:35 +00002169
Dan Gohman8181bd12008-07-27 21:46:04 +00002170 SDValue result =
Scott Michel8efdca42007-12-04 22:23:35 +00002171 DAG.getNode(SPUISD::SHUFB, VT,
2172 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, ValOp),
2173 VecOp,
2174 DAG.getNode(SPUISD::INSERT_MASK, VT,
2175 DAG.getNode(ISD::ADD, PtrVT,
2176 PtrBase,
2177 DAG.getConstant(CN->getValue(),
Scott Michel5a6f17b2008-01-30 02:55:46 +00002178 PtrVT))));
Scott Michel8efdca42007-12-04 22:23:35 +00002179
2180 return result;
2181}
2182
Dan Gohman8181bd12008-07-27 21:46:04 +00002183static SDValue LowerI8Math(SDValue Op, SelectionDAG &DAG, unsigned Opc)
Scott Michel97872d32008-02-23 18:41:37 +00002184{
Dan Gohman8181bd12008-07-27 21:46:04 +00002185 SDValue N0 = Op.getOperand(0); // Everything has at least one operand
Scott Michel8efdca42007-12-04 22:23:35 +00002186
2187 assert(Op.getValueType() == MVT::i8);
2188 switch (Opc) {
2189 default:
2190 assert(0 && "Unhandled i8 math operator");
2191 /*NOTREACHED*/
2192 break;
2193 case ISD::SUB: {
2194 // 8-bit subtraction: Promote the arguments up to 16-bits and truncate
2195 // the result:
Dan Gohman8181bd12008-07-27 21:46:04 +00002196 SDValue N1 = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002197 N0 = (N0.getOpcode() != ISD::Constant
2198 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N0)
2199 : DAG.getConstant(cast<ConstantSDNode>(N0)->getValue(), MVT::i16));
2200 N1 = (N1.getOpcode() != ISD::Constant
2201 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N1)
2202 : DAG.getConstant(cast<ConstantSDNode>(N1)->getValue(), MVT::i16));
Scott Michel4ec722e2008-07-16 17:17:29 +00002203 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
Scott Michel8efdca42007-12-04 22:23:35 +00002204 DAG.getNode(Opc, MVT::i16, N0, N1));
Scott Michel4ec722e2008-07-16 17:17:29 +00002205 }
Scott Michel8efdca42007-12-04 22:23:35 +00002206 case ISD::ROTR:
2207 case ISD::ROTL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002208 SDValue N1 = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002209 unsigned N1Opc;
2210 N0 = (N0.getOpcode() != ISD::Constant
2211 ? DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, N0)
2212 : DAG.getConstant(cast<ConstantSDNode>(N0)->getValue(), MVT::i16));
Duncan Sandsec142ee2008-06-08 20:54:56 +00002213 N1Opc = N1.getValueType().bitsLT(MVT::i16) ? ISD::ZERO_EXTEND : ISD::TRUNCATE;
Scott Michel8efdca42007-12-04 22:23:35 +00002214 N1 = (N1.getOpcode() != ISD::Constant
2215 ? DAG.getNode(N1Opc, MVT::i16, N1)
2216 : DAG.getConstant(cast<ConstantSDNode>(N1)->getValue(), MVT::i16));
Dan Gohman8181bd12008-07-27 21:46:04 +00002217 SDValue ExpandArg =
Scott Michel8efdca42007-12-04 22:23:35 +00002218 DAG.getNode(ISD::OR, MVT::i16, N0,
2219 DAG.getNode(ISD::SHL, MVT::i16,
2220 N0, DAG.getConstant(8, MVT::i16)));
Scott Michel4ec722e2008-07-16 17:17:29 +00002221 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
Scott Michel8efdca42007-12-04 22:23:35 +00002222 DAG.getNode(Opc, MVT::i16, ExpandArg, N1));
2223 }
2224 case ISD::SRL:
2225 case ISD::SHL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002226 SDValue N1 = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002227 unsigned N1Opc;
2228 N0 = (N0.getOpcode() != ISD::Constant
2229 ? DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, N0)
2230 : DAG.getConstant(cast<ConstantSDNode>(N0)->getValue(), MVT::i16));
Duncan Sandsec142ee2008-06-08 20:54:56 +00002231 N1Opc = N1.getValueType().bitsLT(MVT::i16) ? ISD::ZERO_EXTEND : ISD::TRUNCATE;
Scott Michel8efdca42007-12-04 22:23:35 +00002232 N1 = (N1.getOpcode() != ISD::Constant
2233 ? DAG.getNode(N1Opc, MVT::i16, N1)
2234 : DAG.getConstant(cast<ConstantSDNode>(N1)->getValue(), MVT::i16));
Scott Michel4ec722e2008-07-16 17:17:29 +00002235 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
Scott Michel8efdca42007-12-04 22:23:35 +00002236 DAG.getNode(Opc, MVT::i16, N0, N1));
2237 }
2238 case ISD::SRA: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002239 SDValue N1 = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002240 unsigned N1Opc;
2241 N0 = (N0.getOpcode() != ISD::Constant
2242 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N0)
2243 : DAG.getConstant(cast<ConstantSDNode>(N0)->getValue(), MVT::i16));
Duncan Sandsec142ee2008-06-08 20:54:56 +00002244 N1Opc = N1.getValueType().bitsLT(MVT::i16) ? ISD::SIGN_EXTEND : ISD::TRUNCATE;
Scott Michel8efdca42007-12-04 22:23:35 +00002245 N1 = (N1.getOpcode() != ISD::Constant
2246 ? DAG.getNode(N1Opc, MVT::i16, N1)
2247 : DAG.getConstant(cast<ConstantSDNode>(N1)->getValue(), MVT::i16));
Scott Michel4ec722e2008-07-16 17:17:29 +00002248 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
Scott Michel8efdca42007-12-04 22:23:35 +00002249 DAG.getNode(Opc, MVT::i16, N0, N1));
2250 }
2251 case ISD::MUL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002252 SDValue N1 = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002253 unsigned N1Opc;
2254 N0 = (N0.getOpcode() != ISD::Constant
2255 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N0)
2256 : DAG.getConstant(cast<ConstantSDNode>(N0)->getValue(), MVT::i16));
Duncan Sandsec142ee2008-06-08 20:54:56 +00002257 N1Opc = N1.getValueType().bitsLT(MVT::i16) ? ISD::SIGN_EXTEND : ISD::TRUNCATE;
Scott Michel8efdca42007-12-04 22:23:35 +00002258 N1 = (N1.getOpcode() != ISD::Constant
2259 ? DAG.getNode(N1Opc, MVT::i16, N1)
2260 : DAG.getConstant(cast<ConstantSDNode>(N1)->getValue(), MVT::i16));
Scott Michel4ec722e2008-07-16 17:17:29 +00002261 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
Scott Michel8efdca42007-12-04 22:23:35 +00002262 DAG.getNode(Opc, MVT::i16, N0, N1));
2263 break;
2264 }
2265 }
2266
Dan Gohman8181bd12008-07-27 21:46:04 +00002267 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00002268}
2269
Dan Gohman8181bd12008-07-27 21:46:04 +00002270static SDValue LowerI64Math(SDValue Op, SelectionDAG &DAG, unsigned Opc)
Scott Michel97872d32008-02-23 18:41:37 +00002271{
Duncan Sands92c43912008-06-06 12:08:01 +00002272 MVT VT = Op.getValueType();
2273 MVT VecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits()));
Scott Michel97872d32008-02-23 18:41:37 +00002274
Dan Gohman8181bd12008-07-27 21:46:04 +00002275 SDValue Op0 = Op.getOperand(0);
Scott Michel97872d32008-02-23 18:41:37 +00002276
2277 switch (Opc) {
2278 case ISD::ZERO_EXTEND:
2279 case ISD::SIGN_EXTEND:
2280 case ISD::ANY_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00002281 MVT Op0VT = Op0.getValueType();
2282 MVT Op0VecVT = MVT::getVectorVT(Op0VT, (128 / Op0VT.getSizeInBits()));
Scott Michel97872d32008-02-23 18:41:37 +00002283
2284 assert(Op0VT == MVT::i32
2285 && "CellSPU: Zero/sign extending something other than i32");
Scott Michelbc5fbc12008-04-30 00:30:08 +00002286 DEBUG(cerr << "CellSPU: LowerI64Math custom lowering zero/sign/any extend\n");
Scott Michel97872d32008-02-23 18:41:37 +00002287
2288 unsigned NewOpc = (Opc == ISD::SIGN_EXTEND
2289 ? SPUISD::ROTBYTES_RIGHT_S
2290 : SPUISD::ROTQUAD_RZ_BYTES);
Dan Gohman8181bd12008-07-27 21:46:04 +00002291 SDValue PromoteScalar =
Scott Michel97872d32008-02-23 18:41:37 +00002292 DAG.getNode(SPUISD::PROMOTE_SCALAR, Op0VecVT, Op0);
2293
2294 return DAG.getNode(SPUISD::EXTRACT_ELT0, VT,
2295 DAG.getNode(ISD::BIT_CONVERT, VecVT,
2296 DAG.getNode(NewOpc, Op0VecVT,
2297 PromoteScalar,
2298 DAG.getConstant(4, MVT::i32))));
2299 }
2300
Scott Michel67224b22008-06-02 22:18:03 +00002301 case ISD::ADD: {
2302 // Turn operands into vectors to satisfy type checking (shufb works on
2303 // vectors)
Dan Gohman8181bd12008-07-27 21:46:04 +00002304 SDValue Op0 =
Scott Michel67224b22008-06-02 22:18:03 +00002305 DAG.getNode(SPUISD::PROMOTE_SCALAR, MVT::v2i64, Op.getOperand(0));
Dan Gohman8181bd12008-07-27 21:46:04 +00002306 SDValue Op1 =
Scott Michel67224b22008-06-02 22:18:03 +00002307 DAG.getNode(SPUISD::PROMOTE_SCALAR, MVT::v2i64, Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00002308 SmallVector<SDValue, 16> ShufBytes;
Scott Michel67224b22008-06-02 22:18:03 +00002309
2310 // Create the shuffle mask for "rotating" the borrow up one register slot
2311 // once the borrow is generated.
2312 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
2313 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
2314 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
2315 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
2316
Dan Gohman8181bd12008-07-27 21:46:04 +00002317 SDValue CarryGen =
Scott Michel67224b22008-06-02 22:18:03 +00002318 DAG.getNode(SPUISD::CARRY_GENERATE, MVT::v2i64, Op0, Op1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002319 SDValue ShiftedCarry =
Scott Michel67224b22008-06-02 22:18:03 +00002320 DAG.getNode(SPUISD::SHUFB, MVT::v2i64,
2321 CarryGen, CarryGen,
2322 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
2323 &ShufBytes[0], ShufBytes.size()));
2324
2325 return DAG.getNode(SPUISD::EXTRACT_ELT0, MVT::i64,
2326 DAG.getNode(SPUISD::ADD_EXTENDED, MVT::v2i64,
2327 Op0, Op1, ShiftedCarry));
2328 }
2329
2330 case ISD::SUB: {
2331 // Turn operands into vectors to satisfy type checking (shufb works on
2332 // vectors)
Dan Gohman8181bd12008-07-27 21:46:04 +00002333 SDValue Op0 =
Scott Michel67224b22008-06-02 22:18:03 +00002334 DAG.getNode(SPUISD::PROMOTE_SCALAR, MVT::v2i64, Op.getOperand(0));
Dan Gohman8181bd12008-07-27 21:46:04 +00002335 SDValue Op1 =
Scott Michel67224b22008-06-02 22:18:03 +00002336 DAG.getNode(SPUISD::PROMOTE_SCALAR, MVT::v2i64, Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00002337 SmallVector<SDValue, 16> ShufBytes;
Scott Michel67224b22008-06-02 22:18:03 +00002338
2339 // Create the shuffle mask for "rotating" the borrow up one register slot
2340 // once the borrow is generated.
2341 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
2342 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
2343 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
2344 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
2345
Dan Gohman8181bd12008-07-27 21:46:04 +00002346 SDValue BorrowGen =
Scott Michel67224b22008-06-02 22:18:03 +00002347 DAG.getNode(SPUISD::BORROW_GENERATE, MVT::v2i64, Op0, Op1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002348 SDValue ShiftedBorrow =
Scott Michel67224b22008-06-02 22:18:03 +00002349 DAG.getNode(SPUISD::SHUFB, MVT::v2i64,
2350 BorrowGen, BorrowGen,
2351 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
2352 &ShufBytes[0], ShufBytes.size()));
2353
2354 return DAG.getNode(SPUISD::EXTRACT_ELT0, MVT::i64,
2355 DAG.getNode(SPUISD::SUB_EXTENDED, MVT::v2i64,
2356 Op0, Op1, ShiftedBorrow));
2357 }
2358
Scott Michel97872d32008-02-23 18:41:37 +00002359 case ISD::SHL: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002360 SDValue ShiftAmt = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00002361 MVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002362 SDValue Op0Vec = DAG.getNode(SPUISD::PROMOTE_SCALAR, VecVT, Op0);
2363 SDValue MaskLower =
Scott Michel97872d32008-02-23 18:41:37 +00002364 DAG.getNode(SPUISD::SELB, VecVT,
2365 Op0Vec,
2366 DAG.getConstant(0, VecVT),
Scott Michel67224b22008-06-02 22:18:03 +00002367 DAG.getNode(SPUISD::SELECT_MASK, VecVT,
Scott Michel97872d32008-02-23 18:41:37 +00002368 DAG.getConstant(0xff00ULL, MVT::i16)));
Dan Gohman8181bd12008-07-27 21:46:04 +00002369 SDValue ShiftAmtBytes =
Scott Michel97872d32008-02-23 18:41:37 +00002370 DAG.getNode(ISD::SRL, ShiftAmtVT,
2371 ShiftAmt,
2372 DAG.getConstant(3, ShiftAmtVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002373 SDValue ShiftAmtBits =
Scott Michel97872d32008-02-23 18:41:37 +00002374 DAG.getNode(ISD::AND, ShiftAmtVT,
2375 ShiftAmt,
2376 DAG.getConstant(7, ShiftAmtVT));
2377
2378 return DAG.getNode(SPUISD::EXTRACT_ELT0, VT,
2379 DAG.getNode(SPUISD::SHLQUAD_L_BITS, VecVT,
2380 DAG.getNode(SPUISD::SHLQUAD_L_BYTES, VecVT,
2381 MaskLower, ShiftAmtBytes),
2382 ShiftAmtBits));
2383 }
2384
2385 case ISD::SRL: {
Duncan Sands92c43912008-06-06 12:08:01 +00002386 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002387 SDValue ShiftAmt = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00002388 MVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002389 SDValue ShiftAmtBytes =
Scott Michel97872d32008-02-23 18:41:37 +00002390 DAG.getNode(ISD::SRL, ShiftAmtVT,
2391 ShiftAmt,
2392 DAG.getConstant(3, ShiftAmtVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002393 SDValue ShiftAmtBits =
Scott Michel97872d32008-02-23 18:41:37 +00002394 DAG.getNode(ISD::AND, ShiftAmtVT,
2395 ShiftAmt,
2396 DAG.getConstant(7, ShiftAmtVT));
2397
2398 return DAG.getNode(SPUISD::ROTQUAD_RZ_BITS, VT,
2399 DAG.getNode(SPUISD::ROTQUAD_RZ_BYTES, VT,
2400 Op0, ShiftAmtBytes),
2401 ShiftAmtBits);
2402 }
Scott Michel67224b22008-06-02 22:18:03 +00002403
2404 case ISD::SRA: {
2405 // Promote Op0 to vector
Dan Gohman8181bd12008-07-27 21:46:04 +00002406 SDValue Op0 =
Scott Michel67224b22008-06-02 22:18:03 +00002407 DAG.getNode(SPUISD::PROMOTE_SCALAR, MVT::v2i64, Op.getOperand(0));
Dan Gohman8181bd12008-07-27 21:46:04 +00002408 SDValue ShiftAmt = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00002409 MVT ShiftVT = ShiftAmt.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00002410
2411 // Negate variable shift amounts
2412 if (!isa<ConstantSDNode>(ShiftAmt)) {
2413 ShiftAmt = DAG.getNode(ISD::SUB, ShiftVT,
2414 DAG.getConstant(0, ShiftVT), ShiftAmt);
2415 }
2416
Dan Gohman8181bd12008-07-27 21:46:04 +00002417 SDValue UpperHalfSign =
Scott Michel67224b22008-06-02 22:18:03 +00002418 DAG.getNode(SPUISD::EXTRACT_ELT0, MVT::i32,
2419 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
2420 DAG.getNode(SPUISD::VEC_SRA, MVT::v2i64,
2421 Op0, DAG.getConstant(31, MVT::i32))));
Dan Gohman8181bd12008-07-27 21:46:04 +00002422 SDValue UpperHalfSignMask =
Scott Michel67224b22008-06-02 22:18:03 +00002423 DAG.getNode(SPUISD::SELECT_MASK, MVT::v2i64, UpperHalfSign);
Dan Gohman8181bd12008-07-27 21:46:04 +00002424 SDValue UpperLowerMask =
Scott Michel67224b22008-06-02 22:18:03 +00002425 DAG.getNode(SPUISD::SELECT_MASK, MVT::v2i64,
2426 DAG.getConstant(0xff00, MVT::i16));
Dan Gohman8181bd12008-07-27 21:46:04 +00002427 SDValue UpperLowerSelect =
Scott Michel67224b22008-06-02 22:18:03 +00002428 DAG.getNode(SPUISD::SELB, MVT::v2i64,
2429 UpperHalfSignMask, Op0, UpperLowerMask);
Dan Gohman8181bd12008-07-27 21:46:04 +00002430 SDValue RotateLeftBytes =
Scott Michel67224b22008-06-02 22:18:03 +00002431 DAG.getNode(SPUISD::ROTBYTES_LEFT_BITS, MVT::v2i64,
2432 UpperLowerSelect, ShiftAmt);
Dan Gohman8181bd12008-07-27 21:46:04 +00002433 SDValue RotateLeftBits =
Scott Michel67224b22008-06-02 22:18:03 +00002434 DAG.getNode(SPUISD::ROTBYTES_LEFT, MVT::v2i64,
2435 RotateLeftBytes, ShiftAmt);
2436
2437 return DAG.getNode(SPUISD::EXTRACT_ELT0, MVT::i64,
2438 RotateLeftBits);
2439 }
Scott Michel97872d32008-02-23 18:41:37 +00002440 }
2441
Dan Gohman8181bd12008-07-27 21:46:04 +00002442 return SDValue();
Scott Michel97872d32008-02-23 18:41:37 +00002443}
2444
Scott Michel8efdca42007-12-04 22:23:35 +00002445//! Lower byte immediate operations for v16i8 vectors:
Dan Gohman8181bd12008-07-27 21:46:04 +00002446static SDValue
2447LowerByteImmed(SDValue Op, SelectionDAG &DAG) {
2448 SDValue ConstVec;
2449 SDValue Arg;
Duncan Sands92c43912008-06-06 12:08:01 +00002450 MVT VT = Op.getValueType();
Scott Michel8efdca42007-12-04 22:23:35 +00002451
2452 ConstVec = Op.getOperand(0);
2453 Arg = Op.getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002454 if (ConstVec.getNode()->getOpcode() != ISD::BUILD_VECTOR) {
2455 if (ConstVec.getNode()->getOpcode() == ISD::BIT_CONVERT) {
Scott Michel8efdca42007-12-04 22:23:35 +00002456 ConstVec = ConstVec.getOperand(0);
2457 } else {
2458 ConstVec = Op.getOperand(1);
2459 Arg = Op.getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +00002460 if (ConstVec.getNode()->getOpcode() == ISD::BIT_CONVERT) {
Scott Michel5a6f17b2008-01-30 02:55:46 +00002461 ConstVec = ConstVec.getOperand(0);
Scott Michel8efdca42007-12-04 22:23:35 +00002462 }
2463 }
2464 }
2465
Gabor Greif1c80d112008-08-28 21:40:38 +00002466 if (ConstVec.getNode()->getOpcode() == ISD::BUILD_VECTOR) {
Scott Michel8efdca42007-12-04 22:23:35 +00002467 uint64_t VectorBits[2];
2468 uint64_t UndefBits[2];
2469 uint64_t SplatBits, SplatUndef;
2470 int SplatSize;
2471
Gabor Greif1c80d112008-08-28 21:40:38 +00002472 if (!GetConstantBuildVectorBits(ConstVec.getNode(), VectorBits, UndefBits)
Scott Michel5a6f17b2008-01-30 02:55:46 +00002473 && isConstantSplat(VectorBits, UndefBits,
Duncan Sands92c43912008-06-06 12:08:01 +00002474 VT.getVectorElementType().getSizeInBits(),
Scott Michel5a6f17b2008-01-30 02:55:46 +00002475 SplatBits, SplatUndef, SplatSize)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002476 SDValue tcVec[16];
2477 SDValue tc = DAG.getTargetConstant(SplatBits & 0xff, MVT::i8);
Scott Michel8efdca42007-12-04 22:23:35 +00002478 const size_t tcVecSize = sizeof(tcVec) / sizeof(tcVec[0]);
2479
2480 // Turn the BUILD_VECTOR into a set of target constants:
2481 for (size_t i = 0; i < tcVecSize; ++i)
Scott Michel5a6f17b2008-01-30 02:55:46 +00002482 tcVec[i] = tc;
Scott Michel8efdca42007-12-04 22:23:35 +00002483
Gabor Greif1c80d112008-08-28 21:40:38 +00002484 return DAG.getNode(Op.getNode()->getOpcode(), VT, Arg,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002485 DAG.getNode(ISD::BUILD_VECTOR, VT, tcVec, tcVecSize));
Scott Michel8efdca42007-12-04 22:23:35 +00002486 }
2487 }
Nate Begeman7569e762008-07-29 19:07:27 +00002488 // These operations (AND, OR, XOR) are legal, they just couldn't be custom
2489 // lowered. Return the operation, rather than a null SDValue.
2490 return Op;
Scott Michel8efdca42007-12-04 22:23:35 +00002491}
2492
2493//! Lower i32 multiplication
Dan Gohman8181bd12008-07-27 21:46:04 +00002494static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG, MVT VT,
Scott Michel8efdca42007-12-04 22:23:35 +00002495 unsigned Opc) {
Duncan Sands92c43912008-06-06 12:08:01 +00002496 switch (VT.getSimpleVT()) {
Scott Michel8efdca42007-12-04 22:23:35 +00002497 default:
2498 cerr << "CellSPU: Unknown LowerMUL value type, got "
Duncan Sands92c43912008-06-06 12:08:01 +00002499 << Op.getValueType().getMVTString()
Scott Michel5a6f17b2008-01-30 02:55:46 +00002500 << "\n";
Scott Michel8efdca42007-12-04 22:23:35 +00002501 abort();
2502 /*NOTREACHED*/
2503
2504 case MVT::i32: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002505 SDValue rA = Op.getOperand(0);
2506 SDValue rB = Op.getOperand(1);
Scott Michel8efdca42007-12-04 22:23:35 +00002507
2508 return DAG.getNode(ISD::ADD, MVT::i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002509 DAG.getNode(ISD::ADD, MVT::i32,
2510 DAG.getNode(SPUISD::MPYH, MVT::i32, rA, rB),
2511 DAG.getNode(SPUISD::MPYH, MVT::i32, rB, rA)),
2512 DAG.getNode(SPUISD::MPYU, MVT::i32, rA, rB));
Scott Michel8efdca42007-12-04 22:23:35 +00002513 }
2514 }
2515
Dan Gohman8181bd12008-07-27 21:46:04 +00002516 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00002517}
2518
2519//! Custom lowering for CTPOP (count population)
2520/*!
2521 Custom lowering code that counts the number ones in the input
2522 operand. SPU has such an instruction, but it counts the number of
2523 ones per byte, which then have to be accumulated.
2524*/
Dan Gohman8181bd12008-07-27 21:46:04 +00002525static SDValue LowerCTPOP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002526 MVT VT = Op.getValueType();
2527 MVT vecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits()));
Scott Michel8efdca42007-12-04 22:23:35 +00002528
Duncan Sands92c43912008-06-06 12:08:01 +00002529 switch (VT.getSimpleVT()) {
2530 default:
2531 assert(false && "Invalid value type!");
Scott Michel8efdca42007-12-04 22:23:35 +00002532 case MVT::i8: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002533 SDValue N = Op.getOperand(0);
2534 SDValue Elt0 = DAG.getConstant(0, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00002535
Dan Gohman8181bd12008-07-27 21:46:04 +00002536 SDValue Promote = DAG.getNode(SPUISD::PROMOTE_SCALAR, vecVT, N, N);
2537 SDValue CNTB = DAG.getNode(SPUISD::CNTB, vecVT, Promote);
Scott Michel8efdca42007-12-04 22:23:35 +00002538
2539 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i8, CNTB, Elt0);
2540 }
2541
2542 case MVT::i16: {
2543 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +00002544 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel8efdca42007-12-04 22:23:35 +00002545
Chris Lattner1b989192007-12-31 04:13:23 +00002546 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R16CRegClass);
Scott Michel8efdca42007-12-04 22:23:35 +00002547
Dan Gohman8181bd12008-07-27 21:46:04 +00002548 SDValue N = Op.getOperand(0);
2549 SDValue Elt0 = DAG.getConstant(0, MVT::i16);
2550 SDValue Mask0 = DAG.getConstant(0x0f, MVT::i16);
2551 SDValue Shift1 = DAG.getConstant(8, MVT::i16);
Scott Michel8efdca42007-12-04 22:23:35 +00002552
Dan Gohman8181bd12008-07-27 21:46:04 +00002553 SDValue Promote = DAG.getNode(SPUISD::PROMOTE_SCALAR, vecVT, N, N);
2554 SDValue CNTB = DAG.getNode(SPUISD::CNTB, vecVT, Promote);
Scott Michel8efdca42007-12-04 22:23:35 +00002555
2556 // CNTB_result becomes the chain to which all of the virtual registers
2557 // CNTB_reg, SUM1_reg become associated:
Dan Gohman8181bd12008-07-27 21:46:04 +00002558 SDValue CNTB_result =
Scott Michel8efdca42007-12-04 22:23:35 +00002559 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, CNTB, Elt0);
Scott Michel4ec722e2008-07-16 17:17:29 +00002560
Dan Gohman8181bd12008-07-27 21:46:04 +00002561 SDValue CNTB_rescopy =
Scott Michel8efdca42007-12-04 22:23:35 +00002562 DAG.getCopyToReg(CNTB_result, CNTB_reg, CNTB_result);
2563
Dan Gohman8181bd12008-07-27 21:46:04 +00002564 SDValue Tmp1 = DAG.getCopyFromReg(CNTB_rescopy, CNTB_reg, MVT::i16);
Scott Michel8efdca42007-12-04 22:23:35 +00002565
2566 return DAG.getNode(ISD::AND, MVT::i16,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002567 DAG.getNode(ISD::ADD, MVT::i16,
2568 DAG.getNode(ISD::SRL, MVT::i16,
2569 Tmp1, Shift1),
2570 Tmp1),
2571 Mask0);
Scott Michel8efdca42007-12-04 22:23:35 +00002572 }
2573
2574 case MVT::i32: {
2575 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +00002576 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel8efdca42007-12-04 22:23:35 +00002577
Chris Lattner1b989192007-12-31 04:13:23 +00002578 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
2579 unsigned SUM1_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
Scott Michel8efdca42007-12-04 22:23:35 +00002580
Dan Gohman8181bd12008-07-27 21:46:04 +00002581 SDValue N = Op.getOperand(0);
2582 SDValue Elt0 = DAG.getConstant(0, MVT::i32);
2583 SDValue Mask0 = DAG.getConstant(0xff, MVT::i32);
2584 SDValue Shift1 = DAG.getConstant(16, MVT::i32);
2585 SDValue Shift2 = DAG.getConstant(8, MVT::i32);
Scott Michel8efdca42007-12-04 22:23:35 +00002586
Dan Gohman8181bd12008-07-27 21:46:04 +00002587 SDValue Promote = DAG.getNode(SPUISD::PROMOTE_SCALAR, vecVT, N, N);
2588 SDValue CNTB = DAG.getNode(SPUISD::CNTB, vecVT, Promote);
Scott Michel8efdca42007-12-04 22:23:35 +00002589
2590 // CNTB_result becomes the chain to which all of the virtual registers
2591 // CNTB_reg, SUM1_reg become associated:
Dan Gohman8181bd12008-07-27 21:46:04 +00002592 SDValue CNTB_result =
Scott Michel8efdca42007-12-04 22:23:35 +00002593 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, CNTB, Elt0);
Scott Michel4ec722e2008-07-16 17:17:29 +00002594
Dan Gohman8181bd12008-07-27 21:46:04 +00002595 SDValue CNTB_rescopy =
Scott Michel8efdca42007-12-04 22:23:35 +00002596 DAG.getCopyToReg(CNTB_result, CNTB_reg, CNTB_result);
2597
Dan Gohman8181bd12008-07-27 21:46:04 +00002598 SDValue Comp1 =
Scott Michel8efdca42007-12-04 22:23:35 +00002599 DAG.getNode(ISD::SRL, MVT::i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002600 DAG.getCopyFromReg(CNTB_rescopy, CNTB_reg, MVT::i32), Shift1);
Scott Michel8efdca42007-12-04 22:23:35 +00002601
Dan Gohman8181bd12008-07-27 21:46:04 +00002602 SDValue Sum1 =
Scott Michel8efdca42007-12-04 22:23:35 +00002603 DAG.getNode(ISD::ADD, MVT::i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002604 Comp1, DAG.getCopyFromReg(CNTB_rescopy, CNTB_reg, MVT::i32));
Scott Michel8efdca42007-12-04 22:23:35 +00002605
Dan Gohman8181bd12008-07-27 21:46:04 +00002606 SDValue Sum1_rescopy =
Scott Michel8efdca42007-12-04 22:23:35 +00002607 DAG.getCopyToReg(CNTB_result, SUM1_reg, Sum1);
2608
Dan Gohman8181bd12008-07-27 21:46:04 +00002609 SDValue Comp2 =
Scott Michel8efdca42007-12-04 22:23:35 +00002610 DAG.getNode(ISD::SRL, MVT::i32,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002611 DAG.getCopyFromReg(Sum1_rescopy, SUM1_reg, MVT::i32),
2612 Shift2);
Dan Gohman8181bd12008-07-27 21:46:04 +00002613 SDValue Sum2 =
Scott Michel8efdca42007-12-04 22:23:35 +00002614 DAG.getNode(ISD::ADD, MVT::i32, Comp2,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002615 DAG.getCopyFromReg(Sum1_rescopy, SUM1_reg, MVT::i32));
Scott Michel8efdca42007-12-04 22:23:35 +00002616
2617 return DAG.getNode(ISD::AND, MVT::i32, Sum2, Mask0);
2618 }
2619
2620 case MVT::i64:
2621 break;
2622 }
2623
Dan Gohman8181bd12008-07-27 21:46:04 +00002624 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00002625}
2626
2627/// LowerOperation - Provide custom lowering hooks for some operations.
2628///
Dan Gohman8181bd12008-07-27 21:46:04 +00002629SDValue
2630SPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
Scott Michel8efdca42007-12-04 22:23:35 +00002631{
Scott Michel97872d32008-02-23 18:41:37 +00002632 unsigned Opc = (unsigned) Op.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00002633 MVT VT = Op.getValueType();
Scott Michel97872d32008-02-23 18:41:37 +00002634
2635 switch (Opc) {
Scott Michel8efdca42007-12-04 22:23:35 +00002636 default: {
2637 cerr << "SPUTargetLowering::LowerOperation(): need to lower this!\n";
Scott Michel97872d32008-02-23 18:41:37 +00002638 cerr << "Op.getOpcode() = " << Opc << "\n";
Gabor Greif1c80d112008-08-28 21:40:38 +00002639 cerr << "*Op.getNode():\n";
2640 Op.getNode()->dump();
Scott Michel8efdca42007-12-04 22:23:35 +00002641 abort();
2642 }
2643 case ISD::LOAD:
2644 case ISD::SEXTLOAD:
2645 case ISD::ZEXTLOAD:
2646 return LowerLOAD(Op, DAG, SPUTM.getSubtargetImpl());
2647 case ISD::STORE:
2648 return LowerSTORE(Op, DAG, SPUTM.getSubtargetImpl());
2649 case ISD::ConstantPool:
2650 return LowerConstantPool(Op, DAG, SPUTM.getSubtargetImpl());
2651 case ISD::GlobalAddress:
2652 return LowerGlobalAddress(Op, DAG, SPUTM.getSubtargetImpl());
2653 case ISD::JumpTable:
2654 return LowerJumpTable(Op, DAG, SPUTM.getSubtargetImpl());
2655 case ISD::Constant:
2656 return LowerConstant(Op, DAG);
2657 case ISD::ConstantFP:
2658 return LowerConstantFP(Op, DAG);
Scott Michel394e26d2008-01-17 20:38:41 +00002659 case ISD::BRCOND:
2660 return LowerBRCOND(Op, DAG);
Scott Michel8efdca42007-12-04 22:23:35 +00002661 case ISD::FORMAL_ARGUMENTS:
Scott Michel394e26d2008-01-17 20:38:41 +00002662 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Scott Michel8efdca42007-12-04 22:23:35 +00002663 case ISD::CALL:
Scott Micheldbac4cf2008-01-11 02:53:15 +00002664 return LowerCALL(Op, DAG, SPUTM.getSubtargetImpl());
Scott Michel8efdca42007-12-04 22:23:35 +00002665 case ISD::RET:
2666 return LowerRET(Op, DAG, getTargetMachine());
2667
Scott Michel97872d32008-02-23 18:41:37 +00002668
2669 // i8, i64 math ops:
2670 case ISD::ZERO_EXTEND:
2671 case ISD::SIGN_EXTEND:
2672 case ISD::ANY_EXTEND:
Scott Michel67224b22008-06-02 22:18:03 +00002673 case ISD::ADD:
Scott Michel8efdca42007-12-04 22:23:35 +00002674 case ISD::SUB:
2675 case ISD::ROTR:
2676 case ISD::ROTL:
2677 case ISD::SRL:
2678 case ISD::SHL:
Scott Michel67224b22008-06-02 22:18:03 +00002679 case ISD::SRA: {
Scott Michel97872d32008-02-23 18:41:37 +00002680 if (VT == MVT::i8)
2681 return LowerI8Math(Op, DAG, Opc);
2682 else if (VT == MVT::i64)
2683 return LowerI64Math(Op, DAG, Opc);
2684 break;
Scott Michel67224b22008-06-02 22:18:03 +00002685 }
Scott Michel8efdca42007-12-04 22:23:35 +00002686
2687 // Vector-related lowering.
2688 case ISD::BUILD_VECTOR:
2689 return LowerBUILD_VECTOR(Op, DAG);
2690 case ISD::SCALAR_TO_VECTOR:
2691 return LowerSCALAR_TO_VECTOR(Op, DAG);
2692 case ISD::VECTOR_SHUFFLE:
2693 return LowerVECTOR_SHUFFLE(Op, DAG);
2694 case ISD::EXTRACT_VECTOR_ELT:
2695 return LowerEXTRACT_VECTOR_ELT(Op, DAG);
2696 case ISD::INSERT_VECTOR_ELT:
2697 return LowerINSERT_VECTOR_ELT(Op, DAG);
2698
2699 // Look for ANDBI, ORBI and XORBI opportunities and lower appropriately:
2700 case ISD::AND:
2701 case ISD::OR:
2702 case ISD::XOR:
2703 return LowerByteImmed(Op, DAG);
2704
2705 // Vector and i8 multiply:
2706 case ISD::MUL:
Duncan Sands92c43912008-06-06 12:08:01 +00002707 if (VT.isVector())
Scott Michel8efdca42007-12-04 22:23:35 +00002708 return LowerVectorMUL(Op, DAG);
Scott Michel97872d32008-02-23 18:41:37 +00002709 else if (VT == MVT::i8)
2710 return LowerI8Math(Op, DAG, Opc);
Scott Michel8efdca42007-12-04 22:23:35 +00002711 else
Scott Michel97872d32008-02-23 18:41:37 +00002712 return LowerMUL(Op, DAG, VT, Opc);
Scott Michel8efdca42007-12-04 22:23:35 +00002713
2714 case ISD::FDIV:
Scott Michel97872d32008-02-23 18:41:37 +00002715 if (VT == MVT::f32 || VT == MVT::v4f32)
Scott Michel8efdca42007-12-04 22:23:35 +00002716 return LowerFDIVf32(Op, DAG);
2717// else if (Op.getValueType() == MVT::f64)
2718// return LowerFDIVf64(Op, DAG);
2719 else
2720 assert(0 && "Calling FDIV on unsupported MVT");
2721
2722 case ISD::CTPOP:
2723 return LowerCTPOP(Op, DAG);
2724 }
2725
Dan Gohman8181bd12008-07-27 21:46:04 +00002726 return SDValue();
Scott Michel8efdca42007-12-04 22:23:35 +00002727}
2728
2729//===----------------------------------------------------------------------===//
Scott Michel8efdca42007-12-04 22:23:35 +00002730// Target Optimization Hooks
2731//===----------------------------------------------------------------------===//
2732
Dan Gohman8181bd12008-07-27 21:46:04 +00002733SDValue
Scott Michel8efdca42007-12-04 22:23:35 +00002734SPUTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const
2735{
2736#if 0
2737 TargetMachine &TM = getTargetMachine();
Scott Michelf9f42e62008-01-29 02:16:57 +00002738#endif
2739 const SPUSubtarget *ST = SPUTM.getSubtargetImpl();
Scott Michel8efdca42007-12-04 22:23:35 +00002740 SelectionDAG &DAG = DCI.DAG;
Dan Gohman8181bd12008-07-27 21:46:04 +00002741 SDValue Op0 = N->getOperand(0); // everything has at least one operand
2742 SDValue Result; // Initially, NULL result
Scott Michel8efdca42007-12-04 22:23:35 +00002743
2744 switch (N->getOpcode()) {
2745 default: break;
Scott Michelf9f42e62008-01-29 02:16:57 +00002746 case ISD::ADD: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002747 SDValue Op1 = N->getOperand(1);
Scott Michelf9f42e62008-01-29 02:16:57 +00002748
Scott Michel67224b22008-06-02 22:18:03 +00002749 if (isa<ConstantSDNode>(Op1) && Op0.getOpcode() == SPUISD::IndirectAddr) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002750 SDValue Op01 = Op0.getOperand(1);
Scott Michelf9f42e62008-01-29 02:16:57 +00002751 if (Op01.getOpcode() == ISD::Constant
2752 || Op01.getOpcode() == ISD::TargetConstant) {
2753 // (add <const>, (SPUindirect <arg>, <const>)) ->
2754 // (SPUindirect <arg>, <const + const>)
2755 ConstantSDNode *CN0 = cast<ConstantSDNode>(Op1);
2756 ConstantSDNode *CN1 = cast<ConstantSDNode>(Op01);
Dan Gohman8181bd12008-07-27 21:46:04 +00002757 SDValue combinedConst =
Scott Michelf9f42e62008-01-29 02:16:57 +00002758 DAG.getConstant(CN0->getValue() + CN1->getValue(),
2759 Op0.getValueType());
2760
2761 DEBUG(cerr << "Replace: (add " << CN0->getValue() << ", "
2762 << "(SPUindirect <arg>, " << CN1->getValue() << "))\n");
2763 DEBUG(cerr << "With: (SPUindirect <arg>, "
2764 << CN0->getValue() + CN1->getValue() << ")\n");
2765 return DAG.getNode(SPUISD::IndirectAddr, Op0.getValueType(),
2766 Op0.getOperand(0), combinedConst);
2767 }
Scott Michel67224b22008-06-02 22:18:03 +00002768 } else if (isa<ConstantSDNode>(Op0)
Scott Michelf9f42e62008-01-29 02:16:57 +00002769 && Op1.getOpcode() == SPUISD::IndirectAddr) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002770 SDValue Op11 = Op1.getOperand(1);
Scott Michelf9f42e62008-01-29 02:16:57 +00002771 if (Op11.getOpcode() == ISD::Constant
2772 || Op11.getOpcode() == ISD::TargetConstant) {
2773 // (add (SPUindirect <arg>, <const>), <const>) ->
2774 // (SPUindirect <arg>, <const + const>)
2775 ConstantSDNode *CN0 = cast<ConstantSDNode>(Op0);
2776 ConstantSDNode *CN1 = cast<ConstantSDNode>(Op11);
Dan Gohman8181bd12008-07-27 21:46:04 +00002777 SDValue combinedConst =
Scott Michelf9f42e62008-01-29 02:16:57 +00002778 DAG.getConstant(CN0->getValue() + CN1->getValue(),
2779 Op0.getValueType());
2780
2781 DEBUG(cerr << "Replace: (add " << CN0->getValue() << ", "
2782 << "(SPUindirect <arg>, " << CN1->getValue() << "))\n");
2783 DEBUG(cerr << "With: (SPUindirect <arg>, "
2784 << CN0->getValue() + CN1->getValue() << ")\n");
2785
2786 return DAG.getNode(SPUISD::IndirectAddr, Op1.getValueType(),
2787 Op1.getOperand(0), combinedConst);
2788 }
2789 }
Scott Michel97872d32008-02-23 18:41:37 +00002790 break;
2791 }
2792 case ISD::SIGN_EXTEND:
2793 case ISD::ZERO_EXTEND:
2794 case ISD::ANY_EXTEND: {
2795 if (Op0.getOpcode() == SPUISD::EXTRACT_ELT0 &&
2796 N->getValueType(0) == Op0.getValueType()) {
2797 // (any_extend (SPUextract_elt0 <arg>)) ->
2798 // (SPUextract_elt0 <arg>)
2799 // Types must match, however...
2800 DEBUG(cerr << "Replace: ");
2801 DEBUG(N->dump(&DAG));
2802 DEBUG(cerr << "\nWith: ");
Gabor Greif1c80d112008-08-28 21:40:38 +00002803 DEBUG(Op0.getNode()->dump(&DAG));
Scott Michel97872d32008-02-23 18:41:37 +00002804 DEBUG(cerr << "\n");
2805
2806 return Op0;
2807 }
2808 break;
2809 }
2810 case SPUISD::IndirectAddr: {
2811 if (!ST->usingLargeMem() && Op0.getOpcode() == SPUISD::AFormAddr) {
2812 ConstantSDNode *CN = cast<ConstantSDNode>(N->getOperand(1));
2813 if (CN->getValue() == 0) {
2814 // (SPUindirect (SPUaform <addr>, 0), 0) ->
2815 // (SPUaform <addr>, 0)
2816
2817 DEBUG(cerr << "Replace: ");
2818 DEBUG(N->dump(&DAG));
2819 DEBUG(cerr << "\nWith: ");
Gabor Greif1c80d112008-08-28 21:40:38 +00002820 DEBUG(Op0.getNode()->dump(&DAG));
Scott Michel97872d32008-02-23 18:41:37 +00002821 DEBUG(cerr << "\n");
2822
2823 return Op0;
2824 }
2825 }
2826 break;
2827 }
2828 case SPUISD::SHLQUAD_L_BITS:
2829 case SPUISD::SHLQUAD_L_BYTES:
2830 case SPUISD::VEC_SHL:
2831 case SPUISD::VEC_SRL:
2832 case SPUISD::VEC_SRA:
2833 case SPUISD::ROTQUAD_RZ_BYTES:
2834 case SPUISD::ROTQUAD_RZ_BITS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002835 SDValue Op1 = N->getOperand(1);
Scott Michel97872d32008-02-23 18:41:37 +00002836
2837 if (isa<ConstantSDNode>(Op1)) {
2838 // Kill degenerate vector shifts:
2839 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
2840
2841 if (CN->getValue() == 0) {
2842 Result = Op0;
2843 }
2844 }
2845 break;
2846 }
2847 case SPUISD::PROMOTE_SCALAR: {
2848 switch (Op0.getOpcode()) {
2849 default:
2850 break;
2851 case ISD::ANY_EXTEND:
2852 case ISD::ZERO_EXTEND:
2853 case ISD::SIGN_EXTEND: {
2854 // (SPUpromote_scalar (any|sign|zero_extend (SPUextract_elt0 <arg>))) ->
2855 // <arg>
2856 // but only if the SPUpromote_scalar and <arg> types match.
Dan Gohman8181bd12008-07-27 21:46:04 +00002857 SDValue Op00 = Op0.getOperand(0);
Scott Michel97872d32008-02-23 18:41:37 +00002858 if (Op00.getOpcode() == SPUISD::EXTRACT_ELT0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002859 SDValue Op000 = Op00.getOperand(0);
Scott Michel97872d32008-02-23 18:41:37 +00002860 if (Op000.getValueType() == N->getValueType(0)) {
2861 Result = Op000;
2862 }
2863 }
2864 break;
2865 }
2866 case SPUISD::EXTRACT_ELT0: {
2867 // (SPUpromote_scalar (SPUextract_elt0 <arg>)) ->
2868 // <arg>
2869 Result = Op0.getOperand(0);
2870 break;
Scott Michel4ec722e2008-07-16 17:17:29 +00002871 }
Scott Michel97872d32008-02-23 18:41:37 +00002872 }
2873 break;
Scott Michelf9f42e62008-01-29 02:16:57 +00002874 }
2875 }
Scott Michel394e26d2008-01-17 20:38:41 +00002876 // Otherwise, return unchanged.
Scott Michelbc5fbc12008-04-30 00:30:08 +00002877#if 1
Gabor Greif1c80d112008-08-28 21:40:38 +00002878 if (Result.getNode()) {
Scott Michel97872d32008-02-23 18:41:37 +00002879 DEBUG(cerr << "\nReplace.SPU: ");
2880 DEBUG(N->dump(&DAG));
2881 DEBUG(cerr << "\nWith: ");
Gabor Greif1c80d112008-08-28 21:40:38 +00002882 DEBUG(Result.getNode()->dump(&DAG));
Scott Michel97872d32008-02-23 18:41:37 +00002883 DEBUG(cerr << "\n");
2884 }
2885#endif
2886
2887 return Result;
Scott Michel8efdca42007-12-04 22:23:35 +00002888}
2889
2890//===----------------------------------------------------------------------===//
2891// Inline Assembly Support
2892//===----------------------------------------------------------------------===//
2893
2894/// getConstraintType - Given a constraint letter, return the type of
2895/// constraint it is for this target.
Scott Michel4ec722e2008-07-16 17:17:29 +00002896SPUTargetLowering::ConstraintType
Scott Michel8efdca42007-12-04 22:23:35 +00002897SPUTargetLowering::getConstraintType(const std::string &ConstraintLetter) const {
2898 if (ConstraintLetter.size() == 1) {
2899 switch (ConstraintLetter[0]) {
2900 default: break;
2901 case 'b':
2902 case 'r':
2903 case 'f':
2904 case 'v':
2905 case 'y':
2906 return C_RegisterClass;
Scott Michel4ec722e2008-07-16 17:17:29 +00002907 }
Scott Michel8efdca42007-12-04 22:23:35 +00002908 }
2909 return TargetLowering::getConstraintType(ConstraintLetter);
2910}
2911
Scott Michel4ec722e2008-07-16 17:17:29 +00002912std::pair<unsigned, const TargetRegisterClass*>
Scott Michel8efdca42007-12-04 22:23:35 +00002913SPUTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00002914 MVT VT) const
Scott Michel8efdca42007-12-04 22:23:35 +00002915{
2916 if (Constraint.size() == 1) {
2917 // GCC RS6000 Constraint Letters
2918 switch (Constraint[0]) {
2919 case 'b': // R1-R31
2920 case 'r': // R0-R31
2921 if (VT == MVT::i64)
2922 return std::make_pair(0U, SPU::R64CRegisterClass);
2923 return std::make_pair(0U, SPU::R32CRegisterClass);
2924 case 'f':
2925 if (VT == MVT::f32)
2926 return std::make_pair(0U, SPU::R32FPRegisterClass);
2927 else if (VT == MVT::f64)
2928 return std::make_pair(0U, SPU::R64FPRegisterClass);
2929 break;
Scott Michel4ec722e2008-07-16 17:17:29 +00002930 case 'v':
Scott Michel8efdca42007-12-04 22:23:35 +00002931 return std::make_pair(0U, SPU::GPRCRegisterClass);
2932 }
2933 }
Scott Michel4ec722e2008-07-16 17:17:29 +00002934
Scott Michel8efdca42007-12-04 22:23:35 +00002935 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2936}
2937
Scott Michel97872d32008-02-23 18:41:37 +00002938//! Compute used/known bits for a SPU operand
Scott Michel8efdca42007-12-04 22:23:35 +00002939void
Dan Gohman8181bd12008-07-27 21:46:04 +00002940SPUTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00002941 const APInt &Mask,
Scott Michel4ec722e2008-07-16 17:17:29 +00002942 APInt &KnownZero,
Dan Gohman229fa052008-02-13 00:35:47 +00002943 APInt &KnownOne,
Scott Michel5a6f17b2008-01-30 02:55:46 +00002944 const SelectionDAG &DAG,
2945 unsigned Depth ) const {
Scott Michelbc5fbc12008-04-30 00:30:08 +00002946#if 0
Scott Michel97872d32008-02-23 18:41:37 +00002947 const uint64_t uint64_sizebits = sizeof(uint64_t) * 8;
Scott Michelbc5fbc12008-04-30 00:30:08 +00002948#endif
Scott Michel97872d32008-02-23 18:41:37 +00002949
2950 switch (Op.getOpcode()) {
2951 default:
2952 // KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
2953 break;
2954
2955#if 0
2956 case CALL:
2957 case SHUFB:
2958 case INSERT_MASK:
2959 case CNTB:
2960#endif
2961
2962 case SPUISD::PROMOTE_SCALAR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002963 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002964 MVT Op0VT = Op0.getValueType();
2965 unsigned Op0VTBits = Op0VT.getSizeInBits();
2966 uint64_t InMask = Op0VT.getIntegerVTBitMask();
Scott Michelbc5fbc12008-04-30 00:30:08 +00002967 KnownZero |= APInt(Op0VTBits, ~InMask, false);
2968 KnownOne |= APInt(Op0VTBits, InMask, false);
Scott Michel97872d32008-02-23 18:41:37 +00002969 break;
2970 }
Scott Michel4ec722e2008-07-16 17:17:29 +00002971
Scott Michel97872d32008-02-23 18:41:37 +00002972 case SPUISD::LDRESULT:
2973 case SPUISD::EXTRACT_ELT0:
2974 case SPUISD::EXTRACT_ELT0_CHAINED: {
Duncan Sands92c43912008-06-06 12:08:01 +00002975 MVT OpVT = Op.getValueType();
2976 unsigned OpVTBits = OpVT.getSizeInBits();
2977 uint64_t InMask = OpVT.getIntegerVTBitMask();
Scott Michelbc5fbc12008-04-30 00:30:08 +00002978 KnownZero |= APInt(OpVTBits, ~InMask, false);
2979 KnownOne |= APInt(OpVTBits, InMask, false);
Scott Michel97872d32008-02-23 18:41:37 +00002980 break;
2981 }
2982
2983#if 0
2984 case EXTRACT_I1_ZEXT:
2985 case EXTRACT_I1_SEXT:
2986 case EXTRACT_I8_ZEXT:
2987 case EXTRACT_I8_SEXT:
2988 case MPY:
2989 case MPYU:
2990 case MPYH:
2991 case MPYHH:
Scott Michelbc5fbc12008-04-30 00:30:08 +00002992 case SPUISD::SHLQUAD_L_BITS:
2993 case SPUISD::SHLQUAD_L_BYTES:
2994 case SPUISD::VEC_SHL:
2995 case SPUISD::VEC_SRL:
2996 case SPUISD::VEC_SRA:
2997 case SPUISD::VEC_ROTL:
2998 case SPUISD::VEC_ROTR:
2999 case SPUISD::ROTQUAD_RZ_BYTES:
3000 case SPUISD::ROTQUAD_RZ_BITS:
3001 case SPUISD::ROTBYTES_RIGHT_S:
3002 case SPUISD::ROTBYTES_LEFT:
3003 case SPUISD::ROTBYTES_LEFT_CHAINED:
Scott Michel67224b22008-06-02 22:18:03 +00003004 case SPUISD::SELECT_MASK:
3005 case SPUISD::SELB:
3006 case SPUISD::FPInterp:
3007 case SPUISD::FPRecipEst:
3008 case SPUISD::SEXT32TO64:
Scott Michel97872d32008-02-23 18:41:37 +00003009#endif
3010 }
Scott Michel8efdca42007-12-04 22:23:35 +00003011}
3012
Scott Michelbc5fbc12008-04-30 00:30:08 +00003013// LowerAsmOperandForConstraint
3014void
Dan Gohman8181bd12008-07-27 21:46:04 +00003015SPUTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Scott Michelbc5fbc12008-04-30 00:30:08 +00003016 char ConstraintLetter,
Dan Gohman8181bd12008-07-27 21:46:04 +00003017 std::vector<SDValue> &Ops,
Scott Michelbc5fbc12008-04-30 00:30:08 +00003018 SelectionDAG &DAG) const {
3019 // Default, for the time being, to the base class handler
3020 TargetLowering::LowerAsmOperandForConstraint(Op, ConstraintLetter, Ops, DAG);
3021}
3022
Scott Michel8efdca42007-12-04 22:23:35 +00003023/// isLegalAddressImmediate - Return true if the integer value can be used
3024/// as the offset of the target addressing mode.
3025bool SPUTargetLowering::isLegalAddressImmediate(int64_t V, const Type *Ty) const {
3026 // SPU's addresses are 256K:
3027 return (V > -(1 << 18) && V < (1 << 18) - 1);
3028}
3029
3030bool SPUTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Scott Michel4ec722e2008-07-16 17:17:29 +00003031 return false;
Scott Michel8efdca42007-12-04 22:23:35 +00003032}