blob: bfdb9e8ac8f9725fbca1c1de69093612138902b5 [file] [log] [blame]
Scott Michel266bc8f2007-12-04 22:23:35 +00001//===-- SPUISelLowering.cpp - Cell SPU DAG Lowering Implementation --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Michel266bc8f2007-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"
17#include "llvm/ADT/VectorExtras.h"
18#include "llvm/Analysis/ScalarEvolutionExpressions.h"
19#include "llvm/CodeGen/CallingConvLower.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000024#include "llvm/CodeGen/SelectionDAG.h"
Scott Michel266bc8f2007-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
40 //! MVT::ValueType mapping to useful data for Cell SPU
41 struct valtype_map_s {
Scott Michel7f9ba9b2008-01-30 02:55:46 +000042 const MVT::ValueType valtype;
43 const int prefslot_byte;
Scott Michel266bc8f2007-12-04 22:23:35 +000044 };
45
46 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
59 const valtype_map_s *getValueTypeMapEntry(MVT::ValueType VT) {
60 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 Michel7f9ba9b2008-01-30 02:55:46 +000064 retval = valtype_map + i;
65 break;
Scott Michel266bc8f2007-12-04 22:23:35 +000066 }
67 }
68
69#ifndef NDEBUG
70 if (retval == 0) {
71 cerr << "getValueTypeMapEntry returns NULL for "
Scott Michel7f9ba9b2008-01-30 02:55:46 +000072 << MVT::getValueTypeString(VT)
73 << "\n";
Scott Michel266bc8f2007-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 Michel9de5d0d2008-01-11 02:53:15 +000085 address, external symbol, constant pool) or an A-form
Scott Michel266bc8f2007-12-04 22:23:35 +000086 address.
87 */
88 bool isMemoryOperand(const SDOperand &Op)
89 {
90 const unsigned Opc = Op.getOpcode();
91 return (Opc == ISD::GlobalAddress
92 || Opc == ISD::GlobalTLSAddress
Scott Michel266bc8f2007-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 Michel266bc8f2007-12-04 22:23:35 +000098 || Opc == ISD::TargetJumpTable
99 || Opc == ISD::TargetConstantPool
100 || Opc == ISD::TargetExternalSymbol
Scott Michel9de5d0d2008-01-11 02:53:15 +0000101 || Opc == SPUISD::AFormAddr);
Scott Michel266bc8f2007-12-04 22:23:35 +0000102 }
Scott Michel58c58182008-01-17 20:38:41 +0000103
104 //! Predicate that returns true if the operand is an indirect target
105 bool isIndirectOperand(const SDOperand &Op)
106 {
107 const unsigned Opc = Op.getOpcode();
108 return (Opc == ISD::Register
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000109 || Opc == SPUISD::LDRESULT);
Scott Michel58c58182008-01-17 20:38:41 +0000110 }
Scott Michel266bc8f2007-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);
123
124 // Set up the SPU's register classes:
125 // NOTE: i8 register class is not registered because we cannot determine when
126 // we need to zero or sign extend for custom-lowered loads and stores.
Scott Michel504c3692007-12-17 22:32:34 +0000127 // NOTE: Ignore the previous note. For now. :-)
128 addRegisterClass(MVT::i8, SPU::R8CRegisterClass);
129 addRegisterClass(MVT::i16, SPU::R16CRegisterClass);
130 addRegisterClass(MVT::i32, SPU::R32CRegisterClass);
131 addRegisterClass(MVT::i64, SPU::R64CRegisterClass);
132 addRegisterClass(MVT::f32, SPU::R32FPRegisterClass);
133 addRegisterClass(MVT::f64, SPU::R64FPRegisterClass);
Scott Michel266bc8f2007-12-04 22:23:35 +0000134 addRegisterClass(MVT::i128, SPU::GPRCRegisterClass);
135
136 // SPU has no sign or zero extended loads for i1, i8, i16:
Scott Michel58c58182008-01-17 20:38:41 +0000137 setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote);
Scott Michel266bc8f2007-12-04 22:23:35 +0000138 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
139 setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote);
Chris Lattnerddf89562008-01-17 19:59:44 +0000140 setTruncStoreAction(MVT::i8, MVT::i1, Custom);
141 setTruncStoreAction(MVT::i16, MVT::i1, Custom);
142 setTruncStoreAction(MVT::i32, MVT::i1, Custom);
143 setTruncStoreAction(MVT::i64, MVT::i1, Custom);
144 setTruncStoreAction(MVT::i128, MVT::i1, Custom);
Scott Michel266bc8f2007-12-04 22:23:35 +0000145
146 setLoadXAction(ISD::EXTLOAD, MVT::i8, Custom);
147 setLoadXAction(ISD::SEXTLOAD, MVT::i8, Custom);
148 setLoadXAction(ISD::ZEXTLOAD, MVT::i8, Custom);
Chris Lattnerddf89562008-01-17 19:59:44 +0000149 setTruncStoreAction(MVT::i8 , MVT::i8, Custom);
150 setTruncStoreAction(MVT::i16 , MVT::i8, Custom);
151 setTruncStoreAction(MVT::i32 , MVT::i8, Custom);
152 setTruncStoreAction(MVT::i64 , MVT::i8, Custom);
153 setTruncStoreAction(MVT::i128, MVT::i8, Custom);
154
Scott Michel266bc8f2007-12-04 22:23:35 +0000155 setLoadXAction(ISD::EXTLOAD, MVT::i16, Custom);
156 setLoadXAction(ISD::SEXTLOAD, MVT::i16, Custom);
157 setLoadXAction(ISD::ZEXTLOAD, MVT::i16, Custom);
158
159 // SPU constant load actions are custom lowered:
160 setOperationAction(ISD::Constant, MVT::i64, Custom);
Nate Begemanccef5802008-02-14 18:43:04 +0000161 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
Scott Michel266bc8f2007-12-04 22:23:35 +0000162 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
163
164 // SPU's loads and stores have to be custom lowered:
165 for (unsigned sctype = (unsigned) MVT::i1; sctype < (unsigned) MVT::f128;
166 ++sctype) {
167 setOperationAction(ISD::LOAD, sctype, Custom);
168 setOperationAction(ISD::STORE, sctype, Custom);
169 }
170
Scott Michel58c58182008-01-17 20:38:41 +0000171 // Custom lower BRCOND for i1, i8 to "promote" the result to
172 // i32 and i16, respectively.
173 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Scott Michel266bc8f2007-12-04 22:23:35 +0000174
175 // Expand the jumptable branches
176 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
177 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
178 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
179
180 // SPU has no intrinsics for these particular operations:
181 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
182 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
183 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
184
185 // PowerPC has no SREM/UREM instructions
186 setOperationAction(ISD::SREM, MVT::i32, Expand);
187 setOperationAction(ISD::UREM, MVT::i32, Expand);
188 setOperationAction(ISD::SREM, MVT::i64, Expand);
189 setOperationAction(ISD::UREM, MVT::i64, Expand);
190
191 // We don't support sin/cos/sqrt/fmod
192 setOperationAction(ISD::FSIN , MVT::f64, Expand);
193 setOperationAction(ISD::FCOS , MVT::f64, Expand);
194 setOperationAction(ISD::FREM , MVT::f64, Expand);
195 setOperationAction(ISD::FSIN , MVT::f32, Expand);
196 setOperationAction(ISD::FCOS , MVT::f32, Expand);
197 setOperationAction(ISD::FREM , MVT::f32, Expand);
198
199 // If we're enabling GP optimizations, use hardware square root
200 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
201 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
202
203 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
204 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
205
206 // SPU can do rotate right and left, so legalize it... but customize for i8
207 // because instructions don't exist.
208 setOperationAction(ISD::ROTR, MVT::i32, Legal);
209 setOperationAction(ISD::ROTR, MVT::i16, Legal);
210 setOperationAction(ISD::ROTR, MVT::i8, Custom);
211 setOperationAction(ISD::ROTL, MVT::i32, Legal);
212 setOperationAction(ISD::ROTL, MVT::i16, Legal);
213 setOperationAction(ISD::ROTL, MVT::i8, Custom);
214 // SPU has no native version of shift left/right for i8
215 setOperationAction(ISD::SHL, MVT::i8, Custom);
216 setOperationAction(ISD::SRL, MVT::i8, Custom);
217 setOperationAction(ISD::SRA, MVT::i8, Custom);
218
219 // Custom lower i32 multiplications
220 setOperationAction(ISD::MUL, MVT::i32, Custom);
221
222 // Need to custom handle (some) common i8 math ops
223 setOperationAction(ISD::SUB, MVT::i8, Custom);
224 setOperationAction(ISD::MUL, MVT::i8, Custom);
225
226 // SPU does not have BSWAP. It does have i32 support CTLZ.
227 // CTPOP has to be custom lowered.
228 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
229 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
230
231 setOperationAction(ISD::CTPOP, MVT::i8, Custom);
232 setOperationAction(ISD::CTPOP, MVT::i16, Custom);
233 setOperationAction(ISD::CTPOP, MVT::i32, Custom);
234 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
235
236 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
237 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
238
239 setOperationAction(ISD::CTLZ , MVT::i32, Legal);
240
241 // SPU does not have select or setcc
242 setOperationAction(ISD::SELECT, MVT::i1, Expand);
243 setOperationAction(ISD::SELECT, MVT::i8, Expand);
244 setOperationAction(ISD::SELECT, MVT::i16, Expand);
245 setOperationAction(ISD::SELECT, MVT::i32, Expand);
246 setOperationAction(ISD::SELECT, MVT::i64, Expand);
247 setOperationAction(ISD::SELECT, MVT::f32, Expand);
248 setOperationAction(ISD::SELECT, MVT::f64, Expand);
249
250 setOperationAction(ISD::SETCC, MVT::i1, Expand);
251 setOperationAction(ISD::SETCC, MVT::i8, Expand);
252 setOperationAction(ISD::SETCC, MVT::i16, Expand);
253 setOperationAction(ISD::SETCC, MVT::i32, Expand);
254 setOperationAction(ISD::SETCC, MVT::i64, Expand);
255 setOperationAction(ISD::SETCC, MVT::f32, Expand);
256 setOperationAction(ISD::SETCC, MVT::f64, Expand);
257
258 // SPU has a legal FP -> signed INT instruction
259 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Legal);
260 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
261 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Legal);
262 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
263
264 // FDIV on SPU requires custom lowering
265 setOperationAction(ISD::FDIV, MVT::f32, Custom);
266 //setOperationAction(ISD::FDIV, MVT::f64, Custom);
267
268 // SPU has [U|S]INT_TO_FP
269 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Legal);
270 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
271 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
272 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Legal);
273 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
274 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
275 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
276 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
277
Scott Michel86c041f2007-12-20 00:44:13 +0000278 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Legal);
279 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Legal);
280 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Legal);
281 setOperationAction(ISD::BIT_CONVERT, MVT::f64, Legal);
Scott Michel266bc8f2007-12-04 22:23:35 +0000282
283 // We cannot sextinreg(i1). Expand to shifts.
284 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
285
286 // Support label based line numbers.
287 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
288 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
289
290 // We want to legalize GlobalAddress and ConstantPool nodes into the
291 // appropriate instructions to materialize the address.
Scott Michel053c1da2008-01-29 02:16:57 +0000292 for (unsigned sctype = (unsigned) MVT::i1; sctype < (unsigned) MVT::f128;
293 ++sctype) {
294 setOperationAction(ISD::GlobalAddress, sctype, Custom);
295 setOperationAction(ISD::ConstantPool, sctype, Custom);
296 setOperationAction(ISD::JumpTable, sctype, Custom);
297 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000298
299 // RET must be custom lowered, to meet ABI requirements
300 setOperationAction(ISD::RET, MVT::Other, Custom);
301
302 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
303 setOperationAction(ISD::VASTART , MVT::Other, Custom);
304
305 // Use the default implementation.
306 setOperationAction(ISD::VAARG , MVT::Other, Expand);
307 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
308 setOperationAction(ISD::VAEND , MVT::Other, Expand);
309 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
310 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
311 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
312 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Expand);
313
314 // Cell SPU has instructions for converting between i64 and fp.
315 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
316 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
317
318 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
319 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
320
321 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
322 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
323
324 // First set operation action for all vector types to expand. Then we
325 // will selectively turn on ones that can be effectively codegen'd.
326 addRegisterClass(MVT::v16i8, SPU::VECREGRegisterClass);
327 addRegisterClass(MVT::v8i16, SPU::VECREGRegisterClass);
328 addRegisterClass(MVT::v4i32, SPU::VECREGRegisterClass);
329 addRegisterClass(MVT::v2i64, SPU::VECREGRegisterClass);
330 addRegisterClass(MVT::v4f32, SPU::VECREGRegisterClass);
331 addRegisterClass(MVT::v2f64, SPU::VECREGRegisterClass);
332
333 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
334 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
335 // add/sub are legal for all supported vector VT's.
336 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Legal);
337 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Legal);
338 // mul has to be custom lowered.
339 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Custom);
340
341 setOperationAction(ISD::AND , (MVT::ValueType)VT, Legal);
342 setOperationAction(ISD::OR , (MVT::ValueType)VT, Legal);
343 setOperationAction(ISD::XOR , (MVT::ValueType)VT, Legal);
344 setOperationAction(ISD::LOAD , (MVT::ValueType)VT, Legal);
345 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Legal);
346 setOperationAction(ISD::STORE, (MVT::ValueType)VT, Legal);
347
348 // These operations need to be expanded:
349 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
350 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
351 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
352 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
353 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Custom);
354
355 // Custom lower build_vector, constant pool spills, insert and
356 // extract vector elements:
357 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
358 setOperationAction(ISD::ConstantPool, (MVT::ValueType)VT, Custom);
359 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Custom);
360 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
361 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
362 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
363 }
364
365 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
366 setOperationAction(ISD::AND, MVT::v16i8, Custom);
367 setOperationAction(ISD::OR, MVT::v16i8, Custom);
368 setOperationAction(ISD::XOR, MVT::v16i8, Custom);
369 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000370
Scott Michel266bc8f2007-12-04 22:23:35 +0000371 setSetCCResultType(MVT::i32);
372 setShiftAmountType(MVT::i32);
373 setSetCCResultContents(ZeroOrOneSetCCResult);
374
375 setStackPointerRegisterToSaveRestore(SPU::R1);
376
377 // We have target-specific dag combine patterns for the following nodes:
Scott Michel053c1da2008-01-29 02:16:57 +0000378 setTargetDAGCombine(ISD::ADD);
Scott Michel266bc8f2007-12-04 22:23:35 +0000379
380 computeRegisterProperties();
381}
382
383const char *
384SPUTargetLowering::getTargetNodeName(unsigned Opcode) const
385{
386 if (node_names.empty()) {
387 node_names[(unsigned) SPUISD::RET_FLAG] = "SPUISD::RET_FLAG";
388 node_names[(unsigned) SPUISD::Hi] = "SPUISD::Hi";
389 node_names[(unsigned) SPUISD::Lo] = "SPUISD::Lo";
390 node_names[(unsigned) SPUISD::PCRelAddr] = "SPUISD::PCRelAddr";
Scott Michel9de5d0d2008-01-11 02:53:15 +0000391 node_names[(unsigned) SPUISD::AFormAddr] = "SPUISD::AFormAddr";
Scott Michel053c1da2008-01-29 02:16:57 +0000392 node_names[(unsigned) SPUISD::IndirectAddr] = "SPUISD::IndirectAddr";
Scott Michel266bc8f2007-12-04 22:23:35 +0000393 node_names[(unsigned) SPUISD::LDRESULT] = "SPUISD::LDRESULT";
394 node_names[(unsigned) SPUISD::CALL] = "SPUISD::CALL";
395 node_names[(unsigned) SPUISD::SHUFB] = "SPUISD::SHUFB";
396 node_names[(unsigned) SPUISD::INSERT_MASK] = "SPUISD::INSERT_MASK";
397 node_names[(unsigned) SPUISD::CNTB] = "SPUISD::CNTB";
398 node_names[(unsigned) SPUISD::PROMOTE_SCALAR] = "SPUISD::PROMOTE_SCALAR";
399 node_names[(unsigned) SPUISD::EXTRACT_ELT0] = "SPUISD::EXTRACT_ELT0";
400 node_names[(unsigned) SPUISD::EXTRACT_ELT0_CHAINED] = "SPUISD::EXTRACT_ELT0_CHAINED";
401 node_names[(unsigned) SPUISD::EXTRACT_I1_ZEXT] = "SPUISD::EXTRACT_I1_ZEXT";
402 node_names[(unsigned) SPUISD::EXTRACT_I1_SEXT] = "SPUISD::EXTRACT_I1_SEXT";
403 node_names[(unsigned) SPUISD::EXTRACT_I8_ZEXT] = "SPUISD::EXTRACT_I8_ZEXT";
404 node_names[(unsigned) SPUISD::EXTRACT_I8_SEXT] = "SPUISD::EXTRACT_I8_SEXT";
405 node_names[(unsigned) SPUISD::MPY] = "SPUISD::MPY";
406 node_names[(unsigned) SPUISD::MPYU] = "SPUISD::MPYU";
407 node_names[(unsigned) SPUISD::MPYH] = "SPUISD::MPYH";
408 node_names[(unsigned) SPUISD::MPYHH] = "SPUISD::MPYHH";
409 node_names[(unsigned) SPUISD::VEC_SHL] = "SPUISD::VEC_SHL";
410 node_names[(unsigned) SPUISD::VEC_SRL] = "SPUISD::VEC_SRL";
411 node_names[(unsigned) SPUISD::VEC_SRA] = "SPUISD::VEC_SRA";
412 node_names[(unsigned) SPUISD::VEC_ROTL] = "SPUISD::VEC_ROTL";
413 node_names[(unsigned) SPUISD::VEC_ROTR] = "SPUISD::VEC_ROTR";
414 node_names[(unsigned) SPUISD::ROTBYTES_RIGHT_Z] =
415 "SPUISD::ROTBYTES_RIGHT_Z";
416 node_names[(unsigned) SPUISD::ROTBYTES_RIGHT_S] =
417 "SPUISD::ROTBYTES_RIGHT_S";
418 node_names[(unsigned) SPUISD::ROTBYTES_LEFT] = "SPUISD::ROTBYTES_LEFT";
419 node_names[(unsigned) SPUISD::ROTBYTES_LEFT_CHAINED] =
420 "SPUISD::ROTBYTES_LEFT_CHAINED";
421 node_names[(unsigned) SPUISD::FSMBI] = "SPUISD::FSMBI";
422 node_names[(unsigned) SPUISD::SELB] = "SPUISD::SELB";
Scott Michel266bc8f2007-12-04 22:23:35 +0000423 node_names[(unsigned) SPUISD::FPInterp] = "SPUISD::FPInterp";
424 node_names[(unsigned) SPUISD::FPRecipEst] = "SPUISD::FPRecipEst";
425 node_names[(unsigned) SPUISD::SEXT32TO64] = "SPUISD::SEXT32TO64";
426 }
427
428 std::map<unsigned, const char *>::iterator i = node_names.find(Opcode);
429
430 return ((i != node_names.end()) ? i->second : 0);
431}
432
433//===----------------------------------------------------------------------===//
434// Calling convention code:
435//===----------------------------------------------------------------------===//
436
437#include "SPUGenCallingConv.inc"
438
439//===----------------------------------------------------------------------===//
440// LowerOperation implementation
441//===----------------------------------------------------------------------===//
442
Scott Michel9de5d0d2008-01-11 02:53:15 +0000443/// Aligned load common code for CellSPU
444/*!
445 \param[in] Op The SelectionDAG load or store operand
446 \param[in] DAG The selection DAG
447 \param[in] ST CellSPU subtarget information structure
448 \param[in,out] alignment Caller initializes this to the load or store node's
449 value from getAlignment(), may be updated while generating the aligned load
450 \param[in,out] alignOffs Aligned offset; set by AlignedLoad to the aligned
451 offset (divisible by 16, modulo 16 == 0)
452 \param[in,out] prefSlotOffs Preferred slot offset; set by AlignedLoad to the
453 offset of the preferred slot (modulo 16 != 0)
454 \param[in,out] VT Caller initializes this value type to the the load or store
455 node's loaded or stored value type; may be updated if an i1-extended load or
456 store.
457 \param[out] was16aligned true if the base pointer had 16-byte alignment,
458 otherwise false. Can help to determine if the chunk needs to be rotated.
459
460 Both load and store lowering load a block of data aligned on a 16-byte
461 boundary. This is the common aligned load code shared between both.
462 */
463static SDOperand
464AlignedLoad(SDOperand Op, SelectionDAG &DAG, const SPUSubtarget *ST,
465 LSBaseSDNode *LSN,
466 unsigned &alignment, int &alignOffs, int &prefSlotOffs,
Chris Lattner3f732802008-01-12 22:54:07 +0000467 MVT::ValueType &VT, bool &was16aligned)
Scott Michel9de5d0d2008-01-11 02:53:15 +0000468{
469 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
470 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
471 SDOperand basePtr = LSN->getBasePtr();
472 SDOperand chain = LSN->getChain();
473
474 if (basePtr.getOpcode() == ISD::ADD) {
475 SDOperand Op1 = basePtr.Val->getOperand(1);
476
477 if (Op1.getOpcode() == ISD::Constant || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel58c58182008-01-17 20:38:41 +0000478 const ConstantSDNode *CN = cast<ConstantSDNode>(basePtr.getOperand(1));
Scott Michel9de5d0d2008-01-11 02:53:15 +0000479
480 alignOffs = (int) CN->getValue();
481 prefSlotOffs = (int) (alignOffs & 0xf);
482
483 // Adjust the rotation amount to ensure that the final result ends up in
484 // the preferred slot:
485 prefSlotOffs -= vtm->prefslot_byte;
486 basePtr = basePtr.getOperand(0);
487
Scott Michel58c58182008-01-17 20:38:41 +0000488 // Loading from memory, can we adjust alignment?
489 if (basePtr.getOpcode() == SPUISD::AFormAddr) {
490 SDOperand APtr = basePtr.getOperand(0);
491 if (APtr.getOpcode() == ISD::TargetGlobalAddress) {
492 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(APtr);
493 alignment = GSDN->getGlobal()->getAlignment();
494 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000495 }
496 } else {
497 alignOffs = 0;
498 prefSlotOffs = -vtm->prefslot_byte;
499 }
500 } else {
501 alignOffs = 0;
502 prefSlotOffs = -vtm->prefslot_byte;
503 }
504
505 if (alignment == 16) {
506 // Realign the base pointer as a D-Form address:
507 if (!isMemoryOperand(basePtr) || (alignOffs & ~0xf) != 0) {
Scott Michel58c58182008-01-17 20:38:41 +0000508 basePtr = DAG.getNode(ISD::ADD, PtrVT,
509 basePtr,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000510 DAG.getConstant((alignOffs & ~0xf), PtrVT));
Scott Michel9de5d0d2008-01-11 02:53:15 +0000511 }
512
513 // Emit the vector load:
514 was16aligned = true;
515 return DAG.getLoad(MVT::v16i8, chain, basePtr,
516 LSN->getSrcValue(), LSN->getSrcValueOffset(),
517 LSN->isVolatile(), 16);
518 }
519
520 // Unaligned load or we're using the "large memory" model, which means that
521 // we have to be very pessimistic:
Scott Michel58c58182008-01-17 20:38:41 +0000522 if (isMemoryOperand(basePtr) || isIndirectOperand(basePtr)) {
Scott Michel053c1da2008-01-29 02:16:57 +0000523 basePtr = DAG.getNode(SPUISD::IndirectAddr, PtrVT, basePtr, DAG.getConstant(0, PtrVT));
Scott Michel9de5d0d2008-01-11 02:53:15 +0000524 }
525
526 // Add the offset
Scott Michel053c1da2008-01-29 02:16:57 +0000527 basePtr = DAG.getNode(ISD::ADD, PtrVT, basePtr,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000528 DAG.getConstant((alignOffs & ~0xf), PtrVT));
Scott Michel9de5d0d2008-01-11 02:53:15 +0000529 was16aligned = false;
530 return DAG.getLoad(MVT::v16i8, chain, basePtr,
531 LSN->getSrcValue(), LSN->getSrcValueOffset(),
532 LSN->isVolatile(), 16);
533}
534
Scott Michel266bc8f2007-12-04 22:23:35 +0000535/// Custom lower loads for CellSPU
536/*!
537 All CellSPU loads and stores are aligned to 16-byte boundaries, so for elements
538 within a 16-byte block, we have to rotate to extract the requested element.
539 */
540static SDOperand
541LowerLOAD(SDOperand Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
542 LoadSDNode *LN = cast<LoadSDNode>(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000543 SDOperand the_chain = LN->getChain();
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000544 MVT::ValueType VT = LN->getMemoryVT();
Scott Michel266bc8f2007-12-04 22:23:35 +0000545 MVT::ValueType OpVT = Op.Val->getValueType(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000546 ISD::LoadExtType ExtType = LN->getExtensionType();
547 unsigned alignment = LN->getAlignment();
Scott Michel266bc8f2007-12-04 22:23:35 +0000548 SDOperand Ops[8];
549
Scott Michel266bc8f2007-12-04 22:23:35 +0000550 switch (LN->getAddressingMode()) {
551 case ISD::UNINDEXED: {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000552 int offset, rotamt;
553 bool was16aligned;
554 SDOperand result =
555 AlignedLoad(Op, DAG, ST, LN,alignment, offset, rotamt, VT, was16aligned);
Scott Michel266bc8f2007-12-04 22:23:35 +0000556
Scott Michel9de5d0d2008-01-11 02:53:15 +0000557 if (result.Val == 0)
Scott Michel266bc8f2007-12-04 22:23:35 +0000558 return result;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000559
560 the_chain = result.getValue(1);
561 // Rotate the chunk if necessary
562 if (rotamt < 0)
563 rotamt += 16;
Scott Michel497e8882008-01-11 21:01:19 +0000564 if (rotamt != 0 || !was16aligned) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000565 SDVTList vecvts = DAG.getVTList(MVT::v16i8, MVT::Other);
566
Scott Michel58c58182008-01-17 20:38:41 +0000567 Ops[0] = the_chain;
568 Ops[1] = result;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000569 if (was16aligned) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000570 Ops[2] = DAG.getConstant(rotamt, MVT::i16);
571 } else {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000572 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel9de5d0d2008-01-11 02:53:15 +0000573 LoadSDNode *LN1 = cast<LoadSDNode>(result);
Scott Michel497e8882008-01-11 21:01:19 +0000574 Ops[2] = DAG.getNode(ISD::ADD, PtrVT, LN1->getBasePtr(),
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000575 DAG.getConstant(rotamt, PtrVT));
Scott Michel9de5d0d2008-01-11 02:53:15 +0000576 }
577
578 result = DAG.getNode(SPUISD::ROTBYTES_LEFT_CHAINED, vecvts, Ops, 3);
579 the_chain = result.getValue(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000580 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000581
582 if (VT == OpVT || ExtType == ISD::EXTLOAD) {
583 SDVTList scalarvts;
584 MVT::ValueType vecVT = MVT::v16i8;
585
586 // Convert the loaded v16i8 vector to the appropriate vector type
587 // specified by the operand:
588 if (OpVT == VT) {
589 if (VT != MVT::i1)
590 vecVT = MVT::getVectorType(VT, (128 / MVT::getSizeInBits(VT)));
591 } else
592 vecVT = MVT::getVectorType(OpVT, (128 / MVT::getSizeInBits(OpVT)));
593
594 Ops[0] = the_chain;
595 Ops[1] = DAG.getNode(ISD::BIT_CONVERT, vecVT, result);
596 scalarvts = DAG.getVTList((OpVT == VT ? VT : OpVT), MVT::Other);
597 result = DAG.getNode(SPUISD::EXTRACT_ELT0_CHAINED, scalarvts, Ops, 2);
598 the_chain = result.getValue(1);
599 } else {
600 // Handle the sign and zero-extending loads for i1 and i8:
601 unsigned NewOpC;
602
603 if (ExtType == ISD::SEXTLOAD) {
604 NewOpC = (OpVT == MVT::i1
605 ? SPUISD::EXTRACT_I1_SEXT
606 : SPUISD::EXTRACT_I8_SEXT);
607 } else {
608 assert(ExtType == ISD::ZEXTLOAD);
609 NewOpC = (OpVT == MVT::i1
610 ? SPUISD::EXTRACT_I1_ZEXT
611 : SPUISD::EXTRACT_I8_ZEXT);
612 }
613
614 result = DAG.getNode(NewOpC, OpVT, result);
615 }
616
617 SDVTList retvts = DAG.getVTList(OpVT, MVT::Other);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000618 SDOperand retops[2] = {
Scott Michel58c58182008-01-17 20:38:41 +0000619 result,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000620 the_chain
Scott Michel58c58182008-01-17 20:38:41 +0000621 };
Scott Michel9de5d0d2008-01-11 02:53:15 +0000622
Scott Michel58c58182008-01-17 20:38:41 +0000623 result = DAG.getNode(SPUISD::LDRESULT, retvts,
624 retops, sizeof(retops) / sizeof(retops[0]));
Scott Michel9de5d0d2008-01-11 02:53:15 +0000625 return result;
Scott Michel266bc8f2007-12-04 22:23:35 +0000626 }
627 case ISD::PRE_INC:
628 case ISD::PRE_DEC:
629 case ISD::POST_INC:
630 case ISD::POST_DEC:
631 case ISD::LAST_INDEXED_MODE:
632 cerr << "LowerLOAD: Got a LoadSDNode with an addr mode other than "
633 "UNINDEXED\n";
634 cerr << (unsigned) LN->getAddressingMode() << "\n";
635 abort();
636 /*NOTREACHED*/
637 }
638
639 return SDOperand();
640}
641
642/// Custom lower stores for CellSPU
643/*!
644 All CellSPU stores are aligned to 16-byte boundaries, so for elements
645 within a 16-byte block, we have to generate a shuffle to insert the
646 requested element into its place, then store the resulting block.
647 */
648static SDOperand
649LowerSTORE(SDOperand Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
650 StoreSDNode *SN = cast<StoreSDNode>(Op);
651 SDOperand Value = SN->getValue();
652 MVT::ValueType VT = Value.getValueType();
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000653 MVT::ValueType StVT = (!SN->isTruncatingStore() ? VT : SN->getMemoryVT());
Scott Michel266bc8f2007-12-04 22:23:35 +0000654 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel9de5d0d2008-01-11 02:53:15 +0000655 unsigned alignment = SN->getAlignment();
Scott Michel266bc8f2007-12-04 22:23:35 +0000656
657 switch (SN->getAddressingMode()) {
658 case ISD::UNINDEXED: {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000659 int chunk_offset, slot_offset;
660 bool was16aligned;
Scott Michel266bc8f2007-12-04 22:23:35 +0000661
662 // The vector type we really want to load from the 16-byte chunk, except
663 // in the case of MVT::i1, which has to be v16i8.
Scott Michel9de5d0d2008-01-11 02:53:15 +0000664 unsigned vecVT, stVecVT = MVT::v16i8;
665
Scott Michel266bc8f2007-12-04 22:23:35 +0000666 if (StVT != MVT::i1)
667 stVecVT = MVT::getVectorType(StVT, (128 / MVT::getSizeInBits(StVT)));
Scott Michel266bc8f2007-12-04 22:23:35 +0000668 vecVT = MVT::getVectorType(VT, (128 / MVT::getSizeInBits(VT)));
669
Scott Michel9de5d0d2008-01-11 02:53:15 +0000670 SDOperand alignLoadVec =
671 AlignedLoad(Op, DAG, ST, SN, alignment,
672 chunk_offset, slot_offset, VT, was16aligned);
Scott Michel266bc8f2007-12-04 22:23:35 +0000673
Scott Michel9de5d0d2008-01-11 02:53:15 +0000674 if (alignLoadVec.Val == 0)
675 return alignLoadVec;
Scott Michel266bc8f2007-12-04 22:23:35 +0000676
Scott Michel9de5d0d2008-01-11 02:53:15 +0000677 LoadSDNode *LN = cast<LoadSDNode>(alignLoadVec);
678 SDOperand basePtr = LN->getBasePtr();
679 SDOperand the_chain = alignLoadVec.getValue(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000680 SDOperand theValue = SN->getValue();
681 SDOperand result;
682
683 if (StVT != VT
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000684 && (theValue.getOpcode() == ISD::AssertZext
685 || theValue.getOpcode() == ISD::AssertSext)) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000686 // Drill down and get the value for zero- and sign-extended
687 // quantities
688 theValue = theValue.getOperand(0);
689 }
690
Scott Michel9de5d0d2008-01-11 02:53:15 +0000691 chunk_offset &= 0xf;
Scott Michel266bc8f2007-12-04 22:23:35 +0000692
Scott Michel9de5d0d2008-01-11 02:53:15 +0000693 SDOperand insertEltOffs = DAG.getConstant(chunk_offset, PtrVT);
694 SDOperand insertEltPtr;
695 SDOperand insertEltOp;
696
697 // If the base pointer is already a D-form address, then just create
698 // a new D-form address with a slot offset and the orignal base pointer.
699 // Otherwise generate a D-form address with the slot offset relative
700 // to the stack pointer, which is always aligned.
Scott Michel497e8882008-01-11 21:01:19 +0000701 DEBUG(cerr << "CellSPU LowerSTORE: basePtr = ");
702 DEBUG(basePtr.Val->dump(&DAG));
703 DEBUG(cerr << "\n");
704
Scott Michel053c1da2008-01-29 02:16:57 +0000705 if (basePtr.getOpcode() == SPUISD::IndirectAddr ||
706 (basePtr.getOpcode() == ISD::ADD
707 && basePtr.getOperand(0).getOpcode() == SPUISD::IndirectAddr)) {
Scott Michel497e8882008-01-11 21:01:19 +0000708 insertEltPtr = basePtr;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000709 } else {
Scott Michel053c1da2008-01-29 02:16:57 +0000710#if 0
711 // $sp is always aligned, so use it when necessary to avoid loading
712 // an address
713 SDOperand ptrP =
714 basePtr.Val->hasOneUse() ? DAG.getRegister(SPU::R1, PtrVT) : basePtr;
715 insertEltPtr = DAG.getNode(ISD::ADD, PtrVT, ptrP, insertEltOffs);
716#else
717 insertEltPtr = DAG.getNode(ISD::ADD, PtrVT, basePtr, insertEltOffs);
718#endif
Scott Michel9de5d0d2008-01-11 02:53:15 +0000719 }
720
721 insertEltOp = DAG.getNode(SPUISD::INSERT_MASK, stVecVT, insertEltPtr);
Scott Michel266bc8f2007-12-04 22:23:35 +0000722 result = DAG.getNode(SPUISD::SHUFB, vecVT,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000723 DAG.getNode(ISD::SCALAR_TO_VECTOR, vecVT, theValue),
724 alignLoadVec,
725 DAG.getNode(ISD::BIT_CONVERT, vecVT, insertEltOp));
Scott Michel266bc8f2007-12-04 22:23:35 +0000726
Scott Michel9de5d0d2008-01-11 02:53:15 +0000727 result = DAG.getStore(the_chain, result, basePtr,
Scott Michel266bc8f2007-12-04 22:23:35 +0000728 LN->getSrcValue(), LN->getSrcValueOffset(),
729 LN->isVolatile(), LN->getAlignment());
730
731 return result;
732 /*UNREACHED*/
733 }
734 case ISD::PRE_INC:
735 case ISD::PRE_DEC:
736 case ISD::POST_INC:
737 case ISD::POST_DEC:
738 case ISD::LAST_INDEXED_MODE:
739 cerr << "LowerLOAD: Got a LoadSDNode with an addr mode other than "
740 "UNINDEXED\n";
741 cerr << (unsigned) SN->getAddressingMode() << "\n";
742 abort();
743 /*NOTREACHED*/
744 }
745
746 return SDOperand();
747}
748
749/// Generate the address of a constant pool entry.
750static SDOperand
751LowerConstantPool(SDOperand Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
752 MVT::ValueType PtrVT = Op.getValueType();
753 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
754 Constant *C = CP->getConstVal();
755 SDOperand CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
Scott Michel266bc8f2007-12-04 22:23:35 +0000756 SDOperand Zero = DAG.getConstant(0, PtrVT);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000757 const TargetMachine &TM = DAG.getTarget();
Scott Michel266bc8f2007-12-04 22:23:35 +0000758
759 if (TM.getRelocationModel() == Reloc::Static) {
760 if (!ST->usingLargeMem()) {
761 // Just return the SDOperand with the constant pool address in it.
Scott Michel58c58182008-01-17 20:38:41 +0000762 return DAG.getNode(SPUISD::AFormAddr, PtrVT, CPI, Zero);
Scott Michel266bc8f2007-12-04 22:23:35 +0000763 } else {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000764#if 1
Scott Michel266bc8f2007-12-04 22:23:35 +0000765 SDOperand Hi = DAG.getNode(SPUISD::Hi, PtrVT, CPI, Zero);
766 SDOperand Lo = DAG.getNode(SPUISD::Lo, PtrVT, CPI, Zero);
767
768 return DAG.getNode(ISD::ADD, PtrVT, Lo, Hi);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000769#else
Scott Michel053c1da2008-01-29 02:16:57 +0000770 return DAG.getNode(SPUISD::IndirectAddr, PtrVT, CPI, Zero);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000771#endif
Scott Michel266bc8f2007-12-04 22:23:35 +0000772 }
773 }
774
775 assert(0 &&
776 "LowerConstantPool: Relocation model other than static not supported.");
777 return SDOperand();
778}
779
780static SDOperand
781LowerJumpTable(SDOperand Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
782 MVT::ValueType PtrVT = Op.getValueType();
783 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
784 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
785 SDOperand Zero = DAG.getConstant(0, PtrVT);
786 const TargetMachine &TM = DAG.getTarget();
787
788 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michel053c1da2008-01-29 02:16:57 +0000789 SDOperand JmpAForm = DAG.getNode(SPUISD::AFormAddr, PtrVT, JTI, Zero);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000790 return (!ST->usingLargeMem()
Scott Michel053c1da2008-01-29 02:16:57 +0000791 ? JmpAForm
792 : DAG.getNode(SPUISD::IndirectAddr, PtrVT, JmpAForm, Zero));
Scott Michel266bc8f2007-12-04 22:23:35 +0000793 }
794
795 assert(0 &&
796 "LowerJumpTable: Relocation model other than static not supported.");
797 return SDOperand();
798}
799
800static SDOperand
801LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
802 MVT::ValueType PtrVT = Op.getValueType();
803 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
804 GlobalValue *GV = GSDN->getGlobal();
805 SDOperand GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
Scott Michel266bc8f2007-12-04 22:23:35 +0000806 const TargetMachine &TM = DAG.getTarget();
Scott Michel9de5d0d2008-01-11 02:53:15 +0000807 SDOperand Zero = DAG.getConstant(0, PtrVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000808
809 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michel053c1da2008-01-29 02:16:57 +0000810 if (!ST->usingLargeMem()) {
811 return DAG.getNode(SPUISD::AFormAddr, PtrVT, GA, Zero);
812 } else {
813 SDOperand Hi = DAG.getNode(SPUISD::Hi, PtrVT, GA, Zero);
814 SDOperand Lo = DAG.getNode(SPUISD::Lo, PtrVT, GA, Zero);
815 return DAG.getNode(SPUISD::IndirectAddr, PtrVT, Hi, Lo);
816 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000817 } else {
818 cerr << "LowerGlobalAddress: Relocation model other than static not "
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000819 << "supported.\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000820 abort();
821 /*NOTREACHED*/
822 }
823
824 return SDOperand();
825}
826
827//! Custom lower i64 integer constants
828/*!
829 This code inserts all of the necessary juggling that needs to occur to load
830 a 64-bit constant into a register.
831 */
832static SDOperand
833LowerConstant(SDOperand Op, SelectionDAG &DAG) {
834 unsigned VT = Op.getValueType();
835 ConstantSDNode *CN = cast<ConstantSDNode>(Op.Val);
836
837 if (VT == MVT::i64) {
838 SDOperand T = DAG.getConstant(CN->getValue(), MVT::i64);
839 return DAG.getNode(SPUISD::EXTRACT_ELT0, VT,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000840 DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i64, T, T));
Scott Michel266bc8f2007-12-04 22:23:35 +0000841
842 } else {
843 cerr << "LowerConstant: unhandled constant type "
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000844 << MVT::getValueTypeString(VT)
845 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000846 abort();
847 /*NOTREACHED*/
848 }
849
850 return SDOperand();
851}
852
Nate Begemanccef5802008-02-14 18:43:04 +0000853//! Custom lower double precision floating point constants
Scott Michel266bc8f2007-12-04 22:23:35 +0000854static SDOperand
855LowerConstantFP(SDOperand Op, SelectionDAG &DAG) {
856 unsigned VT = Op.getValueType();
857 ConstantFPSDNode *FP = cast<ConstantFPSDNode>(Op.Val);
858
859 assert((FP != 0) &&
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000860 "LowerConstantFP: Node is not ConstantFPSDNode");
Scott Michel266bc8f2007-12-04 22:23:35 +0000861
Nate Begemanccef5802008-02-14 18:43:04 +0000862 if (VT == MVT::f64) {
Scott Michel170783a2007-12-19 20:15:47 +0000863 uint64_t dbits = DoubleToBits(FP->getValueAPF().convertToDouble());
Scott Michel266bc8f2007-12-04 22:23:35 +0000864 return DAG.getNode(ISD::BIT_CONVERT, VT,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000865 LowerConstant(DAG.getConstant(dbits, MVT::i64), DAG));
Scott Michel266bc8f2007-12-04 22:23:35 +0000866 }
867
868 return SDOperand();
869}
870
Scott Michel58c58182008-01-17 20:38:41 +0000871//! Lower MVT::i1, MVT::i8 brcond to a promoted type (MVT::i32, MVT::i16)
872static SDOperand
873LowerBRCOND(SDOperand Op, SelectionDAG &DAG)
874{
875 SDOperand Cond = Op.getOperand(1);
876 MVT::ValueType CondVT = Cond.getValueType();
877 MVT::ValueType CondNVT;
878
879 if (CondVT == MVT::i1 || CondVT == MVT::i8) {
880 CondNVT = (CondVT == MVT::i1 ? MVT::i32 : MVT::i16);
881 return DAG.getNode(ISD::BRCOND, Op.getValueType(),
882 Op.getOperand(0),
883 DAG.getNode(ISD::ZERO_EXTEND, CondNVT, Op.getOperand(1)),
884 Op.getOperand(2));
885 } else
886 return SDOperand(); // Unchanged
887}
888
Scott Michel266bc8f2007-12-04 22:23:35 +0000889static SDOperand
890LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG, int &VarArgsFrameIndex)
891{
892 MachineFunction &MF = DAG.getMachineFunction();
893 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +0000894 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +0000895 SmallVector<SDOperand, 8> ArgValues;
896 SDOperand Root = Op.getOperand(0);
897 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
898
899 const unsigned *ArgRegs = SPURegisterInfo::getArgRegs();
900 const unsigned NumArgRegs = SPURegisterInfo::getNumArgRegs();
901
902 unsigned ArgOffset = SPUFrameInfo::minStackSize();
903 unsigned ArgRegIdx = 0;
904 unsigned StackSlotSize = SPUFrameInfo::stackSlotSize();
905
906 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
907
908 // Add DAG nodes to load the arguments or copy them out of registers.
909 for (unsigned ArgNo = 0, e = Op.Val->getNumValues()-1; ArgNo != e; ++ArgNo) {
910 SDOperand ArgVal;
911 bool needsLoad = false;
912 MVT::ValueType ObjectVT = Op.getValue(ArgNo).getValueType();
913 unsigned ObjSize = MVT::getSizeInBits(ObjectVT)/8;
914
915 switch (ObjectVT) {
916 default: {
917 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000918 << MVT::getValueTypeString(ObjectVT)
Scott Michel266bc8f2007-12-04 22:23:35 +0000919 << "\n";
920 abort();
921 }
922 case MVT::i8:
923 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000924 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R8CRegClass);
925 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel266bc8f2007-12-04 22:23:35 +0000926 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i8);
927 ++ArgRegIdx;
928 } else {
929 needsLoad = true;
930 }
931 break;
932 case MVT::i16:
933 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000934 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R16CRegClass);
935 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel266bc8f2007-12-04 22:23:35 +0000936 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i16);
937 ++ArgRegIdx;
938 } else {
939 needsLoad = true;
940 }
941 break;
942 case MVT::i32:
943 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000944 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
945 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel266bc8f2007-12-04 22:23:35 +0000946 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i32);
947 ++ArgRegIdx;
948 } else {
949 needsLoad = true;
950 }
951 break;
952 case MVT::i64:
953 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000954 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R64CRegClass);
955 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel266bc8f2007-12-04 22:23:35 +0000956 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::i64);
957 ++ArgRegIdx;
958 } else {
959 needsLoad = true;
960 }
961 break;
962 case MVT::f32:
963 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000964 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32FPRegClass);
965 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel266bc8f2007-12-04 22:23:35 +0000966 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::f32);
967 ++ArgRegIdx;
968 } else {
969 needsLoad = true;
970 }
971 break;
972 case MVT::f64:
973 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000974 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R64FPRegClass);
975 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel266bc8f2007-12-04 22:23:35 +0000976 ArgVal = DAG.getCopyFromReg(Root, VReg, MVT::f64);
977 ++ArgRegIdx;
978 } else {
979 needsLoad = true;
980 }
981 break;
982 case MVT::v2f64:
983 case MVT::v4f32:
984 case MVT::v4i32:
985 case MVT::v8i16:
986 case MVT::v16i8:
987 if (!isVarArg && ArgRegIdx < NumArgRegs) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000988 unsigned VReg = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
989 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel266bc8f2007-12-04 22:23:35 +0000990 ArgVal = DAG.getCopyFromReg(Root, VReg, ObjectVT);
991 ++ArgRegIdx;
992 } else {
993 needsLoad = true;
994 }
995 break;
996 }
997
998 // We need to load the argument to a virtual register if we determined above
999 // that we ran out of physical registers of the appropriate type
1000 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00001001 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1002 SDOperand FIN = DAG.getFrameIndex(FI, PtrVT);
1003 ArgVal = DAG.getLoad(ObjectVT, Root, FIN, NULL, 0);
Scott Michel266bc8f2007-12-04 22:23:35 +00001004 ArgOffset += StackSlotSize;
1005 }
1006
1007 ArgValues.push_back(ArgVal);
1008 }
1009
1010 // If the function takes variable number of arguments, make a frame index for
1011 // the start of the first vararg value... for expansion of llvm.va_start.
1012 if (isVarArg) {
1013 VarArgsFrameIndex = MFI->CreateFixedObject(MVT::getSizeInBits(PtrVT)/8,
1014 ArgOffset);
1015 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
1016 // If this function is vararg, store any remaining integer argument regs to
1017 // their spots on the stack so that they may be loaded by deferencing the
1018 // result of va_next.
1019 SmallVector<SDOperand, 8> MemOps;
1020 for (; ArgRegIdx != NumArgRegs; ++ArgRegIdx) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001021 unsigned VReg = RegInfo.createVirtualRegister(&SPU::GPRCRegClass);
1022 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Scott Michel266bc8f2007-12-04 22:23:35 +00001023 SDOperand Val = DAG.getCopyFromReg(Root, VReg, PtrVT);
1024 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1025 MemOps.push_back(Store);
1026 // Increment the address by four for the next argument to store
1027 SDOperand PtrOff = DAG.getConstant(MVT::getSizeInBits(PtrVT)/8, PtrVT);
1028 FIN = DAG.getNode(ISD::ADD, PtrOff.getValueType(), FIN, PtrOff);
1029 }
1030 if (!MemOps.empty())
1031 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,&MemOps[0],MemOps.size());
1032 }
1033
1034 ArgValues.push_back(Root);
1035
1036 // Return the new list of results.
1037 std::vector<MVT::ValueType> RetVT(Op.Val->value_begin(),
1038 Op.Val->value_end());
1039 return DAG.getNode(ISD::MERGE_VALUES, RetVT, &ArgValues[0], ArgValues.size());
1040}
1041
1042/// isLSAAddress - Return the immediate to use if the specified
1043/// value is representable as a LSA address.
1044static SDNode *isLSAAddress(SDOperand Op, SelectionDAG &DAG) {
1045 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
1046 if (!C) return 0;
1047
1048 int Addr = C->getValue();
1049 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
1050 (Addr << 14 >> 14) != Addr)
1051 return 0; // Top 14 bits have to be sext of immediate.
1052
1053 return DAG.getConstant((int)C->getValue() >> 2, MVT::i32).Val;
1054}
1055
1056static
1057SDOperand
Scott Michel9de5d0d2008-01-11 02:53:15 +00001058LowerCALL(SDOperand Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001059 SDOperand Chain = Op.getOperand(0);
1060#if 0
1061 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1062 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1063#endif
1064 SDOperand Callee = Op.getOperand(4);
1065 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1066 unsigned StackSlotSize = SPUFrameInfo::stackSlotSize();
1067 const unsigned *ArgRegs = SPURegisterInfo::getArgRegs();
1068 const unsigned NumArgRegs = SPURegisterInfo::getNumArgRegs();
1069
1070 // Handy pointer type
1071 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1072
1073 // Accumulate how many bytes are to be pushed on the stack, including the
1074 // linkage area, and parameter passing area. According to the SPU ABI,
1075 // we minimally need space for [LR] and [SP]
1076 unsigned NumStackBytes = SPUFrameInfo::minStackSize();
1077
1078 // Set up a copy of the stack pointer for use loading and storing any
1079 // arguments that may not fit in the registers available for argument
1080 // passing.
1081 SDOperand StackPtr = DAG.getRegister(SPU::R1, MVT::i32);
1082
1083 // Figure out which arguments are going to go in registers, and which in
1084 // memory.
1085 unsigned ArgOffset = SPUFrameInfo::minStackSize(); // Just below [LR]
1086 unsigned ArgRegIdx = 0;
1087
1088 // Keep track of registers passing arguments
1089 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1090 // And the arguments passed on the stack
1091 SmallVector<SDOperand, 8> MemOpChains;
1092
1093 for (unsigned i = 0; i != NumOps; ++i) {
1094 SDOperand Arg = Op.getOperand(5+2*i);
1095
1096 // PtrOff will be used to store the current argument to the stack if a
1097 // register cannot be found for it.
1098 SDOperand PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
1099 PtrOff = DAG.getNode(ISD::ADD, PtrVT, StackPtr, PtrOff);
1100
1101 switch (Arg.getValueType()) {
1102 default: assert(0 && "Unexpected ValueType for argument!");
1103 case MVT::i32:
1104 case MVT::i64:
1105 case MVT::i128:
1106 if (ArgRegIdx != NumArgRegs) {
1107 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1108 } else {
1109 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001110 ArgOffset += StackSlotSize;
Scott Michel266bc8f2007-12-04 22:23:35 +00001111 }
1112 break;
1113 case MVT::f32:
1114 case MVT::f64:
1115 if (ArgRegIdx != NumArgRegs) {
1116 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1117 } else {
1118 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001119 ArgOffset += StackSlotSize;
Scott Michel266bc8f2007-12-04 22:23:35 +00001120 }
1121 break;
1122 case MVT::v4f32:
1123 case MVT::v4i32:
1124 case MVT::v8i16:
1125 case MVT::v16i8:
1126 if (ArgRegIdx != NumArgRegs) {
1127 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1128 } else {
1129 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001130 ArgOffset += StackSlotSize;
Scott Michel266bc8f2007-12-04 22:23:35 +00001131 }
1132 break;
1133 }
1134 }
1135
1136 // Update number of stack bytes actually used, insert a call sequence start
1137 NumStackBytes = (ArgOffset - SPUFrameInfo::minStackSize());
1138 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumStackBytes, PtrVT));
1139
1140 if (!MemOpChains.empty()) {
1141 // Adjust the stack pointer for the stack arguments.
1142 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1143 &MemOpChains[0], MemOpChains.size());
1144 }
1145
1146 // Build a sequence of copy-to-reg nodes chained together with token chain
1147 // and flag operands which copy the outgoing args into the appropriate regs.
1148 SDOperand InFlag;
1149 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1150 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1151 InFlag);
1152 InFlag = Chain.getValue(1);
1153 }
1154
1155 std::vector<MVT::ValueType> NodeTys;
1156 NodeTys.push_back(MVT::Other); // Returns a chain
1157 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1158
1159 SmallVector<SDOperand, 8> Ops;
1160 unsigned CallOpc = SPUISD::CALL;
1161
1162 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1163 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1164 // node so that legalize doesn't hack it.
1165 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1166 GlobalValue *GV = G->getGlobal();
1167 unsigned CalleeVT = Callee.getValueType();
Scott Michel9de5d0d2008-01-11 02:53:15 +00001168 SDOperand Zero = DAG.getConstant(0, PtrVT);
1169 SDOperand GA = DAG.getTargetGlobalAddress(GV, CalleeVT);
Scott Michel266bc8f2007-12-04 22:23:35 +00001170
Scott Michel9de5d0d2008-01-11 02:53:15 +00001171 if (!ST->usingLargeMem()) {
1172 // Turn calls to targets that are defined (i.e., have bodies) into BRSL
1173 // style calls, otherwise, external symbols are BRASL calls. This assumes
1174 // that declared/defined symbols are in the same compilation unit and can
1175 // be reached through PC-relative jumps.
1176 //
1177 // NOTE:
1178 // This may be an unsafe assumption for JIT and really large compilation
1179 // units.
1180 if (GV->isDeclaration()) {
1181 Callee = DAG.getNode(SPUISD::AFormAddr, CalleeVT, GA, Zero);
1182 } else {
1183 Callee = DAG.getNode(SPUISD::PCRelAddr, CalleeVT, GA, Zero);
1184 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001185 } else {
Scott Michel9de5d0d2008-01-11 02:53:15 +00001186 // "Large memory" mode: Turn all calls into indirect calls with a X-form
1187 // address pairs:
Scott Michel053c1da2008-01-29 02:16:57 +00001188 Callee = DAG.getNode(SPUISD::IndirectAddr, PtrVT, GA, Zero);
Scott Michel266bc8f2007-12-04 22:23:35 +00001189 }
1190 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1191 Callee = DAG.getExternalSymbol(S->getSymbol(), Callee.getValueType());
Scott Michel9de5d0d2008-01-11 02:53:15 +00001192 else if (SDNode *Dest = isLSAAddress(Callee, DAG)) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001193 // If this is an absolute destination address that appears to be a legal
1194 // local store address, use the munged value.
1195 Callee = SDOperand(Dest, 0);
Scott Michel9de5d0d2008-01-11 02:53:15 +00001196 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001197
1198 Ops.push_back(Chain);
1199 Ops.push_back(Callee);
1200
1201 // Add argument registers to the end of the list so that they are known live
1202 // into the call.
1203 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1204 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1205 RegsToPass[i].second.getValueType()));
1206
1207 if (InFlag.Val)
1208 Ops.push_back(InFlag);
1209 Chain = DAG.getNode(CallOpc, NodeTys, &Ops[0], Ops.size());
1210 InFlag = Chain.getValue(1);
1211
Evan Chengebaaa912008-02-05 22:44:06 +00001212 Chain = DAG.getCALLSEQ_END(Chain,
1213 DAG.getConstant(NumStackBytes, PtrVT),
1214 DAG.getConstant(0, PtrVT),
1215 InFlag);
1216 if (Op.Val->getValueType(0) != MVT::Other)
1217 InFlag = Chain.getValue(1);
1218
Scott Michel266bc8f2007-12-04 22:23:35 +00001219 SDOperand ResultVals[3];
1220 unsigned NumResults = 0;
1221 NodeTys.clear();
1222
1223 // If the call has results, copy the values out of the ret val registers.
1224 switch (Op.Val->getValueType(0)) {
1225 default: assert(0 && "Unexpected ret value!");
1226 case MVT::Other: break;
1227 case MVT::i32:
1228 if (Op.Val->getValueType(1) == MVT::i32) {
1229 Chain = DAG.getCopyFromReg(Chain, SPU::R4, MVT::i32, InFlag).getValue(1);
1230 ResultVals[0] = Chain.getValue(0);
1231 Chain = DAG.getCopyFromReg(Chain, SPU::R3, MVT::i32,
1232 Chain.getValue(2)).getValue(1);
1233 ResultVals[1] = Chain.getValue(0);
1234 NumResults = 2;
1235 NodeTys.push_back(MVT::i32);
1236 } else {
1237 Chain = DAG.getCopyFromReg(Chain, SPU::R3, MVT::i32, InFlag).getValue(1);
1238 ResultVals[0] = Chain.getValue(0);
1239 NumResults = 1;
1240 }
1241 NodeTys.push_back(MVT::i32);
1242 break;
1243 case MVT::i64:
1244 Chain = DAG.getCopyFromReg(Chain, SPU::R3, MVT::i64, InFlag).getValue(1);
1245 ResultVals[0] = Chain.getValue(0);
1246 NumResults = 1;
1247 NodeTys.push_back(MVT::i64);
1248 break;
1249 case MVT::f32:
1250 case MVT::f64:
1251 Chain = DAG.getCopyFromReg(Chain, SPU::R3, Op.Val->getValueType(0),
1252 InFlag).getValue(1);
1253 ResultVals[0] = Chain.getValue(0);
1254 NumResults = 1;
1255 NodeTys.push_back(Op.Val->getValueType(0));
1256 break;
1257 case MVT::v2f64:
1258 case MVT::v4f32:
1259 case MVT::v4i32:
1260 case MVT::v8i16:
1261 case MVT::v16i8:
1262 Chain = DAG.getCopyFromReg(Chain, SPU::R3, Op.Val->getValueType(0),
1263 InFlag).getValue(1);
1264 ResultVals[0] = Chain.getValue(0);
1265 NumResults = 1;
1266 NodeTys.push_back(Op.Val->getValueType(0));
1267 break;
1268 }
1269
Scott Michel266bc8f2007-12-04 22:23:35 +00001270 NodeTys.push_back(MVT::Other);
1271
1272 // If the function returns void, just return the chain.
1273 if (NumResults == 0)
1274 return Chain;
1275
1276 // Otherwise, merge everything together with a MERGE_VALUES node.
1277 ResultVals[NumResults++] = Chain;
1278 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1279 ResultVals, NumResults);
1280 return Res.getValue(Op.ResNo);
1281}
1282
1283static SDOperand
1284LowerRET(SDOperand Op, SelectionDAG &DAG, TargetMachine &TM) {
1285 SmallVector<CCValAssign, 16> RVLocs;
1286 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
1287 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
1288 CCState CCInfo(CC, isVarArg, TM, RVLocs);
1289 CCInfo.AnalyzeReturn(Op.Val, RetCC_SPU);
1290
1291 // If this is the first return lowered for this function, add the regs to the
1292 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001293 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001294 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner84bc5422007-12-31 04:13:23 +00001295 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Scott Michel266bc8f2007-12-04 22:23:35 +00001296 }
1297
1298 SDOperand Chain = Op.getOperand(0);
1299 SDOperand Flag;
1300
1301 // Copy the result values into the output registers.
1302 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1303 CCValAssign &VA = RVLocs[i];
1304 assert(VA.isRegLoc() && "Can only return in registers!");
1305 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
1306 Flag = Chain.getValue(1);
1307 }
1308
1309 if (Flag.Val)
1310 return DAG.getNode(SPUISD::RET_FLAG, MVT::Other, Chain, Flag);
1311 else
1312 return DAG.getNode(SPUISD::RET_FLAG, MVT::Other, Chain);
1313}
1314
1315
1316//===----------------------------------------------------------------------===//
1317// Vector related lowering:
1318//===----------------------------------------------------------------------===//
1319
1320static ConstantSDNode *
1321getVecImm(SDNode *N) {
1322 SDOperand OpVal(0, 0);
1323
1324 // Check to see if this buildvec has a single non-undef value in its elements.
1325 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1326 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1327 if (OpVal.Val == 0)
1328 OpVal = N->getOperand(i);
1329 else if (OpVal != N->getOperand(i))
1330 return 0;
1331 }
1332
1333 if (OpVal.Val != 0) {
1334 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
1335 return CN;
1336 }
1337 }
1338
1339 return 0; // All UNDEF: use implicit def.; not Constant node
1340}
1341
1342/// get_vec_i18imm - Test if this vector is a vector filled with the same value
1343/// and the value fits into an unsigned 18-bit constant, and if so, return the
1344/// constant
1345SDOperand SPU::get_vec_u18imm(SDNode *N, SelectionDAG &DAG,
1346 MVT::ValueType ValueType) {
1347 if (ConstantSDNode *CN = getVecImm(N)) {
1348 uint64_t Value = CN->getValue();
1349 if (Value <= 0x3ffff)
1350 return DAG.getConstant(Value, ValueType);
1351 }
1352
1353 return SDOperand();
1354}
1355
1356/// get_vec_i16imm - Test if this vector is a vector filled with the same value
1357/// and the value fits into a signed 16-bit constant, and if so, return the
1358/// constant
1359SDOperand SPU::get_vec_i16imm(SDNode *N, SelectionDAG &DAG,
1360 MVT::ValueType ValueType) {
1361 if (ConstantSDNode *CN = getVecImm(N)) {
1362 if (ValueType == MVT::i32) {
1363 int Value = (int) CN->getValue();
1364 int SExtValue = ((Value & 0xffff) << 16) >> 16;
1365
1366 if (Value == SExtValue)
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001367 return DAG.getConstant(Value, ValueType);
Scott Michel266bc8f2007-12-04 22:23:35 +00001368 } else if (ValueType == MVT::i16) {
1369 short Value = (short) CN->getValue();
1370 int SExtValue = ((int) Value << 16) >> 16;
1371
1372 if (Value == (short) SExtValue)
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001373 return DAG.getConstant(Value, ValueType);
Scott Michel266bc8f2007-12-04 22:23:35 +00001374 } else if (ValueType == MVT::i64) {
1375 int64_t Value = CN->getValue();
1376 int64_t SExtValue = ((Value & 0xffff) << (64 - 16)) >> (64 - 16);
1377
1378 if (Value == SExtValue)
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001379 return DAG.getConstant(Value, ValueType);
Scott Michel266bc8f2007-12-04 22:23:35 +00001380 }
1381 }
1382
1383 return SDOperand();
1384}
1385
1386/// get_vec_i10imm - Test if this vector is a vector filled with the same value
1387/// and the value fits into a signed 10-bit constant, and if so, return the
1388/// constant
1389SDOperand SPU::get_vec_i10imm(SDNode *N, SelectionDAG &DAG,
1390 MVT::ValueType ValueType) {
1391 if (ConstantSDNode *CN = getVecImm(N)) {
1392 int Value = (int) CN->getValue();
1393 if ((ValueType == MVT::i32 && isS10Constant(Value))
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001394 || (ValueType == MVT::i16 && isS10Constant((short) Value)))
Scott Michel266bc8f2007-12-04 22:23:35 +00001395 return DAG.getConstant(Value, ValueType);
1396 }
1397
1398 return SDOperand();
1399}
1400
1401/// get_vec_i8imm - Test if this vector is a vector filled with the same value
1402/// and the value fits into a signed 8-bit constant, and if so, return the
1403/// constant.
1404///
1405/// @note: The incoming vector is v16i8 because that's the only way we can load
1406/// constant vectors. Thus, we test to see if the upper and lower bytes are the
1407/// same value.
1408SDOperand SPU::get_vec_i8imm(SDNode *N, SelectionDAG &DAG,
1409 MVT::ValueType ValueType) {
1410 if (ConstantSDNode *CN = getVecImm(N)) {
1411 int Value = (int) CN->getValue();
1412 if (ValueType == MVT::i16
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001413 && Value <= 0xffff /* truncated from uint64_t */
1414 && ((short) Value >> 8) == ((short) Value & 0xff))
Scott Michel266bc8f2007-12-04 22:23:35 +00001415 return DAG.getConstant(Value & 0xff, ValueType);
1416 else if (ValueType == MVT::i8
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001417 && (Value & 0xff) == Value)
Scott Michel266bc8f2007-12-04 22:23:35 +00001418 return DAG.getConstant(Value, ValueType);
1419 }
1420
1421 return SDOperand();
1422}
1423
1424/// get_ILHUvec_imm - Test if this vector is a vector filled with the same value
1425/// and the value fits into a signed 16-bit constant, and if so, return the
1426/// constant
1427SDOperand SPU::get_ILHUvec_imm(SDNode *N, SelectionDAG &DAG,
1428 MVT::ValueType ValueType) {
1429 if (ConstantSDNode *CN = getVecImm(N)) {
1430 uint64_t Value = CN->getValue();
1431 if ((ValueType == MVT::i32
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001432 && ((unsigned) Value & 0xffff0000) == (unsigned) Value)
1433 || (ValueType == MVT::i64 && (Value & 0xffff0000) == Value))
Scott Michel266bc8f2007-12-04 22:23:35 +00001434 return DAG.getConstant(Value >> 16, ValueType);
1435 }
1436
1437 return SDOperand();
1438}
1439
1440/// get_v4i32_imm - Catch-all for general 32-bit constant vectors
1441SDOperand SPU::get_v4i32_imm(SDNode *N, SelectionDAG &DAG) {
1442 if (ConstantSDNode *CN = getVecImm(N)) {
1443 return DAG.getConstant((unsigned) CN->getValue(), MVT::i32);
1444 }
1445
1446 return SDOperand();
1447}
1448
1449/// get_v4i32_imm - Catch-all for general 64-bit constant vectors
1450SDOperand SPU::get_v2i64_imm(SDNode *N, SelectionDAG &DAG) {
1451 if (ConstantSDNode *CN = getVecImm(N)) {
1452 return DAG.getConstant((unsigned) CN->getValue(), MVT::i64);
1453 }
1454
1455 return SDOperand();
1456}
1457
1458// If this is a vector of constants or undefs, get the bits. A bit in
1459// UndefBits is set if the corresponding element of the vector is an
1460// ISD::UNDEF value. For undefs, the corresponding VectorBits values are
1461// zero. Return true if this is not an array of constants, false if it is.
1462//
1463static bool GetConstantBuildVectorBits(SDNode *BV, uint64_t VectorBits[2],
1464 uint64_t UndefBits[2]) {
1465 // Start with zero'd results.
1466 VectorBits[0] = VectorBits[1] = UndefBits[0] = UndefBits[1] = 0;
1467
1468 unsigned EltBitSize = MVT::getSizeInBits(BV->getOperand(0).getValueType());
1469 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
1470 SDOperand OpVal = BV->getOperand(i);
1471
1472 unsigned PartNo = i >= e/2; // In the upper 128 bits?
1473 unsigned SlotNo = e/2 - (i & (e/2-1))-1; // Which subpiece of the uint64_t.
1474
1475 uint64_t EltBits = 0;
1476 if (OpVal.getOpcode() == ISD::UNDEF) {
1477 uint64_t EltUndefBits = ~0ULL >> (64-EltBitSize);
1478 UndefBits[PartNo] |= EltUndefBits << (SlotNo*EltBitSize);
1479 continue;
1480 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
1481 EltBits = CN->getValue() & (~0ULL >> (64-EltBitSize));
1482 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
1483 const APFloat &apf = CN->getValueAPF();
1484 EltBits = (CN->getValueType(0) == MVT::f32
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001485 ? FloatToBits(apf.convertToFloat())
1486 : DoubleToBits(apf.convertToDouble()));
Scott Michel266bc8f2007-12-04 22:23:35 +00001487 } else {
1488 // Nonconstant element.
1489 return true;
1490 }
1491
1492 VectorBits[PartNo] |= EltBits << (SlotNo*EltBitSize);
1493 }
1494
1495 //printf("%llx %llx %llx %llx\n",
1496 // VectorBits[0], VectorBits[1], UndefBits[0], UndefBits[1]);
1497 return false;
1498}
1499
1500/// If this is a splat (repetition) of a value across the whole vector, return
1501/// the smallest size that splats it. For example, "0x01010101010101..." is a
1502/// splat of 0x01, 0x0101, and 0x01010101. We return SplatBits = 0x01 and
1503/// SplatSize = 1 byte.
1504static bool isConstantSplat(const uint64_t Bits128[2],
1505 const uint64_t Undef128[2],
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001506 int MinSplatBits,
Scott Michel266bc8f2007-12-04 22:23:35 +00001507 uint64_t &SplatBits, uint64_t &SplatUndef,
1508 int &SplatSize) {
1509 // Don't let undefs prevent splats from matching. See if the top 64-bits are
1510 // the same as the lower 64-bits, ignoring undefs.
1511 uint64_t Bits64 = Bits128[0] | Bits128[1];
1512 uint64_t Undef64 = Undef128[0] & Undef128[1];
1513 uint32_t Bits32 = uint32_t(Bits64) | uint32_t(Bits64 >> 32);
1514 uint32_t Undef32 = uint32_t(Undef64) & uint32_t(Undef64 >> 32);
1515 uint16_t Bits16 = uint16_t(Bits32) | uint16_t(Bits32 >> 16);
1516 uint16_t Undef16 = uint16_t(Undef32) & uint16_t(Undef32 >> 16);
1517
1518 if ((Bits128[0] & ~Undef128[1]) == (Bits128[1] & ~Undef128[0])) {
1519 if (MinSplatBits < 64) {
1520
1521 // Check that the top 32-bits are the same as the lower 32-bits, ignoring
1522 // undefs.
1523 if ((Bits64 & (~Undef64 >> 32)) == ((Bits64 >> 32) & ~Undef64)) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001524 if (MinSplatBits < 32) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001525
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001526 // If the top 16-bits are different than the lower 16-bits, ignoring
1527 // undefs, we have an i32 splat.
1528 if ((Bits32 & (~Undef32 >> 16)) == ((Bits32 >> 16) & ~Undef32)) {
1529 if (MinSplatBits < 16) {
1530 // If the top 8-bits are different than the lower 8-bits, ignoring
1531 // undefs, we have an i16 splat.
1532 if ((Bits16 & (uint16_t(~Undef16) >> 8)) == ((Bits16 >> 8) & ~Undef16)) {
1533 // Otherwise, we have an 8-bit splat.
1534 SplatBits = uint8_t(Bits16) | uint8_t(Bits16 >> 8);
1535 SplatUndef = uint8_t(Undef16) & uint8_t(Undef16 >> 8);
1536 SplatSize = 1;
1537 return true;
1538 }
1539 } else {
1540 SplatBits = Bits16;
1541 SplatUndef = Undef16;
1542 SplatSize = 2;
1543 return true;
1544 }
1545 }
1546 } else {
1547 SplatBits = Bits32;
1548 SplatUndef = Undef32;
1549 SplatSize = 4;
1550 return true;
1551 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001552 }
1553 } else {
1554 SplatBits = Bits128[0];
1555 SplatUndef = Undef128[0];
1556 SplatSize = 8;
1557 return true;
1558 }
1559 }
1560
1561 return false; // Can't be a splat if two pieces don't match.
1562}
1563
1564// If this is a case we can't handle, return null and let the default
1565// expansion code take care of it. If we CAN select this case, and if it
1566// selects to a single instruction, return Op. Otherwise, if we can codegen
1567// this case more efficiently than a constant pool load, lower it to the
1568// sequence of ops that should be used.
1569static SDOperand LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
1570 MVT::ValueType VT = Op.getValueType();
1571 // If this is a vector of constants or undefs, get the bits. A bit in
1572 // UndefBits is set if the corresponding element of the vector is an
1573 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
1574 // zero.
1575 uint64_t VectorBits[2];
1576 uint64_t UndefBits[2];
1577 uint64_t SplatBits, SplatUndef;
1578 int SplatSize;
1579 if (GetConstantBuildVectorBits(Op.Val, VectorBits, UndefBits)
1580 || !isConstantSplat(VectorBits, UndefBits,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001581 MVT::getSizeInBits(MVT::getVectorElementType(VT)),
Scott Michel266bc8f2007-12-04 22:23:35 +00001582 SplatBits, SplatUndef, SplatSize))
1583 return SDOperand(); // Not a constant vector, not a splat.
1584
1585 switch (VT) {
1586 default:
1587 case MVT::v4f32: {
1588 uint32_t Value32 = SplatBits;
1589 assert(SplatSize == 4
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001590 && "LowerBUILD_VECTOR: Unexpected floating point vector element.");
Scott Michel266bc8f2007-12-04 22:23:35 +00001591 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
1592 SDOperand T = DAG.getConstant(Value32, MVT::i32);
1593 return DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001594 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, T, T, T, T));
Scott Michel266bc8f2007-12-04 22:23:35 +00001595 break;
1596 }
1597 case MVT::v2f64: {
1598 uint64_t f64val = SplatBits;
1599 assert(SplatSize == 8
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001600 && "LowerBUILD_VECTOR: 64-bit float vector element: unexpected size.");
Scott Michel266bc8f2007-12-04 22:23:35 +00001601 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
1602 SDOperand T = DAG.getConstant(f64val, MVT::i64);
1603 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001604 DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i64, T, T));
Scott Michel266bc8f2007-12-04 22:23:35 +00001605 break;
1606 }
1607 case MVT::v16i8: {
1608 // 8-bit constants have to be expanded to 16-bits
1609 unsigned short Value16 = SplatBits | (SplatBits << 8);
1610 SDOperand Ops[8];
1611 for (int i = 0; i < 8; ++i)
1612 Ops[i] = DAG.getConstant(Value16, MVT::i16);
1613 return DAG.getNode(ISD::BIT_CONVERT, VT,
1614 DAG.getNode(ISD::BUILD_VECTOR, MVT::v8i16, Ops, 8));
1615 }
1616 case MVT::v8i16: {
1617 unsigned short Value16;
1618 if (SplatSize == 2)
1619 Value16 = (unsigned short) (SplatBits & 0xffff);
1620 else
1621 Value16 = (unsigned short) (SplatBits | (SplatBits << 8));
1622 SDOperand T = DAG.getConstant(Value16, MVT::getVectorElementType(VT));
1623 SDOperand Ops[8];
1624 for (int i = 0; i < 8; ++i) Ops[i] = T;
1625 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops, 8);
1626 }
1627 case MVT::v4i32: {
1628 unsigned int Value = SplatBits;
1629 SDOperand T = DAG.getConstant(Value, MVT::getVectorElementType(VT));
1630 return DAG.getNode(ISD::BUILD_VECTOR, VT, T, T, T, T);
1631 }
1632 case MVT::v2i64: {
1633 uint64_t val = SplatBits;
1634 uint32_t upper = uint32_t(val >> 32);
1635 uint32_t lower = uint32_t(val);
1636
1637 if (val != 0) {
1638 SDOperand LO32;
1639 SDOperand HI32;
1640 SmallVector<SDOperand, 16> ShufBytes;
1641 SDOperand Result;
1642 bool upper_special, lower_special;
1643
1644 // NOTE: This code creates common-case shuffle masks that can be easily
1645 // detected as common expressions. It is not attempting to create highly
1646 // specialized masks to replace any and all 0's, 0xff's and 0x80's.
1647
1648 // Detect if the upper or lower half is a special shuffle mask pattern:
1649 upper_special = (upper == 0 || upper == 0xffffffff || upper == 0x80000000);
1650 lower_special = (lower == 0 || lower == 0xffffffff || lower == 0x80000000);
1651
1652 // Create lower vector if not a special pattern
1653 if (!lower_special) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001654 SDOperand LO32C = DAG.getConstant(lower, MVT::i32);
1655 LO32 = DAG.getNode(ISD::BIT_CONVERT, VT,
1656 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1657 LO32C, LO32C, LO32C, LO32C));
Scott Michel266bc8f2007-12-04 22:23:35 +00001658 }
1659
1660 // Create upper vector if not a special pattern
1661 if (!upper_special) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001662 SDOperand HI32C = DAG.getConstant(upper, MVT::i32);
1663 HI32 = DAG.getNode(ISD::BIT_CONVERT, VT,
1664 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1665 HI32C, HI32C, HI32C, HI32C));
Scott Michel266bc8f2007-12-04 22:23:35 +00001666 }
1667
1668 // If either upper or lower are special, then the two input operands are
1669 // the same (basically, one of them is a "don't care")
1670 if (lower_special)
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001671 LO32 = HI32;
Scott Michel266bc8f2007-12-04 22:23:35 +00001672 if (upper_special)
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001673 HI32 = LO32;
Scott Michel266bc8f2007-12-04 22:23:35 +00001674 if (lower_special && upper_special) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001675 // Unhappy situation... both upper and lower are special, so punt with
1676 // a target constant:
Scott Michel266bc8f2007-12-04 22:23:35 +00001677 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001678 HI32 = LO32 = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Zero, Zero,
Scott Michel266bc8f2007-12-04 22:23:35 +00001679 Zero, Zero);
1680 }
1681
1682 for (int i = 0; i < 4; ++i) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001683 for (int j = 0; j < 4; ++j) {
1684 SDOperand V;
1685 bool process_upper, process_lower;
1686 uint64_t val = 0;
Scott Michel266bc8f2007-12-04 22:23:35 +00001687
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001688 process_upper = (upper_special && (i & 1) == 0);
1689 process_lower = (lower_special && (i & 1) == 1);
Scott Michel266bc8f2007-12-04 22:23:35 +00001690
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001691 if (process_upper || process_lower) {
1692 if ((process_upper && upper == 0)
1693 || (process_lower && lower == 0))
1694 val = 0x80;
1695 else if ((process_upper && upper == 0xffffffff)
1696 || (process_lower && lower == 0xffffffff))
1697 val = 0xc0;
1698 else if ((process_upper && upper == 0x80000000)
1699 || (process_lower && lower == 0x80000000))
1700 val = (j == 0 ? 0xe0 : 0x80);
1701 } else
1702 val = i * 4 + j + ((i & 1) * 16);
Scott Michel266bc8f2007-12-04 22:23:35 +00001703
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001704 ShufBytes.push_back(DAG.getConstant(val, MVT::i8));
1705 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001706 }
1707
1708 return DAG.getNode(SPUISD::SHUFB, VT, HI32, LO32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001709 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8,
1710 &ShufBytes[0], ShufBytes.size()));
Scott Michel266bc8f2007-12-04 22:23:35 +00001711 } else {
1712 // For zero, this can be lowered efficiently via v4i32 BUILD_VECTOR
1713 SDOperand Zero = DAG.getConstant(0, MVT::i32);
1714 return DAG.getNode(ISD::BIT_CONVERT, VT,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001715 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1716 Zero, Zero, Zero, Zero));
Scott Michel266bc8f2007-12-04 22:23:35 +00001717 }
1718 }
1719 }
1720
1721 return SDOperand();
1722}
1723
1724/// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3) to something on
1725/// which the Cell can operate. The code inspects V3 to ascertain whether the
1726/// permutation vector, V3, is monotonically increasing with one "exception"
1727/// element, e.g., (0, 1, _, 3). If this is the case, then generate a
1728/// INSERT_MASK synthetic instruction. Otherwise, spill V3 to the constant pool.
1729/// In either case, the net result is going to eventually invoke SHUFB to
1730/// permute/shuffle the bytes from V1 and V2.
1731/// \note
1732/// INSERT_MASK is eventually selected as one of the C*D instructions, generate
1733/// control word for byte/halfword/word insertion. This takes care of a single
1734/// element move from V2 into V1.
1735/// \note
1736/// SPUISD::SHUFB is eventually selected as Cell's <i>shufb</i> instructions.
1737static SDOperand LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
1738 SDOperand V1 = Op.getOperand(0);
1739 SDOperand V2 = Op.getOperand(1);
1740 SDOperand PermMask = Op.getOperand(2);
1741
1742 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
1743
1744 // If we have a single element being moved from V1 to V2, this can be handled
1745 // using the C*[DX] compute mask instructions, but the vector elements have
1746 // to be monotonically increasing with one exception element.
1747 MVT::ValueType EltVT = MVT::getVectorElementType(V1.getValueType());
1748 unsigned EltsFromV2 = 0;
1749 unsigned V2Elt = 0;
1750 unsigned V2EltIdx0 = 0;
1751 unsigned CurrElt = 0;
1752 bool monotonic = true;
1753 if (EltVT == MVT::i8)
1754 V2EltIdx0 = 16;
1755 else if (EltVT == MVT::i16)
1756 V2EltIdx0 = 8;
1757 else if (EltVT == MVT::i32)
1758 V2EltIdx0 = 4;
1759 else
1760 assert(0 && "Unhandled vector type in LowerVECTOR_SHUFFLE");
1761
1762 for (unsigned i = 0, e = PermMask.getNumOperands();
1763 EltsFromV2 <= 1 && monotonic && i != e;
1764 ++i) {
1765 unsigned SrcElt;
1766 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
1767 SrcElt = 0;
1768 else
1769 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
1770
1771 if (SrcElt >= V2EltIdx0) {
1772 ++EltsFromV2;
1773 V2Elt = (V2EltIdx0 - SrcElt) << 2;
1774 } else if (CurrElt != SrcElt) {
1775 monotonic = false;
1776 }
1777
1778 ++CurrElt;
1779 }
1780
1781 if (EltsFromV2 == 1 && monotonic) {
1782 // Compute mask and shuffle
1783 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00001784 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1785 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00001786 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1787 // Initialize temporary register to 0
1788 SDOperand InitTempReg =
1789 DAG.getCopyToReg(DAG.getEntryNode(), VReg, DAG.getConstant(0, PtrVT));
1790 // Copy register's contents as index in INSERT_MASK:
1791 SDOperand ShufMaskOp =
1792 DAG.getNode(SPUISD::INSERT_MASK, V1.getValueType(),
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001793 DAG.getTargetConstant(V2Elt, MVT::i32),
1794 DAG.getCopyFromReg(InitTempReg, VReg, PtrVT));
Scott Michel266bc8f2007-12-04 22:23:35 +00001795 // Use shuffle mask in SHUFB synthetic instruction:
1796 return DAG.getNode(SPUISD::SHUFB, V1.getValueType(), V2, V1, ShufMaskOp);
1797 } else {
1798 // Convert the SHUFFLE_VECTOR mask's input element units to the actual bytes.
1799 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
1800
1801 SmallVector<SDOperand, 16> ResultMask;
1802 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
1803 unsigned SrcElt;
1804 if (PermMask.getOperand(i).getOpcode() == ISD::UNDEF)
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001805 SrcElt = 0;
Scott Michel266bc8f2007-12-04 22:23:35 +00001806 else
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001807 SrcElt = cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00001808
1809 for (unsigned j = 0; j != BytesPerElement; ++j) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001810 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
1811 MVT::i8));
Scott Michel266bc8f2007-12-04 22:23:35 +00001812 }
1813 }
1814
1815 SDOperand VPermMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001816 &ResultMask[0], ResultMask.size());
Scott Michel266bc8f2007-12-04 22:23:35 +00001817 return DAG.getNode(SPUISD::SHUFB, V1.getValueType(), V1, V2, VPermMask);
1818 }
1819}
1820
1821static SDOperand LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001822 SDOperand Op0 = Op.getOperand(0); // Op0 = the scalar
Scott Michel266bc8f2007-12-04 22:23:35 +00001823
1824 if (Op0.Val->getOpcode() == ISD::Constant) {
1825 // For a constant, build the appropriate constant vector, which will
1826 // eventually simplify to a vector register load.
1827
1828 ConstantSDNode *CN = cast<ConstantSDNode>(Op0.Val);
1829 SmallVector<SDOperand, 16> ConstVecValues;
1830 MVT::ValueType VT;
1831 size_t n_copies;
1832
1833 // Create a constant vector:
1834 switch (Op.getValueType()) {
1835 default: assert(0 && "Unexpected constant value type in "
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001836 "LowerSCALAR_TO_VECTOR");
Scott Michel266bc8f2007-12-04 22:23:35 +00001837 case MVT::v16i8: n_copies = 16; VT = MVT::i8; break;
1838 case MVT::v8i16: n_copies = 8; VT = MVT::i16; break;
1839 case MVT::v4i32: n_copies = 4; VT = MVT::i32; break;
1840 case MVT::v4f32: n_copies = 4; VT = MVT::f32; break;
1841 case MVT::v2i64: n_copies = 2; VT = MVT::i64; break;
1842 case MVT::v2f64: n_copies = 2; VT = MVT::f64; break;
1843 }
1844
1845 SDOperand CValue = DAG.getConstant(CN->getValue(), VT);
1846 for (size_t j = 0; j < n_copies; ++j)
1847 ConstVecValues.push_back(CValue);
1848
1849 return DAG.getNode(ISD::BUILD_VECTOR, Op.getValueType(),
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001850 &ConstVecValues[0], ConstVecValues.size());
Scott Michel266bc8f2007-12-04 22:23:35 +00001851 } else {
1852 // Otherwise, copy the value from one register to another:
1853 switch (Op0.getValueType()) {
1854 default: assert(0 && "Unexpected value type in LowerSCALAR_TO_VECTOR");
1855 case MVT::i8:
1856 case MVT::i16:
1857 case MVT::i32:
1858 case MVT::i64:
1859 case MVT::f32:
1860 case MVT::f64:
1861 return DAG.getNode(SPUISD::PROMOTE_SCALAR, Op.getValueType(), Op0, Op0);
1862 }
1863 }
1864
1865 return SDOperand();
1866}
1867
1868static SDOperand LowerVectorMUL(SDOperand Op, SelectionDAG &DAG) {
1869 switch (Op.getValueType()) {
1870 case MVT::v4i32: {
1871 SDOperand rA = Op.getOperand(0);
1872 SDOperand rB = Op.getOperand(1);
1873 SDOperand HiProd1 = DAG.getNode(SPUISD::MPYH, MVT::v4i32, rA, rB);
1874 SDOperand HiProd2 = DAG.getNode(SPUISD::MPYH, MVT::v4i32, rB, rA);
1875 SDOperand LoProd = DAG.getNode(SPUISD::MPYU, MVT::v4i32, rA, rB);
1876 SDOperand Residual1 = DAG.getNode(ISD::ADD, MVT::v4i32, LoProd, HiProd1);
1877
1878 return DAG.getNode(ISD::ADD, MVT::v4i32, Residual1, HiProd2);
1879 break;
1880 }
1881
1882 // Multiply two v8i16 vectors (pipeline friendly version):
1883 // a) multiply lower halves, mask off upper 16-bit of 32-bit product
1884 // b) multiply upper halves, rotate left by 16 bits (inserts 16 lower zeroes)
1885 // c) Use SELB to select upper and lower halves from the intermediate results
1886 //
1887 // NOTE: We really want to move the FSMBI to earlier to actually get the
1888 // dual-issue. This code does manage to do this, even if it's a little on
1889 // the wacky side
1890 case MVT::v8i16: {
1891 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00001892 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +00001893 SDOperand Chain = Op.getOperand(0);
1894 SDOperand rA = Op.getOperand(0);
1895 SDOperand rB = Op.getOperand(1);
Chris Lattner84bc5422007-12-31 04:13:23 +00001896 unsigned FSMBIreg = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
1897 unsigned HiProdReg = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00001898
1899 SDOperand FSMBOp =
1900 DAG.getCopyToReg(Chain, FSMBIreg,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001901 DAG.getNode(SPUISD::FSMBI, MVT::v8i16,
1902 DAG.getConstant(0xcccc, MVT::i32)));
Scott Michel266bc8f2007-12-04 22:23:35 +00001903
1904 SDOperand HHProd =
1905 DAG.getCopyToReg(FSMBOp, HiProdReg,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001906 DAG.getNode(SPUISD::MPYHH, MVT::v8i16, rA, rB));
Scott Michel266bc8f2007-12-04 22:23:35 +00001907
1908 SDOperand HHProd_v4i32 =
1909 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001910 DAG.getCopyFromReg(HHProd, HiProdReg, MVT::v4i32));
Scott Michel266bc8f2007-12-04 22:23:35 +00001911
1912 return DAG.getNode(SPUISD::SELB, MVT::v8i16,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001913 DAG.getNode(SPUISD::MPY, MVT::v8i16, rA, rB),
1914 DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
1915 DAG.getNode(SPUISD::VEC_SHL, MVT::v4i32,
1916 HHProd_v4i32,
1917 DAG.getConstant(16, MVT::i16))),
1918 DAG.getCopyFromReg(FSMBOp, FSMBIreg, MVT::v4i32));
Scott Michel266bc8f2007-12-04 22:23:35 +00001919 }
1920
1921 // This M00sE is N@stI! (apologies to Monty Python)
1922 //
1923 // SPU doesn't know how to do any 8-bit multiplication, so the solution
1924 // is to break it all apart, sign extend, and reassemble the various
1925 // intermediate products.
1926 case MVT::v16i8: {
1927 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00001928 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +00001929 SDOperand Chain = Op.getOperand(0);
1930 SDOperand rA = Op.getOperand(0);
1931 SDOperand rB = Op.getOperand(1);
1932 SDOperand c8 = DAG.getConstant(8, MVT::i8);
1933 SDOperand c16 = DAG.getConstant(16, MVT::i8);
1934
Chris Lattner84bc5422007-12-31 04:13:23 +00001935 unsigned FSMBreg_2222 = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
1936 unsigned LoProd_reg = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
1937 unsigned HiProd_reg = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00001938
1939 SDOperand LLProd =
1940 DAG.getNode(SPUISD::MPY, MVT::v8i16,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001941 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rA),
1942 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rB));
Scott Michel266bc8f2007-12-04 22:23:35 +00001943
1944 SDOperand rALH = DAG.getNode(SPUISD::VEC_SRA, MVT::v8i16, rA, c8);
1945
1946 SDOperand rBLH = DAG.getNode(SPUISD::VEC_SRA, MVT::v8i16, rB, c8);
1947
1948 SDOperand LHProd =
1949 DAG.getNode(SPUISD::VEC_SHL, MVT::v8i16,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001950 DAG.getNode(SPUISD::MPY, MVT::v8i16, rALH, rBLH), c8);
Scott Michel266bc8f2007-12-04 22:23:35 +00001951
1952 SDOperand FSMBdef_2222 =
1953 DAG.getCopyToReg(Chain, FSMBreg_2222,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001954 DAG.getNode(SPUISD::FSMBI, MVT::v8i16,
1955 DAG.getConstant(0x2222, MVT::i32)));
Scott Michel266bc8f2007-12-04 22:23:35 +00001956
1957 SDOperand FSMBuse_2222 =
1958 DAG.getCopyFromReg(FSMBdef_2222, FSMBreg_2222, MVT::v4i32);
1959
1960 SDOperand LoProd_1 =
1961 DAG.getCopyToReg(Chain, LoProd_reg,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001962 DAG.getNode(SPUISD::SELB, MVT::v8i16, LLProd, LHProd,
1963 FSMBuse_2222));
Scott Michel266bc8f2007-12-04 22:23:35 +00001964
1965 SDOperand LoProdMask = DAG.getConstant(0xffff, MVT::i32);
1966
1967 SDOperand LoProd =
1968 DAG.getNode(ISD::AND, MVT::v4i32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001969 DAG.getCopyFromReg(LoProd_1, LoProd_reg, MVT::v4i32),
1970 DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1971 LoProdMask, LoProdMask,
1972 LoProdMask, LoProdMask));
Scott Michel266bc8f2007-12-04 22:23:35 +00001973
1974 SDOperand rAH =
1975 DAG.getNode(SPUISD::VEC_SRA, MVT::v4i32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001976 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, rA), c16);
Scott Michel266bc8f2007-12-04 22:23:35 +00001977
1978 SDOperand rBH =
1979 DAG.getNode(SPUISD::VEC_SRA, MVT::v4i32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001980 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, rB), c16);
Scott Michel266bc8f2007-12-04 22:23:35 +00001981
1982 SDOperand HLProd =
1983 DAG.getNode(SPUISD::MPY, MVT::v8i16,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001984 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rAH),
1985 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, rBH));
Scott Michel266bc8f2007-12-04 22:23:35 +00001986
1987 SDOperand HHProd_1 =
1988 DAG.getNode(SPUISD::MPY, MVT::v8i16,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001989 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16,
1990 DAG.getNode(SPUISD::VEC_SRA, MVT::v4i32, rAH, c8)),
1991 DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16,
1992 DAG.getNode(SPUISD::VEC_SRA, MVT::v4i32, rBH, c8)));
Scott Michel266bc8f2007-12-04 22:23:35 +00001993
1994 SDOperand HHProd =
1995 DAG.getCopyToReg(Chain, HiProd_reg,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001996 DAG.getNode(SPUISD::SELB, MVT::v8i16,
1997 HLProd,
1998 DAG.getNode(SPUISD::VEC_SHL, MVT::v8i16, HHProd_1, c8),
1999 FSMBuse_2222));
Scott Michel266bc8f2007-12-04 22:23:35 +00002000
2001 SDOperand HiProd =
2002 DAG.getNode(SPUISD::VEC_SHL, MVT::v4i32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002003 DAG.getCopyFromReg(HHProd, HiProd_reg, MVT::v4i32), c16);
Scott Michel266bc8f2007-12-04 22:23:35 +00002004
2005 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002006 DAG.getNode(ISD::OR, MVT::v4i32,
2007 LoProd, HiProd));
Scott Michel266bc8f2007-12-04 22:23:35 +00002008 }
2009
2010 default:
2011 cerr << "CellSPU: Unknown vector multiplication, got "
2012 << MVT::getValueTypeString(Op.getValueType())
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002013 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +00002014 abort();
2015 /*NOTREACHED*/
2016 }
2017
2018 return SDOperand();
2019}
2020
2021static SDOperand LowerFDIVf32(SDOperand Op, SelectionDAG &DAG) {
2022 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00002023 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +00002024
2025 SDOperand A = Op.getOperand(0);
2026 SDOperand B = Op.getOperand(1);
2027 unsigned VT = Op.getValueType();
2028
2029 unsigned VRegBR, VRegC;
2030
2031 if (VT == MVT::f32) {
Chris Lattner84bc5422007-12-31 04:13:23 +00002032 VRegBR = RegInfo.createVirtualRegister(&SPU::R32FPRegClass);
2033 VRegC = RegInfo.createVirtualRegister(&SPU::R32FPRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00002034 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +00002035 VRegBR = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
2036 VRegC = RegInfo.createVirtualRegister(&SPU::VECREGRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00002037 }
2038 // TODO: make sure we're feeding FPInterp the right arguments
2039 // Right now: fi B, frest(B)
2040
2041 // Computes BRcpl =
2042 // (Floating Interpolate (FP Reciprocal Estimate B))
2043 SDOperand BRcpl =
2044 DAG.getCopyToReg(DAG.getEntryNode(), VRegBR,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002045 DAG.getNode(SPUISD::FPInterp, VT, B,
2046 DAG.getNode(SPUISD::FPRecipEst, VT, B)));
Scott Michel266bc8f2007-12-04 22:23:35 +00002047
2048 // Computes A * BRcpl and stores in a temporary register
2049 SDOperand AxBRcpl =
2050 DAG.getCopyToReg(BRcpl, VRegC,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002051 DAG.getNode(ISD::FMUL, VT, A,
2052 DAG.getCopyFromReg(BRcpl, VRegBR, VT)));
Scott Michel266bc8f2007-12-04 22:23:35 +00002053 // What's the Chain variable do? It's magic!
2054 // TODO: set Chain = Op(0).getEntryNode()
2055
2056 return DAG.getNode(ISD::FADD, VT,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002057 DAG.getCopyFromReg(AxBRcpl, VRegC, VT),
2058 DAG.getNode(ISD::FMUL, VT,
2059 DAG.getCopyFromReg(AxBRcpl, VRegBR, VT),
2060 DAG.getNode(ISD::FSUB, VT, A,
2061 DAG.getNode(ISD::FMUL, VT, B,
2062 DAG.getCopyFromReg(AxBRcpl, VRegC, VT)))));
Scott Michel266bc8f2007-12-04 22:23:35 +00002063}
2064
Scott Michel266bc8f2007-12-04 22:23:35 +00002065static SDOperand LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2066 unsigned VT = Op.getValueType();
2067 SDOperand N = Op.getOperand(0);
2068 SDOperand Elt = Op.getOperand(1);
2069 SDOperand ShufMask[16];
2070 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt);
2071
2072 assert(C != 0 && "LowerEXTRACT_VECTOR_ELT expecting constant SDNode");
2073
2074 int EltNo = (int) C->getValue();
2075
2076 // sanity checks:
2077 if (VT == MVT::i8 && EltNo >= 16)
2078 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i8 extraction slot > 15");
2079 else if (VT == MVT::i16 && EltNo >= 8)
2080 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i16 extraction slot > 7");
2081 else if (VT == MVT::i32 && EltNo >= 4)
2082 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i32 extraction slot > 4");
2083 else if (VT == MVT::i64 && EltNo >= 2)
2084 assert(0 && "SPU LowerEXTRACT_VECTOR_ELT: i64 extraction slot > 2");
2085
2086 if (EltNo == 0 && (VT == MVT::i32 || VT == MVT::i64)) {
2087 // i32 and i64: Element 0 is the preferred slot
2088 return DAG.getNode(SPUISD::EXTRACT_ELT0, VT, N);
2089 }
2090
2091 // Need to generate shuffle mask and extract:
Scott Michel0e5665b2007-12-19 21:17:42 +00002092 int prefslot_begin = -1, prefslot_end = -1;
Scott Michel266bc8f2007-12-04 22:23:35 +00002093 int elt_byte = EltNo * MVT::getSizeInBits(VT) / 8;
2094
2095 switch (VT) {
2096 case MVT::i8: {
2097 prefslot_begin = prefslot_end = 3;
2098 break;
2099 }
2100 case MVT::i16: {
2101 prefslot_begin = 2; prefslot_end = 3;
2102 break;
2103 }
2104 case MVT::i32: {
2105 prefslot_begin = 0; prefslot_end = 3;
2106 break;
2107 }
2108 case MVT::i64: {
2109 prefslot_begin = 0; prefslot_end = 7;
2110 break;
2111 }
2112 }
2113
Scott Michel0e5665b2007-12-19 21:17:42 +00002114 assert(prefslot_begin != -1 && prefslot_end != -1 &&
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002115 "LowerEXTRACT_VECTOR_ELT: preferred slots uninitialized");
Scott Michel0e5665b2007-12-19 21:17:42 +00002116
Scott Michel266bc8f2007-12-04 22:23:35 +00002117 for (int i = 0; i < 16; ++i) {
2118 // zero fill uppper part of preferred slot, don't care about the
2119 // other slots:
2120 unsigned int mask_val;
2121
2122 if (i <= prefslot_end) {
2123 mask_val =
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002124 ((i < prefslot_begin)
2125 ? 0x80
2126 : elt_byte + (i - prefslot_begin));
Scott Michel266bc8f2007-12-04 22:23:35 +00002127
Scott Michel0e5665b2007-12-19 21:17:42 +00002128 ShufMask[i] = DAG.getConstant(mask_val, MVT::i8);
Scott Michel266bc8f2007-12-04 22:23:35 +00002129 } else
2130 ShufMask[i] = ShufMask[i % (prefslot_end + 1)];
2131 }
2132
2133 SDOperand ShufMaskVec =
2134 DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002135 &ShufMask[0],
2136 sizeof(ShufMask) / sizeof(ShufMask[0]));
Scott Michel266bc8f2007-12-04 22:23:35 +00002137
2138 return DAG.getNode(SPUISD::EXTRACT_ELT0, VT,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002139 DAG.getNode(SPUISD::SHUFB, N.getValueType(),
2140 N, N, ShufMaskVec));
2141
Scott Michel266bc8f2007-12-04 22:23:35 +00002142}
2143
2144static SDOperand LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2145 SDOperand VecOp = Op.getOperand(0);
2146 SDOperand ValOp = Op.getOperand(1);
2147 SDOperand IdxOp = Op.getOperand(2);
2148 MVT::ValueType VT = Op.getValueType();
2149
2150 ConstantSDNode *CN = cast<ConstantSDNode>(IdxOp);
2151 assert(CN != 0 && "LowerINSERT_VECTOR_ELT: Index is not constant!");
2152
2153 MVT::ValueType PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2154 // Use $2 because it's always 16-byte aligned and it's available:
2155 SDOperand PtrBase = DAG.getRegister(SPU::R2, PtrVT);
2156
2157 SDOperand result =
2158 DAG.getNode(SPUISD::SHUFB, VT,
2159 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, ValOp),
2160 VecOp,
2161 DAG.getNode(SPUISD::INSERT_MASK, VT,
2162 DAG.getNode(ISD::ADD, PtrVT,
2163 PtrBase,
2164 DAG.getConstant(CN->getValue(),
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002165 PtrVT))));
Scott Michel266bc8f2007-12-04 22:23:35 +00002166
2167 return result;
2168}
2169
2170static SDOperand LowerI8Math(SDOperand Op, SelectionDAG &DAG, unsigned Opc) {
2171 SDOperand N0 = Op.getOperand(0); // Everything has at least one operand
2172
2173 assert(Op.getValueType() == MVT::i8);
2174 switch (Opc) {
2175 default:
2176 assert(0 && "Unhandled i8 math operator");
2177 /*NOTREACHED*/
2178 break;
2179 case ISD::SUB: {
2180 // 8-bit subtraction: Promote the arguments up to 16-bits and truncate
2181 // the result:
2182 SDOperand N1 = Op.getOperand(1);
2183 N0 = (N0.getOpcode() != ISD::Constant
2184 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N0)
2185 : DAG.getConstant(cast<ConstantSDNode>(N0)->getValue(), MVT::i16));
2186 N1 = (N1.getOpcode() != ISD::Constant
2187 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N1)
2188 : DAG.getConstant(cast<ConstantSDNode>(N1)->getValue(), MVT::i16));
2189 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
2190 DAG.getNode(Opc, MVT::i16, N0, N1));
2191 }
2192 case ISD::ROTR:
2193 case ISD::ROTL: {
2194 SDOperand N1 = Op.getOperand(1);
2195 unsigned N1Opc;
2196 N0 = (N0.getOpcode() != ISD::Constant
2197 ? DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, N0)
2198 : DAG.getConstant(cast<ConstantSDNode>(N0)->getValue(), MVT::i16));
2199 N1Opc = (N1.getValueType() < MVT::i16 ? ISD::ZERO_EXTEND : ISD::TRUNCATE);
2200 N1 = (N1.getOpcode() != ISD::Constant
2201 ? DAG.getNode(N1Opc, MVT::i16, N1)
2202 : DAG.getConstant(cast<ConstantSDNode>(N1)->getValue(), MVT::i16));
2203 SDOperand ExpandArg =
2204 DAG.getNode(ISD::OR, MVT::i16, N0,
2205 DAG.getNode(ISD::SHL, MVT::i16,
2206 N0, DAG.getConstant(8, MVT::i16)));
2207 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
2208 DAG.getNode(Opc, MVT::i16, ExpandArg, N1));
2209 }
2210 case ISD::SRL:
2211 case ISD::SHL: {
2212 SDOperand N1 = Op.getOperand(1);
2213 unsigned N1Opc;
2214 N0 = (N0.getOpcode() != ISD::Constant
2215 ? DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, N0)
2216 : DAG.getConstant(cast<ConstantSDNode>(N0)->getValue(), MVT::i16));
2217 N1Opc = (N1.getValueType() < MVT::i16 ? ISD::ZERO_EXTEND : ISD::TRUNCATE);
2218 N1 = (N1.getOpcode() != ISD::Constant
2219 ? DAG.getNode(N1Opc, MVT::i16, N1)
2220 : DAG.getConstant(cast<ConstantSDNode>(N1)->getValue(), MVT::i16));
2221 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
2222 DAG.getNode(Opc, MVT::i16, N0, N1));
2223 }
2224 case ISD::SRA: {
2225 SDOperand N1 = Op.getOperand(1);
2226 unsigned N1Opc;
2227 N0 = (N0.getOpcode() != ISD::Constant
2228 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N0)
2229 : DAG.getConstant(cast<ConstantSDNode>(N0)->getValue(), MVT::i16));
2230 N1Opc = (N1.getValueType() < MVT::i16 ? ISD::SIGN_EXTEND : ISD::TRUNCATE);
2231 N1 = (N1.getOpcode() != ISD::Constant
2232 ? DAG.getNode(N1Opc, MVT::i16, N1)
2233 : DAG.getConstant(cast<ConstantSDNode>(N1)->getValue(), MVT::i16));
2234 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
2235 DAG.getNode(Opc, MVT::i16, N0, N1));
2236 }
2237 case ISD::MUL: {
2238 SDOperand N1 = Op.getOperand(1);
2239 unsigned N1Opc;
2240 N0 = (N0.getOpcode() != ISD::Constant
2241 ? DAG.getNode(ISD::SIGN_EXTEND, MVT::i16, N0)
2242 : DAG.getConstant(cast<ConstantSDNode>(N0)->getValue(), MVT::i16));
2243 N1Opc = (N1.getValueType() < MVT::i16 ? ISD::SIGN_EXTEND : ISD::TRUNCATE);
2244 N1 = (N1.getOpcode() != ISD::Constant
2245 ? DAG.getNode(N1Opc, MVT::i16, N1)
2246 : DAG.getConstant(cast<ConstantSDNode>(N1)->getValue(), MVT::i16));
2247 return DAG.getNode(ISD::TRUNCATE, MVT::i8,
2248 DAG.getNode(Opc, MVT::i16, N0, N1));
2249 break;
2250 }
2251 }
2252
2253 return SDOperand();
2254}
2255
2256//! Lower byte immediate operations for v16i8 vectors:
2257static SDOperand
2258LowerByteImmed(SDOperand Op, SelectionDAG &DAG) {
2259 SDOperand ConstVec;
2260 SDOperand Arg;
2261 MVT::ValueType VT = Op.getValueType();
2262
2263 ConstVec = Op.getOperand(0);
2264 Arg = Op.getOperand(1);
2265 if (ConstVec.Val->getOpcode() != ISD::BUILD_VECTOR) {
2266 if (ConstVec.Val->getOpcode() == ISD::BIT_CONVERT) {
2267 ConstVec = ConstVec.getOperand(0);
2268 } else {
2269 ConstVec = Op.getOperand(1);
2270 Arg = Op.getOperand(0);
2271 if (ConstVec.Val->getOpcode() == ISD::BIT_CONVERT) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002272 ConstVec = ConstVec.getOperand(0);
Scott Michel266bc8f2007-12-04 22:23:35 +00002273 }
2274 }
2275 }
2276
2277 if (ConstVec.Val->getOpcode() == ISD::BUILD_VECTOR) {
2278 uint64_t VectorBits[2];
2279 uint64_t UndefBits[2];
2280 uint64_t SplatBits, SplatUndef;
2281 int SplatSize;
2282
2283 if (!GetConstantBuildVectorBits(ConstVec.Val, VectorBits, UndefBits)
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002284 && isConstantSplat(VectorBits, UndefBits,
2285 MVT::getSizeInBits(MVT::getVectorElementType(VT)),
2286 SplatBits, SplatUndef, SplatSize)) {
Scott Michel266bc8f2007-12-04 22:23:35 +00002287 SDOperand tcVec[16];
2288 SDOperand tc = DAG.getTargetConstant(SplatBits & 0xff, MVT::i8);
2289 const size_t tcVecSize = sizeof(tcVec) / sizeof(tcVec[0]);
2290
2291 // Turn the BUILD_VECTOR into a set of target constants:
2292 for (size_t i = 0; i < tcVecSize; ++i)
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002293 tcVec[i] = tc;
Scott Michel266bc8f2007-12-04 22:23:35 +00002294
2295 return DAG.getNode(Op.Val->getOpcode(), VT, Arg,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002296 DAG.getNode(ISD::BUILD_VECTOR, VT, tcVec, tcVecSize));
Scott Michel266bc8f2007-12-04 22:23:35 +00002297 }
2298 }
2299
2300 return SDOperand();
2301}
2302
2303//! Lower i32 multiplication
2304static SDOperand LowerMUL(SDOperand Op, SelectionDAG &DAG, unsigned VT,
2305 unsigned Opc) {
2306 switch (VT) {
2307 default:
2308 cerr << "CellSPU: Unknown LowerMUL value type, got "
2309 << MVT::getValueTypeString(Op.getValueType())
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002310 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +00002311 abort();
2312 /*NOTREACHED*/
2313
2314 case MVT::i32: {
2315 SDOperand rA = Op.getOperand(0);
2316 SDOperand rB = Op.getOperand(1);
2317
2318 return DAG.getNode(ISD::ADD, MVT::i32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002319 DAG.getNode(ISD::ADD, MVT::i32,
2320 DAG.getNode(SPUISD::MPYH, MVT::i32, rA, rB),
2321 DAG.getNode(SPUISD::MPYH, MVT::i32, rB, rA)),
2322 DAG.getNode(SPUISD::MPYU, MVT::i32, rA, rB));
Scott Michel266bc8f2007-12-04 22:23:35 +00002323 }
2324 }
2325
2326 return SDOperand();
2327}
2328
2329//! Custom lowering for CTPOP (count population)
2330/*!
2331 Custom lowering code that counts the number ones in the input
2332 operand. SPU has such an instruction, but it counts the number of
2333 ones per byte, which then have to be accumulated.
2334*/
2335static SDOperand LowerCTPOP(SDOperand Op, SelectionDAG &DAG) {
2336 unsigned VT = Op.getValueType();
2337 unsigned vecVT = MVT::getVectorType(VT, (128 / MVT::getSizeInBits(VT)));
2338
2339 switch (VT) {
2340 case MVT::i8: {
2341 SDOperand N = Op.getOperand(0);
2342 SDOperand Elt0 = DAG.getConstant(0, MVT::i32);
2343
2344 SDOperand Promote = DAG.getNode(SPUISD::PROMOTE_SCALAR, vecVT, N, N);
2345 SDOperand CNTB = DAG.getNode(SPUISD::CNTB, vecVT, Promote);
2346
2347 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i8, CNTB, Elt0);
2348 }
2349
2350 case MVT::i16: {
2351 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00002352 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +00002353
Chris Lattner84bc5422007-12-31 04:13:23 +00002354 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R16CRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00002355
2356 SDOperand N = Op.getOperand(0);
2357 SDOperand Elt0 = DAG.getConstant(0, MVT::i16);
2358 SDOperand Mask0 = DAG.getConstant(0x0f, MVT::i16);
2359 SDOperand Shift1 = DAG.getConstant(8, MVT::i16);
2360
2361 SDOperand Promote = DAG.getNode(SPUISD::PROMOTE_SCALAR, vecVT, N, N);
2362 SDOperand CNTB = DAG.getNode(SPUISD::CNTB, vecVT, Promote);
2363
2364 // CNTB_result becomes the chain to which all of the virtual registers
2365 // CNTB_reg, SUM1_reg become associated:
2366 SDOperand CNTB_result =
2367 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, CNTB, Elt0);
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002368
Scott Michel266bc8f2007-12-04 22:23:35 +00002369 SDOperand CNTB_rescopy =
2370 DAG.getCopyToReg(CNTB_result, CNTB_reg, CNTB_result);
2371
2372 SDOperand Tmp1 = DAG.getCopyFromReg(CNTB_rescopy, CNTB_reg, MVT::i16);
2373
2374 return DAG.getNode(ISD::AND, MVT::i16,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002375 DAG.getNode(ISD::ADD, MVT::i16,
2376 DAG.getNode(ISD::SRL, MVT::i16,
2377 Tmp1, Shift1),
2378 Tmp1),
2379 Mask0);
Scott Michel266bc8f2007-12-04 22:23:35 +00002380 }
2381
2382 case MVT::i32: {
2383 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00002384 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +00002385
Chris Lattner84bc5422007-12-31 04:13:23 +00002386 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
2387 unsigned SUM1_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00002388
2389 SDOperand N = Op.getOperand(0);
2390 SDOperand Elt0 = DAG.getConstant(0, MVT::i32);
2391 SDOperand Mask0 = DAG.getConstant(0xff, MVT::i32);
2392 SDOperand Shift1 = DAG.getConstant(16, MVT::i32);
2393 SDOperand Shift2 = DAG.getConstant(8, MVT::i32);
2394
2395 SDOperand Promote = DAG.getNode(SPUISD::PROMOTE_SCALAR, vecVT, N, N);
2396 SDOperand CNTB = DAG.getNode(SPUISD::CNTB, vecVT, Promote);
2397
2398 // CNTB_result becomes the chain to which all of the virtual registers
2399 // CNTB_reg, SUM1_reg become associated:
2400 SDOperand CNTB_result =
2401 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, CNTB, Elt0);
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002402
Scott Michel266bc8f2007-12-04 22:23:35 +00002403 SDOperand CNTB_rescopy =
2404 DAG.getCopyToReg(CNTB_result, CNTB_reg, CNTB_result);
2405
2406 SDOperand Comp1 =
2407 DAG.getNode(ISD::SRL, MVT::i32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002408 DAG.getCopyFromReg(CNTB_rescopy, CNTB_reg, MVT::i32), Shift1);
Scott Michel266bc8f2007-12-04 22:23:35 +00002409
2410 SDOperand Sum1 =
2411 DAG.getNode(ISD::ADD, MVT::i32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002412 Comp1, DAG.getCopyFromReg(CNTB_rescopy, CNTB_reg, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +00002413
2414 SDOperand Sum1_rescopy =
2415 DAG.getCopyToReg(CNTB_result, SUM1_reg, Sum1);
2416
2417 SDOperand Comp2 =
2418 DAG.getNode(ISD::SRL, MVT::i32,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002419 DAG.getCopyFromReg(Sum1_rescopy, SUM1_reg, MVT::i32),
2420 Shift2);
Scott Michel266bc8f2007-12-04 22:23:35 +00002421 SDOperand Sum2 =
2422 DAG.getNode(ISD::ADD, MVT::i32, Comp2,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002423 DAG.getCopyFromReg(Sum1_rescopy, SUM1_reg, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +00002424
2425 return DAG.getNode(ISD::AND, MVT::i32, Sum2, Mask0);
2426 }
2427
2428 case MVT::i64:
2429 break;
2430 }
2431
2432 return SDOperand();
2433}
2434
2435/// LowerOperation - Provide custom lowering hooks for some operations.
2436///
2437SDOperand
2438SPUTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG)
2439{
2440 switch (Op.getOpcode()) {
2441 default: {
2442 cerr << "SPUTargetLowering::LowerOperation(): need to lower this!\n";
2443 cerr << "Op.getOpcode() = " << Op.getOpcode() << "\n";
2444 cerr << "*Op.Val:\n";
2445 Op.Val->dump();
2446 abort();
2447 }
2448 case ISD::LOAD:
2449 case ISD::SEXTLOAD:
2450 case ISD::ZEXTLOAD:
2451 return LowerLOAD(Op, DAG, SPUTM.getSubtargetImpl());
2452 case ISD::STORE:
2453 return LowerSTORE(Op, DAG, SPUTM.getSubtargetImpl());
2454 case ISD::ConstantPool:
2455 return LowerConstantPool(Op, DAG, SPUTM.getSubtargetImpl());
2456 case ISD::GlobalAddress:
2457 return LowerGlobalAddress(Op, DAG, SPUTM.getSubtargetImpl());
2458 case ISD::JumpTable:
2459 return LowerJumpTable(Op, DAG, SPUTM.getSubtargetImpl());
2460 case ISD::Constant:
2461 return LowerConstant(Op, DAG);
2462 case ISD::ConstantFP:
2463 return LowerConstantFP(Op, DAG);
Scott Michel58c58182008-01-17 20:38:41 +00002464 case ISD::BRCOND:
2465 return LowerBRCOND(Op, DAG);
Scott Michel266bc8f2007-12-04 22:23:35 +00002466 case ISD::FORMAL_ARGUMENTS:
Scott Michel58c58182008-01-17 20:38:41 +00002467 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Scott Michel266bc8f2007-12-04 22:23:35 +00002468 case ISD::CALL:
Scott Michel9de5d0d2008-01-11 02:53:15 +00002469 return LowerCALL(Op, DAG, SPUTM.getSubtargetImpl());
Scott Michel266bc8f2007-12-04 22:23:35 +00002470 case ISD::RET:
2471 return LowerRET(Op, DAG, getTargetMachine());
2472
2473 // i8 math ops:
2474 case ISD::SUB:
2475 case ISD::ROTR:
2476 case ISD::ROTL:
2477 case ISD::SRL:
2478 case ISD::SHL:
2479 case ISD::SRA:
2480 return LowerI8Math(Op, DAG, Op.getOpcode());
2481
2482 // Vector-related lowering.
2483 case ISD::BUILD_VECTOR:
2484 return LowerBUILD_VECTOR(Op, DAG);
2485 case ISD::SCALAR_TO_VECTOR:
2486 return LowerSCALAR_TO_VECTOR(Op, DAG);
2487 case ISD::VECTOR_SHUFFLE:
2488 return LowerVECTOR_SHUFFLE(Op, DAG);
2489 case ISD::EXTRACT_VECTOR_ELT:
2490 return LowerEXTRACT_VECTOR_ELT(Op, DAG);
2491 case ISD::INSERT_VECTOR_ELT:
2492 return LowerINSERT_VECTOR_ELT(Op, DAG);
2493
2494 // Look for ANDBI, ORBI and XORBI opportunities and lower appropriately:
2495 case ISD::AND:
2496 case ISD::OR:
2497 case ISD::XOR:
2498 return LowerByteImmed(Op, DAG);
2499
2500 // Vector and i8 multiply:
2501 case ISD::MUL:
2502 if (MVT::isVector(Op.getValueType()))
2503 return LowerVectorMUL(Op, DAG);
2504 else if (Op.getValueType() == MVT::i8)
2505 return LowerI8Math(Op, DAG, Op.getOpcode());
2506 else
2507 return LowerMUL(Op, DAG, Op.getValueType(), Op.getOpcode());
2508
2509 case ISD::FDIV:
2510 if (Op.getValueType() == MVT::f32 || Op.getValueType() == MVT::v4f32)
2511 return LowerFDIVf32(Op, DAG);
2512// else if (Op.getValueType() == MVT::f64)
2513// return LowerFDIVf64(Op, DAG);
2514 else
2515 assert(0 && "Calling FDIV on unsupported MVT");
2516
2517 case ISD::CTPOP:
2518 return LowerCTPOP(Op, DAG);
2519 }
2520
2521 return SDOperand();
2522}
2523
2524//===----------------------------------------------------------------------===//
Scott Michel266bc8f2007-12-04 22:23:35 +00002525// Target Optimization Hooks
2526//===----------------------------------------------------------------------===//
2527
2528SDOperand
2529SPUTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const
2530{
2531#if 0
2532 TargetMachine &TM = getTargetMachine();
Scott Michel053c1da2008-01-29 02:16:57 +00002533#endif
2534 const SPUSubtarget *ST = SPUTM.getSubtargetImpl();
Scott Michel266bc8f2007-12-04 22:23:35 +00002535 SelectionDAG &DAG = DCI.DAG;
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002536 SDOperand N0 = N->getOperand(0); // everything has at least one operand
Scott Michel266bc8f2007-12-04 22:23:35 +00002537
2538 switch (N->getOpcode()) {
2539 default: break;
Scott Michel053c1da2008-01-29 02:16:57 +00002540 case SPUISD::IndirectAddr: {
2541 if (!ST->usingLargeMem() && N0.getOpcode() == SPUISD::AFormAddr) {
2542 ConstantSDNode *CN = cast<ConstantSDNode>(N->getOperand(1));
2543 if (CN->getValue() == 0) {
2544 // (SPUindirect (SPUaform <addr>, 0), 0) ->
2545 // (SPUaform <addr>, 0)
2546
2547 DEBUG(cerr << "Replace: ");
2548 DEBUG(N->dump(&DAG));
2549 DEBUG(cerr << "\nWith: ");
2550 DEBUG(N0.Val->dump(&DAG));
2551 DEBUG(cerr << "\n");
2552
2553 return N0;
2554 }
2555 }
Scott Michel266bc8f2007-12-04 22:23:35 +00002556 }
Scott Michel053c1da2008-01-29 02:16:57 +00002557 case ISD::ADD: {
2558 SDOperand Op0 = N->getOperand(0);
2559 SDOperand Op1 = N->getOperand(1);
2560
2561 if ((Op1.getOpcode() == ISD::Constant
2562 || Op1.getOpcode() == ISD::TargetConstant)
2563 && Op0.getOpcode() == SPUISD::IndirectAddr) {
2564 SDOperand Op01 = Op0.getOperand(1);
2565 if (Op01.getOpcode() == ISD::Constant
2566 || Op01.getOpcode() == ISD::TargetConstant) {
2567 // (add <const>, (SPUindirect <arg>, <const>)) ->
2568 // (SPUindirect <arg>, <const + const>)
2569 ConstantSDNode *CN0 = cast<ConstantSDNode>(Op1);
2570 ConstantSDNode *CN1 = cast<ConstantSDNode>(Op01);
2571 SDOperand combinedConst =
2572 DAG.getConstant(CN0->getValue() + CN1->getValue(),
2573 Op0.getValueType());
2574
2575 DEBUG(cerr << "Replace: (add " << CN0->getValue() << ", "
2576 << "(SPUindirect <arg>, " << CN1->getValue() << "))\n");
2577 DEBUG(cerr << "With: (SPUindirect <arg>, "
2578 << CN0->getValue() + CN1->getValue() << ")\n");
2579 return DAG.getNode(SPUISD::IndirectAddr, Op0.getValueType(),
2580 Op0.getOperand(0), combinedConst);
2581 }
2582 } else if ((Op0.getOpcode() == ISD::Constant
2583 || Op0.getOpcode() == ISD::TargetConstant)
2584 && Op1.getOpcode() == SPUISD::IndirectAddr) {
2585 SDOperand Op11 = Op1.getOperand(1);
2586 if (Op11.getOpcode() == ISD::Constant
2587 || Op11.getOpcode() == ISD::TargetConstant) {
2588 // (add (SPUindirect <arg>, <const>), <const>) ->
2589 // (SPUindirect <arg>, <const + const>)
2590 ConstantSDNode *CN0 = cast<ConstantSDNode>(Op0);
2591 ConstantSDNode *CN1 = cast<ConstantSDNode>(Op11);
2592 SDOperand combinedConst =
2593 DAG.getConstant(CN0->getValue() + CN1->getValue(),
2594 Op0.getValueType());
2595
2596 DEBUG(cerr << "Replace: (add " << CN0->getValue() << ", "
2597 << "(SPUindirect <arg>, " << CN1->getValue() << "))\n");
2598 DEBUG(cerr << "With: (SPUindirect <arg>, "
2599 << CN0->getValue() + CN1->getValue() << ")\n");
2600
2601 return DAG.getNode(SPUISD::IndirectAddr, Op1.getValueType(),
2602 Op1.getOperand(0), combinedConst);
2603 }
2604 }
2605 }
2606 }
Scott Michel58c58182008-01-17 20:38:41 +00002607 // Otherwise, return unchanged.
Scott Michel266bc8f2007-12-04 22:23:35 +00002608 return SDOperand();
2609}
2610
2611//===----------------------------------------------------------------------===//
2612// Inline Assembly Support
2613//===----------------------------------------------------------------------===//
2614
2615/// getConstraintType - Given a constraint letter, return the type of
2616/// constraint it is for this target.
2617SPUTargetLowering::ConstraintType
2618SPUTargetLowering::getConstraintType(const std::string &ConstraintLetter) const {
2619 if (ConstraintLetter.size() == 1) {
2620 switch (ConstraintLetter[0]) {
2621 default: break;
2622 case 'b':
2623 case 'r':
2624 case 'f':
2625 case 'v':
2626 case 'y':
2627 return C_RegisterClass;
2628 }
2629 }
2630 return TargetLowering::getConstraintType(ConstraintLetter);
2631}
2632
2633std::pair<unsigned, const TargetRegisterClass*>
2634SPUTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
2635 MVT::ValueType VT) const
2636{
2637 if (Constraint.size() == 1) {
2638 // GCC RS6000 Constraint Letters
2639 switch (Constraint[0]) {
2640 case 'b': // R1-R31
2641 case 'r': // R0-R31
2642 if (VT == MVT::i64)
2643 return std::make_pair(0U, SPU::R64CRegisterClass);
2644 return std::make_pair(0U, SPU::R32CRegisterClass);
2645 case 'f':
2646 if (VT == MVT::f32)
2647 return std::make_pair(0U, SPU::R32FPRegisterClass);
2648 else if (VT == MVT::f64)
2649 return std::make_pair(0U, SPU::R64FPRegisterClass);
2650 break;
2651 case 'v':
2652 return std::make_pair(0U, SPU::GPRCRegisterClass);
2653 }
2654 }
2655
2656 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2657}
2658
2659void
2660SPUTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00002661 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002662 APInt &KnownZero,
2663 APInt &KnownOne,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002664 const SelectionDAG &DAG,
2665 unsigned Depth ) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002666 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Scott Michel266bc8f2007-12-04 22:23:35 +00002667}
2668
2669// LowerAsmOperandForConstraint
2670void
2671SPUTargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
2672 char ConstraintLetter,
2673 std::vector<SDOperand> &Ops,
2674 SelectionDAG &DAG) {
2675 // Default, for the time being, to the base class handler
2676 TargetLowering::LowerAsmOperandForConstraint(Op, ConstraintLetter, Ops, DAG);
2677}
2678
2679/// isLegalAddressImmediate - Return true if the integer value can be used
2680/// as the offset of the target addressing mode.
2681bool SPUTargetLowering::isLegalAddressImmediate(int64_t V, const Type *Ty) const {
2682 // SPU's addresses are 256K:
2683 return (V > -(1 << 18) && V < (1 << 18) - 1);
2684}
2685
2686bool SPUTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
2687 return false;
2688}