blob: ce7ba72e77dcc7ef7797a15a52c91bc6756b39bc [file] [log] [blame]
Scott Michel6e22c652007-12-04 22:23:35 +00001//
Scott Michel839ad0a2009-03-17 01:15:45 +00002//===-- SPUISelLowering.cpp - Cell SPU DAG Lowering Implementation --------===//
Scott Michel6e22c652007-12-04 22:23:35 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Michel6e22c652007-12-04 22:23:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SPUTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPURegisterNames.h"
15#include "SPUISelLowering.h"
16#include "SPUTargetMachine.h"
Scott Michelc3a19102008-04-30 00:30:08 +000017#include "SPUFrameInfo.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
20#include "llvm/Intrinsics.h"
Scott Michel9e3e4a92009-01-26 03:31:40 +000021#include "llvm/CallingConv.h"
Scott Michel6e22c652007-12-04 22:23:35 +000022#include "llvm/CodeGen/CallingConvLower.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Scott Michel6e22c652007-12-04 22:23:35 +000027#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
29#include "llvm/Target/TargetOptions.h"
30#include "llvm/ADT/VectorExtras.h"
Scott Michel6e22c652007-12-04 22:23:35 +000031#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000032#include "llvm/Support/ErrorHandling.h"
Scott Michel6e22c652007-12-04 22:23:35 +000033#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/raw_ostream.h"
Scott Michel6e22c652007-12-04 22:23:35 +000035#include <map>
36
37using namespace llvm;
38
39// Used in getTargetNodeName() below
40namespace {
41 std::map<unsigned, const char *> node_names;
42
Duncan Sands13237ac2008-06-06 12:08:01 +000043 //! MVT mapping to useful data for Cell SPU
Scott Michel6e22c652007-12-04 22:23:35 +000044 struct valtype_map_s {
Scott Michel0be03392008-11-22 23:50:42 +000045 const MVT valtype;
46 const int prefslot_byte;
Scott Michel6e22c652007-12-04 22:23:35 +000047 };
Scott Michelfe095082008-07-16 17:17:29 +000048
Scott Michel6e22c652007-12-04 22:23:35 +000049 const valtype_map_s valtype_map[] = {
50 { MVT::i1, 3 },
51 { MVT::i8, 3 },
52 { MVT::i16, 2 },
53 { MVT::i32, 0 },
54 { MVT::f32, 0 },
55 { MVT::i64, 0 },
56 { MVT::f64, 0 },
57 { MVT::i128, 0 }
58 };
59
60 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
61
Duncan Sands13237ac2008-06-06 12:08:01 +000062 const valtype_map_s *getValueTypeMapEntry(MVT VT) {
Scott Michel6e22c652007-12-04 22:23:35 +000063 const valtype_map_s *retval = 0;
64
65 for (size_t i = 0; i < n_valtype_map; ++i) {
66 if (valtype_map[i].valtype == VT) {
Scott Michelbb713ae2008-01-30 02:55:46 +000067 retval = valtype_map + i;
68 break;
Scott Michel6e22c652007-12-04 22:23:35 +000069 }
70 }
71
72#ifndef NDEBUG
73 if (retval == 0) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +000074 std::string msg;
75 raw_string_ostream Msg(msg);
76 Msg << "getValueTypeMapEntry returns NULL for "
77 << VT.getMVTString();
78 llvm_report_error(Msg.str());
Scott Michel6e22c652007-12-04 22:23:35 +000079 }
80#endif
81
82 return retval;
83 }
Scott Michela292fc62009-01-15 04:41:47 +000084
Scott Michel9e3e4a92009-01-26 03:31:40 +000085 //! Expand a library call into an actual call DAG node
86 /*!
87 \note
88 This code is taken from SelectionDAGLegalize, since it is not exposed as
89 part of the LLVM SelectionDAG API.
90 */
91
92 SDValue
93 ExpandLibCall(RTLIB::Libcall LC, SDValue Op, SelectionDAG &DAG,
94 bool isSigned, SDValue &Hi, SPUTargetLowering &TLI) {
95 // The input chain to this libcall is the entry node of the function.
96 // Legalizing the call will automatically add the previous call to the
97 // dependence.
98 SDValue InChain = DAG.getEntryNode();
99
100 TargetLowering::ArgListTy Args;
101 TargetLowering::ArgListEntry Entry;
102 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
103 MVT ArgVT = Op.getOperand(i).getValueType();
Owen Anderson4056ca92009-07-29 22:17:13 +0000104 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michel9e3e4a92009-01-26 03:31:40 +0000105 Entry.Node = Op.getOperand(i);
106 Entry.Ty = ArgTy;
107 Entry.isSExt = isSigned;
108 Entry.isZExt = !isSigned;
109 Args.push_back(Entry);
110 }
111 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
112 TLI.getPointerTy());
113
114 // Splice the libcall in wherever FindInputOutputChains tells us to.
Owen Anderson4056ca92009-07-29 22:17:13 +0000115 const Type *RetTy = Op.getNode()->getValueType(0).getTypeForMVT();
Scott Michel9e3e4a92009-01-26 03:31:40 +0000116 std::pair<SDValue, SDValue> CallInfo =
117 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Tilmann Schelleraea60592009-07-03 06:44:53 +0000118 0, CallingConv::C, false, Callee, Args, DAG,
Dale Johannesen9c310712009-02-07 19:59:05 +0000119 Op.getDebugLoc());
Scott Michel9e3e4a92009-01-26 03:31:40 +0000120
121 return CallInfo.first;
122 }
Scott Michel6e22c652007-12-04 22:23:35 +0000123}
124
125SPUTargetLowering::SPUTargetLowering(SPUTargetMachine &TM)
Chris Lattner5e693ed2009-07-28 03:13:23 +0000126 : TargetLowering(TM, new TargetLoweringObjectFileELF()),
127 SPUTM(TM) {
Scott Michel6e22c652007-12-04 22:23:35 +0000128 // Fold away setcc operations if possible.
129 setPow2DivIsCheap();
130
131 // Use _setjmp/_longjmp instead of setjmp/longjmp.
132 setUseUnderscoreSetJmp(true);
133 setUseUnderscoreLongJmp(true);
Scott Michelfe095082008-07-16 17:17:29 +0000134
Scott Micheled7d79f2009-01-21 04:58:48 +0000135 // Set RTLIB libcall names as used by SPU:
136 setLibcallName(RTLIB::DIV_F64, "__fast_divdf3");
137
Scott Michel6e22c652007-12-04 22:23:35 +0000138 // Set up the SPU's register classes:
Scott Michelc5cccb92007-12-17 22:32:34 +0000139 addRegisterClass(MVT::i8, SPU::R8CRegisterClass);
140 addRegisterClass(MVT::i16, SPU::R16CRegisterClass);
141 addRegisterClass(MVT::i32, SPU::R32CRegisterClass);
142 addRegisterClass(MVT::i64, SPU::R64CRegisterClass);
143 addRegisterClass(MVT::f32, SPU::R32FPRegisterClass);
144 addRegisterClass(MVT::f64, SPU::R64FPRegisterClass);
Scott Michel6e22c652007-12-04 22:23:35 +0000145 addRegisterClass(MVT::i128, SPU::GPRCRegisterClass);
Scott Michelfe095082008-07-16 17:17:29 +0000146
Scott Michel6e22c652007-12-04 22:23:35 +0000147 // SPU has no sign or zero extended loads for i1, i8, i16:
Evan Cheng07d53b12008-10-14 21:26:46 +0000148 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
149 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
150 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +0000151
Scott Michel82335272008-12-27 04:51:36 +0000152 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
153 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Scott Michel73640252008-12-02 19:53:53 +0000154
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000155 setTruncStoreAction(MVT::i128, MVT::i64, Expand);
156 setTruncStoreAction(MVT::i128, MVT::i32, Expand);
157 setTruncStoreAction(MVT::i128, MVT::i16, Expand);
158 setTruncStoreAction(MVT::i128, MVT::i8, Expand);
159
160 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
161
Scott Michel6e22c652007-12-04 22:23:35 +0000162 // SPU constant load actions are custom lowered:
Nate Begeman4b3210a2008-02-14 18:43:04 +0000163 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
Scott Michel6e22c652007-12-04 22:23:35 +0000164 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
165
166 // SPU's loads and stores have to be custom lowered:
Scott Michel6887caf2009-01-06 03:36:14 +0000167 for (unsigned sctype = (unsigned) MVT::i8; sctype < (unsigned) MVT::i128;
Scott Michel6e22c652007-12-04 22:23:35 +0000168 ++sctype) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000169 MVT VT = (MVT::SimpleValueType)sctype;
170
Scott Michel82335272008-12-27 04:51:36 +0000171 setOperationAction(ISD::LOAD, VT, Custom);
172 setOperationAction(ISD::STORE, VT, Custom);
173 setLoadExtAction(ISD::EXTLOAD, VT, Custom);
174 setLoadExtAction(ISD::ZEXTLOAD, VT, Custom);
175 setLoadExtAction(ISD::SEXTLOAD, VT, Custom);
176
177 for (unsigned stype = sctype - 1; stype >= (unsigned) MVT::i8; --stype) {
178 MVT StoreVT = (MVT::SimpleValueType) stype;
179 setTruncStoreAction(VT, StoreVT, Expand);
180 }
Scott Michel6e22c652007-12-04 22:23:35 +0000181 }
182
Scott Michel82335272008-12-27 04:51:36 +0000183 for (unsigned sctype = (unsigned) MVT::f32; sctype < (unsigned) MVT::f64;
184 ++sctype) {
185 MVT VT = (MVT::SimpleValueType) sctype;
186
187 setOperationAction(ISD::LOAD, VT, Custom);
188 setOperationAction(ISD::STORE, VT, Custom);
189
190 for (unsigned stype = sctype - 1; stype >= (unsigned) MVT::f32; --stype) {
191 MVT StoreVT = (MVT::SimpleValueType) stype;
192 setTruncStoreAction(VT, StoreVT, Expand);
193 }
194 }
195
Scott Michel6e22c652007-12-04 22:23:35 +0000196 // Expand the jumptable branches
197 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
198 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Scott Michel0be03392008-11-22 23:50:42 +0000199
200 // Custom lower SELECT_CC for most cases, but expand by default
Scott Michelfe095082008-07-16 17:17:29 +0000201 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Scott Michel0be03392008-11-22 23:50:42 +0000202 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
203 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
204 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
205 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Scott Michel6e22c652007-12-04 22:23:35 +0000206
207 // SPU has no intrinsics for these particular operations:
Andrew Lenharthfedcf472008-02-16 14:46:26 +0000208 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
209
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000210 // SPU has no division/remainder instructions
211 setOperationAction(ISD::SREM, MVT::i8, Expand);
212 setOperationAction(ISD::UREM, MVT::i8, Expand);
213 setOperationAction(ISD::SDIV, MVT::i8, Expand);
214 setOperationAction(ISD::UDIV, MVT::i8, Expand);
215 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
216 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
217 setOperationAction(ISD::SREM, MVT::i16, Expand);
218 setOperationAction(ISD::UREM, MVT::i16, Expand);
219 setOperationAction(ISD::SDIV, MVT::i16, Expand);
220 setOperationAction(ISD::UDIV, MVT::i16, Expand);
221 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
222 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
223 setOperationAction(ISD::SREM, MVT::i32, Expand);
224 setOperationAction(ISD::UREM, MVT::i32, Expand);
225 setOperationAction(ISD::SDIV, MVT::i32, Expand);
226 setOperationAction(ISD::UDIV, MVT::i32, Expand);
227 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
228 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
229 setOperationAction(ISD::SREM, MVT::i64, Expand);
230 setOperationAction(ISD::UREM, MVT::i64, Expand);
231 setOperationAction(ISD::SDIV, MVT::i64, Expand);
232 setOperationAction(ISD::UDIV, MVT::i64, Expand);
233 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
234 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
235 setOperationAction(ISD::SREM, MVT::i128, Expand);
236 setOperationAction(ISD::UREM, MVT::i128, Expand);
237 setOperationAction(ISD::SDIV, MVT::i128, Expand);
238 setOperationAction(ISD::UDIV, MVT::i128, Expand);
239 setOperationAction(ISD::SDIVREM, MVT::i128, Expand);
240 setOperationAction(ISD::UDIVREM, MVT::i128, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000241
Scott Michel6e22c652007-12-04 22:23:35 +0000242 // We don't support sin/cos/sqrt/fmod
243 setOperationAction(ISD::FSIN , MVT::f64, Expand);
244 setOperationAction(ISD::FCOS , MVT::f64, Expand);
245 setOperationAction(ISD::FREM , MVT::f64, Expand);
246 setOperationAction(ISD::FSIN , MVT::f32, Expand);
247 setOperationAction(ISD::FCOS , MVT::f32, Expand);
248 setOperationAction(ISD::FREM , MVT::f32, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000249
Scott Michel9e3e4a92009-01-26 03:31:40 +0000250 // Expand fsqrt to the appropriate libcall (NOTE: should use h/w fsqrt
251 // for f32!)
Scott Michel6e22c652007-12-04 22:23:35 +0000252 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
253 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000254
Scott Michel6e22c652007-12-04 22:23:35 +0000255 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
256 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
257
258 // SPU can do rotate right and left, so legalize it... but customize for i8
259 // because instructions don't exist.
Bill Wendlingaebd2662008-08-31 02:59:23 +0000260
261 // FIXME: Change from "expand" to appropriate type once ROTR is supported in
262 // .td files.
263 setOperationAction(ISD::ROTR, MVT::i32, Expand /*Legal*/);
264 setOperationAction(ISD::ROTR, MVT::i16, Expand /*Legal*/);
265 setOperationAction(ISD::ROTR, MVT::i8, Expand /*Custom*/);
266
Scott Michel6e22c652007-12-04 22:23:35 +0000267 setOperationAction(ISD::ROTL, MVT::i32, Legal);
268 setOperationAction(ISD::ROTL, MVT::i16, Legal);
269 setOperationAction(ISD::ROTL, MVT::i8, Custom);
Scott Michel3726019a2008-11-20 16:36:33 +0000270
Scott Michel6e22c652007-12-04 22:23:35 +0000271 // SPU has no native version of shift left/right for i8
272 setOperationAction(ISD::SHL, MVT::i8, Custom);
273 setOperationAction(ISD::SRL, MVT::i8, Custom);
274 setOperationAction(ISD::SRA, MVT::i8, Custom);
Scott Michelc6918c12008-11-21 02:56:16 +0000275
Scott Michel41236c02008-12-30 23:28:25 +0000276 // Make these operations legal and handle them during instruction selection:
277 setOperationAction(ISD::SHL, MVT::i64, Legal);
278 setOperationAction(ISD::SRL, MVT::i64, Legal);
279 setOperationAction(ISD::SRA, MVT::i64, Legal);
Scott Michel6e22c652007-12-04 22:23:35 +0000280
Scott Michelfe095082008-07-16 17:17:29 +0000281 // Custom lower i8, i32 and i64 multiplications
282 setOperationAction(ISD::MUL, MVT::i8, Custom);
Scott Michelb8ee30d2008-12-29 03:23:36 +0000283 setOperationAction(ISD::MUL, MVT::i32, Legal);
Scott Michela292fc62009-01-15 04:41:47 +0000284 setOperationAction(ISD::MUL, MVT::i64, Legal);
Scott Michelc6918c12008-11-21 02:56:16 +0000285
Eli Friedman48021d12009-06-16 06:40:59 +0000286 // Expand double-width multiplication
287 // FIXME: It would probably be reasonable to support some of these operations
288 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
289 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
290 setOperationAction(ISD::MULHU, MVT::i8, Expand);
291 setOperationAction(ISD::MULHS, MVT::i8, Expand);
292 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
293 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
294 setOperationAction(ISD::MULHU, MVT::i16, Expand);
295 setOperationAction(ISD::MULHS, MVT::i16, Expand);
296 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
297 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
298 setOperationAction(ISD::MULHU, MVT::i32, Expand);
299 setOperationAction(ISD::MULHS, MVT::i32, Expand);
300 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
301 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
302 setOperationAction(ISD::MULHU, MVT::i64, Expand);
303 setOperationAction(ISD::MULHS, MVT::i64, Expand);
304
Scott Micheld831cc42008-06-02 22:18:03 +0000305 // Need to custom handle (some) common i8, i64 math ops
Scott Michel41236c02008-12-30 23:28:25 +0000306 setOperationAction(ISD::ADD, MVT::i8, Custom);
Scott Michela292fc62009-01-15 04:41:47 +0000307 setOperationAction(ISD::ADD, MVT::i64, Legal);
Scott Michel6e22c652007-12-04 22:23:35 +0000308 setOperationAction(ISD::SUB, MVT::i8, Custom);
Scott Michela292fc62009-01-15 04:41:47 +0000309 setOperationAction(ISD::SUB, MVT::i64, Legal);
Scott Michelfe095082008-07-16 17:17:29 +0000310
Scott Michel6e22c652007-12-04 22:23:35 +0000311 // SPU does not have BSWAP. It does have i32 support CTLZ.
312 // CTPOP has to be custom lowered.
313 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
314 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
315
316 setOperationAction(ISD::CTPOP, MVT::i8, Custom);
317 setOperationAction(ISD::CTPOP, MVT::i16, Custom);
318 setOperationAction(ISD::CTPOP, MVT::i32, Custom);
319 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000320 setOperationAction(ISD::CTPOP, MVT::i128, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000321
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000322 setOperationAction(ISD::CTTZ , MVT::i8, Expand);
323 setOperationAction(ISD::CTTZ , MVT::i16, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000324 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
325 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000326 setOperationAction(ISD::CTTZ , MVT::i128, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000327
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000328 setOperationAction(ISD::CTLZ , MVT::i8, Promote);
329 setOperationAction(ISD::CTLZ , MVT::i16, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +0000330 setOperationAction(ISD::CTLZ , MVT::i32, Legal);
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000331 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
332 setOperationAction(ISD::CTLZ , MVT::i128, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000333
Scott Micheld831cc42008-06-02 22:18:03 +0000334 // SPU has a version of select that implements (a&~c)|(b&c), just like
Scott Michel92275422008-03-10 23:49:09 +0000335 // select ought to work:
Scott Michel48e33752008-03-10 16:58:52 +0000336 setOperationAction(ISD::SELECT, MVT::i8, Legal);
Scott Michel42f56b42008-03-05 23:02:02 +0000337 setOperationAction(ISD::SELECT, MVT::i16, Legal);
338 setOperationAction(ISD::SELECT, MVT::i32, Legal);
Scott Michel82335272008-12-27 04:51:36 +0000339 setOperationAction(ISD::SELECT, MVT::i64, Legal);
Scott Michel6e22c652007-12-04 22:23:35 +0000340
Scott Michel48e33752008-03-10 16:58:52 +0000341 setOperationAction(ISD::SETCC, MVT::i8, Legal);
342 setOperationAction(ISD::SETCC, MVT::i16, Legal);
Scott Michelb8ee30d2008-12-29 03:23:36 +0000343 setOperationAction(ISD::SETCC, MVT::i32, Legal);
344 setOperationAction(ISD::SETCC, MVT::i64, Legal);
Scott Micheled7d79f2009-01-21 04:58:48 +0000345 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Scott Michel42f56b42008-03-05 23:02:02 +0000346
Scott Michel82335272008-12-27 04:51:36 +0000347 // Custom lower i128 -> i64 truncates
Scott Michel73640252008-12-02 19:53:53 +0000348 setOperationAction(ISD::TRUNCATE, MVT::i64, Custom);
349
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000350 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
351 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
352 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
353 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
Scott Michel9e3e4a92009-01-26 03:31:40 +0000354 // SPU has a legal FP -> signed INT instruction for f32, but for f64, need
355 // to expand to a libcall, hence the custom lowering:
356 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
357 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000358 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Expand);
359 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
360 setOperationAction(ISD::FP_TO_SINT, MVT::i128, Expand);
361 setOperationAction(ISD::FP_TO_UINT, MVT::i128, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000362
363 // FDIV on SPU requires custom lowering
Scott Michel9e3e4a92009-01-26 03:31:40 +0000364 setOperationAction(ISD::FDIV, MVT::f64, Expand); // to libcall
Scott Michel6e22c652007-12-04 22:23:35 +0000365
Scott Michel49483182009-01-26 22:33:37 +0000366 // SPU has [U|S]INT_TO_FP for f32->i32, but not for f64->i32, f64->i64:
Scott Michel9e3e4a92009-01-26 03:31:40 +0000367 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Scott Michel6e22c652007-12-04 22:23:35 +0000368 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Scott Michel9e3e4a92009-01-26 03:31:40 +0000369 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
370 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Scott Michel6e22c652007-12-04 22:23:35 +0000371 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
Scott Michel9e3e4a92009-01-26 03:31:40 +0000372 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +0000373 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
374 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
375
Scott Michel5f1470f2007-12-20 00:44:13 +0000376 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Legal);
377 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Legal);
378 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Legal);
379 setOperationAction(ISD::BIT_CONVERT, MVT::f64, Legal);
Scott Michel6e22c652007-12-04 22:23:35 +0000380
381 // We cannot sextinreg(i1). Expand to shifts.
382 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000383
Scott Michel6e22c652007-12-04 22:23:35 +0000384 // Support label based line numbers.
Dan Gohman5c73a882008-06-30 20:59:49 +0000385 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000386 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000387
388 // We want to legalize GlobalAddress and ConstantPool nodes into the
Scott Michel6e22c652007-12-04 22:23:35 +0000389 // appropriate instructions to materialize the address.
Scott Michelc6918c12008-11-21 02:56:16 +0000390 for (unsigned sctype = (unsigned) MVT::i8; sctype < (unsigned) MVT::f128;
Scott Michelceae3bb2008-01-29 02:16:57 +0000391 ++sctype) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000392 MVT VT = (MVT::SimpleValueType)sctype;
393
Scott Michelb8ee30d2008-12-29 03:23:36 +0000394 setOperationAction(ISD::GlobalAddress, VT, Custom);
395 setOperationAction(ISD::ConstantPool, VT, Custom);
396 setOperationAction(ISD::JumpTable, VT, Custom);
Scott Michelceae3bb2008-01-29 02:16:57 +0000397 }
Scott Michel6e22c652007-12-04 22:23:35 +0000398
399 // RET must be custom lowered, to meet ABI requirements
400 setOperationAction(ISD::RET, MVT::Other, Custom);
Scott Michelfe095082008-07-16 17:17:29 +0000401
Scott Michel6e22c652007-12-04 22:23:35 +0000402 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
403 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelfe095082008-07-16 17:17:29 +0000404
Scott Michel6e22c652007-12-04 22:23:35 +0000405 // Use the default implementation.
406 setOperationAction(ISD::VAARG , MVT::Other, Expand);
407 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
408 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000409 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000410 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
411 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
412 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Expand);
413
414 // Cell SPU has instructions for converting between i64 and fp.
415 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
416 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Scott Michelfe095082008-07-16 17:17:29 +0000417
Scott Michel6e22c652007-12-04 22:23:35 +0000418 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
419 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
420
421 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
422 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
423
424 // First set operation action for all vector types to expand. Then we
425 // will selectively turn on ones that can be effectively codegen'd.
426 addRegisterClass(MVT::v16i8, SPU::VECREGRegisterClass);
427 addRegisterClass(MVT::v8i16, SPU::VECREGRegisterClass);
428 addRegisterClass(MVT::v4i32, SPU::VECREGRegisterClass);
429 addRegisterClass(MVT::v2i64, SPU::VECREGRegisterClass);
430 addRegisterClass(MVT::v4f32, SPU::VECREGRegisterClass);
431 addRegisterClass(MVT::v2f64, SPU::VECREGRegisterClass);
432
Scott Michel494daa72009-01-06 23:10:38 +0000433 // "Odd size" vector classes that we're willing to support:
434 addRegisterClass(MVT::v2i32, SPU::VECREGRegisterClass);
435
Duncan Sands13237ac2008-06-06 12:08:01 +0000436 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
437 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
438 MVT VT = (MVT::SimpleValueType)i;
Scott Michel6e22c652007-12-04 22:23:35 +0000439
Duncan Sands13237ac2008-06-06 12:08:01 +0000440 // add/sub are legal for all supported vector VT's.
Scott Michel9e3e4a92009-01-26 03:31:40 +0000441 setOperationAction(ISD::ADD, VT, Legal);
442 setOperationAction(ISD::SUB, VT, Legal);
Duncan Sands13237ac2008-06-06 12:08:01 +0000443 // mul has to be custom lowered.
Scott Michel9e3e4a92009-01-26 03:31:40 +0000444 setOperationAction(ISD::MUL, VT, Legal);
Duncan Sands13237ac2008-06-06 12:08:01 +0000445
Scott Michel9e3e4a92009-01-26 03:31:40 +0000446 setOperationAction(ISD::AND, VT, Legal);
447 setOperationAction(ISD::OR, VT, Legal);
448 setOperationAction(ISD::XOR, VT, Legal);
449 setOperationAction(ISD::LOAD, VT, Legal);
450 setOperationAction(ISD::SELECT, VT, Legal);
451 setOperationAction(ISD::STORE, VT, Legal);
Scott Michelfe095082008-07-16 17:17:29 +0000452
Scott Michel6e22c652007-12-04 22:23:35 +0000453 // These operations need to be expanded:
Scott Michel9e3e4a92009-01-26 03:31:40 +0000454 setOperationAction(ISD::SDIV, VT, Expand);
455 setOperationAction(ISD::SREM, VT, Expand);
456 setOperationAction(ISD::UDIV, VT, Expand);
457 setOperationAction(ISD::UREM, VT, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000458
459 // Custom lower build_vector, constant pool spills, insert and
460 // extract vector elements:
Duncan Sands13237ac2008-06-06 12:08:01 +0000461 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
462 setOperationAction(ISD::ConstantPool, VT, Custom);
463 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
464 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
465 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
466 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
Scott Michel6e22c652007-12-04 22:23:35 +0000467 }
468
Scott Michel6e22c652007-12-04 22:23:35 +0000469 setOperationAction(ISD::AND, MVT::v16i8, Custom);
470 setOperationAction(ISD::OR, MVT::v16i8, Custom);
471 setOperationAction(ISD::XOR, MVT::v16i8, Custom);
472 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
Scott Michel8d5841a2008-01-11 02:53:15 +0000473
Scott Michel41236c02008-12-30 23:28:25 +0000474 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Scott Michelb8ee30d2008-12-29 03:23:36 +0000475
Scott Michel6e22c652007-12-04 22:23:35 +0000476 setShiftAmountType(MVT::i32);
Scott Michel82335272008-12-27 04:51:36 +0000477 setBooleanContents(ZeroOrNegativeOneBooleanContent);
Scott Michelfe095082008-07-16 17:17:29 +0000478
Scott Michel6e22c652007-12-04 22:23:35 +0000479 setStackPointerRegisterToSaveRestore(SPU::R1);
Scott Michelfe095082008-07-16 17:17:29 +0000480
Scott Michel6e22c652007-12-04 22:23:35 +0000481 // We have target-specific dag combine patterns for the following nodes:
Scott Michelceae3bb2008-01-29 02:16:57 +0000482 setTargetDAGCombine(ISD::ADD);
Scott Michel7d5eaec2008-02-23 18:41:37 +0000483 setTargetDAGCombine(ISD::ZERO_EXTEND);
484 setTargetDAGCombine(ISD::SIGN_EXTEND);
485 setTargetDAGCombine(ISD::ANY_EXTEND);
Scott Michelfe095082008-07-16 17:17:29 +0000486
Scott Michel6e22c652007-12-04 22:23:35 +0000487 computeRegisterProperties();
Scott Michel0be03392008-11-22 23:50:42 +0000488
Scott Michel8deac5d2008-12-09 03:37:19 +0000489 // Set pre-RA register scheduler default to BURR, which produces slightly
490 // better code than the default (could also be TDRR, but TargetLowering.h
491 // needs a mod to support that model):
492 setSchedulingPreference(SchedulingForRegPressure);
Scott Michel6e22c652007-12-04 22:23:35 +0000493}
494
495const char *
496SPUTargetLowering::getTargetNodeName(unsigned Opcode) const
497{
498 if (node_names.empty()) {
499 node_names[(unsigned) SPUISD::RET_FLAG] = "SPUISD::RET_FLAG";
500 node_names[(unsigned) SPUISD::Hi] = "SPUISD::Hi";
501 node_names[(unsigned) SPUISD::Lo] = "SPUISD::Lo";
502 node_names[(unsigned) SPUISD::PCRelAddr] = "SPUISD::PCRelAddr";
Scott Michel8d5841a2008-01-11 02:53:15 +0000503 node_names[(unsigned) SPUISD::AFormAddr] = "SPUISD::AFormAddr";
Scott Michelceae3bb2008-01-29 02:16:57 +0000504 node_names[(unsigned) SPUISD::IndirectAddr] = "SPUISD::IndirectAddr";
Scott Michel6e22c652007-12-04 22:23:35 +0000505 node_names[(unsigned) SPUISD::LDRESULT] = "SPUISD::LDRESULT";
506 node_names[(unsigned) SPUISD::CALL] = "SPUISD::CALL";
507 node_names[(unsigned) SPUISD::SHUFB] = "SPUISD::SHUFB";
Scott Michel0be03392008-11-22 23:50:42 +0000508 node_names[(unsigned) SPUISD::SHUFFLE_MASK] = "SPUISD::SHUFFLE_MASK";
Scott Michel6e22c652007-12-04 22:23:35 +0000509 node_names[(unsigned) SPUISD::CNTB] = "SPUISD::CNTB";
Scott Michelb8ee30d2008-12-29 03:23:36 +0000510 node_names[(unsigned) SPUISD::PREFSLOT2VEC] = "SPUISD::PREFSLOT2VEC";
Scott Michelefc8c7a2008-11-24 17:11:17 +0000511 node_names[(unsigned) SPUISD::VEC2PREFSLOT] = "SPUISD::VEC2PREFSLOT";
Scott Michel7d5eaec2008-02-23 18:41:37 +0000512 node_names[(unsigned) SPUISD::SHLQUAD_L_BITS] = "SPUISD::SHLQUAD_L_BITS";
513 node_names[(unsigned) SPUISD::SHLQUAD_L_BYTES] = "SPUISD::SHLQUAD_L_BYTES";
Scott Michel6e22c652007-12-04 22:23:35 +0000514 node_names[(unsigned) SPUISD::VEC_SHL] = "SPUISD::VEC_SHL";
515 node_names[(unsigned) SPUISD::VEC_SRL] = "SPUISD::VEC_SRL";
516 node_names[(unsigned) SPUISD::VEC_SRA] = "SPUISD::VEC_SRA";
517 node_names[(unsigned) SPUISD::VEC_ROTL] = "SPUISD::VEC_ROTL";
518 node_names[(unsigned) SPUISD::VEC_ROTR] = "SPUISD::VEC_ROTR";
Scott Micheled7d79f2009-01-21 04:58:48 +0000519 node_names[(unsigned) SPUISD::ROTBYTES_LEFT] = "SPUISD::ROTBYTES_LEFT";
520 node_names[(unsigned) SPUISD::ROTBYTES_LEFT_BITS] =
521 "SPUISD::ROTBYTES_LEFT_BITS";
Scott Micheld831cc42008-06-02 22:18:03 +0000522 node_names[(unsigned) SPUISD::SELECT_MASK] = "SPUISD::SELECT_MASK";
Scott Michel6e22c652007-12-04 22:23:35 +0000523 node_names[(unsigned) SPUISD::SELB] = "SPUISD::SELB";
Scott Michela292fc62009-01-15 04:41:47 +0000524 node_names[(unsigned) SPUISD::ADD64_MARKER] = "SPUISD::ADD64_MARKER";
525 node_names[(unsigned) SPUISD::SUB64_MARKER] = "SPUISD::SUB64_MARKER";
526 node_names[(unsigned) SPUISD::MUL64_MARKER] = "SPUISD::MUL64_MARKER";
Scott Michel6e22c652007-12-04 22:23:35 +0000527 }
528
529 std::map<unsigned, const char *>::iterator i = node_names.find(Opcode);
530
531 return ((i != node_names.end()) ? i->second : 0);
532}
533
Bill Wendling512ff732009-07-01 18:50:55 +0000534/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling31ceb1b2009-06-30 22:38:32 +0000535unsigned SPUTargetLowering::getFunctionAlignment(const Function *) const {
536 return 3;
537}
538
Scott Michel82335272008-12-27 04:51:36 +0000539//===----------------------------------------------------------------------===//
540// Return the Cell SPU's SETCC result type
541//===----------------------------------------------------------------------===//
542
Duncan Sands8feb6942009-01-01 15:52:00 +0000543MVT SPUTargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel82335272008-12-27 04:51:36 +0000544 // i16 and i32 are valid SETCC result types
545 return ((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) ? VT : MVT::i32);
Scott Michel48e33752008-03-10 16:58:52 +0000546}
547
Scott Michel6e22c652007-12-04 22:23:35 +0000548//===----------------------------------------------------------------------===//
549// Calling convention code:
550//===----------------------------------------------------------------------===//
551
552#include "SPUGenCallingConv.inc"
553
554//===----------------------------------------------------------------------===//
555// LowerOperation implementation
556//===----------------------------------------------------------------------===//
557
558/// Custom lower loads for CellSPU
559/*!
560 All CellSPU loads and stores are aligned to 16-byte boundaries, so for elements
561 within a 16-byte block, we have to rotate to extract the requested element.
Scott Michel40f54d22008-12-04 03:02:42 +0000562
563 For extending loads, we also want to ensure that the following sequence is
564 emitted, e.g. for MVT::f32 extending load to MVT::f64:
565
566\verbatim
Scott Michelb8ee30d2008-12-29 03:23:36 +0000567%1 v16i8,ch = load
Scott Michel40f54d22008-12-04 03:02:42 +0000568%2 v16i8,ch = rotate %1
Scott Michelb8ee30d2008-12-29 03:23:36 +0000569%3 v4f8, ch = bitconvert %2
Scott Michel40f54d22008-12-04 03:02:42 +0000570%4 f32 = vec2perfslot %3
571%5 f64 = fp_extend %4
572\endverbatim
573*/
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000574static SDValue
575LowerLOAD(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel6e22c652007-12-04 22:23:35 +0000576 LoadSDNode *LN = cast<LoadSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000577 SDValue the_chain = LN->getChain();
Scott Michel82335272008-12-27 04:51:36 +0000578 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel40f54d22008-12-04 03:02:42 +0000579 MVT InVT = LN->getMemoryVT();
580 MVT OutVT = Op.getValueType();
Scott Michel6e22c652007-12-04 22:23:35 +0000581 ISD::LoadExtType ExtType = LN->getExtensionType();
582 unsigned alignment = LN->getAlignment();
Scott Michel82335272008-12-27 04:51:36 +0000583 const valtype_map_s *vtm = getValueTypeMapEntry(InVT);
Dale Johannesen021052a2009-02-04 20:06:27 +0000584 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +0000585
Scott Michel6e22c652007-12-04 22:23:35 +0000586 switch (LN->getAddressingMode()) {
587 case ISD::UNINDEXED: {
Scott Michel82335272008-12-27 04:51:36 +0000588 SDValue result;
589 SDValue basePtr = LN->getBasePtr();
590 SDValue rotate;
Scott Michel6e22c652007-12-04 22:23:35 +0000591
Scott Michel82335272008-12-27 04:51:36 +0000592 if (alignment == 16) {
593 ConstantSDNode *CN;
Scott Michel8d5841a2008-01-11 02:53:15 +0000594
Scott Michel82335272008-12-27 04:51:36 +0000595 // Special cases for a known aligned load to simplify the base pointer
596 // and the rotation amount:
597 if (basePtr.getOpcode() == ISD::ADD
598 && (CN = dyn_cast<ConstantSDNode > (basePtr.getOperand(1))) != 0) {
599 // Known offset into basePtr
600 int64_t offset = CN->getSExtValue();
601 int64_t rotamt = int64_t((offset & 0xf) - vtm->prefslot_byte);
Scott Michel8d5841a2008-01-11 02:53:15 +0000602
Scott Michel82335272008-12-27 04:51:36 +0000603 if (rotamt < 0)
604 rotamt += 16;
605
606 rotate = DAG.getConstant(rotamt, MVT::i16);
607
608 // Simplify the base pointer for this case:
609 basePtr = basePtr.getOperand(0);
610 if ((offset & ~0xf) > 0) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000611 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000612 basePtr,
613 DAG.getConstant((offset & ~0xf), PtrVT));
614 }
615 } else if ((basePtr.getOpcode() == SPUISD::AFormAddr)
616 || (basePtr.getOpcode() == SPUISD::IndirectAddr
617 && basePtr.getOperand(0).getOpcode() == SPUISD::Hi
618 && basePtr.getOperand(1).getOpcode() == SPUISD::Lo)) {
619 // Plain aligned a-form address: rotate into preferred slot
620 // Same for (SPUindirect (SPUhi ...), (SPUlo ...))
621 int64_t rotamt = -vtm->prefslot_byte;
622 if (rotamt < 0)
623 rotamt += 16;
624 rotate = DAG.getConstant(rotamt, MVT::i16);
Scott Michel8d5841a2008-01-11 02:53:15 +0000625 } else {
Scott Michel82335272008-12-27 04:51:36 +0000626 // Offset the rotate amount by the basePtr and the preferred slot
627 // byte offset
628 int64_t rotamt = -vtm->prefslot_byte;
629 if (rotamt < 0)
630 rotamt += 16;
Dale Johannesen021052a2009-02-04 20:06:27 +0000631 rotate = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000632 basePtr,
Scott Michelbb713ae2008-01-30 02:55:46 +0000633 DAG.getConstant(rotamt, PtrVT));
Scott Michel8d5841a2008-01-11 02:53:15 +0000634 }
Scott Michel82335272008-12-27 04:51:36 +0000635 } else {
636 // Unaligned load: must be more pessimistic about addressing modes:
637 if (basePtr.getOpcode() == ISD::ADD) {
638 MachineFunction &MF = DAG.getMachineFunction();
639 MachineRegisterInfo &RegInfo = MF.getRegInfo();
640 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
641 SDValue Flag;
Scott Michel8d5841a2008-01-11 02:53:15 +0000642
Scott Michel82335272008-12-27 04:51:36 +0000643 SDValue Op0 = basePtr.getOperand(0);
644 SDValue Op1 = basePtr.getOperand(1);
645
646 if (isa<ConstantSDNode>(Op1)) {
647 // Convert the (add <ptr>, <const>) to an indirect address contained
648 // in a register. Note that this is done because we need to avoid
649 // creating a 0(reg) d-form address due to the SPU's block loads.
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000650 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
Dale Johannesen021052a2009-02-04 20:06:27 +0000651 the_chain = DAG.getCopyToReg(the_chain, dl, VReg, basePtr, Flag);
652 basePtr = DAG.getCopyFromReg(the_chain, dl, VReg, PtrVT);
Scott Michel82335272008-12-27 04:51:36 +0000653 } else {
654 // Convert the (add <arg1>, <arg2>) to an indirect address, which
655 // will likely be lowered as a reg(reg) x-form address.
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000656 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
Scott Michel82335272008-12-27 04:51:36 +0000657 }
658 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000659 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000660 basePtr,
661 DAG.getConstant(0, PtrVT));
662 }
663
664 // Offset the rotate amount by the basePtr and the preferred slot
665 // byte offset
Dale Johannesen021052a2009-02-04 20:06:27 +0000666 rotate = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000667 basePtr,
668 DAG.getConstant(-vtm->prefslot_byte, PtrVT));
Scott Michel6e22c652007-12-04 22:23:35 +0000669 }
Scott Michel8d5841a2008-01-11 02:53:15 +0000670
Scott Michel82335272008-12-27 04:51:36 +0000671 // Re-emit as a v16i8 vector load
Dale Johannesen021052a2009-02-04 20:06:27 +0000672 result = DAG.getLoad(MVT::v16i8, dl, the_chain, basePtr,
Scott Michel82335272008-12-27 04:51:36 +0000673 LN->getSrcValue(), LN->getSrcValueOffset(),
674 LN->isVolatile(), 16);
675
676 // Update the chain
677 the_chain = result.getValue(1);
678
679 // Rotate into the preferred slot:
Dale Johannesen021052a2009-02-04 20:06:27 +0000680 result = DAG.getNode(SPUISD::ROTBYTES_LEFT, dl, MVT::v16i8,
Scott Michel82335272008-12-27 04:51:36 +0000681 result.getValue(0), rotate);
682
Scott Michel40f54d22008-12-04 03:02:42 +0000683 // Convert the loaded v16i8 vector to the appropriate vector type
684 // specified by the operand:
685 MVT vecVT = MVT::getVectorVT(InVT, (128 / InVT.getSizeInBits()));
Dale Johannesen021052a2009-02-04 20:06:27 +0000686 result = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, InVT,
687 DAG.getNode(ISD::BIT_CONVERT, dl, vecVT, result));
Scott Michelfe095082008-07-16 17:17:29 +0000688
Scott Michel40f54d22008-12-04 03:02:42 +0000689 // Handle extending loads by extending the scalar result:
690 if (ExtType == ISD::SEXTLOAD) {
Dale Johannesen021052a2009-02-04 20:06:27 +0000691 result = DAG.getNode(ISD::SIGN_EXTEND, dl, OutVT, result);
Scott Michel40f54d22008-12-04 03:02:42 +0000692 } else if (ExtType == ISD::ZEXTLOAD) {
Dale Johannesen021052a2009-02-04 20:06:27 +0000693 result = DAG.getNode(ISD::ZERO_EXTEND, dl, OutVT, result);
Scott Michel40f54d22008-12-04 03:02:42 +0000694 } else if (ExtType == ISD::EXTLOAD) {
695 unsigned NewOpc = ISD::ANY_EXTEND;
Scott Michel8d5841a2008-01-11 02:53:15 +0000696
Scott Michel40f54d22008-12-04 03:02:42 +0000697 if (OutVT.isFloatingPoint())
Scott Michel95b2a202009-01-26 03:37:41 +0000698 NewOpc = ISD::FP_EXTEND;
Scott Michel8d5841a2008-01-11 02:53:15 +0000699
Dale Johannesen021052a2009-02-04 20:06:27 +0000700 result = DAG.getNode(NewOpc, dl, OutVT, result);
Scott Michel8d5841a2008-01-11 02:53:15 +0000701 }
702
Scott Michel40f54d22008-12-04 03:02:42 +0000703 SDVTList retvts = DAG.getVTList(OutVT, MVT::Other);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000704 SDValue retops[2] = {
Scott Michele4d3e3c2008-01-17 20:38:41 +0000705 result,
Scott Michelbb713ae2008-01-30 02:55:46 +0000706 the_chain
Scott Michele4d3e3c2008-01-17 20:38:41 +0000707 };
Scott Michel8d5841a2008-01-11 02:53:15 +0000708
Dale Johannesen021052a2009-02-04 20:06:27 +0000709 result = DAG.getNode(SPUISD::LDRESULT, dl, retvts,
Scott Michele4d3e3c2008-01-17 20:38:41 +0000710 retops, sizeof(retops) / sizeof(retops[0]));
Scott Michel8d5841a2008-01-11 02:53:15 +0000711 return result;
Scott Michel6e22c652007-12-04 22:23:35 +0000712 }
713 case ISD::PRE_INC:
714 case ISD::PRE_DEC:
715 case ISD::POST_INC:
716 case ISD::POST_DEC:
717 case ISD::LAST_INDEXED_MODE:
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000718 {
719 std::string msg;
720 raw_string_ostream Msg(msg);
721 Msg << "LowerLOAD: Got a LoadSDNode with an addr mode other than "
Scott Michel6e22c652007-12-04 22:23:35 +0000722 "UNINDEXED\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000723 Msg << (unsigned) LN->getAddressingMode();
724 llvm_report_error(Msg.str());
725 /*NOTREACHED*/
726 }
Scott Michel6e22c652007-12-04 22:23:35 +0000727 }
728
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000729 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +0000730}
731
732/// Custom lower stores for CellSPU
733/*!
734 All CellSPU stores are aligned to 16-byte boundaries, so for elements
735 within a 16-byte block, we have to generate a shuffle to insert the
736 requested element into its place, then store the resulting block.
737 */
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000738static SDValue
739LowerSTORE(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel6e22c652007-12-04 22:23:35 +0000740 StoreSDNode *SN = cast<StoreSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000741 SDValue Value = SN->getValue();
Duncan Sands13237ac2008-06-06 12:08:01 +0000742 MVT VT = Value.getValueType();
743 MVT StVT = (!SN->isTruncatingStore() ? VT : SN->getMemoryVT());
744 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen021052a2009-02-04 20:06:27 +0000745 DebugLoc dl = Op.getDebugLoc();
Scott Michel8d5841a2008-01-11 02:53:15 +0000746 unsigned alignment = SN->getAlignment();
Scott Michel6e22c652007-12-04 22:23:35 +0000747
748 switch (SN->getAddressingMode()) {
749 case ISD::UNINDEXED: {
Scott Michelc6918c12008-11-21 02:56:16 +0000750 // The vector type we really want to load from the 16-byte chunk.
Scott Michelef5e6932008-11-19 17:45:08 +0000751 MVT vecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits())),
752 stVecVT = MVT::getVectorVT(StVT, (128 / StVT.getSizeInBits()));
Scott Michel6e22c652007-12-04 22:23:35 +0000753
Scott Michel82335272008-12-27 04:51:36 +0000754 SDValue alignLoadVec;
755 SDValue basePtr = SN->getBasePtr();
756 SDValue the_chain = SN->getChain();
757 SDValue insertEltOffs;
Scott Michel6e22c652007-12-04 22:23:35 +0000758
Scott Michel82335272008-12-27 04:51:36 +0000759 if (alignment == 16) {
760 ConstantSDNode *CN;
761
762 // Special cases for a known aligned load to simplify the base pointer
763 // and insertion byte:
764 if (basePtr.getOpcode() == ISD::ADD
765 && (CN = dyn_cast<ConstantSDNode>(basePtr.getOperand(1))) != 0) {
766 // Known offset into basePtr
767 int64_t offset = CN->getSExtValue();
768
769 // Simplify the base pointer for this case:
770 basePtr = basePtr.getOperand(0);
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000771 insertEltOffs = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000772 basePtr,
773 DAG.getConstant((offset & 0xf), PtrVT));
774
775 if ((offset & ~0xf) > 0) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000776 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000777 basePtr,
778 DAG.getConstant((offset & ~0xf), PtrVT));
779 }
780 } else {
781 // Otherwise, assume it's at byte 0 of basePtr
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000782 insertEltOffs = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000783 basePtr,
784 DAG.getConstant(0, PtrVT));
785 }
786 } else {
787 // Unaligned load: must be more pessimistic about addressing modes:
788 if (basePtr.getOpcode() == ISD::ADD) {
789 MachineFunction &MF = DAG.getMachineFunction();
790 MachineRegisterInfo &RegInfo = MF.getRegInfo();
791 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
792 SDValue Flag;
793
794 SDValue Op0 = basePtr.getOperand(0);
795 SDValue Op1 = basePtr.getOperand(1);
796
797 if (isa<ConstantSDNode>(Op1)) {
798 // Convert the (add <ptr>, <const>) to an indirect address contained
799 // in a register. Note that this is done because we need to avoid
800 // creating a 0(reg) d-form address due to the SPU's block loads.
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000801 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
Dale Johannesen021052a2009-02-04 20:06:27 +0000802 the_chain = DAG.getCopyToReg(the_chain, dl, VReg, basePtr, Flag);
803 basePtr = DAG.getCopyFromReg(the_chain, dl, VReg, PtrVT);
Scott Michel82335272008-12-27 04:51:36 +0000804 } else {
805 // Convert the (add <arg1>, <arg2>) to an indirect address, which
806 // will likely be lowered as a reg(reg) x-form address.
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000807 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
Scott Michel82335272008-12-27 04:51:36 +0000808 }
809 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000810 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000811 basePtr,
812 DAG.getConstant(0, PtrVT));
813 }
814
815 // Insertion point is solely determined by basePtr's contents
Dale Johannesen021052a2009-02-04 20:06:27 +0000816 insertEltOffs = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000817 basePtr,
818 DAG.getConstant(0, PtrVT));
819 }
820
821 // Re-emit as a v16i8 vector load
Dale Johannesen021052a2009-02-04 20:06:27 +0000822 alignLoadVec = DAG.getLoad(MVT::v16i8, dl, the_chain, basePtr,
Scott Michel82335272008-12-27 04:51:36 +0000823 SN->getSrcValue(), SN->getSrcValueOffset(),
824 SN->isVolatile(), 16);
825
826 // Update the chain
827 the_chain = alignLoadVec.getValue(1);
Scott Michel6e22c652007-12-04 22:23:35 +0000828
Scott Michel8d5841a2008-01-11 02:53:15 +0000829 LoadSDNode *LN = cast<LoadSDNode>(alignLoadVec);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000830 SDValue theValue = SN->getValue();
831 SDValue result;
Scott Michel6e22c652007-12-04 22:23:35 +0000832
833 if (StVT != VT
Scott Michelbb713ae2008-01-30 02:55:46 +0000834 && (theValue.getOpcode() == ISD::AssertZext
835 || theValue.getOpcode() == ISD::AssertSext)) {
Scott Michel6e22c652007-12-04 22:23:35 +0000836 // Drill down and get the value for zero- and sign-extended
837 // quantities
Scott Michelfe095082008-07-16 17:17:29 +0000838 theValue = theValue.getOperand(0);
Scott Michel6e22c652007-12-04 22:23:35 +0000839 }
840
Scott Michel8d5841a2008-01-11 02:53:15 +0000841 // If the base pointer is already a D-form address, then just create
842 // a new D-form address with a slot offset and the orignal base pointer.
843 // Otherwise generate a D-form address with the slot offset relative
844 // to the stack pointer, which is always aligned.
Scott Michel82335272008-12-27 04:51:36 +0000845#if !defined(NDEBUG)
846 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
847 cerr << "CellSPU LowerSTORE: basePtr = ";
848 basePtr.getNode()->dump(&DAG);
849 cerr << "\n";
850 }
851#endif
Scott Michel8d5841a2008-01-11 02:53:15 +0000852
Scott Michel3462c8e2008-11-19 15:24:16 +0000853 SDValue insertEltOp =
Dale Johannesen021052a2009-02-04 20:06:27 +0000854 DAG.getNode(SPUISD::SHUFFLE_MASK, dl, vecVT, insertEltOffs);
Scott Michelef5e6932008-11-19 17:45:08 +0000855 SDValue vectorizeOp =
Dale Johannesen021052a2009-02-04 20:06:27 +0000856 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, vecVT, theValue);
Scott Michel3462c8e2008-11-19 15:24:16 +0000857
Dale Johannesen021052a2009-02-04 20:06:27 +0000858 result = DAG.getNode(SPUISD::SHUFB, dl, vecVT,
Scott Michel95b2a202009-01-26 03:37:41 +0000859 vectorizeOp, alignLoadVec,
Scott Micheld1db1ab2009-03-16 18:47:25 +0000860 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +0000861 MVT::v4i32, insertEltOp));
Scott Michel6e22c652007-12-04 22:23:35 +0000862
Dale Johannesen021052a2009-02-04 20:06:27 +0000863 result = DAG.getStore(the_chain, dl, result, basePtr,
Scott Michel6e22c652007-12-04 22:23:35 +0000864 LN->getSrcValue(), LN->getSrcValueOffset(),
865 LN->isVolatile(), LN->getAlignment());
866
Scott Michel187250b2008-12-04 17:16:59 +0000867#if 0 && !defined(NDEBUG)
Scott Michel3462c8e2008-11-19 15:24:16 +0000868 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
869 const SDValue &currentRoot = DAG.getRoot();
870
871 DAG.setRoot(result);
872 cerr << "------- CellSPU:LowerStore result:\n";
873 DAG.dump();
874 cerr << "-------\n";
875 DAG.setRoot(currentRoot);
876 }
877#endif
Scott Michel73640252008-12-02 19:53:53 +0000878
Scott Michel6e22c652007-12-04 22:23:35 +0000879 return result;
880 /*UNREACHED*/
881 }
882 case ISD::PRE_INC:
883 case ISD::PRE_DEC:
884 case ISD::POST_INC:
885 case ISD::POST_DEC:
886 case ISD::LAST_INDEXED_MODE:
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000887 {
888 std::string msg;
889 raw_string_ostream Msg(msg);
890 Msg << "LowerLOAD: Got a LoadSDNode with an addr mode other than "
Scott Michel6e22c652007-12-04 22:23:35 +0000891 "UNINDEXED\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000892 Msg << (unsigned) SN->getAddressingMode();
893 llvm_report_error(Msg.str());
894 /*NOTREACHED*/
895 }
Scott Michel6e22c652007-12-04 22:23:35 +0000896 }
897
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000898 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +0000899}
900
Scott Michela292fc62009-01-15 04:41:47 +0000901//! Generate the address of a constant pool entry.
902SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000903LowerConstantPool(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000904 MVT PtrVT = Op.getValueType();
Scott Michel6e22c652007-12-04 22:23:35 +0000905 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
906 Constant *C = CP->getConstVal();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000907 SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
908 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Michel8d5841a2008-01-11 02:53:15 +0000909 const TargetMachine &TM = DAG.getTarget();
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000910 // FIXME there is no actual debug info here
911 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +0000912
913 if (TM.getRelocationModel() == Reloc::Static) {
914 if (!ST->usingLargeMem()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000915 // Just return the SDValue with the constant pool address in it.
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000916 return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, CPI, Zero);
Scott Michel6e22c652007-12-04 22:23:35 +0000917 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000918 SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, CPI, Zero);
919 SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, CPI, Zero);
920 return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
Scott Michel6e22c652007-12-04 22:23:35 +0000921 }
922 }
923
Torok Edwinfbcc6632009-07-14 16:55:14 +0000924 llvm_unreachable("LowerConstantPool: Relocation model other than static"
Torok Edwin6cdb8972009-07-14 12:22:58 +0000925 " not supported.");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000926 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +0000927}
928
Scott Michela292fc62009-01-15 04:41:47 +0000929//! Alternate entry point for generating the address of a constant pool entry
930SDValue
931SPU::LowerConstantPool(SDValue Op, SelectionDAG &DAG, const SPUTargetMachine &TM) {
932 return ::LowerConstantPool(Op, DAG, TM.getSubtargetImpl());
933}
934
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000935static SDValue
936LowerJumpTable(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000937 MVT PtrVT = Op.getValueType();
Scott Michel6e22c652007-12-04 22:23:35 +0000938 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000939 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
940 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Michel6e22c652007-12-04 22:23:35 +0000941 const TargetMachine &TM = DAG.getTarget();
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000942 // FIXME there is no actual debug info here
943 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +0000944
945 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michel7d5eaec2008-02-23 18:41:37 +0000946 if (!ST->usingLargeMem()) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000947 return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, JTI, Zero);
Scott Michel7d5eaec2008-02-23 18:41:37 +0000948 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000949 SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, JTI, Zero);
950 SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, JTI, Zero);
951 return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
Scott Michel7d5eaec2008-02-23 18:41:37 +0000952 }
Scott Michel6e22c652007-12-04 22:23:35 +0000953 }
954
Torok Edwinfbcc6632009-07-14 16:55:14 +0000955 llvm_unreachable("LowerJumpTable: Relocation model other than static"
Torok Edwin6cdb8972009-07-14 12:22:58 +0000956 " not supported.");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000957 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +0000958}
959
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000960static SDValue
961LowerGlobalAddress(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000962 MVT PtrVT = Op.getValueType();
Scott Michel6e22c652007-12-04 22:23:35 +0000963 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
964 GlobalValue *GV = GSDN->getGlobal();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000965 SDValue GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
Scott Michel6e22c652007-12-04 22:23:35 +0000966 const TargetMachine &TM = DAG.getTarget();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000967 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000968 // FIXME there is no actual debug info here
969 DebugLoc dl = Op.getDebugLoc();
Scott Michelfe095082008-07-16 17:17:29 +0000970
Scott Michel6e22c652007-12-04 22:23:35 +0000971 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michelceae3bb2008-01-29 02:16:57 +0000972 if (!ST->usingLargeMem()) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000973 return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, GA, Zero);
Scott Michelceae3bb2008-01-29 02:16:57 +0000974 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000975 SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, GA, Zero);
976 SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, GA, Zero);
977 return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
Scott Michelceae3bb2008-01-29 02:16:57 +0000978 }
Scott Michel6e22c652007-12-04 22:23:35 +0000979 } else {
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000980 llvm_report_error("LowerGlobalAddress: Relocation model other than static"
981 "not supported.");
Scott Michel6e22c652007-12-04 22:23:35 +0000982 /*NOTREACHED*/
983 }
984
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000985 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +0000986}
987
Nate Begeman4b3210a2008-02-14 18:43:04 +0000988//! Custom lower double precision floating point constants
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000989static SDValue
990LowerConstantFP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000991 MVT VT = Op.getValueType();
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000992 // FIXME there is no actual debug info here
993 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +0000994
Nate Begeman4b3210a2008-02-14 18:43:04 +0000995 if (VT == MVT::f64) {
Scott Michel08a4e202008-12-01 17:56:02 +0000996 ConstantFPSDNode *FP = cast<ConstantFPSDNode>(Op.getNode());
997
998 assert((FP != 0) &&
999 "LowerConstantFP: Node is not ConstantFPSDNode");
Scott Michelb8ee30d2008-12-29 03:23:36 +00001000
Scott Michel098c1132007-12-19 20:15:47 +00001001 uint64_t dbits = DoubleToBits(FP->getValueAPF().convertToDouble());
Scott Michel08a4e202008-12-01 17:56:02 +00001002 SDValue T = DAG.getConstant(dbits, MVT::i64);
Evan Chenga49de9d2009-02-25 22:49:59 +00001003 SDValue Tvec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64, T, T);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001004 return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
Dale Johannesen62fd95d2009-02-07 00:55:49 +00001005 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Tvec));
Scott Michel6e22c652007-12-04 22:23:35 +00001006 }
1007
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001008 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001009}
1010
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001011static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001012LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG, int &VarArgsFrameIndex)
Scott Michel6e22c652007-12-04 22:23:35 +00001013{
1014 MachineFunction &MF = DAG.getMachineFunction();
1015 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattnera10fff52007-12-31 04:13:23 +00001016 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel487c4342008-10-30 01:51:48 +00001017 SmallVector<SDValue, 48> ArgValues;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001018 SDValue Root = Op.getOperand(0);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001019 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Dale Johannesen021052a2009-02-04 20:06:27 +00001020 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00001021
1022 const unsigned *ArgRegs = SPURegisterInfo::getArgRegs();
1023 const unsigned NumArgRegs = SPURegisterInfo::getNumArgRegs();
Scott Michelfe095082008-07-16 17:17:29 +00001024
Scott Michel6e22c652007-12-04 22:23:35 +00001025 unsigned ArgOffset = SPUFrameInfo::minStackSize();
1026 unsigned ArgRegIdx = 0;
1027 unsigned StackSlotSize = SPUFrameInfo::stackSlotSize();
Scott Michelfe095082008-07-16 17:17:29 +00001028
Duncan Sands13237ac2008-06-06 12:08:01 +00001029 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfe095082008-07-16 17:17:29 +00001030
Scott Michel6e22c652007-12-04 22:23:35 +00001031 // Add DAG nodes to load the arguments or copy them out of registers.
Gabor Greif81d6a382008-08-31 15:37:04 +00001032 for (unsigned ArgNo = 0, e = Op.getNode()->getNumValues() - 1;
1033 ArgNo != e; ++ArgNo) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001034 MVT ObjectVT = Op.getValue(ArgNo).getValueType();
1035 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Scott Michel487c4342008-10-30 01:51:48 +00001036 SDValue ArgVal;
Scott Michel6e22c652007-12-04 22:23:35 +00001037
Scott Michel487c4342008-10-30 01:51:48 +00001038 if (ArgRegIdx < NumArgRegs) {
1039 const TargetRegisterClass *ArgRegClass;
Scott Michelfe095082008-07-16 17:17:29 +00001040
Scott Michel487c4342008-10-30 01:51:48 +00001041 switch (ObjectVT.getSimpleVT()) {
1042 default: {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001043 std::string msg;
1044 raw_string_ostream Msg(msg);
1045 Msg << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
1046 << ObjectVT.getMVTString();
1047 llvm_report_error(Msg.str());
Scott Michel487c4342008-10-30 01:51:48 +00001048 }
1049 case MVT::i8:
Scott Michelc6918c12008-11-21 02:56:16 +00001050 ArgRegClass = &SPU::R8CRegClass;
1051 break;
Scott Michel487c4342008-10-30 01:51:48 +00001052 case MVT::i16:
Scott Michelc6918c12008-11-21 02:56:16 +00001053 ArgRegClass = &SPU::R16CRegClass;
1054 break;
Scott Michel487c4342008-10-30 01:51:48 +00001055 case MVT::i32:
Scott Michelc6918c12008-11-21 02:56:16 +00001056 ArgRegClass = &SPU::R32CRegClass;
1057 break;
Scott Michel487c4342008-10-30 01:51:48 +00001058 case MVT::i64:
Scott Michelc6918c12008-11-21 02:56:16 +00001059 ArgRegClass = &SPU::R64CRegClass;
1060 break;
Scott Michel6887caf2009-01-06 03:36:14 +00001061 case MVT::i128:
1062 ArgRegClass = &SPU::GPRCRegClass;
1063 break;
Scott Michel487c4342008-10-30 01:51:48 +00001064 case MVT::f32:
Scott Michelc6918c12008-11-21 02:56:16 +00001065 ArgRegClass = &SPU::R32FPRegClass;
1066 break;
Scott Michel487c4342008-10-30 01:51:48 +00001067 case MVT::f64:
Scott Michelc6918c12008-11-21 02:56:16 +00001068 ArgRegClass = &SPU::R64FPRegClass;
1069 break;
Scott Michel487c4342008-10-30 01:51:48 +00001070 case MVT::v2f64:
1071 case MVT::v4f32:
1072 case MVT::v2i64:
1073 case MVT::v4i32:
1074 case MVT::v8i16:
1075 case MVT::v16i8:
Scott Michelc6918c12008-11-21 02:56:16 +00001076 ArgRegClass = &SPU::VECREGRegClass;
1077 break;
Scott Michel487c4342008-10-30 01:51:48 +00001078 }
1079
1080 unsigned VReg = RegInfo.createVirtualRegister(ArgRegClass);
1081 RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
Dale Johannesen021052a2009-02-04 20:06:27 +00001082 ArgVal = DAG.getCopyFromReg(Root, dl, VReg, ObjectVT);
Scott Michel487c4342008-10-30 01:51:48 +00001083 ++ArgRegIdx;
1084 } else {
1085 // We need to load the argument to a virtual register if we determined
1086 // above that we ran out of physical registers of the appropriate type
1087 // or we're forced to do vararg
Chris Lattnerf6518cf2008-02-13 07:35:30 +00001088 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001089 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00001090 ArgVal = DAG.getLoad(ObjectVT, dl, Root, FIN, NULL, 0);
Scott Michel6e22c652007-12-04 22:23:35 +00001091 ArgOffset += StackSlotSize;
1092 }
Scott Michelfe095082008-07-16 17:17:29 +00001093
Scott Michel6e22c652007-12-04 22:23:35 +00001094 ArgValues.push_back(ArgVal);
Scott Michel487c4342008-10-30 01:51:48 +00001095 // Update the chain
1096 Root = ArgVal.getOperand(0);
Scott Michel6e22c652007-12-04 22:23:35 +00001097 }
Scott Michelfe095082008-07-16 17:17:29 +00001098
Scott Michel487c4342008-10-30 01:51:48 +00001099 // vararg handling:
Scott Michel6e22c652007-12-04 22:23:35 +00001100 if (isVarArg) {
Scott Michel487c4342008-10-30 01:51:48 +00001101 // unsigned int ptr_size = PtrVT.getSizeInBits() / 8;
1102 // We will spill (79-3)+1 registers to the stack
1103 SmallVector<SDValue, 79-3+1> MemOps;
1104
1105 // Create the frame slot
1106
Scott Michel6e22c652007-12-04 22:23:35 +00001107 for (; ArgRegIdx != NumArgRegs; ++ArgRegIdx) {
Scott Michel487c4342008-10-30 01:51:48 +00001108 VarArgsFrameIndex = MFI->CreateFixedObject(StackSlotSize, ArgOffset);
1109 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
1110 SDValue ArgVal = DAG.getRegister(ArgRegs[ArgRegIdx], MVT::v16i8);
Dale Johannesen021052a2009-02-04 20:06:27 +00001111 SDValue Store = DAG.getStore(Root, dl, ArgVal, FIN, NULL, 0);
Scott Michel487c4342008-10-30 01:51:48 +00001112 Root = Store.getOperand(0);
Scott Michel6e22c652007-12-04 22:23:35 +00001113 MemOps.push_back(Store);
Scott Michel487c4342008-10-30 01:51:48 +00001114
1115 // Increment address by stack slot size for the next stored argument
1116 ArgOffset += StackSlotSize;
Scott Michel6e22c652007-12-04 22:23:35 +00001117 }
1118 if (!MemOps.empty())
Scott Micheld1db1ab2009-03-16 18:47:25 +00001119 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen021052a2009-02-04 20:06:27 +00001120 &MemOps[0], MemOps.size());
Scott Michel6e22c652007-12-04 22:23:35 +00001121 }
Scott Michelfe095082008-07-16 17:17:29 +00001122
Scott Michel6e22c652007-12-04 22:23:35 +00001123 ArgValues.push_back(Root);
Scott Michelfe095082008-07-16 17:17:29 +00001124
Scott Michel6e22c652007-12-04 22:23:35 +00001125 // Return the new list of results.
Dale Johannesen021052a2009-02-04 20:06:27 +00001126 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sands3d960942008-12-01 11:41:29 +00001127 &ArgValues[0], ArgValues.size());
Scott Michel6e22c652007-12-04 22:23:35 +00001128}
1129
1130/// isLSAAddress - Return the immediate to use if the specified
1131/// value is representable as a LSA address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001132static SDNode *isLSAAddress(SDValue Op, SelectionDAG &DAG) {
Scott Michelaab89ca2008-11-11 03:06:06 +00001133 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Scott Michel6e22c652007-12-04 22:23:35 +00001134 if (!C) return 0;
Scott Michelfe095082008-07-16 17:17:29 +00001135
Dan Gohmaneffb8942008-09-12 16:56:44 +00001136 int Addr = C->getZExtValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001137 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
1138 (Addr << 14 >> 14) != Addr)
1139 return 0; // Top 14 bits have to be sext of immediate.
Scott Michelfe095082008-07-16 17:17:29 +00001140
Dan Gohmaneffb8942008-09-12 16:56:44 +00001141 return DAG.getConstant((int)C->getZExtValue() >> 2, MVT::i32).getNode();
Scott Michel6e22c652007-12-04 22:23:35 +00001142}
1143
Scott Michel494daa72009-01-06 23:10:38 +00001144static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001145LowerCALL(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Dan Gohmand3fe1742008-09-13 01:54:27 +00001146 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1147 SDValue Chain = TheCall->getChain();
Dan Gohmand3fe1742008-09-13 01:54:27 +00001148 SDValue Callee = TheCall->getCallee();
1149 unsigned NumOps = TheCall->getNumArgs();
Scott Michel6e22c652007-12-04 22:23:35 +00001150 unsigned StackSlotSize = SPUFrameInfo::stackSlotSize();
1151 const unsigned *ArgRegs = SPURegisterInfo::getArgRegs();
1152 const unsigned NumArgRegs = SPURegisterInfo::getNumArgRegs();
Dale Johannesen021052a2009-02-04 20:06:27 +00001153 DebugLoc dl = TheCall->getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00001154
1155 // Handy pointer type
Duncan Sands13237ac2008-06-06 12:08:01 +00001156 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfe095082008-07-16 17:17:29 +00001157
Scott Michel6e22c652007-12-04 22:23:35 +00001158 // Accumulate how many bytes are to be pushed on the stack, including the
1159 // linkage area, and parameter passing area. According to the SPU ABI,
1160 // we minimally need space for [LR] and [SP]
1161 unsigned NumStackBytes = SPUFrameInfo::minStackSize();
Scott Michelfe095082008-07-16 17:17:29 +00001162
Scott Michel6e22c652007-12-04 22:23:35 +00001163 // Set up a copy of the stack pointer for use loading and storing any
1164 // arguments that may not fit in the registers available for argument
1165 // passing.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001166 SDValue StackPtr = DAG.getRegister(SPU::R1, MVT::i32);
Scott Michelfe095082008-07-16 17:17:29 +00001167
Scott Michel6e22c652007-12-04 22:23:35 +00001168 // Figure out which arguments are going to go in registers, and which in
1169 // memory.
1170 unsigned ArgOffset = SPUFrameInfo::minStackSize(); // Just below [LR]
1171 unsigned ArgRegIdx = 0;
1172
1173 // Keep track of registers passing arguments
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001174 std::vector<std::pair<unsigned, SDValue> > RegsToPass;
Scott Michel6e22c652007-12-04 22:23:35 +00001175 // And the arguments passed on the stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001176 SmallVector<SDValue, 8> MemOpChains;
Scott Michel6e22c652007-12-04 22:23:35 +00001177
1178 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmand3fe1742008-09-13 01:54:27 +00001179 SDValue Arg = TheCall->getArg(i);
Scott Michelfe095082008-07-16 17:17:29 +00001180
Scott Michel6e22c652007-12-04 22:23:35 +00001181 // PtrOff will be used to store the current argument to the stack if a
1182 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001183 SDValue PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Dale Johannesen021052a2009-02-04 20:06:27 +00001184 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Scott Michel6e22c652007-12-04 22:23:35 +00001185
Duncan Sands13237ac2008-06-06 12:08:01 +00001186 switch (Arg.getValueType().getSimpleVT()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001187 default: llvm_unreachable("Unexpected ValueType for argument!");
Scott Michel6887caf2009-01-06 03:36:14 +00001188 case MVT::i8:
1189 case MVT::i16:
Scott Michel6e22c652007-12-04 22:23:35 +00001190 case MVT::i32:
1191 case MVT::i64:
1192 case MVT::i128:
1193 if (ArgRegIdx != NumArgRegs) {
1194 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1195 } else {
Dale Johannesen021052a2009-02-04 20:06:27 +00001196 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Scott Michelbb713ae2008-01-30 02:55:46 +00001197 ArgOffset += StackSlotSize;
Scott Michel6e22c652007-12-04 22:23:35 +00001198 }
1199 break;
1200 case MVT::f32:
1201 case MVT::f64:
1202 if (ArgRegIdx != NumArgRegs) {
1203 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1204 } else {
Dale Johannesen021052a2009-02-04 20:06:27 +00001205 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Scott Michelbb713ae2008-01-30 02:55:46 +00001206 ArgOffset += StackSlotSize;
Scott Michel6e22c652007-12-04 22:23:35 +00001207 }
1208 break;
Scott Michelea3c49d2008-12-04 21:01:44 +00001209 case MVT::v2i64:
1210 case MVT::v2f64:
Scott Michel6e22c652007-12-04 22:23:35 +00001211 case MVT::v4f32:
1212 case MVT::v4i32:
1213 case MVT::v8i16:
1214 case MVT::v16i8:
1215 if (ArgRegIdx != NumArgRegs) {
1216 RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
1217 } else {
Dale Johannesen021052a2009-02-04 20:06:27 +00001218 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Scott Michelbb713ae2008-01-30 02:55:46 +00001219 ArgOffset += StackSlotSize;
Scott Michel6e22c652007-12-04 22:23:35 +00001220 }
1221 break;
1222 }
1223 }
1224
1225 // Update number of stack bytes actually used, insert a call sequence start
1226 NumStackBytes = (ArgOffset - SPUFrameInfo::minStackSize());
Chris Lattner27539552008-10-11 22:08:30 +00001227 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumStackBytes,
1228 true));
Scott Michel6e22c652007-12-04 22:23:35 +00001229
1230 if (!MemOpChains.empty()) {
1231 // Adjust the stack pointer for the stack arguments.
Dale Johannesen021052a2009-02-04 20:06:27 +00001232 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Scott Michel6e22c652007-12-04 22:23:35 +00001233 &MemOpChains[0], MemOpChains.size());
1234 }
Scott Michelfe095082008-07-16 17:17:29 +00001235
Scott Michel6e22c652007-12-04 22:23:35 +00001236 // Build a sequence of copy-to-reg nodes chained together with token chain
1237 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001238 SDValue InFlag;
Scott Michel6e22c652007-12-04 22:23:35 +00001239 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Micheld1db1ab2009-03-16 18:47:25 +00001240 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen021052a2009-02-04 20:06:27 +00001241 RegsToPass[i].second, InFlag);
Scott Michel6e22c652007-12-04 22:23:35 +00001242 InFlag = Chain.getValue(1);
1243 }
Scott Michelfe095082008-07-16 17:17:29 +00001244
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001245 SmallVector<SDValue, 8> Ops;
Scott Michel6e22c652007-12-04 22:23:35 +00001246 unsigned CallOpc = SPUISD::CALL;
Scott Michelfe095082008-07-16 17:17:29 +00001247
Bill Wendling24c79f22008-09-16 21:48:12 +00001248 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1249 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1250 // node so that legalize doesn't hack it.
Scott Michelaab89ca2008-11-11 03:06:06 +00001251 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Scott Michel6e22c652007-12-04 22:23:35 +00001252 GlobalValue *GV = G->getGlobal();
Duncan Sands13237ac2008-06-06 12:08:01 +00001253 MVT CalleeVT = Callee.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001254 SDValue Zero = DAG.getConstant(0, PtrVT);
1255 SDValue GA = DAG.getTargetGlobalAddress(GV, CalleeVT);
Scott Michel6e22c652007-12-04 22:23:35 +00001256
Scott Michel8d5841a2008-01-11 02:53:15 +00001257 if (!ST->usingLargeMem()) {
1258 // Turn calls to targets that are defined (i.e., have bodies) into BRSL
1259 // style calls, otherwise, external symbols are BRASL calls. This assumes
1260 // that declared/defined symbols are in the same compilation unit and can
1261 // be reached through PC-relative jumps.
1262 //
1263 // NOTE:
1264 // This may be an unsafe assumption for JIT and really large compilation
1265 // units.
1266 if (GV->isDeclaration()) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001267 Callee = DAG.getNode(SPUISD::AFormAddr, dl, CalleeVT, GA, Zero);
Scott Michel8d5841a2008-01-11 02:53:15 +00001268 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001269 Callee = DAG.getNode(SPUISD::PCRelAddr, dl, CalleeVT, GA, Zero);
Scott Michel8d5841a2008-01-11 02:53:15 +00001270 }
Scott Michel6e22c652007-12-04 22:23:35 +00001271 } else {
Scott Michel8d5841a2008-01-11 02:53:15 +00001272 // "Large memory" mode: Turn all calls into indirect calls with a X-form
1273 // address pairs:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001274 Callee = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, GA, Zero);
Scott Michel6e22c652007-12-04 22:23:35 +00001275 }
Scott Michelb8ee30d2008-12-29 03:23:36 +00001276 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1277 MVT CalleeVT = Callee.getValueType();
1278 SDValue Zero = DAG.getConstant(0, PtrVT);
1279 SDValue ExtSym = DAG.getTargetExternalSymbol(S->getSymbol(),
1280 Callee.getValueType());
1281
1282 if (!ST->usingLargeMem()) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001283 Callee = DAG.getNode(SPUISD::AFormAddr, dl, CalleeVT, ExtSym, Zero);
Scott Michelb8ee30d2008-12-29 03:23:36 +00001284 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001285 Callee = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, ExtSym, Zero);
Scott Michelb8ee30d2008-12-29 03:23:36 +00001286 }
1287 } else if (SDNode *Dest = isLSAAddress(Callee, DAG)) {
Scott Michel6e22c652007-12-04 22:23:35 +00001288 // If this is an absolute destination address that appears to be a legal
1289 // local store address, use the munged value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001290 Callee = SDValue(Dest, 0);
Scott Michel8d5841a2008-01-11 02:53:15 +00001291 }
Scott Michel6e22c652007-12-04 22:23:35 +00001292
1293 Ops.push_back(Chain);
1294 Ops.push_back(Callee);
Scott Michelfe095082008-07-16 17:17:29 +00001295
Scott Michel6e22c652007-12-04 22:23:35 +00001296 // Add argument registers to the end of the list so that they are known live
1297 // into the call.
1298 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Scott Michelfe095082008-07-16 17:17:29 +00001299 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Scott Michel6e22c652007-12-04 22:23:35 +00001300 RegsToPass[i].second.getValueType()));
Scott Michelfe095082008-07-16 17:17:29 +00001301
Gabor Greiff304a7a2008-08-28 21:40:38 +00001302 if (InFlag.getNode())
Scott Michel6e22c652007-12-04 22:23:35 +00001303 Ops.push_back(InFlag);
Duncan Sands739a0542008-07-02 17:40:58 +00001304 // Returns a chain and a flag for retval copy to use.
Dale Johannesen021052a2009-02-04 20:06:27 +00001305 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
Duncan Sands739a0542008-07-02 17:40:58 +00001306 &Ops[0], Ops.size());
Scott Michel6e22c652007-12-04 22:23:35 +00001307 InFlag = Chain.getValue(1);
1308
Chris Lattner27539552008-10-11 22:08:30 +00001309 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumStackBytes, true),
1310 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohmand3fe1742008-09-13 01:54:27 +00001311 if (TheCall->getValueType(0) != MVT::Other)
Evan Cheng0f329162008-02-05 22:44:06 +00001312 InFlag = Chain.getValue(1);
1313
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001314 SDValue ResultVals[3];
Scott Michel6e22c652007-12-04 22:23:35 +00001315 unsigned NumResults = 0;
Scott Michelfe095082008-07-16 17:17:29 +00001316
Scott Michel6e22c652007-12-04 22:23:35 +00001317 // If the call has results, copy the values out of the ret val registers.
Dan Gohmand3fe1742008-09-13 01:54:27 +00001318 switch (TheCall->getValueType(0).getSimpleVT()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001319 default: llvm_unreachable("Unexpected ret value!");
Scott Michel6e22c652007-12-04 22:23:35 +00001320 case MVT::Other: break;
1321 case MVT::i32:
Dan Gohmand3fe1742008-09-13 01:54:27 +00001322 if (TheCall->getValueType(1) == MVT::i32) {
Scott Micheld1db1ab2009-03-16 18:47:25 +00001323 Chain = DAG.getCopyFromReg(Chain, dl, SPU::R4,
Dale Johannesen021052a2009-02-04 20:06:27 +00001324 MVT::i32, InFlag).getValue(1);
Scott Michel6e22c652007-12-04 22:23:35 +00001325 ResultVals[0] = Chain.getValue(0);
Dale Johannesen021052a2009-02-04 20:06:27 +00001326 Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, MVT::i32,
Scott Michel6e22c652007-12-04 22:23:35 +00001327 Chain.getValue(2)).getValue(1);
1328 ResultVals[1] = Chain.getValue(0);
1329 NumResults = 2;
Scott Michel6e22c652007-12-04 22:23:35 +00001330 } else {
Scott Micheld1db1ab2009-03-16 18:47:25 +00001331 Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, MVT::i32,
Dale Johannesen021052a2009-02-04 20:06:27 +00001332 InFlag).getValue(1);
Scott Michel6e22c652007-12-04 22:23:35 +00001333 ResultVals[0] = Chain.getValue(0);
1334 NumResults = 1;
1335 }
Scott Michel6e22c652007-12-04 22:23:35 +00001336 break;
1337 case MVT::i64:
Scott Micheld1db1ab2009-03-16 18:47:25 +00001338 Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, MVT::i64,
Dale Johannesen021052a2009-02-04 20:06:27 +00001339 InFlag).getValue(1);
Scott Michel6e22c652007-12-04 22:23:35 +00001340 ResultVals[0] = Chain.getValue(0);
1341 NumResults = 1;
Scott Michel6e22c652007-12-04 22:23:35 +00001342 break;
Scott Michel6887caf2009-01-06 03:36:14 +00001343 case MVT::i128:
Scott Micheld1db1ab2009-03-16 18:47:25 +00001344 Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, MVT::i128,
Dale Johannesen021052a2009-02-04 20:06:27 +00001345 InFlag).getValue(1);
Scott Michel6887caf2009-01-06 03:36:14 +00001346 ResultVals[0] = Chain.getValue(0);
1347 NumResults = 1;
1348 break;
Scott Michel6e22c652007-12-04 22:23:35 +00001349 case MVT::f32:
1350 case MVT::f64:
Dale Johannesen021052a2009-02-04 20:06:27 +00001351 Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, TheCall->getValueType(0),
Scott Michel6e22c652007-12-04 22:23:35 +00001352 InFlag).getValue(1);
1353 ResultVals[0] = Chain.getValue(0);
1354 NumResults = 1;
Scott Michel6e22c652007-12-04 22:23:35 +00001355 break;
1356 case MVT::v2f64:
Scott Michelea3c49d2008-12-04 21:01:44 +00001357 case MVT::v2i64:
Scott Michel6e22c652007-12-04 22:23:35 +00001358 case MVT::v4f32:
1359 case MVT::v4i32:
1360 case MVT::v8i16:
1361 case MVT::v16i8:
Dale Johannesen021052a2009-02-04 20:06:27 +00001362 Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, TheCall->getValueType(0),
Scott Michel6e22c652007-12-04 22:23:35 +00001363 InFlag).getValue(1);
1364 ResultVals[0] = Chain.getValue(0);
1365 NumResults = 1;
Scott Michel6e22c652007-12-04 22:23:35 +00001366 break;
1367 }
Duncan Sands739a0542008-07-02 17:40:58 +00001368
Scott Michel6e22c652007-12-04 22:23:35 +00001369 // If the function returns void, just return the chain.
1370 if (NumResults == 0)
1371 return Chain;
Scott Michelfe095082008-07-16 17:17:29 +00001372
Scott Michel6e22c652007-12-04 22:23:35 +00001373 // Otherwise, merge everything together with a MERGE_VALUES node.
1374 ResultVals[NumResults++] = Chain;
Dale Johannesen021052a2009-02-04 20:06:27 +00001375 SDValue Res = DAG.getMergeValues(ResultVals, NumResults, dl);
Gabor Greifabfdf922008-08-26 22:36:50 +00001376 return Res.getValue(Op.getResNo());
Scott Michel6e22c652007-12-04 22:23:35 +00001377}
1378
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001379static SDValue
1380LowerRET(SDValue Op, SelectionDAG &DAG, TargetMachine &TM) {
Scott Michel6e22c652007-12-04 22:23:35 +00001381 SmallVector<CCValAssign, 16> RVLocs;
1382 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
1383 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001384 DebugLoc dl = Op.getDebugLoc();
Owen Anderson47db9412009-07-22 00:24:57 +00001385 CCState CCInfo(CC, isVarArg, TM, RVLocs, *DAG.getContext());
Gabor Greiff304a7a2008-08-28 21:40:38 +00001386 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SPU);
Scott Michelfe095082008-07-16 17:17:29 +00001387
Scott Michel6e22c652007-12-04 22:23:35 +00001388 // If this is the first return lowered for this function, add the regs to the
1389 // liveout set for the function.
Chris Lattnera10fff52007-12-31 04:13:23 +00001390 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Scott Michel6e22c652007-12-04 22:23:35 +00001391 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattnera10fff52007-12-31 04:13:23 +00001392 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Scott Michel6e22c652007-12-04 22:23:35 +00001393 }
1394
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001395 SDValue Chain = Op.getOperand(0);
1396 SDValue Flag;
Scott Michelfe095082008-07-16 17:17:29 +00001397
Scott Michel6e22c652007-12-04 22:23:35 +00001398 // Copy the result values into the output registers.
1399 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1400 CCValAssign &VA = RVLocs[i];
1401 assert(VA.isRegLoc() && "Can only return in registers!");
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001402 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1403 Op.getOperand(i*2+1), Flag);
Scott Michel6e22c652007-12-04 22:23:35 +00001404 Flag = Chain.getValue(1);
1405 }
1406
Gabor Greiff304a7a2008-08-28 21:40:38 +00001407 if (Flag.getNode())
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001408 return DAG.getNode(SPUISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Scott Michel6e22c652007-12-04 22:23:35 +00001409 else
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001410 return DAG.getNode(SPUISD::RET_FLAG, dl, MVT::Other, Chain);
Scott Michel6e22c652007-12-04 22:23:35 +00001411}
1412
1413
1414//===----------------------------------------------------------------------===//
1415// Vector related lowering:
1416//===----------------------------------------------------------------------===//
1417
1418static ConstantSDNode *
1419getVecImm(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001420 SDValue OpVal(0, 0);
Scott Michelfe095082008-07-16 17:17:29 +00001421
Scott Michel6e22c652007-12-04 22:23:35 +00001422 // Check to see if this buildvec has a single non-undef value in its elements.
1423 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1424 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001425 if (OpVal.getNode() == 0)
Scott Michel6e22c652007-12-04 22:23:35 +00001426 OpVal = N->getOperand(i);
1427 else if (OpVal != N->getOperand(i))
1428 return 0;
1429 }
Scott Michelfe095082008-07-16 17:17:29 +00001430
Gabor Greiff304a7a2008-08-28 21:40:38 +00001431 if (OpVal.getNode() != 0) {
Scott Michelaab89ca2008-11-11 03:06:06 +00001432 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Scott Michel6e22c652007-12-04 22:23:35 +00001433 return CN;
1434 }
1435 }
1436
Scott Michel839ad0a2009-03-17 01:15:45 +00001437 return 0;
Scott Michel6e22c652007-12-04 22:23:35 +00001438}
1439
1440/// get_vec_i18imm - Test if this vector is a vector filled with the same value
1441/// and the value fits into an unsigned 18-bit constant, and if so, return the
1442/// constant
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001443SDValue SPU::get_vec_u18imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands13237ac2008-06-06 12:08:01 +00001444 MVT ValueType) {
Scott Michel6e22c652007-12-04 22:23:35 +00001445 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001446 uint64_t Value = CN->getZExtValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001447 if (ValueType == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001448 uint64_t UValue = CN->getZExtValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001449 uint32_t upper = uint32_t(UValue >> 32);
1450 uint32_t lower = uint32_t(UValue);
1451 if (upper != lower)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001452 return SDValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001453 Value = Value >> 32;
1454 }
Scott Michel6e22c652007-12-04 22:23:35 +00001455 if (Value <= 0x3ffff)
Dan Gohmanfd820522008-11-05 02:06:09 +00001456 return DAG.getTargetConstant(Value, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001457 }
1458
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001459 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001460}
1461
1462/// get_vec_i16imm - Test if this vector is a vector filled with the same value
1463/// and the value fits into a signed 16-bit constant, and if so, return the
1464/// constant
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001465SDValue SPU::get_vec_i16imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands13237ac2008-06-06 12:08:01 +00001466 MVT ValueType) {
Scott Michel6e22c652007-12-04 22:23:35 +00001467 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohman6e054832008-09-26 21:54:37 +00001468 int64_t Value = CN->getSExtValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001469 if (ValueType == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001470 uint64_t UValue = CN->getZExtValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001471 uint32_t upper = uint32_t(UValue >> 32);
1472 uint32_t lower = uint32_t(UValue);
1473 if (upper != lower)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001474 return SDValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001475 Value = Value >> 32;
1476 }
Scott Michel42f56b42008-03-05 23:02:02 +00001477 if (Value >= -(1 << 15) && Value <= ((1 << 15) - 1)) {
Dan Gohmanfd820522008-11-05 02:06:09 +00001478 return DAG.getTargetConstant(Value, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001479 }
1480 }
1481
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001482 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001483}
1484
1485/// get_vec_i10imm - Test if this vector is a vector filled with the same value
1486/// and the value fits into a signed 10-bit constant, and if so, return the
1487/// constant
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001488SDValue SPU::get_vec_i10imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands13237ac2008-06-06 12:08:01 +00001489 MVT ValueType) {
Scott Michel6e22c652007-12-04 22:23:35 +00001490 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohman6e054832008-09-26 21:54:37 +00001491 int64_t Value = CN->getSExtValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001492 if (ValueType == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001493 uint64_t UValue = CN->getZExtValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001494 uint32_t upper = uint32_t(UValue >> 32);
1495 uint32_t lower = uint32_t(UValue);
1496 if (upper != lower)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001497 return SDValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001498 Value = Value >> 32;
1499 }
Scott Michel42f56b42008-03-05 23:02:02 +00001500 if (isS10Constant(Value))
Dan Gohmanfd820522008-11-05 02:06:09 +00001501 return DAG.getTargetConstant(Value, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001502 }
1503
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001504 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001505}
1506
1507/// get_vec_i8imm - Test if this vector is a vector filled with the same value
1508/// and the value fits into a signed 8-bit constant, and if so, return the
1509/// constant.
1510///
1511/// @note: The incoming vector is v16i8 because that's the only way we can load
1512/// constant vectors. Thus, we test to see if the upper and lower bytes are the
1513/// same value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001514SDValue SPU::get_vec_i8imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands13237ac2008-06-06 12:08:01 +00001515 MVT ValueType) {
Scott Michel6e22c652007-12-04 22:23:35 +00001516 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001517 int Value = (int) CN->getZExtValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001518 if (ValueType == MVT::i16
Scott Michelbb713ae2008-01-30 02:55:46 +00001519 && Value <= 0xffff /* truncated from uint64_t */
1520 && ((short) Value >> 8) == ((short) Value & 0xff))
Dan Gohmanfd820522008-11-05 02:06:09 +00001521 return DAG.getTargetConstant(Value & 0xff, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001522 else if (ValueType == MVT::i8
Scott Michelbb713ae2008-01-30 02:55:46 +00001523 && (Value & 0xff) == Value)
Dan Gohmanfd820522008-11-05 02:06:09 +00001524 return DAG.getTargetConstant(Value, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001525 }
1526
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001527 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001528}
1529
1530/// get_ILHUvec_imm - Test if this vector is a vector filled with the same value
1531/// and the value fits into a signed 16-bit constant, and if so, return the
1532/// constant
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001533SDValue SPU::get_ILHUvec_imm(SDNode *N, SelectionDAG &DAG,
Duncan Sands13237ac2008-06-06 12:08:01 +00001534 MVT ValueType) {
Scott Michel6e22c652007-12-04 22:23:35 +00001535 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001536 uint64_t Value = CN->getZExtValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001537 if ((ValueType == MVT::i32
Scott Michelbb713ae2008-01-30 02:55:46 +00001538 && ((unsigned) Value & 0xffff0000) == (unsigned) Value)
1539 || (ValueType == MVT::i64 && (Value & 0xffff0000) == Value))
Dan Gohmanfd820522008-11-05 02:06:09 +00001540 return DAG.getTargetConstant(Value >> 16, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001541 }
1542
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001543 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001544}
1545
1546/// get_v4i32_imm - Catch-all for general 32-bit constant vectors
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001547SDValue SPU::get_v4i32_imm(SDNode *N, SelectionDAG &DAG) {
Scott Michel6e22c652007-12-04 22:23:35 +00001548 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmanfd820522008-11-05 02:06:09 +00001549 return DAG.getTargetConstant((unsigned) CN->getZExtValue(), MVT::i32);
Scott Michel6e22c652007-12-04 22:23:35 +00001550 }
1551
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001552 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001553}
1554
1555/// get_v4i32_imm - Catch-all for general 64-bit constant vectors
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001556SDValue SPU::get_v2i64_imm(SDNode *N, SelectionDAG &DAG) {
Scott Michel6e22c652007-12-04 22:23:35 +00001557 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmanfd820522008-11-05 02:06:09 +00001558 return DAG.getTargetConstant((unsigned) CN->getZExtValue(), MVT::i64);
Scott Michel6e22c652007-12-04 22:23:35 +00001559 }
1560
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001561 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001562}
1563
Scott Micheled7d79f2009-01-21 04:58:48 +00001564//! Lower a BUILD_VECTOR instruction creatively:
1565SDValue
Scott Michel9e3e4a92009-01-26 03:31:40 +00001566LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001567 MVT VT = Op.getValueType();
Scott Michel839ad0a2009-03-17 01:15:45 +00001568 MVT EltVT = VT.getVectorElementType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001569 DebugLoc dl = Op.getDebugLoc();
Scott Michel839ad0a2009-03-17 01:15:45 +00001570 BuildVectorSDNode *BCN = dyn_cast<BuildVectorSDNode>(Op.getNode());
1571 assert(BCN != 0 && "Expected BuildVectorSDNode in SPU LowerBUILD_VECTOR");
1572 unsigned minSplatBits = EltVT.getSizeInBits();
1573
1574 if (minSplatBits < 16)
1575 minSplatBits = 16;
1576
1577 APInt APSplatBits, APSplatUndef;
1578 unsigned SplatBitSize;
1579 bool HasAnyUndefs;
1580
1581 if (!BCN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
1582 HasAnyUndefs, minSplatBits)
1583 || minSplatBits < SplatBitSize)
1584 return SDValue(); // Wasn't a constant vector or splat exceeded min
1585
1586 uint64_t SplatBits = APSplatBits.getZExtValue();
Scott Michelfe095082008-07-16 17:17:29 +00001587
Duncan Sands13237ac2008-06-06 12:08:01 +00001588 switch (VT.getSimpleVT()) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001589 default: {
1590 std::string msg;
1591 raw_string_ostream Msg(msg);
1592 Msg << "CellSPU: Unhandled VT in LowerBUILD_VECTOR, VT = "
1593 << VT.getMVTString();
1594 llvm_report_error(Msg.str());
Scott Micheled7d79f2009-01-21 04:58:48 +00001595 /*NOTREACHED*/
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001596 }
Scott Michel6e22c652007-12-04 22:23:35 +00001597 case MVT::v4f32: {
Scott Michel9e3e4a92009-01-26 03:31:40 +00001598 uint32_t Value32 = uint32_t(SplatBits);
Chris Lattner78b7cbe2009-03-26 05:29:34 +00001599 assert(SplatBitSize == 32
Scott Michelbb713ae2008-01-30 02:55:46 +00001600 && "LowerBUILD_VECTOR: Unexpected floating point vector element.");
Scott Michel6e22c652007-12-04 22:23:35 +00001601 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001602 SDValue T = DAG.getConstant(Value32, MVT::i32);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001603 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32,
Chris Lattner78b7cbe2009-03-26 05:29:34 +00001604 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, T,T,T,T));
Scott Michel6e22c652007-12-04 22:23:35 +00001605 break;
1606 }
1607 case MVT::v2f64: {
Scott Michel9e3e4a92009-01-26 03:31:40 +00001608 uint64_t f64val = uint64_t(SplatBits);
Chris Lattner78b7cbe2009-03-26 05:29:34 +00001609 assert(SplatBitSize == 64
Scott Michelefc8c7a2008-11-24 17:11:17 +00001610 && "LowerBUILD_VECTOR: 64-bit float vector size > 8 bytes.");
Scott Michel6e22c652007-12-04 22:23:35 +00001611 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001612 SDValue T = DAG.getConstant(f64val, MVT::i64);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001613 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64,
Evan Chenga49de9d2009-02-25 22:49:59 +00001614 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64, T, T));
Scott Michel6e22c652007-12-04 22:23:35 +00001615 break;
1616 }
1617 case MVT::v16i8: {
1618 // 8-bit constants have to be expanded to 16-bits
Scott Michel839ad0a2009-03-17 01:15:45 +00001619 unsigned short Value16 = SplatBits /* | (SplatBits << 8) */;
1620 SmallVector<SDValue, 8> Ops;
1621
1622 Ops.assign(8, DAG.getConstant(Value16, MVT::i16));
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001623 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Scott Michel839ad0a2009-03-17 01:15:45 +00001624 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i16, &Ops[0], Ops.size()));
Scott Michel6e22c652007-12-04 22:23:35 +00001625 }
1626 case MVT::v8i16: {
Scott Michel839ad0a2009-03-17 01:15:45 +00001627 unsigned short Value16 = SplatBits;
1628 SDValue T = DAG.getConstant(Value16, EltVT);
1629 SmallVector<SDValue, 8> Ops;
1630
1631 Ops.assign(8, T);
1632 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Scott Michel6e22c652007-12-04 22:23:35 +00001633 }
1634 case MVT::v4i32: {
Scott Michel839ad0a2009-03-17 01:15:45 +00001635 SDValue T = DAG.getConstant(unsigned(SplatBits), VT.getVectorElementType());
Evan Chenga49de9d2009-02-25 22:49:59 +00001636 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, T, T, T, T);
Scott Michel6e22c652007-12-04 22:23:35 +00001637 }
Scott Michel494daa72009-01-06 23:10:38 +00001638 case MVT::v2i32: {
Scott Michel839ad0a2009-03-17 01:15:45 +00001639 SDValue T = DAG.getConstant(unsigned(SplatBits), VT.getVectorElementType());
Evan Chenga49de9d2009-02-25 22:49:59 +00001640 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, T, T);
Scott Michel494daa72009-01-06 23:10:38 +00001641 }
Scott Michel6e22c652007-12-04 22:23:35 +00001642 case MVT::v2i64: {
Scott Michel839ad0a2009-03-17 01:15:45 +00001643 return SPU::LowerV2I64Splat(VT, DAG, SplatBits, dl);
Scott Michel6e22c652007-12-04 22:23:35 +00001644 }
1645 }
Scott Michelfe095082008-07-16 17:17:29 +00001646
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001647 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001648}
1649
Scott Michel839ad0a2009-03-17 01:15:45 +00001650/*!
1651 */
Scott Michel9e3e4a92009-01-26 03:31:40 +00001652SDValue
Scott Michel839ad0a2009-03-17 01:15:45 +00001653SPU::LowerV2I64Splat(MVT OpVT, SelectionDAG& DAG, uint64_t SplatVal,
1654 DebugLoc dl) {
Scott Michel9e3e4a92009-01-26 03:31:40 +00001655 uint32_t upper = uint32_t(SplatVal >> 32);
1656 uint32_t lower = uint32_t(SplatVal);
1657
1658 if (upper == lower) {
1659 // Magic constant that can be matched by IL, ILA, et. al.
1660 SDValue Val = DAG.getTargetConstant(upper, MVT::i32);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001661 return DAG.getNode(ISD::BIT_CONVERT, dl, OpVT,
Evan Chenga49de9d2009-02-25 22:49:59 +00001662 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
1663 Val, Val, Val, Val));
Scott Michel9e3e4a92009-01-26 03:31:40 +00001664 } else {
Scott Michel9e3e4a92009-01-26 03:31:40 +00001665 bool upper_special, lower_special;
1666
1667 // NOTE: This code creates common-case shuffle masks that can be easily
1668 // detected as common expressions. It is not attempting to create highly
1669 // specialized masks to replace any and all 0's, 0xff's and 0x80's.
1670
1671 // Detect if the upper or lower half is a special shuffle mask pattern:
1672 upper_special = (upper == 0 || upper == 0xffffffff || upper == 0x80000000);
1673 lower_special = (lower == 0 || lower == 0xffffffff || lower == 0x80000000);
1674
Scott Michel839ad0a2009-03-17 01:15:45 +00001675 // Both upper and lower are special, lower to a constant pool load:
1676 if (lower_special && upper_special) {
1677 SDValue SplatValCN = DAG.getConstant(SplatVal, MVT::i64);
1678 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
1679 SplatValCN, SplatValCN);
1680 }
1681
1682 SDValue LO32;
1683 SDValue HI32;
1684 SmallVector<SDValue, 16> ShufBytes;
1685 SDValue Result;
1686
Scott Michel9e3e4a92009-01-26 03:31:40 +00001687 // Create lower vector if not a special pattern
1688 if (!lower_special) {
1689 SDValue LO32C = DAG.getConstant(lower, MVT::i32);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001690 LO32 = DAG.getNode(ISD::BIT_CONVERT, dl, OpVT,
Evan Chenga49de9d2009-02-25 22:49:59 +00001691 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
1692 LO32C, LO32C, LO32C, LO32C));
Scott Michel9e3e4a92009-01-26 03:31:40 +00001693 }
1694
1695 // Create upper vector if not a special pattern
1696 if (!upper_special) {
1697 SDValue HI32C = DAG.getConstant(upper, MVT::i32);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001698 HI32 = DAG.getNode(ISD::BIT_CONVERT, dl, OpVT,
Evan Chenga49de9d2009-02-25 22:49:59 +00001699 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
1700 HI32C, HI32C, HI32C, HI32C));
Scott Michel9e3e4a92009-01-26 03:31:40 +00001701 }
1702
1703 // If either upper or lower are special, then the two input operands are
1704 // the same (basically, one of them is a "don't care")
1705 if (lower_special)
1706 LO32 = HI32;
1707 if (upper_special)
1708 HI32 = LO32;
Scott Michel9e3e4a92009-01-26 03:31:40 +00001709
1710 for (int i = 0; i < 4; ++i) {
1711 uint64_t val = 0;
1712 for (int j = 0; j < 4; ++j) {
1713 SDValue V;
1714 bool process_upper, process_lower;
1715 val <<= 8;
1716 process_upper = (upper_special && (i & 1) == 0);
1717 process_lower = (lower_special && (i & 1) == 1);
1718
1719 if (process_upper || process_lower) {
1720 if ((process_upper && upper == 0)
1721 || (process_lower && lower == 0))
1722 val |= 0x80;
1723 else if ((process_upper && upper == 0xffffffff)
1724 || (process_lower && lower == 0xffffffff))
1725 val |= 0xc0;
1726 else if ((process_upper && upper == 0x80000000)
1727 || (process_lower && lower == 0x80000000))
1728 val |= (j == 0 ? 0xe0 : 0x80);
1729 } else
1730 val |= i * 4 + j + ((i & 1) * 16);
1731 }
1732
1733 ShufBytes.push_back(DAG.getConstant(val, MVT::i32));
1734 }
1735
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001736 return DAG.getNode(SPUISD::SHUFB, dl, OpVT, HI32, LO32,
Evan Chenga49de9d2009-02-25 22:49:59 +00001737 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
1738 &ShufBytes[0], ShufBytes.size()));
Scott Michel9e3e4a92009-01-26 03:31:40 +00001739 }
1740}
1741
Scott Michel6e22c652007-12-04 22:23:35 +00001742/// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3) to something on
1743/// which the Cell can operate. The code inspects V3 to ascertain whether the
1744/// permutation vector, V3, is monotonically increasing with one "exception"
1745/// element, e.g., (0, 1, _, 3). If this is the case, then generate a
Scott Michel0be03392008-11-22 23:50:42 +00001746/// SHUFFLE_MASK synthetic instruction. Otherwise, spill V3 to the constant pool.
Scott Michel6e22c652007-12-04 22:23:35 +00001747/// In either case, the net result is going to eventually invoke SHUFB to
1748/// permute/shuffle the bytes from V1 and V2.
1749/// \note
Scott Michel0be03392008-11-22 23:50:42 +00001750/// SHUFFLE_MASK is eventually selected as one of the C*D instructions, generate
Scott Michel6e22c652007-12-04 22:23:35 +00001751/// control word for byte/halfword/word insertion. This takes care of a single
1752/// element move from V2 into V1.
1753/// \note
1754/// SPUISD::SHUFB is eventually selected as Cell's <i>shufb</i> instructions.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001755static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001756 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001757 SDValue V1 = Op.getOperand(0);
1758 SDValue V2 = Op.getOperand(1);
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001759 DebugLoc dl = Op.getDebugLoc();
Scott Michelfe095082008-07-16 17:17:29 +00001760
Scott Michel6e22c652007-12-04 22:23:35 +00001761 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfe095082008-07-16 17:17:29 +00001762
Scott Michel6e22c652007-12-04 22:23:35 +00001763 // If we have a single element being moved from V1 to V2, this can be handled
1764 // using the C*[DX] compute mask instructions, but the vector elements have
1765 // to be monotonically increasing with one exception element.
Scott Michelea3c49d2008-12-04 21:01:44 +00001766 MVT VecVT = V1.getValueType();
1767 MVT EltVT = VecVT.getVectorElementType();
Scott Michel6e22c652007-12-04 22:23:35 +00001768 unsigned EltsFromV2 = 0;
1769 unsigned V2Elt = 0;
1770 unsigned V2EltIdx0 = 0;
1771 unsigned CurrElt = 0;
Scott Michelea3c49d2008-12-04 21:01:44 +00001772 unsigned MaxElts = VecVT.getVectorNumElements();
1773 unsigned PrevElt = 0;
1774 unsigned V0Elt = 0;
Scott Michel6e22c652007-12-04 22:23:35 +00001775 bool monotonic = true;
Scott Michelea3c49d2008-12-04 21:01:44 +00001776 bool rotate = true;
1777
1778 if (EltVT == MVT::i8) {
Scott Michel6e22c652007-12-04 22:23:35 +00001779 V2EltIdx0 = 16;
Scott Michelea3c49d2008-12-04 21:01:44 +00001780 } else if (EltVT == MVT::i16) {
Scott Michel6e22c652007-12-04 22:23:35 +00001781 V2EltIdx0 = 8;
Scott Michelea3c49d2008-12-04 21:01:44 +00001782 } else if (EltVT == MVT::i32 || EltVT == MVT::f32) {
Scott Michel6e22c652007-12-04 22:23:35 +00001783 V2EltIdx0 = 4;
Scott Michelea3c49d2008-12-04 21:01:44 +00001784 } else if (EltVT == MVT::i64 || EltVT == MVT::f64) {
1785 V2EltIdx0 = 2;
1786 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001787 llvm_unreachable("Unhandled vector type in LowerVECTOR_SHUFFLE");
Scott Michel6e22c652007-12-04 22:23:35 +00001788
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001789 for (unsigned i = 0; i != MaxElts; ++i) {
1790 if (SVN->getMaskElt(i) < 0)
1791 continue;
1792
1793 unsigned SrcElt = SVN->getMaskElt(i);
Scott Michel6e22c652007-12-04 22:23:35 +00001794
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001795 if (monotonic) {
1796 if (SrcElt >= V2EltIdx0) {
1797 if (1 >= (++EltsFromV2)) {
1798 V2Elt = (V2EltIdx0 - SrcElt) << 2;
Scott Michelea3c49d2008-12-04 21:01:44 +00001799 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001800 } else if (CurrElt != SrcElt) {
1801 monotonic = false;
Scott Michelea3c49d2008-12-04 21:01:44 +00001802 }
1803
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001804 ++CurrElt;
1805 }
1806
1807 if (rotate) {
1808 if (PrevElt > 0 && SrcElt < MaxElts) {
1809 if ((PrevElt == SrcElt - 1)
1810 || (PrevElt == MaxElts - 1 && SrcElt == 0)) {
Scott Michelea3c49d2008-12-04 21:01:44 +00001811 PrevElt = SrcElt;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001812 if (SrcElt == 0)
1813 V0Elt = i;
Scott Michelea3c49d2008-12-04 21:01:44 +00001814 } else {
Scott Michelea3c49d2008-12-04 21:01:44 +00001815 rotate = false;
1816 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001817 } else if (PrevElt == 0) {
1818 // First time through, need to keep track of previous element
1819 PrevElt = SrcElt;
1820 } else {
1821 // This isn't a rotation, takes elements from vector 2
1822 rotate = false;
Scott Michelea3c49d2008-12-04 21:01:44 +00001823 }
Scott Michel6e22c652007-12-04 22:23:35 +00001824 }
Scott Michel6e22c652007-12-04 22:23:35 +00001825 }
1826
1827 if (EltsFromV2 == 1 && monotonic) {
1828 // Compute mask and shuffle
1829 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnera10fff52007-12-31 04:13:23 +00001830 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1831 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
Duncan Sands13237ac2008-06-06 12:08:01 +00001832 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel6e22c652007-12-04 22:23:35 +00001833 // Initialize temporary register to 0
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001834 SDValue InitTempReg =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001835 DAG.getCopyToReg(DAG.getEntryNode(), dl, VReg, DAG.getConstant(0, PtrVT));
Scott Michel0be03392008-11-22 23:50:42 +00001836 // Copy register's contents as index in SHUFFLE_MASK:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001837 SDValue ShufMaskOp =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001838 DAG.getNode(SPUISD::SHUFFLE_MASK, dl, MVT::v4i32,
Scott Michelbb713ae2008-01-30 02:55:46 +00001839 DAG.getTargetConstant(V2Elt, MVT::i32),
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001840 DAG.getCopyFromReg(InitTempReg, dl, VReg, PtrVT));
Scott Michel6e22c652007-12-04 22:23:35 +00001841 // Use shuffle mask in SHUFB synthetic instruction:
Scott Micheld1db1ab2009-03-16 18:47:25 +00001842 return DAG.getNode(SPUISD::SHUFB, dl, V1.getValueType(), V2, V1,
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001843 ShufMaskOp);
Scott Michelea3c49d2008-12-04 21:01:44 +00001844 } else if (rotate) {
1845 int rotamt = (MaxElts - V0Elt) * EltVT.getSizeInBits()/8;
Scott Michelb8ee30d2008-12-29 03:23:36 +00001846
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001847 return DAG.getNode(SPUISD::ROTBYTES_LEFT, dl, V1.getValueType(),
Scott Michelea3c49d2008-12-04 21:01:44 +00001848 V1, DAG.getConstant(rotamt, MVT::i16));
Scott Michel6e22c652007-12-04 22:23:35 +00001849 } else {
Gabor Greif81d6a382008-08-31 15:37:04 +00001850 // Convert the SHUFFLE_VECTOR mask's input element units to the
1851 // actual bytes.
Duncan Sands13237ac2008-06-06 12:08:01 +00001852 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfe095082008-07-16 17:17:29 +00001853
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001854 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001855 for (unsigned i = 0, e = MaxElts; i != e; ++i) {
1856 unsigned SrcElt = SVN->getMaskElt(i) < 0 ? 0 : SVN->getMaskElt(i);
Scott Michelfe095082008-07-16 17:17:29 +00001857
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001858 for (unsigned j = 0; j < BytesPerElement; ++j)
1859 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,MVT::i8));
Scott Michel6e22c652007-12-04 22:23:35 +00001860 }
Scott Michelfe095082008-07-16 17:17:29 +00001861
Evan Chenga49de9d2009-02-25 22:49:59 +00001862 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
1863 &ResultMask[0], ResultMask.size());
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001864 return DAG.getNode(SPUISD::SHUFB, dl, V1.getValueType(), V1, V2, VPermMask);
Scott Michel6e22c652007-12-04 22:23:35 +00001865 }
1866}
1867
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001868static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
1869 SDValue Op0 = Op.getOperand(0); // Op0 = the scalar
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001870 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00001871
Gabor Greiff304a7a2008-08-28 21:40:38 +00001872 if (Op0.getNode()->getOpcode() == ISD::Constant) {
Scott Michel6e22c652007-12-04 22:23:35 +00001873 // For a constant, build the appropriate constant vector, which will
1874 // eventually simplify to a vector register load.
1875
Gabor Greiff304a7a2008-08-28 21:40:38 +00001876 ConstantSDNode *CN = cast<ConstantSDNode>(Op0.getNode());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001877 SmallVector<SDValue, 16> ConstVecValues;
Duncan Sands13237ac2008-06-06 12:08:01 +00001878 MVT VT;
Scott Michel6e22c652007-12-04 22:23:35 +00001879 size_t n_copies;
1880
1881 // Create a constant vector:
Duncan Sands13237ac2008-06-06 12:08:01 +00001882 switch (Op.getValueType().getSimpleVT()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001883 default: llvm_unreachable("Unexpected constant value type in "
Torok Edwin6cdb8972009-07-14 12:22:58 +00001884 "LowerSCALAR_TO_VECTOR");
Scott Michel6e22c652007-12-04 22:23:35 +00001885 case MVT::v16i8: n_copies = 16; VT = MVT::i8; break;
1886 case MVT::v8i16: n_copies = 8; VT = MVT::i16; break;
1887 case MVT::v4i32: n_copies = 4; VT = MVT::i32; break;
1888 case MVT::v4f32: n_copies = 4; VT = MVT::f32; break;
1889 case MVT::v2i64: n_copies = 2; VT = MVT::i64; break;
1890 case MVT::v2f64: n_copies = 2; VT = MVT::f64; break;
1891 }
1892
Dan Gohmaneffb8942008-09-12 16:56:44 +00001893 SDValue CValue = DAG.getConstant(CN->getZExtValue(), VT);
Scott Michel6e22c652007-12-04 22:23:35 +00001894 for (size_t j = 0; j < n_copies; ++j)
1895 ConstVecValues.push_back(CValue);
1896
Evan Chenga49de9d2009-02-25 22:49:59 +00001897 return DAG.getNode(ISD::BUILD_VECTOR, dl, Op.getValueType(),
1898 &ConstVecValues[0], ConstVecValues.size());
Scott Michel6e22c652007-12-04 22:23:35 +00001899 } else {
1900 // Otherwise, copy the value from one register to another:
Duncan Sands13237ac2008-06-06 12:08:01 +00001901 switch (Op0.getValueType().getSimpleVT()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001902 default: llvm_unreachable("Unexpected value type in LowerSCALAR_TO_VECTOR");
Scott Michel6e22c652007-12-04 22:23:35 +00001903 case MVT::i8:
1904 case MVT::i16:
1905 case MVT::i32:
1906 case MVT::i64:
1907 case MVT::f32:
1908 case MVT::f64:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001909 return DAG.getNode(SPUISD::PREFSLOT2VEC, dl, Op.getValueType(), Op0, Op0);
Scott Michel6e22c652007-12-04 22:23:35 +00001910 }
1911 }
1912
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001913 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001914}
1915
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001916static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001917 MVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001918 SDValue N = Op.getOperand(0);
1919 SDValue Elt = Op.getOperand(1);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001920 DebugLoc dl = Op.getDebugLoc();
Scott Michel0be03392008-11-22 23:50:42 +00001921 SDValue retval;
Scott Michel6e22c652007-12-04 22:23:35 +00001922
Scott Michel0be03392008-11-22 23:50:42 +00001923 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
1924 // Constant argument:
1925 int EltNo = (int) C->getZExtValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001926
Scott Michel0be03392008-11-22 23:50:42 +00001927 // sanity checks:
1928 if (VT == MVT::i8 && EltNo >= 16)
Torok Edwinfbcc6632009-07-14 16:55:14 +00001929 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i8 extraction slot > 15");
Scott Michel0be03392008-11-22 23:50:42 +00001930 else if (VT == MVT::i16 && EltNo >= 8)
Torok Edwinfbcc6632009-07-14 16:55:14 +00001931 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i16 extraction slot > 7");
Scott Michel0be03392008-11-22 23:50:42 +00001932 else if (VT == MVT::i32 && EltNo >= 4)
Torok Edwinfbcc6632009-07-14 16:55:14 +00001933 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i32 extraction slot > 4");
Scott Michel0be03392008-11-22 23:50:42 +00001934 else if (VT == MVT::i64 && EltNo >= 2)
Torok Edwinfbcc6632009-07-14 16:55:14 +00001935 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i64 extraction slot > 2");
Scott Michel6e22c652007-12-04 22:23:35 +00001936
Scott Michel0be03392008-11-22 23:50:42 +00001937 if (EltNo == 0 && (VT == MVT::i32 || VT == MVT::i64)) {
1938 // i32 and i64: Element 0 is the preferred slot
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001939 return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT, N);
Scott Michel0be03392008-11-22 23:50:42 +00001940 }
Scott Michel6e22c652007-12-04 22:23:35 +00001941
Scott Michel0be03392008-11-22 23:50:42 +00001942 // Need to generate shuffle mask and extract:
1943 int prefslot_begin = -1, prefslot_end = -1;
1944 int elt_byte = EltNo * VT.getSizeInBits() / 8;
1945
1946 switch (VT.getSimpleVT()) {
1947 default:
1948 assert(false && "Invalid value type!");
1949 case MVT::i8: {
1950 prefslot_begin = prefslot_end = 3;
1951 break;
1952 }
1953 case MVT::i16: {
1954 prefslot_begin = 2; prefslot_end = 3;
1955 break;
1956 }
1957 case MVT::i32:
1958 case MVT::f32: {
1959 prefslot_begin = 0; prefslot_end = 3;
1960 break;
1961 }
1962 case MVT::i64:
1963 case MVT::f64: {
1964 prefslot_begin = 0; prefslot_end = 7;
1965 break;
1966 }
1967 }
1968
1969 assert(prefslot_begin != -1 && prefslot_end != -1 &&
1970 "LowerEXTRACT_VECTOR_ELT: preferred slots uninitialized");
1971
1972 unsigned int ShufBytes[16];
1973 for (int i = 0; i < 16; ++i) {
1974 // zero fill uppper part of preferred slot, don't care about the
1975 // other slots:
1976 unsigned int mask_val;
1977 if (i <= prefslot_end) {
1978 mask_val =
1979 ((i < prefslot_begin)
1980 ? 0x80
1981 : elt_byte + (i - prefslot_begin));
1982
1983 ShufBytes[i] = mask_val;
1984 } else
1985 ShufBytes[i] = ShufBytes[i % (prefslot_end + 1)];
1986 }
1987
1988 SDValue ShufMask[4];
1989 for (unsigned i = 0; i < sizeof(ShufMask)/sizeof(ShufMask[0]); ++i) {
Scott Michelea3c49d2008-12-04 21:01:44 +00001990 unsigned bidx = i * 4;
Scott Michel0be03392008-11-22 23:50:42 +00001991 unsigned int bits = ((ShufBytes[bidx] << 24) |
1992 (ShufBytes[bidx+1] << 16) |
1993 (ShufBytes[bidx+2] << 8) |
1994 ShufBytes[bidx+3]);
1995 ShufMask[i] = DAG.getConstant(bits, MVT::i32);
1996 }
1997
Scott Michel839ad0a2009-03-17 01:15:45 +00001998 SDValue ShufMaskVec =
1999 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
2000 &ShufMask[0], sizeof(ShufMask)/sizeof(ShufMask[0]));
Scott Michel0be03392008-11-22 23:50:42 +00002001
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002002 retval = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
2003 DAG.getNode(SPUISD::SHUFB, dl, N.getValueType(),
Scott Michel0be03392008-11-22 23:50:42 +00002004 N, N, ShufMaskVec));
2005 } else {
2006 // Variable index: Rotate the requested element into slot 0, then replicate
2007 // slot 0 across the vector
2008 MVT VecVT = N.getValueType();
2009 if (!VecVT.isSimple() || !VecVT.isVector() || !VecVT.is128BitVector()) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00002010 llvm_report_error("LowerEXTRACT_VECTOR_ELT: Must have a simple, 128-bit"
2011 "vector type!");
Scott Michel0be03392008-11-22 23:50:42 +00002012 }
2013
2014 // Make life easier by making sure the index is zero-extended to i32
2015 if (Elt.getValueType() != MVT::i32)
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002016 Elt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Elt);
Scott Michel0be03392008-11-22 23:50:42 +00002017
2018 // Scale the index to a bit/byte shift quantity
2019 APInt scaleFactor =
Scott Michelefc8c7a2008-11-24 17:11:17 +00002020 APInt(32, uint64_t(16 / N.getValueType().getVectorNumElements()), false);
2021 unsigned scaleShift = scaleFactor.logBase2();
Scott Michel0be03392008-11-22 23:50:42 +00002022 SDValue vecShift;
Scott Michel0be03392008-11-22 23:50:42 +00002023
Scott Michelefc8c7a2008-11-24 17:11:17 +00002024 if (scaleShift > 0) {
2025 // Scale the shift factor:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002026 Elt = DAG.getNode(ISD::SHL, dl, MVT::i32, Elt,
Scott Michel08a4e202008-12-01 17:56:02 +00002027 DAG.getConstant(scaleShift, MVT::i32));
Scott Michel0be03392008-11-22 23:50:42 +00002028 }
2029
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002030 vecShift = DAG.getNode(SPUISD::SHLQUAD_L_BYTES, dl, VecVT, N, Elt);
Scott Michelefc8c7a2008-11-24 17:11:17 +00002031
2032 // Replicate the bytes starting at byte 0 across the entire vector (for
2033 // consistency with the notion of a unified register set)
Scott Michel0be03392008-11-22 23:50:42 +00002034 SDValue replicate;
2035
2036 switch (VT.getSimpleVT()) {
2037 default:
Torok Edwinfb8d6d52009-07-08 20:53:28 +00002038 llvm_report_error("LowerEXTRACT_VECTOR_ELT(varable): Unhandled vector"
2039 "type");
Scott Michel0be03392008-11-22 23:50:42 +00002040 /*NOTREACHED*/
2041 case MVT::i8: {
Scott Michelefc8c7a2008-11-24 17:11:17 +00002042 SDValue factor = DAG.getConstant(0x00000000, MVT::i32);
Scott Michel839ad0a2009-03-17 01:15:45 +00002043 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
2044 factor, factor, factor, factor);
Scott Michel0be03392008-11-22 23:50:42 +00002045 break;
2046 }
2047 case MVT::i16: {
Scott Michelefc8c7a2008-11-24 17:11:17 +00002048 SDValue factor = DAG.getConstant(0x00010001, MVT::i32);
Scott Michel839ad0a2009-03-17 01:15:45 +00002049 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
2050 factor, factor, factor, factor);
Scott Michel0be03392008-11-22 23:50:42 +00002051 break;
2052 }
2053 case MVT::i32:
2054 case MVT::f32: {
2055 SDValue factor = DAG.getConstant(0x00010203, MVT::i32);
Scott Michel839ad0a2009-03-17 01:15:45 +00002056 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
2057 factor, factor, factor, factor);
Scott Michel0be03392008-11-22 23:50:42 +00002058 break;
2059 }
2060 case MVT::i64:
2061 case MVT::f64: {
2062 SDValue loFactor = DAG.getConstant(0x00010203, MVT::i32);
2063 SDValue hiFactor = DAG.getConstant(0x04050607, MVT::i32);
Scott Michel839ad0a2009-03-17 01:15:45 +00002064 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga49de9d2009-02-25 22:49:59 +00002065 loFactor, hiFactor, loFactor, hiFactor);
Scott Michel0be03392008-11-22 23:50:42 +00002066 break;
2067 }
2068 }
2069
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002070 retval = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
2071 DAG.getNode(SPUISD::SHUFB, dl, VecVT,
Scott Michel08a4e202008-12-01 17:56:02 +00002072 vecShift, vecShift, replicate));
Scott Michel6e22c652007-12-04 22:23:35 +00002073 }
2074
Scott Michel0be03392008-11-22 23:50:42 +00002075 return retval;
Scott Michel6e22c652007-12-04 22:23:35 +00002076}
2077
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002078static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
2079 SDValue VecOp = Op.getOperand(0);
2080 SDValue ValOp = Op.getOperand(1);
2081 SDValue IdxOp = Op.getOperand(2);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002082 DebugLoc dl = Op.getDebugLoc();
Duncan Sands13237ac2008-06-06 12:08:01 +00002083 MVT VT = Op.getValueType();
Scott Michel6e22c652007-12-04 22:23:35 +00002084
2085 ConstantSDNode *CN = cast<ConstantSDNode>(IdxOp);
2086 assert(CN != 0 && "LowerINSERT_VECTOR_ELT: Index is not constant!");
2087
Duncan Sands13237ac2008-06-06 12:08:01 +00002088 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel08a4e202008-12-01 17:56:02 +00002089 // Use $sp ($1) because it's always 16-byte aligned and it's available:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002090 SDValue Pointer = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel08a4e202008-12-01 17:56:02 +00002091 DAG.getRegister(SPU::R1, PtrVT),
2092 DAG.getConstant(CN->getSExtValue(), PtrVT));
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002093 SDValue ShufMask = DAG.getNode(SPUISD::SHUFFLE_MASK, dl, VT, Pointer);
Scott Michel6e22c652007-12-04 22:23:35 +00002094
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002095 SDValue result =
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002096 DAG.getNode(SPUISD::SHUFB, dl, VT,
2097 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, ValOp),
Scott Michelb8ee30d2008-12-29 03:23:36 +00002098 VecOp,
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002099 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, ShufMask));
Scott Michel6e22c652007-12-04 22:23:35 +00002100
2101 return result;
2102}
2103
Scott Michel82335272008-12-27 04:51:36 +00002104static SDValue LowerI8Math(SDValue Op, SelectionDAG &DAG, unsigned Opc,
2105 const TargetLowering &TLI)
Scott Michel7d5eaec2008-02-23 18:41:37 +00002106{
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002107 SDValue N0 = Op.getOperand(0); // Everything has at least one operand
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002108 DebugLoc dl = Op.getDebugLoc();
Scott Michel82335272008-12-27 04:51:36 +00002109 MVT ShiftVT = TLI.getShiftAmountTy();
Scott Michel6e22c652007-12-04 22:23:35 +00002110
2111 assert(Op.getValueType() == MVT::i8);
2112 switch (Opc) {
2113 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002114 llvm_unreachable("Unhandled i8 math operator");
Scott Michel6e22c652007-12-04 22:23:35 +00002115 /*NOTREACHED*/
2116 break;
Scott Michel41236c02008-12-30 23:28:25 +00002117 case ISD::ADD: {
2118 // 8-bit addition: Promote the arguments up to 16-bits and truncate
2119 // the result:
2120 SDValue N1 = Op.getOperand(1);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002121 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
2122 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
2123 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2124 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel41236c02008-12-30 23:28:25 +00002125
2126 }
2127
Scott Michel6e22c652007-12-04 22:23:35 +00002128 case ISD::SUB: {
2129 // 8-bit subtraction: Promote the arguments up to 16-bits and truncate
2130 // the result:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002131 SDValue N1 = Op.getOperand(1);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002132 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
2133 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
2134 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2135 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michelfe095082008-07-16 17:17:29 +00002136 }
Scott Michel6e22c652007-12-04 22:23:35 +00002137 case ISD::ROTR:
2138 case ISD::ROTL: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002139 SDValue N1 = Op.getOperand(1);
Scott Michel839ad0a2009-03-17 01:15:45 +00002140 MVT N1VT = N1.getValueType();
2141
2142 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, N0);
2143 if (!N1VT.bitsEq(ShiftVT)) {
2144 unsigned N1Opc = N1.getValueType().bitsLT(ShiftVT)
2145 ? ISD::ZERO_EXTEND
2146 : ISD::TRUNCATE;
2147 N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
2148 }
2149
2150 // Replicate lower 8-bits into upper 8:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002151 SDValue ExpandArg =
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002152 DAG.getNode(ISD::OR, dl, MVT::i16, N0,
2153 DAG.getNode(ISD::SHL, dl, MVT::i16,
Duncan Sands0852f482008-10-30 19:24:28 +00002154 N0, DAG.getConstant(8, MVT::i32)));
Scott Michel839ad0a2009-03-17 01:15:45 +00002155
2156 // Truncate back down to i8
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002157 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2158 DAG.getNode(Opc, dl, MVT::i16, ExpandArg, N1));
Scott Michel6e22c652007-12-04 22:23:35 +00002159 }
2160 case ISD::SRL:
2161 case ISD::SHL: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002162 SDValue N1 = Op.getOperand(1);
Scott Michel839ad0a2009-03-17 01:15:45 +00002163 MVT N1VT = N1.getValueType();
2164
2165 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, N0);
2166 if (!N1VT.bitsEq(ShiftVT)) {
2167 unsigned N1Opc = ISD::ZERO_EXTEND;
2168
2169 if (N1.getValueType().bitsGT(ShiftVT))
2170 N1Opc = ISD::TRUNCATE;
2171
2172 N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
2173 }
2174
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002175 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2176 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel6e22c652007-12-04 22:23:35 +00002177 }
2178 case ISD::SRA: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002179 SDValue N1 = Op.getOperand(1);
Scott Michel839ad0a2009-03-17 01:15:45 +00002180 MVT N1VT = N1.getValueType();
2181
2182 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
2183 if (!N1VT.bitsEq(ShiftVT)) {
2184 unsigned N1Opc = ISD::SIGN_EXTEND;
2185
2186 if (N1VT.bitsGT(ShiftVT))
2187 N1Opc = ISD::TRUNCATE;
2188 N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
2189 }
2190
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002191 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2192 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel6e22c652007-12-04 22:23:35 +00002193 }
2194 case ISD::MUL: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002195 SDValue N1 = Op.getOperand(1);
Scott Michel839ad0a2009-03-17 01:15:45 +00002196
2197 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
2198 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002199 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2200 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel6e22c652007-12-04 22:23:35 +00002201 break;
2202 }
2203 }
2204
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002205 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00002206}
2207
2208//! Lower byte immediate operations for v16i8 vectors:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002209static SDValue
2210LowerByteImmed(SDValue Op, SelectionDAG &DAG) {
2211 SDValue ConstVec;
2212 SDValue Arg;
Duncan Sands13237ac2008-06-06 12:08:01 +00002213 MVT VT = Op.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002214 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00002215
2216 ConstVec = Op.getOperand(0);
2217 Arg = Op.getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002218 if (ConstVec.getNode()->getOpcode() != ISD::BUILD_VECTOR) {
2219 if (ConstVec.getNode()->getOpcode() == ISD::BIT_CONVERT) {
Scott Michel6e22c652007-12-04 22:23:35 +00002220 ConstVec = ConstVec.getOperand(0);
2221 } else {
2222 ConstVec = Op.getOperand(1);
2223 Arg = Op.getOperand(0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002224 if (ConstVec.getNode()->getOpcode() == ISD::BIT_CONVERT) {
Scott Michelbb713ae2008-01-30 02:55:46 +00002225 ConstVec = ConstVec.getOperand(0);
Scott Michel6e22c652007-12-04 22:23:35 +00002226 }
2227 }
2228 }
2229
Gabor Greiff304a7a2008-08-28 21:40:38 +00002230 if (ConstVec.getNode()->getOpcode() == ISD::BUILD_VECTOR) {
Scott Michel839ad0a2009-03-17 01:15:45 +00002231 BuildVectorSDNode *BCN = dyn_cast<BuildVectorSDNode>(ConstVec.getNode());
2232 assert(BCN != 0 && "Expected BuildVectorSDNode in SPU LowerByteImmed");
Scott Michel6e22c652007-12-04 22:23:35 +00002233
Scott Michel839ad0a2009-03-17 01:15:45 +00002234 APInt APSplatBits, APSplatUndef;
2235 unsigned SplatBitSize;
2236 bool HasAnyUndefs;
2237 unsigned minSplatBits = VT.getVectorElementType().getSizeInBits();
2238
2239 if (BCN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2240 HasAnyUndefs, minSplatBits)
2241 && minSplatBits <= SplatBitSize) {
2242 uint64_t SplatBits = APSplatBits.getZExtValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002243 SDValue tc = DAG.getTargetConstant(SplatBits & 0xff, MVT::i8);
Scott Michel6e22c652007-12-04 22:23:35 +00002244
Scott Michel839ad0a2009-03-17 01:15:45 +00002245 SmallVector<SDValue, 16> tcVec;
2246 tcVec.assign(16, tc);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002247 return DAG.getNode(Op.getNode()->getOpcode(), dl, VT, Arg,
Scott Michel839ad0a2009-03-17 01:15:45 +00002248 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &tcVec[0], tcVec.size()));
Scott Michel6e22c652007-12-04 22:23:35 +00002249 }
2250 }
Scott Michel49483182009-01-26 22:33:37 +00002251
Nate Begeman82f19252008-07-29 19:07:27 +00002252 // These operations (AND, OR, XOR) are legal, they just couldn't be custom
2253 // lowered. Return the operation, rather than a null SDValue.
2254 return Op;
Scott Michel6e22c652007-12-04 22:23:35 +00002255}
2256
Scott Michel6e22c652007-12-04 22:23:35 +00002257//! Custom lowering for CTPOP (count population)
2258/*!
2259 Custom lowering code that counts the number ones in the input
2260 operand. SPU has such an instruction, but it counts the number of
2261 ones per byte, which then have to be accumulated.
2262*/
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002263static SDValue LowerCTPOP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands13237ac2008-06-06 12:08:01 +00002264 MVT VT = Op.getValueType();
2265 MVT vecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits()));
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002266 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00002267
Duncan Sands13237ac2008-06-06 12:08:01 +00002268 switch (VT.getSimpleVT()) {
2269 default:
2270 assert(false && "Invalid value type!");
Scott Michel6e22c652007-12-04 22:23:35 +00002271 case MVT::i8: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002272 SDValue N = Op.getOperand(0);
2273 SDValue Elt0 = DAG.getConstant(0, MVT::i32);
Scott Michel6e22c652007-12-04 22:23:35 +00002274
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002275 SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
2276 SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +00002277
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002278 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i8, CNTB, Elt0);
Scott Michel6e22c652007-12-04 22:23:35 +00002279 }
2280
2281 case MVT::i16: {
2282 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnera10fff52007-12-31 04:13:23 +00002283 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel6e22c652007-12-04 22:23:35 +00002284
Chris Lattnera10fff52007-12-31 04:13:23 +00002285 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R16CRegClass);
Scott Michel6e22c652007-12-04 22:23:35 +00002286
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002287 SDValue N = Op.getOperand(0);
2288 SDValue Elt0 = DAG.getConstant(0, MVT::i16);
2289 SDValue Mask0 = DAG.getConstant(0x0f, MVT::i16);
Duncan Sands0852f482008-10-30 19:24:28 +00002290 SDValue Shift1 = DAG.getConstant(8, MVT::i32);
Scott Michel6e22c652007-12-04 22:23:35 +00002291
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002292 SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
2293 SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +00002294
2295 // CNTB_result becomes the chain to which all of the virtual registers
2296 // CNTB_reg, SUM1_reg become associated:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002297 SDValue CNTB_result =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002298 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, CNTB, Elt0);
Scott Michelfe095082008-07-16 17:17:29 +00002299
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002300 SDValue CNTB_rescopy =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002301 DAG.getCopyToReg(CNTB_result, dl, CNTB_reg, CNTB_result);
Scott Michel6e22c652007-12-04 22:23:35 +00002302
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002303 SDValue Tmp1 = DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i16);
Scott Michel6e22c652007-12-04 22:23:35 +00002304
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002305 return DAG.getNode(ISD::AND, dl, MVT::i16,
2306 DAG.getNode(ISD::ADD, dl, MVT::i16,
2307 DAG.getNode(ISD::SRL, dl, MVT::i16,
Scott Michelbb713ae2008-01-30 02:55:46 +00002308 Tmp1, Shift1),
2309 Tmp1),
2310 Mask0);
Scott Michel6e22c652007-12-04 22:23:35 +00002311 }
2312
2313 case MVT::i32: {
2314 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnera10fff52007-12-31 04:13:23 +00002315 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel6e22c652007-12-04 22:23:35 +00002316
Chris Lattnera10fff52007-12-31 04:13:23 +00002317 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
2318 unsigned SUM1_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
Scott Michel6e22c652007-12-04 22:23:35 +00002319
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002320 SDValue N = Op.getOperand(0);
2321 SDValue Elt0 = DAG.getConstant(0, MVT::i32);
2322 SDValue Mask0 = DAG.getConstant(0xff, MVT::i32);
2323 SDValue Shift1 = DAG.getConstant(16, MVT::i32);
2324 SDValue Shift2 = DAG.getConstant(8, MVT::i32);
Scott Michel6e22c652007-12-04 22:23:35 +00002325
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002326 SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
2327 SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +00002328
2329 // CNTB_result becomes the chain to which all of the virtual registers
2330 // CNTB_reg, SUM1_reg become associated:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002331 SDValue CNTB_result =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002332 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, CNTB, Elt0);
Scott Michelfe095082008-07-16 17:17:29 +00002333
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002334 SDValue CNTB_rescopy =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002335 DAG.getCopyToReg(CNTB_result, dl, CNTB_reg, CNTB_result);
Scott Michel6e22c652007-12-04 22:23:35 +00002336
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002337 SDValue Comp1 =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002338 DAG.getNode(ISD::SRL, dl, MVT::i32,
Scott Micheld1db1ab2009-03-16 18:47:25 +00002339 DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i32),
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002340 Shift1);
Scott Michel6e22c652007-12-04 22:23:35 +00002341
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002342 SDValue Sum1 =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002343 DAG.getNode(ISD::ADD, dl, MVT::i32, Comp1,
2344 DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i32));
Scott Michel6e22c652007-12-04 22:23:35 +00002345
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002346 SDValue Sum1_rescopy =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002347 DAG.getCopyToReg(CNTB_result, dl, SUM1_reg, Sum1);
Scott Michel6e22c652007-12-04 22:23:35 +00002348
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002349 SDValue Comp2 =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002350 DAG.getNode(ISD::SRL, dl, MVT::i32,
2351 DAG.getCopyFromReg(Sum1_rescopy, dl, SUM1_reg, MVT::i32),
Scott Michelbb713ae2008-01-30 02:55:46 +00002352 Shift2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002353 SDValue Sum2 =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002354 DAG.getNode(ISD::ADD, dl, MVT::i32, Comp2,
2355 DAG.getCopyFromReg(Sum1_rescopy, dl, SUM1_reg, MVT::i32));
Scott Michel6e22c652007-12-04 22:23:35 +00002356
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002357 return DAG.getNode(ISD::AND, dl, MVT::i32, Sum2, Mask0);
Scott Michel6e22c652007-12-04 22:23:35 +00002358 }
2359
2360 case MVT::i64:
2361 break;
2362 }
2363
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002364 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00002365}
2366
Scott Michel9e3e4a92009-01-26 03:31:40 +00002367//! Lower ISD::FP_TO_SINT, ISD::FP_TO_UINT for i32
Scott Micheled7d79f2009-01-21 04:58:48 +00002368/*!
Scott Michel9e3e4a92009-01-26 03:31:40 +00002369 f32->i32 passes through unchanged, whereas f64->i32 expands to a libcall.
2370 All conversions to i64 are expanded to a libcall.
Scott Micheled7d79f2009-01-21 04:58:48 +00002371 */
Scott Michel9e3e4a92009-01-26 03:31:40 +00002372static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
2373 SPUTargetLowering &TLI) {
Scott Micheled7d79f2009-01-21 04:58:48 +00002374 MVT OpVT = Op.getValueType();
Scott Micheled7d79f2009-01-21 04:58:48 +00002375 SDValue Op0 = Op.getOperand(0);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002376 MVT Op0VT = Op0.getValueType();
Scott Micheled7d79f2009-01-21 04:58:48 +00002377
Scott Michel9e3e4a92009-01-26 03:31:40 +00002378 if ((OpVT == MVT::i32 && Op0VT == MVT::f64)
2379 || OpVT == MVT::i64) {
2380 // Convert f32 / f64 to i32 / i64 via libcall.
2381 RTLIB::Libcall LC =
2382 (Op.getOpcode() == ISD::FP_TO_SINT)
2383 ? RTLIB::getFPTOSINT(Op0VT, OpVT)
2384 : RTLIB::getFPTOUINT(Op0VT, OpVT);
2385 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
2386 SDValue Dummy;
2387 return ExpandLibCall(LC, Op, DAG, false, Dummy, TLI);
2388 }
Scott Micheled7d79f2009-01-21 04:58:48 +00002389
Eli Friedmanacb851a2009-05-27 00:47:34 +00002390 return Op;
Scott Michel9e3e4a92009-01-26 03:31:40 +00002391}
Scott Micheled7d79f2009-01-21 04:58:48 +00002392
Scott Michel9e3e4a92009-01-26 03:31:40 +00002393//! Lower ISD::SINT_TO_FP, ISD::UINT_TO_FP for i32
2394/*!
2395 i32->f32 passes through unchanged, whereas i32->f64 is expanded to a libcall.
2396 All conversions from i64 are expanded to a libcall.
2397 */
2398static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2399 SPUTargetLowering &TLI) {
2400 MVT OpVT = Op.getValueType();
2401 SDValue Op0 = Op.getOperand(0);
2402 MVT Op0VT = Op0.getValueType();
2403
2404 if ((OpVT == MVT::f64 && Op0VT == MVT::i32)
2405 || Op0VT == MVT::i64) {
2406 // Convert i32, i64 to f64 via libcall:
2407 RTLIB::Libcall LC =
2408 (Op.getOpcode() == ISD::SINT_TO_FP)
2409 ? RTLIB::getSINTTOFP(Op0VT, OpVT)
2410 : RTLIB::getUINTTOFP(Op0VT, OpVT);
2411 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd int-to-fp conversion!");
2412 SDValue Dummy;
2413 return ExpandLibCall(LC, Op, DAG, false, Dummy, TLI);
2414 }
2415
Eli Friedmanacb851a2009-05-27 00:47:34 +00002416 return Op;
Scott Micheled7d79f2009-01-21 04:58:48 +00002417}
2418
2419//! Lower ISD::SETCC
2420/*!
2421 This handles MVT::f64 (double floating point) condition lowering
2422 */
Scott Micheled7d79f2009-01-21 04:58:48 +00002423static SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG,
2424 const TargetLowering &TLI) {
Scott Michel9e3e4a92009-01-26 03:31:40 +00002425 CondCodeSDNode *CC = dyn_cast<CondCodeSDNode>(Op.getOperand(2));
Dale Johannesen9c310712009-02-07 19:59:05 +00002426 DebugLoc dl = Op.getDebugLoc();
Scott Michel9e3e4a92009-01-26 03:31:40 +00002427 assert(CC != 0 && "LowerSETCC: CondCodeSDNode should not be null here!\n");
2428
Scott Micheled7d79f2009-01-21 04:58:48 +00002429 SDValue lhs = Op.getOperand(0);
2430 SDValue rhs = Op.getOperand(1);
Scott Micheled7d79f2009-01-21 04:58:48 +00002431 MVT lhsVT = lhs.getValueType();
Scott Micheled7d79f2009-01-21 04:58:48 +00002432 assert(lhsVT == MVT::f64 && "LowerSETCC: type other than MVT::64\n");
2433
Scott Michel9e3e4a92009-01-26 03:31:40 +00002434 MVT ccResultVT = TLI.getSetCCResultType(lhs.getValueType());
2435 APInt ccResultOnes = APInt::getAllOnesValue(ccResultVT.getSizeInBits());
2436 MVT IntVT(MVT::i64);
2437
2438 // Take advantage of the fact that (truncate (sra arg, 32)) is efficiently
2439 // selected to a NOP:
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002440 SDValue i64lhs = DAG.getNode(ISD::BIT_CONVERT, dl, IntVT, lhs);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002441 SDValue lhsHi32 =
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002442 DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
2443 DAG.getNode(ISD::SRL, dl, IntVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002444 i64lhs, DAG.getConstant(32, MVT::i32)));
2445 SDValue lhsHi32abs =
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002446 DAG.getNode(ISD::AND, dl, MVT::i32,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002447 lhsHi32, DAG.getConstant(0x7fffffff, MVT::i32));
2448 SDValue lhsLo32 =
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002449 DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, i64lhs);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002450
2451 // SETO and SETUO only use the lhs operand:
2452 if (CC->get() == ISD::SETO) {
2453 // Evaluates to true if Op0 is not [SQ]NaN - lowers to the inverse of
2454 // SETUO
2455 APInt ccResultAllOnes = APInt::getAllOnesValue(ccResultVT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002456 return DAG.getNode(ISD::XOR, dl, ccResultVT,
2457 DAG.getSetCC(dl, ccResultVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002458 lhs, DAG.getConstantFP(0.0, lhsVT),
2459 ISD::SETUO),
2460 DAG.getConstant(ccResultAllOnes, ccResultVT));
2461 } else if (CC->get() == ISD::SETUO) {
2462 // Evaluates to true if Op0 is [SQ]NaN
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002463 return DAG.getNode(ISD::AND, dl, ccResultVT,
2464 DAG.getSetCC(dl, ccResultVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002465 lhsHi32abs,
2466 DAG.getConstant(0x7ff00000, MVT::i32),
2467 ISD::SETGE),
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002468 DAG.getSetCC(dl, ccResultVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002469 lhsLo32,
2470 DAG.getConstant(0, MVT::i32),
2471 ISD::SETGT));
2472 }
2473
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002474 SDValue i64rhs = DAG.getNode(ISD::BIT_CONVERT, dl, IntVT, rhs);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002475 SDValue rhsHi32 =
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002476 DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
2477 DAG.getNode(ISD::SRL, dl, IntVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002478 i64rhs, DAG.getConstant(32, MVT::i32)));
2479
2480 // If a value is negative, subtract from the sign magnitude constant:
2481 SDValue signMag2TC = DAG.getConstant(0x8000000000000000ULL, IntVT);
2482
2483 // Convert the sign-magnitude representation into 2's complement:
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002484 SDValue lhsSelectMask = DAG.getNode(ISD::SRA, dl, ccResultVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002485 lhsHi32, DAG.getConstant(31, MVT::i32));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002486 SDValue lhsSignMag2TC = DAG.getNode(ISD::SUB, dl, IntVT, signMag2TC, i64lhs);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002487 SDValue lhsSelect =
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002488 DAG.getNode(ISD::SELECT, dl, IntVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002489 lhsSelectMask, lhsSignMag2TC, i64lhs);
2490
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002491 SDValue rhsSelectMask = DAG.getNode(ISD::SRA, dl, ccResultVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002492 rhsHi32, DAG.getConstant(31, MVT::i32));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002493 SDValue rhsSignMag2TC = DAG.getNode(ISD::SUB, dl, IntVT, signMag2TC, i64rhs);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002494 SDValue rhsSelect =
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002495 DAG.getNode(ISD::SELECT, dl, IntVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002496 rhsSelectMask, rhsSignMag2TC, i64rhs);
2497
2498 unsigned compareOp;
2499
Scott Micheled7d79f2009-01-21 04:58:48 +00002500 switch (CC->get()) {
2501 case ISD::SETOEQ:
Scott Micheled7d79f2009-01-21 04:58:48 +00002502 case ISD::SETUEQ:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002503 compareOp = ISD::SETEQ; break;
2504 case ISD::SETOGT:
Scott Micheled7d79f2009-01-21 04:58:48 +00002505 case ISD::SETUGT:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002506 compareOp = ISD::SETGT; break;
2507 case ISD::SETOGE:
Scott Micheled7d79f2009-01-21 04:58:48 +00002508 case ISD::SETUGE:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002509 compareOp = ISD::SETGE; break;
2510 case ISD::SETOLT:
Scott Micheled7d79f2009-01-21 04:58:48 +00002511 case ISD::SETULT:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002512 compareOp = ISD::SETLT; break;
2513 case ISD::SETOLE:
Scott Micheled7d79f2009-01-21 04:58:48 +00002514 case ISD::SETULE:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002515 compareOp = ISD::SETLE; break;
Scott Micheled7d79f2009-01-21 04:58:48 +00002516 case ISD::SETUNE:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002517 case ISD::SETONE:
2518 compareOp = ISD::SETNE; break;
Scott Micheled7d79f2009-01-21 04:58:48 +00002519 default:
Torok Edwinfb8d6d52009-07-08 20:53:28 +00002520 llvm_report_error("CellSPU ISel Select: unimplemented f64 condition");
Scott Micheled7d79f2009-01-21 04:58:48 +00002521 }
2522
Scott Michel9e3e4a92009-01-26 03:31:40 +00002523 SDValue result =
Scott Micheld1db1ab2009-03-16 18:47:25 +00002524 DAG.getSetCC(dl, ccResultVT, lhsSelect, rhsSelect,
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002525 (ISD::CondCode) compareOp);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002526
2527 if ((CC->get() & 0x8) == 0) {
2528 // Ordered comparison:
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002529 SDValue lhsNaN = DAG.getSetCC(dl, ccResultVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002530 lhs, DAG.getConstantFP(0.0, MVT::f64),
2531 ISD::SETO);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002532 SDValue rhsNaN = DAG.getSetCC(dl, ccResultVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002533 rhs, DAG.getConstantFP(0.0, MVT::f64),
2534 ISD::SETO);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002535 SDValue ordered = DAG.getNode(ISD::AND, dl, ccResultVT, lhsNaN, rhsNaN);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002536
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002537 result = DAG.getNode(ISD::AND, dl, ccResultVT, ordered, result);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002538 }
2539
2540 return result;
Scott Micheled7d79f2009-01-21 04:58:48 +00002541}
2542
Scott Michel0be03392008-11-22 23:50:42 +00002543//! Lower ISD::SELECT_CC
2544/*!
2545 ISD::SELECT_CC can (generally) be implemented directly on the SPU using the
2546 SELB instruction.
2547
2548 \note Need to revisit this in the future: if the code path through the true
2549 and false value computations is longer than the latency of a branch (6
2550 cycles), then it would be more advantageous to branch and insert a new basic
2551 block and branch on the condition. However, this code does not make that
2552 assumption, given the simplisitc uses so far.
2553 */
2554
Scott Michel82335272008-12-27 04:51:36 +00002555static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2556 const TargetLowering &TLI) {
Scott Michel0be03392008-11-22 23:50:42 +00002557 MVT VT = Op.getValueType();
2558 SDValue lhs = Op.getOperand(0);
2559 SDValue rhs = Op.getOperand(1);
2560 SDValue trueval = Op.getOperand(2);
2561 SDValue falseval = Op.getOperand(3);
2562 SDValue condition = Op.getOperand(4);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002563 DebugLoc dl = Op.getDebugLoc();
Scott Michel0be03392008-11-22 23:50:42 +00002564
Scott Michel82335272008-12-27 04:51:36 +00002565 // NOTE: SELB's arguments: $rA, $rB, $mask
2566 //
2567 // SELB selects bits from $rA where bits in $mask are 0, bits from $rB
2568 // where bits in $mask are 1. CCond will be inverted, having 1s where the
2569 // condition was true and 0s where the condition was false. Hence, the
2570 // arguments to SELB get reversed.
2571
Scott Michel0be03392008-11-22 23:50:42 +00002572 // Note: Really should be ISD::SELECT instead of SPUISD::SELB, but LLVM's
2573 // legalizer insists on combining SETCC/SELECT into SELECT_CC, so we end up
2574 // with another "cannot select select_cc" assert:
2575
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002576 SDValue compare = DAG.getNode(ISD::SETCC, dl,
Duncan Sands8feb6942009-01-01 15:52:00 +00002577 TLI.getSetCCResultType(Op.getValueType()),
Scott Michel82335272008-12-27 04:51:36 +00002578 lhs, rhs, condition);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002579 return DAG.getNode(SPUISD::SELB, dl, VT, falseval, trueval, compare);
Scott Michel0be03392008-11-22 23:50:42 +00002580}
2581
Scott Michel73640252008-12-02 19:53:53 +00002582//! Custom lower ISD::TRUNCATE
2583static SDValue LowerTRUNCATE(SDValue Op, SelectionDAG &DAG)
2584{
Scott Micheld1db1ab2009-03-16 18:47:25 +00002585 // Type to truncate to
Scott Michel73640252008-12-02 19:53:53 +00002586 MVT VT = Op.getValueType();
2587 MVT::SimpleValueType simpleVT = VT.getSimpleVT();
2588 MVT VecVT = MVT::getVectorVT(VT, (128 / VT.getSizeInBits()));
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002589 DebugLoc dl = Op.getDebugLoc();
Scott Michel73640252008-12-02 19:53:53 +00002590
Scott Micheld1db1ab2009-03-16 18:47:25 +00002591 // Type to truncate from
Scott Michel73640252008-12-02 19:53:53 +00002592 SDValue Op0 = Op.getOperand(0);
2593 MVT Op0VT = Op0.getValueType();
Scott Michel73640252008-12-02 19:53:53 +00002594
Scott Michel82335272008-12-27 04:51:36 +00002595 if (Op0VT.getSimpleVT() == MVT::i128 && simpleVT == MVT::i64) {
Scott Michel6a1f6272009-01-03 00:27:53 +00002596 // Create shuffle mask, least significant doubleword of quadword
Scott Michel82335272008-12-27 04:51:36 +00002597 unsigned maskHigh = 0x08090a0b;
2598 unsigned maskLow = 0x0c0d0e0f;
2599 // Use a shuffle to perform the truncation
Evan Chenga49de9d2009-02-25 22:49:59 +00002600 SDValue shufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
2601 DAG.getConstant(maskHigh, MVT::i32),
2602 DAG.getConstant(maskLow, MVT::i32),
2603 DAG.getConstant(maskHigh, MVT::i32),
2604 DAG.getConstant(maskLow, MVT::i32));
Scott Michel82335272008-12-27 04:51:36 +00002605
Scott Micheld1db1ab2009-03-16 18:47:25 +00002606 SDValue truncShuffle = DAG.getNode(SPUISD::SHUFB, dl, VecVT,
2607 Op0, Op0, shufMask);
Scott Michel82335272008-12-27 04:51:36 +00002608
Scott Micheld1db1ab2009-03-16 18:47:25 +00002609 return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT, truncShuffle);
Scott Michel73640252008-12-02 19:53:53 +00002610 }
2611
Scott Michel82335272008-12-27 04:51:36 +00002612 return SDValue(); // Leave the truncate unmolested
Scott Michel73640252008-12-02 19:53:53 +00002613}
2614
Scott Michel0be03392008-11-22 23:50:42 +00002615//! Custom (target-specific) lowering entry point
2616/*!
2617 This is where LLVM's DAG selection process calls to do target-specific
2618 lowering of nodes.
2619 */
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002620SDValue
2621SPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
Scott Michel6e22c652007-12-04 22:23:35 +00002622{
Scott Michel7d5eaec2008-02-23 18:41:37 +00002623 unsigned Opc = (unsigned) Op.getOpcode();
Duncan Sands13237ac2008-06-06 12:08:01 +00002624 MVT VT = Op.getValueType();
Scott Michel7d5eaec2008-02-23 18:41:37 +00002625
2626 switch (Opc) {
Scott Michel6e22c652007-12-04 22:23:35 +00002627 default: {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00002628#ifndef NDEBUG
Scott Michel6e22c652007-12-04 22:23:35 +00002629 cerr << "SPUTargetLowering::LowerOperation(): need to lower this!\n";
Scott Michel7d5eaec2008-02-23 18:41:37 +00002630 cerr << "Op.getOpcode() = " << Opc << "\n";
Gabor Greiff304a7a2008-08-28 21:40:38 +00002631 cerr << "*Op.getNode():\n";
2632 Op.getNode()->dump();
Torok Edwinfb8d6d52009-07-08 20:53:28 +00002633#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00002634 llvm_unreachable(0);
Scott Michel6e22c652007-12-04 22:23:35 +00002635 }
2636 case ISD::LOAD:
Scott Michel73640252008-12-02 19:53:53 +00002637 case ISD::EXTLOAD:
Scott Michel6e22c652007-12-04 22:23:35 +00002638 case ISD::SEXTLOAD:
2639 case ISD::ZEXTLOAD:
2640 return LowerLOAD(Op, DAG, SPUTM.getSubtargetImpl());
2641 case ISD::STORE:
2642 return LowerSTORE(Op, DAG, SPUTM.getSubtargetImpl());
2643 case ISD::ConstantPool:
2644 return LowerConstantPool(Op, DAG, SPUTM.getSubtargetImpl());
2645 case ISD::GlobalAddress:
2646 return LowerGlobalAddress(Op, DAG, SPUTM.getSubtargetImpl());
2647 case ISD::JumpTable:
2648 return LowerJumpTable(Op, DAG, SPUTM.getSubtargetImpl());
Scott Michel6e22c652007-12-04 22:23:35 +00002649 case ISD::ConstantFP:
2650 return LowerConstantFP(Op, DAG);
2651 case ISD::FORMAL_ARGUMENTS:
Scott Michele4d3e3c2008-01-17 20:38:41 +00002652 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex);
Scott Michel6e22c652007-12-04 22:23:35 +00002653 case ISD::CALL:
Scott Michel8d5841a2008-01-11 02:53:15 +00002654 return LowerCALL(Op, DAG, SPUTM.getSubtargetImpl());
Scott Michel6e22c652007-12-04 22:23:35 +00002655 case ISD::RET:
2656 return LowerRET(Op, DAG, getTargetMachine());
2657
Scott Michel41236c02008-12-30 23:28:25 +00002658 // i8, i64 math ops:
Scott Micheld831cc42008-06-02 22:18:03 +00002659 case ISD::ADD:
Scott Michel6e22c652007-12-04 22:23:35 +00002660 case ISD::SUB:
2661 case ISD::ROTR:
2662 case ISD::ROTL:
2663 case ISD::SRL:
2664 case ISD::SHL:
Scott Micheld831cc42008-06-02 22:18:03 +00002665 case ISD::SRA: {
Scott Michel7d5eaec2008-02-23 18:41:37 +00002666 if (VT == MVT::i8)
Scott Michel82335272008-12-27 04:51:36 +00002667 return LowerI8Math(Op, DAG, Opc, *this);
Scott Michel7d5eaec2008-02-23 18:41:37 +00002668 break;
Scott Micheld831cc42008-06-02 22:18:03 +00002669 }
Scott Michel6e22c652007-12-04 22:23:35 +00002670
Scott Michel9e3e4a92009-01-26 03:31:40 +00002671 case ISD::FP_TO_SINT:
2672 case ISD::FP_TO_UINT:
2673 return LowerFP_TO_INT(Op, DAG, *this);
2674
2675 case ISD::SINT_TO_FP:
2676 case ISD::UINT_TO_FP:
2677 return LowerINT_TO_FP(Op, DAG, *this);
Scott Micheled7d79f2009-01-21 04:58:48 +00002678
Scott Michel6e22c652007-12-04 22:23:35 +00002679 // Vector-related lowering.
2680 case ISD::BUILD_VECTOR:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002681 return LowerBUILD_VECTOR(Op, DAG);
Scott Michel6e22c652007-12-04 22:23:35 +00002682 case ISD::SCALAR_TO_VECTOR:
2683 return LowerSCALAR_TO_VECTOR(Op, DAG);
2684 case ISD::VECTOR_SHUFFLE:
2685 return LowerVECTOR_SHUFFLE(Op, DAG);
2686 case ISD::EXTRACT_VECTOR_ELT:
2687 return LowerEXTRACT_VECTOR_ELT(Op, DAG);
2688 case ISD::INSERT_VECTOR_ELT:
2689 return LowerINSERT_VECTOR_ELT(Op, DAG);
2690
2691 // Look for ANDBI, ORBI and XORBI opportunities and lower appropriately:
2692 case ISD::AND:
2693 case ISD::OR:
2694 case ISD::XOR:
2695 return LowerByteImmed(Op, DAG);
2696
2697 // Vector and i8 multiply:
2698 case ISD::MUL:
Scott Michel41236c02008-12-30 23:28:25 +00002699 if (VT == MVT::i8)
Scott Michel82335272008-12-27 04:51:36 +00002700 return LowerI8Math(Op, DAG, Opc, *this);
Scott Michel6e22c652007-12-04 22:23:35 +00002701
Scott Michel6e22c652007-12-04 22:23:35 +00002702 case ISD::CTPOP:
2703 return LowerCTPOP(Op, DAG);
Scott Michel0be03392008-11-22 23:50:42 +00002704
2705 case ISD::SELECT_CC:
Scott Michel82335272008-12-27 04:51:36 +00002706 return LowerSELECT_CC(Op, DAG, *this);
Scott Michel73640252008-12-02 19:53:53 +00002707
Scott Micheled7d79f2009-01-21 04:58:48 +00002708 case ISD::SETCC:
2709 return LowerSETCC(Op, DAG, *this);
2710
Scott Michel73640252008-12-02 19:53:53 +00002711 case ISD::TRUNCATE:
2712 return LowerTRUNCATE(Op, DAG);
Scott Michel6e22c652007-12-04 22:23:35 +00002713 }
2714
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002715 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00002716}
2717
Duncan Sands6ed40142008-12-01 11:39:25 +00002718void SPUTargetLowering::ReplaceNodeResults(SDNode *N,
2719 SmallVectorImpl<SDValue>&Results,
2720 SelectionDAG &DAG)
Scott Michelabad22c2008-11-10 23:43:06 +00002721{
2722#if 0
2723 unsigned Opc = (unsigned) N->getOpcode();
2724 MVT OpVT = N->getValueType(0);
2725
2726 switch (Opc) {
2727 default: {
2728 cerr << "SPUTargetLowering::ReplaceNodeResults(): need to fix this!\n";
2729 cerr << "Op.getOpcode() = " << Opc << "\n";
2730 cerr << "*Op.getNode():\n";
2731 N->dump();
2732 abort();
2733 /*NOTREACHED*/
2734 }
2735 }
2736#endif
2737
2738 /* Otherwise, return unchanged */
Scott Michelabad22c2008-11-10 23:43:06 +00002739}
2740
Scott Michel6e22c652007-12-04 22:23:35 +00002741//===----------------------------------------------------------------------===//
Scott Michel6e22c652007-12-04 22:23:35 +00002742// Target Optimization Hooks
2743//===----------------------------------------------------------------------===//
2744
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002745SDValue
Scott Michel6e22c652007-12-04 22:23:35 +00002746SPUTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const
2747{
2748#if 0
2749 TargetMachine &TM = getTargetMachine();
Scott Michelceae3bb2008-01-29 02:16:57 +00002750#endif
2751 const SPUSubtarget *ST = SPUTM.getSubtargetImpl();
Scott Michel6e22c652007-12-04 22:23:35 +00002752 SelectionDAG &DAG = DCI.DAG;
Scott Michel08a4e202008-12-01 17:56:02 +00002753 SDValue Op0 = N->getOperand(0); // everything has at least one operand
2754 MVT NodeVT = N->getValueType(0); // The node's value type
Scott Michel82335272008-12-27 04:51:36 +00002755 MVT Op0VT = Op0.getValueType(); // The first operand's result
Scott Michel08a4e202008-12-01 17:56:02 +00002756 SDValue Result; // Initially, empty result
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002757 DebugLoc dl = N->getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00002758
2759 switch (N->getOpcode()) {
2760 default: break;
Scott Michelceae3bb2008-01-29 02:16:57 +00002761 case ISD::ADD: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002762 SDValue Op1 = N->getOperand(1);
Scott Michelceae3bb2008-01-29 02:16:57 +00002763
Scott Michel82335272008-12-27 04:51:36 +00002764 if (Op0.getOpcode() == SPUISD::IndirectAddr
2765 || Op1.getOpcode() == SPUISD::IndirectAddr) {
2766 // Normalize the operands to reduce repeated code
2767 SDValue IndirectArg = Op0, AddArg = Op1;
Scott Michelb8ee30d2008-12-29 03:23:36 +00002768
Scott Michel82335272008-12-27 04:51:36 +00002769 if (Op1.getOpcode() == SPUISD::IndirectAddr) {
2770 IndirectArg = Op1;
2771 AddArg = Op0;
2772 }
2773
2774 if (isa<ConstantSDNode>(AddArg)) {
2775 ConstantSDNode *CN0 = cast<ConstantSDNode > (AddArg);
2776 SDValue IndOp1 = IndirectArg.getOperand(1);
2777
2778 if (CN0->isNullValue()) {
2779 // (add (SPUindirect <arg>, <arg>), 0) ->
2780 // (SPUindirect <arg>, <arg>)
Scott Michelceae3bb2008-01-29 02:16:57 +00002781
Scott Michel187250b2008-12-04 17:16:59 +00002782#if !defined(NDEBUG)
Scott Michel82335272008-12-27 04:51:36 +00002783 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Scott Michel40f54d22008-12-04 03:02:42 +00002784 cerr << "\n"
Scott Michel82335272008-12-27 04:51:36 +00002785 << "Replace: (add (SPUindirect <arg>, <arg>), 0)\n"
2786 << "With: (SPUindirect <arg>, <arg>)\n";
2787 }
Scott Michel40f54d22008-12-04 03:02:42 +00002788#endif
2789
Scott Michel82335272008-12-27 04:51:36 +00002790 return IndirectArg;
2791 } else if (isa<ConstantSDNode>(IndOp1)) {
2792 // (add (SPUindirect <arg>, <const>), <const>) ->
2793 // (SPUindirect <arg>, <const + const>)
2794 ConstantSDNode *CN1 = cast<ConstantSDNode > (IndOp1);
2795 int64_t combinedConst = CN0->getSExtValue() + CN1->getSExtValue();
2796 SDValue combinedValue = DAG.getConstant(combinedConst, Op0VT);
Scott Michelceae3bb2008-01-29 02:16:57 +00002797
Scott Michel82335272008-12-27 04:51:36 +00002798#if !defined(NDEBUG)
2799 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
2800 cerr << "\n"
2801 << "Replace: (add (SPUindirect <arg>, " << CN1->getSExtValue()
2802 << "), " << CN0->getSExtValue() << ")\n"
2803 << "With: (SPUindirect <arg>, "
2804 << combinedConst << ")\n";
2805 }
2806#endif
Scott Michelceae3bb2008-01-29 02:16:57 +00002807
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002808 return DAG.getNode(SPUISD::IndirectAddr, dl, Op0VT,
Scott Michel82335272008-12-27 04:51:36 +00002809 IndirectArg, combinedValue);
2810 }
Scott Michelceae3bb2008-01-29 02:16:57 +00002811 }
2812 }
Scott Michel7d5eaec2008-02-23 18:41:37 +00002813 break;
2814 }
2815 case ISD::SIGN_EXTEND:
2816 case ISD::ZERO_EXTEND:
2817 case ISD::ANY_EXTEND: {
Scott Michel08a4e202008-12-01 17:56:02 +00002818 if (Op0.getOpcode() == SPUISD::VEC2PREFSLOT && NodeVT == Op0VT) {
Scott Michel7d5eaec2008-02-23 18:41:37 +00002819 // (any_extend (SPUextract_elt0 <arg>)) ->
2820 // (SPUextract_elt0 <arg>)
2821 // Types must match, however...
Scott Michel187250b2008-12-04 17:16:59 +00002822#if !defined(NDEBUG)
2823 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Scott Michel40f54d22008-12-04 03:02:42 +00002824 cerr << "\nReplace: ";
2825 N->dump(&DAG);
2826 cerr << "\nWith: ";
2827 Op0.getNode()->dump(&DAG);
2828 cerr << "\n";
Scott Michel187250b2008-12-04 17:16:59 +00002829 }
Scott Michel40f54d22008-12-04 03:02:42 +00002830#endif
Scott Michel7d5eaec2008-02-23 18:41:37 +00002831
2832 return Op0;
2833 }
2834 break;
2835 }
2836 case SPUISD::IndirectAddr: {
2837 if (!ST->usingLargeMem() && Op0.getOpcode() == SPUISD::AFormAddr) {
Scott Micheled7d79f2009-01-21 04:58:48 +00002838 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1));
2839 if (CN != 0 && CN->getZExtValue() == 0) {
Scott Michel7d5eaec2008-02-23 18:41:37 +00002840 // (SPUindirect (SPUaform <addr>, 0), 0) ->
2841 // (SPUaform <addr>, 0)
2842
2843 DEBUG(cerr << "Replace: ");
2844 DEBUG(N->dump(&DAG));
2845 DEBUG(cerr << "\nWith: ");
Gabor Greiff304a7a2008-08-28 21:40:38 +00002846 DEBUG(Op0.getNode()->dump(&DAG));
Scott Michel7d5eaec2008-02-23 18:41:37 +00002847 DEBUG(cerr << "\n");
2848
2849 return Op0;
2850 }
Scott Michel82335272008-12-27 04:51:36 +00002851 } else if (Op0.getOpcode() == ISD::ADD) {
2852 SDValue Op1 = N->getOperand(1);
2853 if (ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(Op1)) {
2854 // (SPUindirect (add <arg>, <arg>), 0) ->
2855 // (SPUindirect <arg>, <arg>)
2856 if (CN1->isNullValue()) {
2857
2858#if !defined(NDEBUG)
2859 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
2860 cerr << "\n"
2861 << "Replace: (SPUindirect (add <arg>, <arg>), 0)\n"
2862 << "With: (SPUindirect <arg>, <arg>)\n";
2863 }
2864#endif
2865
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002866 return DAG.getNode(SPUISD::IndirectAddr, dl, Op0VT,
Scott Michel82335272008-12-27 04:51:36 +00002867 Op0.getOperand(0), Op0.getOperand(1));
2868 }
2869 }
Scott Michel7d5eaec2008-02-23 18:41:37 +00002870 }
2871 break;
2872 }
2873 case SPUISD::SHLQUAD_L_BITS:
2874 case SPUISD::SHLQUAD_L_BYTES:
2875 case SPUISD::VEC_SHL:
2876 case SPUISD::VEC_SRL:
2877 case SPUISD::VEC_SRA:
Scott Michel82335272008-12-27 04:51:36 +00002878 case SPUISD::ROTBYTES_LEFT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002879 SDValue Op1 = N->getOperand(1);
Scott Michel7d5eaec2008-02-23 18:41:37 +00002880
Scott Michel82335272008-12-27 04:51:36 +00002881 // Kill degenerate vector shifts:
2882 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2883 if (CN->isNullValue()) {
Scott Michel7d5eaec2008-02-23 18:41:37 +00002884 Result = Op0;
2885 }
2886 }
2887 break;
2888 }
Scott Michel82335272008-12-27 04:51:36 +00002889 case SPUISD::PREFSLOT2VEC: {
Scott Michel7d5eaec2008-02-23 18:41:37 +00002890 switch (Op0.getOpcode()) {
2891 default:
2892 break;
2893 case ISD::ANY_EXTEND:
2894 case ISD::ZERO_EXTEND:
2895 case ISD::SIGN_EXTEND: {
Scott Michelb8ee30d2008-12-29 03:23:36 +00002896 // (SPUprefslot2vec (any|zero|sign_extend (SPUvec2prefslot <arg>))) ->
Scott Michel7d5eaec2008-02-23 18:41:37 +00002897 // <arg>
Scott Michelb8ee30d2008-12-29 03:23:36 +00002898 // but only if the SPUprefslot2vec and <arg> types match.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002899 SDValue Op00 = Op0.getOperand(0);
Scott Michelefc8c7a2008-11-24 17:11:17 +00002900 if (Op00.getOpcode() == SPUISD::VEC2PREFSLOT) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002901 SDValue Op000 = Op00.getOperand(0);
Scott Michel08a4e202008-12-01 17:56:02 +00002902 if (Op000.getValueType() == NodeVT) {
Scott Michel7d5eaec2008-02-23 18:41:37 +00002903 Result = Op000;
2904 }
2905 }
2906 break;
2907 }
Scott Michelefc8c7a2008-11-24 17:11:17 +00002908 case SPUISD::VEC2PREFSLOT: {
Scott Michelb8ee30d2008-12-29 03:23:36 +00002909 // (SPUprefslot2vec (SPUvec2prefslot <arg>)) ->
Scott Michel7d5eaec2008-02-23 18:41:37 +00002910 // <arg>
2911 Result = Op0.getOperand(0);
2912 break;
Scott Michelfe095082008-07-16 17:17:29 +00002913 }
Scott Michel7d5eaec2008-02-23 18:41:37 +00002914 }
2915 break;
Scott Michelceae3bb2008-01-29 02:16:57 +00002916 }
2917 }
Scott Micheled7d79f2009-01-21 04:58:48 +00002918
Scott Michele4d3e3c2008-01-17 20:38:41 +00002919 // Otherwise, return unchanged.
Scott Michel08a4e202008-12-01 17:56:02 +00002920#ifndef NDEBUG
Gabor Greiff304a7a2008-08-28 21:40:38 +00002921 if (Result.getNode()) {
Scott Michel7d5eaec2008-02-23 18:41:37 +00002922 DEBUG(cerr << "\nReplace.SPU: ");
2923 DEBUG(N->dump(&DAG));
2924 DEBUG(cerr << "\nWith: ");
Gabor Greiff304a7a2008-08-28 21:40:38 +00002925 DEBUG(Result.getNode()->dump(&DAG));
Scott Michel7d5eaec2008-02-23 18:41:37 +00002926 DEBUG(cerr << "\n");
2927 }
2928#endif
2929
2930 return Result;
Scott Michel6e22c652007-12-04 22:23:35 +00002931}
2932
2933//===----------------------------------------------------------------------===//
2934// Inline Assembly Support
2935//===----------------------------------------------------------------------===//
2936
2937/// getConstraintType - Given a constraint letter, return the type of
2938/// constraint it is for this target.
Scott Michelfe095082008-07-16 17:17:29 +00002939SPUTargetLowering::ConstraintType
Scott Michel6e22c652007-12-04 22:23:35 +00002940SPUTargetLowering::getConstraintType(const std::string &ConstraintLetter) const {
2941 if (ConstraintLetter.size() == 1) {
2942 switch (ConstraintLetter[0]) {
2943 default: break;
2944 case 'b':
2945 case 'r':
2946 case 'f':
2947 case 'v':
2948 case 'y':
2949 return C_RegisterClass;
Scott Michelfe095082008-07-16 17:17:29 +00002950 }
Scott Michel6e22c652007-12-04 22:23:35 +00002951 }
2952 return TargetLowering::getConstraintType(ConstraintLetter);
2953}
2954
Scott Michelfe095082008-07-16 17:17:29 +00002955std::pair<unsigned, const TargetRegisterClass*>
Scott Michel6e22c652007-12-04 22:23:35 +00002956SPUTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands13237ac2008-06-06 12:08:01 +00002957 MVT VT) const
Scott Michel6e22c652007-12-04 22:23:35 +00002958{
2959 if (Constraint.size() == 1) {
2960 // GCC RS6000 Constraint Letters
2961 switch (Constraint[0]) {
2962 case 'b': // R1-R31
2963 case 'r': // R0-R31
2964 if (VT == MVT::i64)
2965 return std::make_pair(0U, SPU::R64CRegisterClass);
2966 return std::make_pair(0U, SPU::R32CRegisterClass);
2967 case 'f':
2968 if (VT == MVT::f32)
2969 return std::make_pair(0U, SPU::R32FPRegisterClass);
2970 else if (VT == MVT::f64)
2971 return std::make_pair(0U, SPU::R64FPRegisterClass);
2972 break;
Scott Michelfe095082008-07-16 17:17:29 +00002973 case 'v':
Scott Michel6e22c652007-12-04 22:23:35 +00002974 return std::make_pair(0U, SPU::GPRCRegisterClass);
2975 }
2976 }
Scott Michelfe095082008-07-16 17:17:29 +00002977
Scott Michel6e22c652007-12-04 22:23:35 +00002978 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2979}
2980
Scott Michel7d5eaec2008-02-23 18:41:37 +00002981//! Compute used/known bits for a SPU operand
Scott Michel6e22c652007-12-04 22:23:35 +00002982void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002983SPUTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmane1d9ee62008-02-13 22:28:48 +00002984 const APInt &Mask,
Scott Michelfe095082008-07-16 17:17:29 +00002985 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00002986 APInt &KnownOne,
Scott Michelbb713ae2008-01-30 02:55:46 +00002987 const SelectionDAG &DAG,
2988 unsigned Depth ) const {
Scott Michelc3a19102008-04-30 00:30:08 +00002989#if 0
Dan Gohmancff69532009-04-01 18:45:54 +00002990 const uint64_t uint64_sizebits = sizeof(uint64_t) * CHAR_BIT;
Scott Michel7d5eaec2008-02-23 18:41:37 +00002991
2992 switch (Op.getOpcode()) {
2993 default:
2994 // KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
2995 break;
Scott Michel7d5eaec2008-02-23 18:41:37 +00002996 case CALL:
2997 case SHUFB:
Scott Michel0be03392008-11-22 23:50:42 +00002998 case SHUFFLE_MASK:
Scott Michel7d5eaec2008-02-23 18:41:37 +00002999 case CNTB:
Scott Micheled7d79f2009-01-21 04:58:48 +00003000 case SPUISD::PREFSLOT2VEC:
Scott Michel7d5eaec2008-02-23 18:41:37 +00003001 case SPUISD::LDRESULT:
Scott Micheled7d79f2009-01-21 04:58:48 +00003002 case SPUISD::VEC2PREFSLOT:
Scott Michelc3a19102008-04-30 00:30:08 +00003003 case SPUISD::SHLQUAD_L_BITS:
3004 case SPUISD::SHLQUAD_L_BYTES:
3005 case SPUISD::VEC_SHL:
3006 case SPUISD::VEC_SRL:
3007 case SPUISD::VEC_SRA:
3008 case SPUISD::VEC_ROTL:
3009 case SPUISD::VEC_ROTR:
Scott Michelc3a19102008-04-30 00:30:08 +00003010 case SPUISD::ROTBYTES_LEFT:
Scott Micheld831cc42008-06-02 22:18:03 +00003011 case SPUISD::SELECT_MASK:
3012 case SPUISD::SELB:
Scott Michel7d5eaec2008-02-23 18:41:37 +00003013 }
Scott Micheled7d79f2009-01-21 04:58:48 +00003014#endif
Scott Michel6e22c652007-12-04 22:23:35 +00003015}
Scott Michel41236c02008-12-30 23:28:25 +00003016
Scott Michel82335272008-12-27 04:51:36 +00003017unsigned
3018SPUTargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
3019 unsigned Depth) const {
3020 switch (Op.getOpcode()) {
3021 default:
3022 return 1;
Scott Michel6e22c652007-12-04 22:23:35 +00003023
Scott Michel82335272008-12-27 04:51:36 +00003024 case ISD::SETCC: {
3025 MVT VT = Op.getValueType();
3026
3027 if (VT != MVT::i8 && VT != MVT::i16 && VT != MVT::i32) {
3028 VT = MVT::i32;
3029 }
3030 return VT.getSizeInBits();
3031 }
3032 }
3033}
Scott Michelb8ee30d2008-12-29 03:23:36 +00003034
Scott Michelc3a19102008-04-30 00:30:08 +00003035// LowerAsmOperandForConstraint
3036void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003037SPUTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Scott Michelc3a19102008-04-30 00:30:08 +00003038 char ConstraintLetter,
Evan Chenge0add202008-09-24 00:05:32 +00003039 bool hasMemory,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003040 std::vector<SDValue> &Ops,
Scott Michelc3a19102008-04-30 00:30:08 +00003041 SelectionDAG &DAG) const {
3042 // Default, for the time being, to the base class handler
Evan Chenge0add202008-09-24 00:05:32 +00003043 TargetLowering::LowerAsmOperandForConstraint(Op, ConstraintLetter, hasMemory,
3044 Ops, DAG);
Scott Michelc3a19102008-04-30 00:30:08 +00003045}
3046
Scott Michel6e22c652007-12-04 22:23:35 +00003047/// isLegalAddressImmediate - Return true if the integer value can be used
3048/// as the offset of the target addressing mode.
Gabor Greif81d6a382008-08-31 15:37:04 +00003049bool SPUTargetLowering::isLegalAddressImmediate(int64_t V,
3050 const Type *Ty) const {
Scott Michel6e22c652007-12-04 22:23:35 +00003051 // SPU's addresses are 256K:
3052 return (V > -(1 << 18) && V < (1 << 18) - 1);
3053}
3054
3055bool SPUTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Scott Michelfe095082008-07-16 17:17:29 +00003056 return false;
Scott Michel6e22c652007-12-04 22:23:35 +00003057}
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003058
3059bool
3060SPUTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3061 // The SPU target isn't yet aware of offsets.
3062 return false;
3063}