blob: 1607273a01627e2aca9165c715849ef74b0d1009 [file] [log] [blame]
Scott Michel839ad0a2009-03-17 01:15:45 +00001//===-- SPUISelLowering.cpp - Cell SPU DAG Lowering Implementation --------===//
Scott Michel6e22c652007-12-04 22:23:35 +00002// The LLVM Compiler Infrastructure
3//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00004// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
Scott Michel6e22c652007-12-04 22:23:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the SPUTargetLowering class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SPURegisterNames.h"
14#include "SPUISelLowering.h"
15#include "SPUTargetMachine.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000016#include "SPUFrameLowering.h"
Dan Gohman31ae5862010-04-17 14:41:14 +000017#include "SPUMachineFunction.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"
John Thompsone8360b72010-10-29 17:29:13 +000022#include "llvm/Type.h"
Scott Michel6e22c652007-12-04 22:23:35 +000023#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Scott Michel6e22c652007-12-04 22:23:35 +000028#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000029#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000030#include "llvm/Target/TargetOptions.h"
31#include "llvm/ADT/VectorExtras.h"
Scott Michel6e22c652007-12-04 22:23:35 +000032#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
Scott Michel6e22c652007-12-04 22:23:35 +000034#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000035#include "llvm/Support/raw_ostream.h"
Scott Michel6e22c652007-12-04 22:23:35 +000036#include <map>
37
38using namespace llvm;
39
40// Used in getTargetNodeName() below
41namespace {
42 std::map<unsigned, const char *> node_names;
43
Kalle Raiskila0a9dd402010-11-12 10:14:03 +000044 // Byte offset of the preferred slot (counted from the MSB)
45 int prefslotOffset(EVT VT) {
46 int retval=0;
Wesley Peck527da1b2010-11-23 03:31:01 +000047 if (VT==MVT::i1) retval=3;
48 if (VT==MVT::i8) retval=3;
49 if (VT==MVT::i16) retval=2;
Scott Michel6e22c652007-12-04 22:23:35 +000050
51 return retval;
52 }
Scott Michela292fc62009-01-15 04:41:47 +000053
Scott Michel9e3e4a92009-01-26 03:31:40 +000054 //! Expand a library call into an actual call DAG node
55 /*!
56 \note
57 This code is taken from SelectionDAGLegalize, since it is not exposed as
58 part of the LLVM SelectionDAG API.
59 */
60
61 SDValue
62 ExpandLibCall(RTLIB::Libcall LC, SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +000063 bool isSigned, SDValue &Hi, const SPUTargetLowering &TLI) {
Scott Michel9e3e4a92009-01-26 03:31:40 +000064 // The input chain to this libcall is the entry node of the function.
65 // Legalizing the call will automatically add the previous call to the
66 // dependence.
67 SDValue InChain = DAG.getEntryNode();
68
69 TargetLowering::ArgListTy Args;
70 TargetLowering::ArgListEntry Entry;
71 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000072 EVT ArgVT = Op.getOperand(i).getValueType();
Owen Anderson117c9e82009-08-12 00:36:31 +000073 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michel9e3e4a92009-01-26 03:31:40 +000074 Entry.Node = Op.getOperand(i);
75 Entry.Ty = ArgTy;
76 Entry.isSExt = isSigned;
77 Entry.isZExt = !isSigned;
78 Args.push_back(Entry);
79 }
80 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
81 TLI.getPointerTy());
82
83 // Splice the libcall in wherever FindInputOutputChains tells us to.
Owen Anderson117c9e82009-08-12 00:36:31 +000084 const Type *RetTy =
85 Op.getNode()->getValueType(0).getTypeForEVT(*DAG.getContext());
Scott Michel9e3e4a92009-01-26 03:31:40 +000086 std::pair<SDValue, SDValue> CallInfo =
87 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +000088 0, TLI.getLibcallCallingConv(LC), false,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000089 /*isReturnValueUsed=*/true,
Bill Wendling78c5b7a2010-03-02 01:55:18 +000090 Callee, Args, DAG, Op.getDebugLoc());
Scott Michel9e3e4a92009-01-26 03:31:40 +000091
92 return CallInfo.first;
93 }
Scott Michel6e22c652007-12-04 22:23:35 +000094}
95
96SPUTargetLowering::SPUTargetLowering(SPUTargetMachine &TM)
Chris Lattner5e693ed2009-07-28 03:13:23 +000097 : TargetLowering(TM, new TargetLoweringObjectFileELF()),
98 SPUTM(TM) {
Scott Michel6e22c652007-12-04 22:23:35 +000099
100 // Use _setjmp/_longjmp instead of setjmp/longjmp.
101 setUseUnderscoreSetJmp(true);
102 setUseUnderscoreLongJmp(true);
Scott Michelfe095082008-07-16 17:17:29 +0000103
Scott Micheled7d79f2009-01-21 04:58:48 +0000104 // Set RTLIB libcall names as used by SPU:
105 setLibcallName(RTLIB::DIV_F64, "__fast_divdf3");
106
Scott Michel6e22c652007-12-04 22:23:35 +0000107 // Set up the SPU's register classes:
Owen Anderson9f944592009-08-11 20:47:22 +0000108 addRegisterClass(MVT::i8, SPU::R8CRegisterClass);
109 addRegisterClass(MVT::i16, SPU::R16CRegisterClass);
110 addRegisterClass(MVT::i32, SPU::R32CRegisterClass);
111 addRegisterClass(MVT::i64, SPU::R64CRegisterClass);
112 addRegisterClass(MVT::f32, SPU::R32FPRegisterClass);
113 addRegisterClass(MVT::f64, SPU::R64FPRegisterClass);
114 addRegisterClass(MVT::i128, SPU::GPRCRegisterClass);
Scott Michelfe095082008-07-16 17:17:29 +0000115
Scott Michel6e22c652007-12-04 22:23:35 +0000116 // SPU has no sign or zero extended loads for i1, i8, i16:
Owen Anderson9f944592009-08-11 20:47:22 +0000117 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
118 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
119 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +0000120
Owen Anderson9f944592009-08-11 20:47:22 +0000121 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
122 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Scott Michel73640252008-12-02 19:53:53 +0000123
Owen Anderson9f944592009-08-11 20:47:22 +0000124 setTruncStoreAction(MVT::i128, MVT::i64, Expand);
125 setTruncStoreAction(MVT::i128, MVT::i32, Expand);
126 setTruncStoreAction(MVT::i128, MVT::i16, Expand);
127 setTruncStoreAction(MVT::i128, MVT::i8, Expand);
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000128
Owen Anderson9f944592009-08-11 20:47:22 +0000129 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000130
Scott Michel6e22c652007-12-04 22:23:35 +0000131 // SPU constant load actions are custom lowered:
Owen Anderson9f944592009-08-11 20:47:22 +0000132 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
133 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Scott Michel6e22c652007-12-04 22:23:35 +0000134
135 // SPU's loads and stores have to be custom lowered:
Owen Anderson9f944592009-08-11 20:47:22 +0000136 for (unsigned sctype = (unsigned) MVT::i8; sctype < (unsigned) MVT::i128;
Scott Michel6e22c652007-12-04 22:23:35 +0000137 ++sctype) {
Owen Anderson9f944592009-08-11 20:47:22 +0000138 MVT::SimpleValueType VT = (MVT::SimpleValueType)sctype;
Duncan Sands13237ac2008-06-06 12:08:01 +0000139
Scott Michel82335272008-12-27 04:51:36 +0000140 setOperationAction(ISD::LOAD, VT, Custom);
141 setOperationAction(ISD::STORE, VT, Custom);
142 setLoadExtAction(ISD::EXTLOAD, VT, Custom);
143 setLoadExtAction(ISD::ZEXTLOAD, VT, Custom);
144 setLoadExtAction(ISD::SEXTLOAD, VT, Custom);
145
Owen Anderson9f944592009-08-11 20:47:22 +0000146 for (unsigned stype = sctype - 1; stype >= (unsigned) MVT::i8; --stype) {
147 MVT::SimpleValueType StoreVT = (MVT::SimpleValueType) stype;
Scott Michel82335272008-12-27 04:51:36 +0000148 setTruncStoreAction(VT, StoreVT, Expand);
149 }
Scott Michel6e22c652007-12-04 22:23:35 +0000150 }
151
Owen Anderson9f944592009-08-11 20:47:22 +0000152 for (unsigned sctype = (unsigned) MVT::f32; sctype < (unsigned) MVT::f64;
Scott Michel82335272008-12-27 04:51:36 +0000153 ++sctype) {
Owen Anderson9f944592009-08-11 20:47:22 +0000154 MVT::SimpleValueType VT = (MVT::SimpleValueType) sctype;
Scott Michel82335272008-12-27 04:51:36 +0000155
156 setOperationAction(ISD::LOAD, VT, Custom);
157 setOperationAction(ISD::STORE, VT, Custom);
158
Owen Anderson9f944592009-08-11 20:47:22 +0000159 for (unsigned stype = sctype - 1; stype >= (unsigned) MVT::f32; --stype) {
160 MVT::SimpleValueType StoreVT = (MVT::SimpleValueType) stype;
Scott Michel82335272008-12-27 04:51:36 +0000161 setTruncStoreAction(VT, StoreVT, Expand);
162 }
163 }
164
Scott Michel6e22c652007-12-04 22:23:35 +0000165 // Expand the jumptable branches
Owen Anderson9f944592009-08-11 20:47:22 +0000166 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
167 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Scott Michel0be03392008-11-22 23:50:42 +0000168
169 // Custom lower SELECT_CC for most cases, but expand by default
Owen Anderson9f944592009-08-11 20:47:22 +0000170 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
171 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
172 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
173 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
174 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Scott Michel6e22c652007-12-04 22:23:35 +0000175
176 // SPU has no intrinsics for these particular operations:
Owen Anderson9f944592009-08-11 20:47:22 +0000177 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Andrew Lenharthfedcf472008-02-16 14:46:26 +0000178
Eli Friedmanaeb44a32009-07-17 06:36:24 +0000179 // SPU has no division/remainder instructions
Owen Anderson9f944592009-08-11 20:47:22 +0000180 setOperationAction(ISD::SREM, MVT::i8, Expand);
181 setOperationAction(ISD::UREM, MVT::i8, Expand);
182 setOperationAction(ISD::SDIV, MVT::i8, Expand);
183 setOperationAction(ISD::UDIV, MVT::i8, Expand);
184 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
185 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
186 setOperationAction(ISD::SREM, MVT::i16, Expand);
187 setOperationAction(ISD::UREM, MVT::i16, Expand);
188 setOperationAction(ISD::SDIV, MVT::i16, Expand);
189 setOperationAction(ISD::UDIV, MVT::i16, Expand);
190 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
191 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
192 setOperationAction(ISD::SREM, MVT::i32, Expand);
193 setOperationAction(ISD::UREM, MVT::i32, Expand);
194 setOperationAction(ISD::SDIV, MVT::i32, Expand);
195 setOperationAction(ISD::UDIV, MVT::i32, Expand);
196 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
197 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
198 setOperationAction(ISD::SREM, MVT::i64, Expand);
199 setOperationAction(ISD::UREM, MVT::i64, Expand);
200 setOperationAction(ISD::SDIV, MVT::i64, Expand);
201 setOperationAction(ISD::UDIV, MVT::i64, Expand);
202 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
203 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
204 setOperationAction(ISD::SREM, MVT::i128, Expand);
205 setOperationAction(ISD::UREM, MVT::i128, Expand);
206 setOperationAction(ISD::SDIV, MVT::i128, Expand);
207 setOperationAction(ISD::UDIV, MVT::i128, Expand);
208 setOperationAction(ISD::SDIVREM, MVT::i128, Expand);
209 setOperationAction(ISD::UDIVREM, MVT::i128, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000210
Scott Michel6e22c652007-12-04 22:23:35 +0000211 // We don't support sin/cos/sqrt/fmod
Owen Anderson9f944592009-08-11 20:47:22 +0000212 setOperationAction(ISD::FSIN , MVT::f64, Expand);
213 setOperationAction(ISD::FCOS , MVT::f64, Expand);
214 setOperationAction(ISD::FREM , MVT::f64, Expand);
215 setOperationAction(ISD::FSIN , MVT::f32, Expand);
216 setOperationAction(ISD::FCOS , MVT::f32, Expand);
217 setOperationAction(ISD::FREM , MVT::f32, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000218
Scott Michel9e3e4a92009-01-26 03:31:40 +0000219 // Expand fsqrt to the appropriate libcall (NOTE: should use h/w fsqrt
220 // for f32!)
Owen Anderson9f944592009-08-11 20:47:22 +0000221 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
222 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000223
Owen Anderson9f944592009-08-11 20:47:22 +0000224 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
225 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000226
227 // SPU can do rotate right and left, so legalize it... but customize for i8
228 // because instructions don't exist.
Bill Wendlingaebd2662008-08-31 02:59:23 +0000229
230 // FIXME: Change from "expand" to appropriate type once ROTR is supported in
231 // .td files.
Owen Anderson9f944592009-08-11 20:47:22 +0000232 setOperationAction(ISD::ROTR, MVT::i32, Expand /*Legal*/);
233 setOperationAction(ISD::ROTR, MVT::i16, Expand /*Legal*/);
234 setOperationAction(ISD::ROTR, MVT::i8, Expand /*Custom*/);
Bill Wendlingaebd2662008-08-31 02:59:23 +0000235
Owen Anderson9f944592009-08-11 20:47:22 +0000236 setOperationAction(ISD::ROTL, MVT::i32, Legal);
237 setOperationAction(ISD::ROTL, MVT::i16, Legal);
238 setOperationAction(ISD::ROTL, MVT::i8, Custom);
Scott Michel3726019a2008-11-20 16:36:33 +0000239
Scott Michel6e22c652007-12-04 22:23:35 +0000240 // SPU has no native version of shift left/right for i8
Owen Anderson9f944592009-08-11 20:47:22 +0000241 setOperationAction(ISD::SHL, MVT::i8, Custom);
242 setOperationAction(ISD::SRL, MVT::i8, Custom);
243 setOperationAction(ISD::SRA, MVT::i8, Custom);
Scott Michelc6918c12008-11-21 02:56:16 +0000244
Scott Michel41236c02008-12-30 23:28:25 +0000245 // Make these operations legal and handle them during instruction selection:
Owen Anderson9f944592009-08-11 20:47:22 +0000246 setOperationAction(ISD::SHL, MVT::i64, Legal);
247 setOperationAction(ISD::SRL, MVT::i64, Legal);
248 setOperationAction(ISD::SRA, MVT::i64, Legal);
Scott Michel6e22c652007-12-04 22:23:35 +0000249
Scott Michelfe095082008-07-16 17:17:29 +0000250 // Custom lower i8, i32 and i64 multiplications
Owen Anderson9f944592009-08-11 20:47:22 +0000251 setOperationAction(ISD::MUL, MVT::i8, Custom);
252 setOperationAction(ISD::MUL, MVT::i32, Legal);
253 setOperationAction(ISD::MUL, MVT::i64, Legal);
Scott Michelc6918c12008-11-21 02:56:16 +0000254
Eli Friedman48021d12009-06-16 06:40:59 +0000255 // Expand double-width multiplication
256 // FIXME: It would probably be reasonable to support some of these operations
Owen Anderson9f944592009-08-11 20:47:22 +0000257 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
258 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
259 setOperationAction(ISD::MULHU, MVT::i8, Expand);
260 setOperationAction(ISD::MULHS, MVT::i8, Expand);
261 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
262 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
263 setOperationAction(ISD::MULHU, MVT::i16, Expand);
264 setOperationAction(ISD::MULHS, MVT::i16, Expand);
265 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
266 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
267 setOperationAction(ISD::MULHU, MVT::i32, Expand);
268 setOperationAction(ISD::MULHS, MVT::i32, Expand);
269 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
270 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
271 setOperationAction(ISD::MULHU, MVT::i64, Expand);
272 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Eli Friedman48021d12009-06-16 06:40:59 +0000273
Scott Micheld831cc42008-06-02 22:18:03 +0000274 // Need to custom handle (some) common i8, i64 math ops
Owen Anderson9f944592009-08-11 20:47:22 +0000275 setOperationAction(ISD::ADD, MVT::i8, Custom);
276 setOperationAction(ISD::ADD, MVT::i64, Legal);
277 setOperationAction(ISD::SUB, MVT::i8, Custom);
278 setOperationAction(ISD::SUB, MVT::i64, Legal);
Scott Michelfe095082008-07-16 17:17:29 +0000279
Scott Michel6e22c652007-12-04 22:23:35 +0000280 // SPU does not have BSWAP. It does have i32 support CTLZ.
281 // CTPOP has to be custom lowered.
Owen Anderson9f944592009-08-11 20:47:22 +0000282 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
283 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000284
Owen Anderson9f944592009-08-11 20:47:22 +0000285 setOperationAction(ISD::CTPOP, MVT::i8, Custom);
286 setOperationAction(ISD::CTPOP, MVT::i16, Custom);
287 setOperationAction(ISD::CTPOP, MVT::i32, Custom);
288 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
289 setOperationAction(ISD::CTPOP, MVT::i128, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000290
Owen Anderson9f944592009-08-11 20:47:22 +0000291 setOperationAction(ISD::CTTZ , MVT::i8, Expand);
292 setOperationAction(ISD::CTTZ , MVT::i16, Expand);
293 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
294 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
295 setOperationAction(ISD::CTTZ , MVT::i128, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000296
Owen Anderson9f944592009-08-11 20:47:22 +0000297 setOperationAction(ISD::CTLZ , MVT::i8, Promote);
298 setOperationAction(ISD::CTLZ , MVT::i16, Promote);
299 setOperationAction(ISD::CTLZ , MVT::i32, Legal);
300 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
301 setOperationAction(ISD::CTLZ , MVT::i128, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000302
Scott Micheld831cc42008-06-02 22:18:03 +0000303 // SPU has a version of select that implements (a&~c)|(b&c), just like
Scott Michel92275422008-03-10 23:49:09 +0000304 // select ought to work:
Owen Anderson9f944592009-08-11 20:47:22 +0000305 setOperationAction(ISD::SELECT, MVT::i8, Legal);
306 setOperationAction(ISD::SELECT, MVT::i16, Legal);
307 setOperationAction(ISD::SELECT, MVT::i32, Legal);
308 setOperationAction(ISD::SELECT, MVT::i64, Legal);
Scott Michel6e22c652007-12-04 22:23:35 +0000309
Owen Anderson9f944592009-08-11 20:47:22 +0000310 setOperationAction(ISD::SETCC, MVT::i8, Legal);
311 setOperationAction(ISD::SETCC, MVT::i16, Legal);
312 setOperationAction(ISD::SETCC, MVT::i32, Legal);
313 setOperationAction(ISD::SETCC, MVT::i64, Legal);
314 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Scott Michel42f56b42008-03-05 23:02:02 +0000315
Scott Michel82335272008-12-27 04:51:36 +0000316 // Custom lower i128 -> i64 truncates
Owen Anderson9f944592009-08-11 20:47:22 +0000317 setOperationAction(ISD::TRUNCATE, MVT::i64, Custom);
Scott Michel73640252008-12-02 19:53:53 +0000318
Scott Michelc5dd8bd2009-08-25 22:37:34 +0000319 // Custom lower i32/i64 -> i128 sign extend
Scott Michel8d1602a2009-08-24 22:28:53 +0000320 setOperationAction(ISD::SIGN_EXTEND, MVT::i128, Custom);
321
Owen Anderson9f944592009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
323 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
324 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
325 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
Scott Michel9e3e4a92009-01-26 03:31:40 +0000326 // SPU has a legal FP -> signed INT instruction for f32, but for f64, need
327 // to expand to a libcall, hence the custom lowering:
Owen Anderson9f944592009-08-11 20:47:22 +0000328 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
329 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
330 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Expand);
331 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
332 setOperationAction(ISD::FP_TO_SINT, MVT::i128, Expand);
333 setOperationAction(ISD::FP_TO_UINT, MVT::i128, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000334
335 // FDIV on SPU requires custom lowering
Owen Anderson9f944592009-08-11 20:47:22 +0000336 setOperationAction(ISD::FDIV, MVT::f64, Expand); // to libcall
Scott Michel6e22c652007-12-04 22:23:35 +0000337
Scott Michel49483182009-01-26 22:33:37 +0000338 // SPU has [U|S]INT_TO_FP for f32->i32, but not for f64->i32, f64->i64:
Owen Anderson9f944592009-08-11 20:47:22 +0000339 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
340 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
341 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
342 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
343 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
344 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
345 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
346 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Scott Michel6e22c652007-12-04 22:23:35 +0000347
Wesley Peck527da1b2010-11-23 03:31:01 +0000348 setOperationAction(ISD::BITCAST, MVT::i32, Legal);
349 setOperationAction(ISD::BITCAST, MVT::f32, Legal);
350 setOperationAction(ISD::BITCAST, MVT::i64, Legal);
351 setOperationAction(ISD::BITCAST, MVT::f64, Legal);
Scott Michel6e22c652007-12-04 22:23:35 +0000352
353 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson9f944592009-08-11 20:47:22 +0000354 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Scott Michelfe095082008-07-16 17:17:29 +0000355
Scott Michelfe095082008-07-16 17:17:29 +0000356 // We want to legalize GlobalAddress and ConstantPool nodes into the
Scott Michel6e22c652007-12-04 22:23:35 +0000357 // appropriate instructions to materialize the address.
Owen Anderson9f944592009-08-11 20:47:22 +0000358 for (unsigned sctype = (unsigned) MVT::i8; sctype < (unsigned) MVT::f128;
Scott Michelceae3bb2008-01-29 02:16:57 +0000359 ++sctype) {
Owen Anderson9f944592009-08-11 20:47:22 +0000360 MVT::SimpleValueType VT = (MVT::SimpleValueType)sctype;
Duncan Sands13237ac2008-06-06 12:08:01 +0000361
Scott Michelb8ee30d2008-12-29 03:23:36 +0000362 setOperationAction(ISD::GlobalAddress, VT, Custom);
363 setOperationAction(ISD::ConstantPool, VT, Custom);
364 setOperationAction(ISD::JumpTable, VT, Custom);
Scott Michelceae3bb2008-01-29 02:16:57 +0000365 }
Scott Michel6e22c652007-12-04 22:23:35 +0000366
Scott Michel6e22c652007-12-04 22:23:35 +0000367 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson9f944592009-08-11 20:47:22 +0000368 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelfe095082008-07-16 17:17:29 +0000369
Scott Michel6e22c652007-12-04 22:23:35 +0000370 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000371 setOperationAction(ISD::VAARG , MVT::Other, Expand);
372 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
373 setOperationAction(ISD::VAEND , MVT::Other, Expand);
374 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
375 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
377 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000378
379 // Cell SPU has instructions for converting between i64 and fp.
Owen Anderson9f944592009-08-11 20:47:22 +0000380 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
381 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Scott Michelfe095082008-07-16 17:17:29 +0000382
Scott Michel6e22c652007-12-04 22:23:35 +0000383 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
Owen Anderson9f944592009-08-11 20:47:22 +0000384 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +0000385
386 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson9f944592009-08-11 20:47:22 +0000387 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000388
389 // First set operation action for all vector types to expand. Then we
390 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson9f944592009-08-11 20:47:22 +0000391 addRegisterClass(MVT::v16i8, SPU::VECREGRegisterClass);
392 addRegisterClass(MVT::v8i16, SPU::VECREGRegisterClass);
393 addRegisterClass(MVT::v4i32, SPU::VECREGRegisterClass);
394 addRegisterClass(MVT::v2i64, SPU::VECREGRegisterClass);
395 addRegisterClass(MVT::v4f32, SPU::VECREGRegisterClass);
396 addRegisterClass(MVT::v2f64, SPU::VECREGRegisterClass);
Scott Michel6e22c652007-12-04 22:23:35 +0000397
Owen Anderson9f944592009-08-11 20:47:22 +0000398 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
399 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
400 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Scott Michel6e22c652007-12-04 22:23:35 +0000401
Duncan Sands13237ac2008-06-06 12:08:01 +0000402 // add/sub are legal for all supported vector VT's.
Scott Michel9e3e4a92009-01-26 03:31:40 +0000403 setOperationAction(ISD::ADD, VT, Legal);
404 setOperationAction(ISD::SUB, VT, Legal);
Duncan Sands13237ac2008-06-06 12:08:01 +0000405 // mul has to be custom lowered.
Scott Michel9e3e4a92009-01-26 03:31:40 +0000406 setOperationAction(ISD::MUL, VT, Legal);
Duncan Sands13237ac2008-06-06 12:08:01 +0000407
Scott Michel9e3e4a92009-01-26 03:31:40 +0000408 setOperationAction(ISD::AND, VT, Legal);
409 setOperationAction(ISD::OR, VT, Legal);
410 setOperationAction(ISD::XOR, VT, Legal);
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000411 setOperationAction(ISD::LOAD, VT, Custom);
Scott Michel9e3e4a92009-01-26 03:31:40 +0000412 setOperationAction(ISD::SELECT, VT, Legal);
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000413 setOperationAction(ISD::STORE, VT, Custom);
Scott Michelfe095082008-07-16 17:17:29 +0000414
Scott Michel6e22c652007-12-04 22:23:35 +0000415 // These operations need to be expanded:
Scott Michel9e3e4a92009-01-26 03:31:40 +0000416 setOperationAction(ISD::SDIV, VT, Expand);
417 setOperationAction(ISD::SREM, VT, Expand);
418 setOperationAction(ISD::UDIV, VT, Expand);
419 setOperationAction(ISD::UREM, VT, Expand);
Scott Michel6e22c652007-12-04 22:23:35 +0000420
421 // Custom lower build_vector, constant pool spills, insert and
422 // extract vector elements:
Duncan Sands13237ac2008-06-06 12:08:01 +0000423 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
424 setOperationAction(ISD::ConstantPool, VT, Custom);
425 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
426 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
427 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
428 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
Scott Michel6e22c652007-12-04 22:23:35 +0000429 }
430
Owen Anderson9f944592009-08-11 20:47:22 +0000431 setOperationAction(ISD::AND, MVT::v16i8, Custom);
432 setOperationAction(ISD::OR, MVT::v16i8, Custom);
433 setOperationAction(ISD::XOR, MVT::v16i8, Custom);
434 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
Scott Michel8d5841a2008-01-11 02:53:15 +0000435
Owen Anderson9f944592009-08-11 20:47:22 +0000436 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Scott Michelb8ee30d2008-12-29 03:23:36 +0000437
Scott Michel82335272008-12-27 04:51:36 +0000438 setBooleanContents(ZeroOrNegativeOneBooleanContent);
Scott Michelfe095082008-07-16 17:17:29 +0000439
Scott Michel6e22c652007-12-04 22:23:35 +0000440 setStackPointerRegisterToSaveRestore(SPU::R1);
Scott Michelfe095082008-07-16 17:17:29 +0000441
Scott Michel6e22c652007-12-04 22:23:35 +0000442 // We have target-specific dag combine patterns for the following nodes:
Scott Michelceae3bb2008-01-29 02:16:57 +0000443 setTargetDAGCombine(ISD::ADD);
Scott Michel7d5eaec2008-02-23 18:41:37 +0000444 setTargetDAGCombine(ISD::ZERO_EXTEND);
445 setTargetDAGCombine(ISD::SIGN_EXTEND);
446 setTargetDAGCombine(ISD::ANY_EXTEND);
Scott Michelfe095082008-07-16 17:17:29 +0000447
Eli Friedman2518f832011-05-06 20:34:06 +0000448 setMinFunctionAlignment(3);
449
Scott Michel6e22c652007-12-04 22:23:35 +0000450 computeRegisterProperties();
Scott Michel0be03392008-11-22 23:50:42 +0000451
Scott Michel8deac5d2008-12-09 03:37:19 +0000452 // Set pre-RA register scheduler default to BURR, which produces slightly
453 // better code than the default (could also be TDRR, but TargetLowering.h
454 // needs a mod to support that model):
Evan Cheng738e9202010-05-19 20:19:50 +0000455 setSchedulingPreference(Sched::RegPressure);
Scott Michel6e22c652007-12-04 22:23:35 +0000456}
457
458const char *
459SPUTargetLowering::getTargetNodeName(unsigned Opcode) const
460{
461 if (node_names.empty()) {
462 node_names[(unsigned) SPUISD::RET_FLAG] = "SPUISD::RET_FLAG";
463 node_names[(unsigned) SPUISD::Hi] = "SPUISD::Hi";
464 node_names[(unsigned) SPUISD::Lo] = "SPUISD::Lo";
465 node_names[(unsigned) SPUISD::PCRelAddr] = "SPUISD::PCRelAddr";
Scott Michel8d5841a2008-01-11 02:53:15 +0000466 node_names[(unsigned) SPUISD::AFormAddr] = "SPUISD::AFormAddr";
Scott Michelceae3bb2008-01-29 02:16:57 +0000467 node_names[(unsigned) SPUISD::IndirectAddr] = "SPUISD::IndirectAddr";
Scott Michel6e22c652007-12-04 22:23:35 +0000468 node_names[(unsigned) SPUISD::LDRESULT] = "SPUISD::LDRESULT";
469 node_names[(unsigned) SPUISD::CALL] = "SPUISD::CALL";
470 node_names[(unsigned) SPUISD::SHUFB] = "SPUISD::SHUFB";
Scott Michel0be03392008-11-22 23:50:42 +0000471 node_names[(unsigned) SPUISD::SHUFFLE_MASK] = "SPUISD::SHUFFLE_MASK";
Scott Michel6e22c652007-12-04 22:23:35 +0000472 node_names[(unsigned) SPUISD::CNTB] = "SPUISD::CNTB";
Scott Michelb8ee30d2008-12-29 03:23:36 +0000473 node_names[(unsigned) SPUISD::PREFSLOT2VEC] = "SPUISD::PREFSLOT2VEC";
Scott Michelefc8c7a2008-11-24 17:11:17 +0000474 node_names[(unsigned) SPUISD::VEC2PREFSLOT] = "SPUISD::VEC2PREFSLOT";
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000475 node_names[(unsigned) SPUISD::SHL_BITS] = "SPUISD::SHL_BITS";
476 node_names[(unsigned) SPUISD::SHL_BYTES] = "SPUISD::SHL_BYTES";
Scott Michel6e22c652007-12-04 22:23:35 +0000477 node_names[(unsigned) SPUISD::VEC_ROTL] = "SPUISD::VEC_ROTL";
478 node_names[(unsigned) SPUISD::VEC_ROTR] = "SPUISD::VEC_ROTR";
Scott Micheled7d79f2009-01-21 04:58:48 +0000479 node_names[(unsigned) SPUISD::ROTBYTES_LEFT] = "SPUISD::ROTBYTES_LEFT";
480 node_names[(unsigned) SPUISD::ROTBYTES_LEFT_BITS] =
481 "SPUISD::ROTBYTES_LEFT_BITS";
Scott Micheld831cc42008-06-02 22:18:03 +0000482 node_names[(unsigned) SPUISD::SELECT_MASK] = "SPUISD::SELECT_MASK";
Scott Michel6e22c652007-12-04 22:23:35 +0000483 node_names[(unsigned) SPUISD::SELB] = "SPUISD::SELB";
Scott Michela292fc62009-01-15 04:41:47 +0000484 node_names[(unsigned) SPUISD::ADD64_MARKER] = "SPUISD::ADD64_MARKER";
485 node_names[(unsigned) SPUISD::SUB64_MARKER] = "SPUISD::SUB64_MARKER";
486 node_names[(unsigned) SPUISD::MUL64_MARKER] = "SPUISD::MUL64_MARKER";
Scott Michel6e22c652007-12-04 22:23:35 +0000487 }
488
489 std::map<unsigned, const char *>::iterator i = node_names.find(Opcode);
490
491 return ((i != node_names.end()) ? i->second : 0);
492}
493
Scott Michel82335272008-12-27 04:51:36 +0000494//===----------------------------------------------------------------------===//
495// Return the Cell SPU's SETCC result type
496//===----------------------------------------------------------------------===//
497
Owen Anderson9f944592009-08-11 20:47:22 +0000498MVT::SimpleValueType SPUTargetLowering::getSetCCResultType(EVT VT) const {
Kalle Raiskilae0a1d2b2010-11-24 12:59:16 +0000499 // i8, i16 and i32 are valid SETCC result types
500 MVT::SimpleValueType retval;
501
502 switch(VT.getSimpleVT().SimpleTy){
503 case MVT::i1:
504 case MVT::i8:
505 retval = MVT::i8; break;
506 case MVT::i16:
507 retval = MVT::i16; break;
508 case MVT::i32:
509 default:
510 retval = MVT::i32;
511 }
512 return retval;
Scott Michel48e33752008-03-10 16:58:52 +0000513}
514
Scott Michel6e22c652007-12-04 22:23:35 +0000515//===----------------------------------------------------------------------===//
516// Calling convention code:
517//===----------------------------------------------------------------------===//
518
519#include "SPUGenCallingConv.inc"
520
521//===----------------------------------------------------------------------===//
522// LowerOperation implementation
523//===----------------------------------------------------------------------===//
524
525/// Custom lower loads for CellSPU
526/*!
527 All CellSPU loads and stores are aligned to 16-byte boundaries, so for elements
528 within a 16-byte block, we have to rotate to extract the requested element.
Scott Michel40f54d22008-12-04 03:02:42 +0000529
530 For extending loads, we also want to ensure that the following sequence is
Owen Anderson9f944592009-08-11 20:47:22 +0000531 emitted, e.g. for MVT::f32 extending load to MVT::f64:
Scott Michel40f54d22008-12-04 03:02:42 +0000532
533\verbatim
Scott Michelb8ee30d2008-12-29 03:23:36 +0000534%1 v16i8,ch = load
Scott Michel40f54d22008-12-04 03:02:42 +0000535%2 v16i8,ch = rotate %1
Scott Michelb8ee30d2008-12-29 03:23:36 +0000536%3 v4f8, ch = bitconvert %2
Scott Michel40f54d22008-12-04 03:02:42 +0000537%4 f32 = vec2perfslot %3
538%5 f64 = fp_extend %4
539\endverbatim
540*/
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000541static SDValue
542LowerLOAD(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel6e22c652007-12-04 22:23:35 +0000543 LoadSDNode *LN = cast<LoadSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000544 SDValue the_chain = LN->getChain();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000545 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
546 EVT InVT = LN->getMemoryVT();
547 EVT OutVT = Op.getValueType();
Scott Michel6e22c652007-12-04 22:23:35 +0000548 ISD::LoadExtType ExtType = LN->getExtensionType();
549 unsigned alignment = LN->getAlignment();
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000550 int pso = prefslotOffset(InVT);
Dale Johannesen021052a2009-02-04 20:06:27 +0000551 DebugLoc dl = Op.getDebugLoc();
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000552 EVT vecVT = InVT.isVector()? InVT: EVT::getVectorVT(*DAG.getContext(), InVT,
553 (128 / InVT.getSizeInBits()));
Scott Michel6e22c652007-12-04 22:23:35 +0000554
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000555 // two sanity checks
Wesley Peck527da1b2010-11-23 03:31:01 +0000556 assert( LN->getAddressingMode() == ISD::UNINDEXED
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000557 && "we should get only UNINDEXED adresses");
558 // clean aligned loads can be selected as-is
Kalle Raiskila7e7b4ac2011-01-17 11:59:20 +0000559 if (InVT.getSizeInBits() == 128 && (alignment%16) == 0)
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000560 return SDValue();
561
Wesley Peck527da1b2010-11-23 03:31:01 +0000562 // Get pointerinfos to the memory chunk(s) that contain the data to load
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000563 uint64_t mpi_offset = LN->getPointerInfo().Offset;
564 mpi_offset -= mpi_offset%16;
Kalle Raiskila731d3922010-11-15 10:12:32 +0000565 MachinePointerInfo lowMemPtr(LN->getPointerInfo().V, mpi_offset);
566 MachinePointerInfo highMemPtr(LN->getPointerInfo().V, mpi_offset+16);
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000567
Kalle Raiskila731d3922010-11-15 10:12:32 +0000568 SDValue result;
569 SDValue basePtr = LN->getBasePtr();
570 SDValue rotate;
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000571
Kalle Raiskila7e7b4ac2011-01-17 11:59:20 +0000572 if ((alignment%16) == 0) {
Kalle Raiskila731d3922010-11-15 10:12:32 +0000573 ConstantSDNode *CN;
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000574
Kalle Raiskila731d3922010-11-15 10:12:32 +0000575 // Special cases for a known aligned load to simplify the base pointer
576 // and the rotation amount:
577 if (basePtr.getOpcode() == ISD::ADD
578 && (CN = dyn_cast<ConstantSDNode > (basePtr.getOperand(1))) != 0) {
579 // Known offset into basePtr
580 int64_t offset = CN->getSExtValue();
581 int64_t rotamt = int64_t((offset & 0xf) - pso);
Scott Michel6e22c652007-12-04 22:23:35 +0000582
Kalle Raiskila731d3922010-11-15 10:12:32 +0000583 if (rotamt < 0)
584 rotamt += 16;
Scott Michel8d5841a2008-01-11 02:53:15 +0000585
Kalle Raiskila731d3922010-11-15 10:12:32 +0000586 rotate = DAG.getConstant(rotamt, MVT::i16);
Scott Michel8d5841a2008-01-11 02:53:15 +0000587
Kalle Raiskila731d3922010-11-15 10:12:32 +0000588 // Simplify the base pointer for this case:
589 basePtr = basePtr.getOperand(0);
590 if ((offset & ~0xf) > 0) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000591 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000592 basePtr,
Kalle Raiskila731d3922010-11-15 10:12:32 +0000593 DAG.getConstant((offset & ~0xf), PtrVT));
Scott Michel82335272008-12-27 04:51:36 +0000594 }
Kalle Raiskila731d3922010-11-15 10:12:32 +0000595 } else if ((basePtr.getOpcode() == SPUISD::AFormAddr)
596 || (basePtr.getOpcode() == SPUISD::IndirectAddr
597 && basePtr.getOperand(0).getOpcode() == SPUISD::Hi
598 && basePtr.getOperand(1).getOpcode() == SPUISD::Lo)) {
599 // Plain aligned a-form address: rotate into preferred slot
600 // Same for (SPUindirect (SPUhi ...), (SPUlo ...))
601 int64_t rotamt = -pso;
602 if (rotamt < 0)
603 rotamt += 16;
604 rotate = DAG.getConstant(rotamt, MVT::i16);
605 } else {
Scott Michel82335272008-12-27 04:51:36 +0000606 // Offset the rotate amount by the basePtr and the preferred slot
607 // byte offset
Kalle Raiskila731d3922010-11-15 10:12:32 +0000608 int64_t rotamt = -pso;
609 if (rotamt < 0)
610 rotamt += 16;
Dale Johannesen021052a2009-02-04 20:06:27 +0000611 rotate = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000612 basePtr,
Kalle Raiskila731d3922010-11-15 10:12:32 +0000613 DAG.getConstant(rotamt, PtrVT));
Scott Michel6e22c652007-12-04 22:23:35 +0000614 }
Kalle Raiskila731d3922010-11-15 10:12:32 +0000615 } else {
616 // Unaligned load: must be more pessimistic about addressing modes:
617 if (basePtr.getOpcode() == ISD::ADD) {
618 MachineFunction &MF = DAG.getMachineFunction();
619 MachineRegisterInfo &RegInfo = MF.getRegInfo();
620 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
621 SDValue Flag;
Scott Michel8d5841a2008-01-11 02:53:15 +0000622
Kalle Raiskila731d3922010-11-15 10:12:32 +0000623 SDValue Op0 = basePtr.getOperand(0);
624 SDValue Op1 = basePtr.getOperand(1);
625
626 if (isa<ConstantSDNode>(Op1)) {
627 // Convert the (add <ptr>, <const>) to an indirect address contained
628 // in a register. Note that this is done because we need to avoid
629 // creating a 0(reg) d-form address due to the SPU's block loads.
630 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
631 the_chain = DAG.getCopyToReg(the_chain, dl, VReg, basePtr, Flag);
632 basePtr = DAG.getCopyFromReg(the_chain, dl, VReg, PtrVT);
633 } else {
634 // Convert the (add <arg1>, <arg2>) to an indirect address, which
635 // will likely be lowered as a reg(reg) x-form address.
636 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
637 }
638 } else {
639 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
640 basePtr,
641 DAG.getConstant(0, PtrVT));
642 }
643
644 // Offset the rotate amount by the basePtr and the preferred slot
645 // byte offset
646 rotate = DAG.getNode(ISD::ADD, dl, PtrVT,
647 basePtr,
648 DAG.getConstant(-pso, PtrVT));
649 }
650
651 // Do the load as a i128 to allow possible shifting
652 SDValue low = DAG.getLoad(MVT::i128, dl, the_chain, basePtr,
653 lowMemPtr,
654 LN->isVolatile(), LN->isNonTemporal(), 16);
Wesley Peck527da1b2010-11-23 03:31:01 +0000655
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000656 // When the size is not greater than alignment we get all data with just
657 // one load
658 if (alignment >= InVT.getSizeInBits()/8) {
Scott Michel82335272008-12-27 04:51:36 +0000659 // Update the chain
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000660 the_chain = low.getValue(1);
Scott Michel82335272008-12-27 04:51:36 +0000661
662 // Rotate into the preferred slot:
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000663 result = DAG.getNode(SPUISD::ROTBYTES_LEFT, dl, MVT::i128,
664 low.getValue(0), rotate);
Scott Michel82335272008-12-27 04:51:36 +0000665
Scott Michel40f54d22008-12-04 03:02:42 +0000666 // Convert the loaded v16i8 vector to the appropriate vector type
667 // specified by the operand:
Wesley Peck527da1b2010-11-23 03:31:01 +0000668 EVT vecVT = EVT::getVectorVT(*DAG.getContext(),
Owen Anderson117c9e82009-08-12 00:36:31 +0000669 InVT, (128 / InVT.getSizeInBits()));
Dale Johannesen021052a2009-02-04 20:06:27 +0000670 result = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, InVT,
Wesley Peck527da1b2010-11-23 03:31:01 +0000671 DAG.getNode(ISD::BITCAST, dl, vecVT, result));
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000672 }
673 // When alignment is less than the size, we might need (known only at
674 // run-time) two loads
Wesley Peck527da1b2010-11-23 03:31:01 +0000675 // TODO: if the memory address is composed only from constants, we have
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000676 // extra kowledge, and might avoid the second load
677 else {
678 // storage position offset from lower 16 byte aligned memory chunk
Wesley Peck527da1b2010-11-23 03:31:01 +0000679 SDValue offset = DAG.getNode(ISD::AND, dl, MVT::i32,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000680 basePtr, DAG.getConstant( 0xf, MVT::i32 ) );
Wesley Peck527da1b2010-11-23 03:31:01 +0000681 // get a registerfull of ones. (this implementation is a workaround: LLVM
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000682 // cannot handle 128 bit signed int constants)
Kalle Raiskila731d3922010-11-15 10:12:32 +0000683 SDValue ones = DAG.getConstant(-1, MVT::v4i32 );
Wesley Peck527da1b2010-11-23 03:31:01 +0000684 ones = DAG.getNode(ISD::BITCAST, dl, MVT::i128, ones);
Scott Michelfe095082008-07-16 17:17:29 +0000685
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000686 SDValue high = DAG.getLoad(MVT::i128, dl, the_chain,
Wesley Peck527da1b2010-11-23 03:31:01 +0000687 DAG.getNode(ISD::ADD, dl, PtrVT,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000688 basePtr,
689 DAG.getConstant(16, PtrVT)),
690 highMemPtr,
691 LN->isVolatile(), LN->isNonTemporal(), 16);
692
693 the_chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, low.getValue(1),
694 high.getValue(1));
695
696 // Shift the (possible) high part right to compensate the misalignemnt.
Wesley Peck527da1b2010-11-23 03:31:01 +0000697 // if there is no highpart (i.e. value is i64 and offset is 4), this
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000698 // will zero out the high value.
Wesley Peck527da1b2010-11-23 03:31:01 +0000699 high = DAG.getNode(SPUISD::SRL_BYTES, dl, MVT::i128, high,
Kalle Raiskila731d3922010-11-15 10:12:32 +0000700 DAG.getNode(ISD::SUB, dl, MVT::i32,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000701 DAG.getConstant( 16, MVT::i32),
702 offset
703 ));
Wesley Peck527da1b2010-11-23 03:31:01 +0000704
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000705 // Shift the low similarly
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000706 // TODO: add SPUISD::SHL_BYTES
Kalle Raiskila731d3922010-11-15 10:12:32 +0000707 low = DAG.getNode(SPUISD::SHL_BYTES, dl, MVT::i128, low, offset );
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000708
709 // Merge the two parts
Wesley Peck527da1b2010-11-23 03:31:01 +0000710 result = DAG.getNode(ISD::BITCAST, dl, vecVT,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000711 DAG.getNode(ISD::OR, dl, MVT::i128, low, high));
712
713 if (!InVT.isVector()) {
Kalle Raiskila731d3922010-11-15 10:12:32 +0000714 result = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, InVT, result );
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000715 }
716
717 }
Scott Michel40f54d22008-12-04 03:02:42 +0000718 // Handle extending loads by extending the scalar result:
719 if (ExtType == ISD::SEXTLOAD) {
Dale Johannesen021052a2009-02-04 20:06:27 +0000720 result = DAG.getNode(ISD::SIGN_EXTEND, dl, OutVT, result);
Scott Michel40f54d22008-12-04 03:02:42 +0000721 } else if (ExtType == ISD::ZEXTLOAD) {
Dale Johannesen021052a2009-02-04 20:06:27 +0000722 result = DAG.getNode(ISD::ZERO_EXTEND, dl, OutVT, result);
Scott Michel40f54d22008-12-04 03:02:42 +0000723 } else if (ExtType == ISD::EXTLOAD) {
724 unsigned NewOpc = ISD::ANY_EXTEND;
Scott Michel8d5841a2008-01-11 02:53:15 +0000725
Scott Michel40f54d22008-12-04 03:02:42 +0000726 if (OutVT.isFloatingPoint())
Scott Michel95b2a202009-01-26 03:37:41 +0000727 NewOpc = ISD::FP_EXTEND;
Scott Michel8d5841a2008-01-11 02:53:15 +0000728
Dale Johannesen021052a2009-02-04 20:06:27 +0000729 result = DAG.getNode(NewOpc, dl, OutVT, result);
Scott Michel8d5841a2008-01-11 02:53:15 +0000730 }
731
Owen Anderson9f944592009-08-11 20:47:22 +0000732 SDVTList retvts = DAG.getVTList(OutVT, MVT::Other);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000733 SDValue retops[2] = {
Scott Michele4d3e3c2008-01-17 20:38:41 +0000734 result,
Scott Michelbb713ae2008-01-30 02:55:46 +0000735 the_chain
Scott Michele4d3e3c2008-01-17 20:38:41 +0000736 };
Scott Michel8d5841a2008-01-11 02:53:15 +0000737
Dale Johannesen021052a2009-02-04 20:06:27 +0000738 result = DAG.getNode(SPUISD::LDRESULT, dl, retvts,
Scott Michele4d3e3c2008-01-17 20:38:41 +0000739 retops, sizeof(retops) / sizeof(retops[0]));
Scott Michel8d5841a2008-01-11 02:53:15 +0000740 return result;
Scott Michel6e22c652007-12-04 22:23:35 +0000741}
742
743/// Custom lower stores for CellSPU
744/*!
745 All CellSPU stores are aligned to 16-byte boundaries, so for elements
746 within a 16-byte block, we have to generate a shuffle to insert the
747 requested element into its place, then store the resulting block.
748 */
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000749static SDValue
750LowerSTORE(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel6e22c652007-12-04 22:23:35 +0000751 StoreSDNode *SN = cast<StoreSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000752 SDValue Value = SN->getValue();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000753 EVT VT = Value.getValueType();
754 EVT StVT = (!SN->isTruncatingStore() ? VT : SN->getMemoryVT());
755 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen021052a2009-02-04 20:06:27 +0000756 DebugLoc dl = Op.getDebugLoc();
Scott Michel8d5841a2008-01-11 02:53:15 +0000757 unsigned alignment = SN->getAlignment();
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000758 SDValue result;
759 EVT vecVT = StVT.isVector()? StVT: EVT::getVectorVT(*DAG.getContext(), StVT,
760 (128 / StVT.getSizeInBits()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000761 // Get pointerinfos to the memory chunk(s) that contain the data to load
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000762 uint64_t mpi_offset = SN->getPointerInfo().Offset;
763 mpi_offset -= mpi_offset%16;
Kalle Raiskila731d3922010-11-15 10:12:32 +0000764 MachinePointerInfo lowMemPtr(SN->getPointerInfo().V, mpi_offset);
765 MachinePointerInfo highMemPtr(SN->getPointerInfo().V, mpi_offset+16);
Scott Michel6e22c652007-12-04 22:23:35 +0000766
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000767
768 // two sanity checks
Wesley Peck527da1b2010-11-23 03:31:01 +0000769 assert( SN->getAddressingMode() == ISD::UNINDEXED
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000770 && "we should get only UNINDEXED adresses");
771 // clean aligned loads can be selected as-is
Kalle Raiskila7e7b4ac2011-01-17 11:59:20 +0000772 if (StVT.getSizeInBits() == 128 && (alignment%16) == 0)
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000773 return SDValue();
774
Kalle Raiskila731d3922010-11-15 10:12:32 +0000775 SDValue alignLoadVec;
776 SDValue basePtr = SN->getBasePtr();
777 SDValue the_chain = SN->getChain();
778 SDValue insertEltOffs;
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000779
Kalle Raiskila7e7b4ac2011-01-17 11:59:20 +0000780 if ((alignment%16) == 0) {
Kalle Raiskila731d3922010-11-15 10:12:32 +0000781 ConstantSDNode *CN;
782 // Special cases for a known aligned load to simplify the base pointer
783 // and insertion byte:
784 if (basePtr.getOpcode() == ISD::ADD
785 && (CN = dyn_cast<ConstantSDNode>(basePtr.getOperand(1))) != 0) {
786 // Known offset into basePtr
787 int64_t offset = CN->getSExtValue();
Scott Michel6e22c652007-12-04 22:23:35 +0000788
Kalle Raiskila731d3922010-11-15 10:12:32 +0000789 // Simplify the base pointer for this case:
790 basePtr = basePtr.getOperand(0);
791 insertEltOffs = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
792 basePtr,
793 DAG.getConstant((offset & 0xf), PtrVT));
Scott Michel6e22c652007-12-04 22:23:35 +0000794
Kalle Raiskila731d3922010-11-15 10:12:32 +0000795 if ((offset & ~0xf) > 0) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000796 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000797 basePtr,
Kalle Raiskila731d3922010-11-15 10:12:32 +0000798 DAG.getConstant((offset & ~0xf), PtrVT));
Scott Michel82335272008-12-27 04:51:36 +0000799 }
Kalle Raiskila731d3922010-11-15 10:12:32 +0000800 } else {
801 // Otherwise, assume it's at byte 0 of basePtr
802 insertEltOffs = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
803 basePtr,
804 DAG.getConstant(0, PtrVT));
805 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel82335272008-12-27 04:51:36 +0000806 basePtr,
807 DAG.getConstant(0, PtrVT));
808 }
Kalle Raiskila731d3922010-11-15 10:12:32 +0000809 } else {
810 // Unaligned load: must be more pessimistic about addressing modes:
811 if (basePtr.getOpcode() == ISD::ADD) {
812 MachineFunction &MF = DAG.getMachineFunction();
813 MachineRegisterInfo &RegInfo = MF.getRegInfo();
814 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
815 SDValue Flag;
Scott Michel82335272008-12-27 04:51:36 +0000816
Kalle Raiskila731d3922010-11-15 10:12:32 +0000817 SDValue Op0 = basePtr.getOperand(0);
818 SDValue Op1 = basePtr.getOperand(1);
819
820 if (isa<ConstantSDNode>(Op1)) {
821 // Convert the (add <ptr>, <const>) to an indirect address contained
822 // in a register. Note that this is done because we need to avoid
823 // creating a 0(reg) d-form address due to the SPU's block loads.
824 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
825 the_chain = DAG.getCopyToReg(the_chain, dl, VReg, basePtr, Flag);
826 basePtr = DAG.getCopyFromReg(the_chain, dl, VReg, PtrVT);
827 } else {
828 // Convert the (add <arg1>, <arg2>) to an indirect address, which
829 // will likely be lowered as a reg(reg) x-form address.
830 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
831 }
832 } else {
833 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
834 basePtr,
835 DAG.getConstant(0, PtrVT));
836 }
837
838 // Insertion point is solely determined by basePtr's contents
839 insertEltOffs = DAG.getNode(ISD::ADD, dl, PtrVT,
840 basePtr,
841 DAG.getConstant(0, PtrVT));
842 }
843
844 // Load the lower part of the memory to which to store.
845 SDValue low = DAG.getLoad(vecVT, dl, the_chain, basePtr,
846 lowMemPtr, SN->isVolatile(), SN->isNonTemporal(), 16);
Scott Michel82335272008-12-27 04:51:36 +0000847
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000848 // if we don't need to store over the 16 byte boundary, one store suffices
849 if (alignment >= StVT.getSizeInBits()/8) {
Scott Michel82335272008-12-27 04:51:36 +0000850 // Update the chain
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000851 the_chain = low.getValue(1);
Scott Michel6e22c652007-12-04 22:23:35 +0000852
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000853 LoadSDNode *LN = cast<LoadSDNode>(low);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000854 SDValue theValue = SN->getValue();
Scott Michel6e22c652007-12-04 22:23:35 +0000855
856 if (StVT != VT
Scott Michelbb713ae2008-01-30 02:55:46 +0000857 && (theValue.getOpcode() == ISD::AssertZext
858 || theValue.getOpcode() == ISD::AssertSext)) {
Scott Michel6e22c652007-12-04 22:23:35 +0000859 // Drill down and get the value for zero- and sign-extended
860 // quantities
Scott Michelfe095082008-07-16 17:17:29 +0000861 theValue = theValue.getOperand(0);
Scott Michel6e22c652007-12-04 22:23:35 +0000862 }
863
Scott Michel8d5841a2008-01-11 02:53:15 +0000864 // If the base pointer is already a D-form address, then just create
865 // a new D-form address with a slot offset and the orignal base pointer.
866 // Otherwise generate a D-form address with the slot offset relative
867 // to the stack pointer, which is always aligned.
Scott Michel82335272008-12-27 04:51:36 +0000868#if !defined(NDEBUG)
869 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Chris Lattner317dbbc2009-08-23 07:05:07 +0000870 errs() << "CellSPU LowerSTORE: basePtr = ";
Scott Michel82335272008-12-27 04:51:36 +0000871 basePtr.getNode()->dump(&DAG);
Chris Lattner317dbbc2009-08-23 07:05:07 +0000872 errs() << "\n";
Scott Michel82335272008-12-27 04:51:36 +0000873 }
874#endif
Scott Michel8d5841a2008-01-11 02:53:15 +0000875
Kalle Raiskila8f3e3ba2010-08-24 11:05:51 +0000876 SDValue insertEltOp = DAG.getNode(SPUISD::SHUFFLE_MASK, dl, vecVT,
877 insertEltOffs);
Wesley Peck527da1b2010-11-23 03:31:01 +0000878 SDValue vectorizeOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, vecVT,
Kalle Raiskila8f3e3ba2010-08-24 11:05:51 +0000879 theValue);
880
Dale Johannesen021052a2009-02-04 20:06:27 +0000881 result = DAG.getNode(SPUISD::SHUFB, dl, vecVT,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000882 vectorizeOp, low,
Wesley Peck527da1b2010-11-23 03:31:01 +0000883 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson9f944592009-08-11 20:47:22 +0000884 MVT::v4i32, insertEltOp));
Scott Michel6e22c652007-12-04 22:23:35 +0000885
Dale Johannesen021052a2009-02-04 20:06:27 +0000886 result = DAG.getStore(the_chain, dl, result, basePtr,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000887 lowMemPtr,
David Greenecfa68982010-02-15 16:55:58 +0000888 LN->isVolatile(), LN->isNonTemporal(),
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000889 16);
Scott Michel6e22c652007-12-04 22:23:35 +0000890
Scott Michel6e22c652007-12-04 22:23:35 +0000891 }
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000892 // do the store when it might cross the 16 byte memory access boundary.
893 else {
Wesley Peck527da1b2010-11-23 03:31:01 +0000894 // TODO issue a warning if SN->isVolatile()== true? This is likely not
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000895 // what the user wanted.
Wesley Peck527da1b2010-11-23 03:31:01 +0000896
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000897 // address offset from nearest lower 16byte alinged address
Wesley Peck527da1b2010-11-23 03:31:01 +0000898 SDValue offset = DAG.getNode(ISD::AND, dl, MVT::i32,
899 SN->getBasePtr(),
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000900 DAG.getConstant(0xf, MVT::i32));
901 // 16 - offset
Wesley Peck527da1b2010-11-23 03:31:01 +0000902 SDValue offset_compl = DAG.getNode(ISD::SUB, dl, MVT::i32,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000903 DAG.getConstant( 16, MVT::i32),
904 offset);
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000905 // 16 - sizeof(Value)
Wesley Peck527da1b2010-11-23 03:31:01 +0000906 SDValue surplus = DAG.getNode(ISD::SUB, dl, MVT::i32,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000907 DAG.getConstant( 16, MVT::i32),
908 DAG.getConstant( VT.getSizeInBits()/8,
909 MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +0000910 // get a registerfull of ones
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000911 SDValue ones = DAG.getConstant(-1, MVT::v4i32);
Wesley Peck527da1b2010-11-23 03:31:01 +0000912 ones = DAG.getNode(ISD::BITCAST, dl, MVT::i128, ones);
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000913
914 // Create the 128 bit masks that have ones where the data to store is
915 // located.
Wesley Peck527da1b2010-11-23 03:31:01 +0000916 SDValue lowmask, himask;
917 // if the value to store don't fill up the an entire 128 bits, zero
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000918 // out the last bits of the mask so that only the value we want to store
Wesley Peck527da1b2010-11-23 03:31:01 +0000919 // is masked.
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000920 // this is e.g. in the case of store i32, align 2
921 if (!VT.isVector()){
922 Value = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, Value);
923 lowmask = DAG.getNode(SPUISD::SRL_BYTES, dl, MVT::i128, ones, surplus);
Wesley Peck527da1b2010-11-23 03:31:01 +0000924 lowmask = DAG.getNode(SPUISD::SHL_BYTES, dl, MVT::i128, lowmask,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000925 surplus);
Wesley Peck527da1b2010-11-23 03:31:01 +0000926 Value = DAG.getNode(ISD::BITCAST, dl, MVT::i128, Value);
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000927 Value = DAG.getNode(ISD::AND, dl, MVT::i128, Value, lowmask);
Wesley Peck527da1b2010-11-23 03:31:01 +0000928
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000929 }
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000930 else {
931 lowmask = ones;
Wesley Peck527da1b2010-11-23 03:31:01 +0000932 Value = DAG.getNode(ISD::BITCAST, dl, MVT::i128, Value);
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000933 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000934 // this will zero, if there are no data that goes to the high quad
935 himask = DAG.getNode(SPUISD::SHL_BYTES, dl, MVT::i128, lowmask,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000936 offset_compl);
Wesley Peck527da1b2010-11-23 03:31:01 +0000937 lowmask = DAG.getNode(SPUISD::SRL_BYTES, dl, MVT::i128, lowmask,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000938 offset);
Wesley Peck527da1b2010-11-23 03:31:01 +0000939
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000940 // Load in the old data and zero out the parts that will be overwritten with
941 // the new data to store.
Wesley Peck527da1b2010-11-23 03:31:01 +0000942 SDValue hi = DAG.getLoad(MVT::i128, dl, the_chain,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000943 DAG.getNode(ISD::ADD, dl, PtrVT, basePtr,
944 DAG.getConstant( 16, PtrVT)),
945 highMemPtr,
946 SN->isVolatile(), SN->isNonTemporal(), 16);
947 the_chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, low.getValue(1),
948 hi.getValue(1));
Scott Michel6e22c652007-12-04 22:23:35 +0000949
Wesley Peck527da1b2010-11-23 03:31:01 +0000950 low = DAG.getNode(ISD::AND, dl, MVT::i128,
951 DAG.getNode( ISD::BITCAST, dl, MVT::i128, low),
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000952 DAG.getNode( ISD::XOR, dl, MVT::i128, lowmask, ones));
Wesley Peck527da1b2010-11-23 03:31:01 +0000953 hi = DAG.getNode(ISD::AND, dl, MVT::i128,
954 DAG.getNode( ISD::BITCAST, dl, MVT::i128, hi),
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000955 DAG.getNode( ISD::XOR, dl, MVT::i128, himask, ones));
956
957 // Shift the Value to store into place. rlow contains the parts that go to
Wesley Peck527da1b2010-11-23 03:31:01 +0000958 // the lower memory chunk, rhi has the parts that go to the upper one.
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000959 SDValue rlow = DAG.getNode(SPUISD::SRL_BYTES, dl, MVT::i128, Value, offset);
960 rlow = DAG.getNode(ISD::AND, dl, MVT::i128, rlow, lowmask);
Wesley Peck527da1b2010-11-23 03:31:01 +0000961 SDValue rhi = DAG.getNode(SPUISD::SHL_BYTES, dl, MVT::i128, Value,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000962 offset_compl);
963
964 // Merge the old data and the new data and store the results
Wesley Peck527da1b2010-11-23 03:31:01 +0000965 // Need to convert vectors here to integer as 'OR'ing floats assert
966 rlow = DAG.getNode(ISD::OR, dl, MVT::i128,
967 DAG.getNode(ISD::BITCAST, dl, MVT::i128, low),
968 DAG.getNode(ISD::BITCAST, dl, MVT::i128, rlow));
969 rhi = DAG.getNode(ISD::OR, dl, MVT::i128,
970 DAG.getNode(ISD::BITCAST, dl, MVT::i128, hi),
971 DAG.getNode(ISD::BITCAST, dl, MVT::i128, rhi));
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000972
973 low = DAG.getStore(the_chain, dl, rlow, basePtr,
974 lowMemPtr,
975 SN->isVolatile(), SN->isNonTemporal(), 16);
Wesley Peck527da1b2010-11-23 03:31:01 +0000976 hi = DAG.getStore(the_chain, dl, rhi,
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000977 DAG.getNode(ISD::ADD, dl, PtrVT, basePtr,
978 DAG.getConstant( 16, PtrVT)),
979 highMemPtr,
980 SN->isVolatile(), SN->isNonTemporal(), 16);
981 result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, low.getValue(0),
982 hi.getValue(0));
Wesley Peck527da1b2010-11-23 03:31:01 +0000983 }
Kalle Raiskila0a9dd402010-11-12 10:14:03 +0000984
985 return result;
Scott Michel6e22c652007-12-04 22:23:35 +0000986}
987
Scott Michela292fc62009-01-15 04:41:47 +0000988//! Generate the address of a constant pool entry.
Dan Gohmana6d0afc2009-08-07 01:32:21 +0000989static SDValue
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000990LowerConstantPool(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000991 EVT PtrVT = Op.getValueType();
Scott Michel6e22c652007-12-04 22:23:35 +0000992 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000993 const Constant *C = CP->getConstVal();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000994 SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
995 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Michel8d5841a2008-01-11 02:53:15 +0000996 const TargetMachine &TM = DAG.getTarget();
Dale Johannesen400dc2e2009-02-06 21:50:26 +0000997 // FIXME there is no actual debug info here
998 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +0000999
1000 if (TM.getRelocationModel() == Reloc::Static) {
1001 if (!ST->usingLargeMem()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001002 // Just return the SDValue with the constant pool address in it.
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001003 return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, CPI, Zero);
Scott Michel6e22c652007-12-04 22:23:35 +00001004 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001005 SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, CPI, Zero);
1006 SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, CPI, Zero);
1007 return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
Scott Michel6e22c652007-12-04 22:23:35 +00001008 }
1009 }
1010
Torok Edwinfbcc6632009-07-14 16:55:14 +00001011 llvm_unreachable("LowerConstantPool: Relocation model other than static"
Torok Edwin6cdb8972009-07-14 12:22:58 +00001012 " not supported.");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001013 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001014}
1015
Scott Michela292fc62009-01-15 04:41:47 +00001016//! Alternate entry point for generating the address of a constant pool entry
1017SDValue
1018SPU::LowerConstantPool(SDValue Op, SelectionDAG &DAG, const SPUTargetMachine &TM) {
1019 return ::LowerConstantPool(Op, DAG, TM.getSubtargetImpl());
1020}
1021
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001022static SDValue
1023LowerJumpTable(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001024 EVT PtrVT = Op.getValueType();
Scott Michel6e22c652007-12-04 22:23:35 +00001025 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001026 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1027 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Michel6e22c652007-12-04 22:23:35 +00001028 const TargetMachine &TM = DAG.getTarget();
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001029 // FIXME there is no actual debug info here
1030 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00001031
1032 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michel7d5eaec2008-02-23 18:41:37 +00001033 if (!ST->usingLargeMem()) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001034 return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, JTI, Zero);
Scott Michel7d5eaec2008-02-23 18:41:37 +00001035 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001036 SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, JTI, Zero);
1037 SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, JTI, Zero);
1038 return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
Scott Michel7d5eaec2008-02-23 18:41:37 +00001039 }
Scott Michel6e22c652007-12-04 22:23:35 +00001040 }
1041
Torok Edwinfbcc6632009-07-14 16:55:14 +00001042 llvm_unreachable("LowerJumpTable: Relocation model other than static"
Torok Edwin6cdb8972009-07-14 12:22:58 +00001043 " not supported.");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001044 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001045}
1046
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001047static SDValue
1048LowerGlobalAddress(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001049 EVT PtrVT = Op.getValueType();
Scott Michel6e22c652007-12-04 22:23:35 +00001050 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001051 const GlobalValue *GV = GSDN->getGlobal();
Devang Patela3ca21b2010-07-06 22:08:15 +00001052 SDValue GA = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
1053 PtrVT, GSDN->getOffset());
Scott Michel6e22c652007-12-04 22:23:35 +00001054 const TargetMachine &TM = DAG.getTarget();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001055 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001056 // FIXME there is no actual debug info here
1057 DebugLoc dl = Op.getDebugLoc();
Scott Michelfe095082008-07-16 17:17:29 +00001058
Scott Michel6e22c652007-12-04 22:23:35 +00001059 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michelceae3bb2008-01-29 02:16:57 +00001060 if (!ST->usingLargeMem()) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001061 return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, GA, Zero);
Scott Michelceae3bb2008-01-29 02:16:57 +00001062 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001063 SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, GA, Zero);
1064 SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, GA, Zero);
1065 return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
Scott Michelceae3bb2008-01-29 02:16:57 +00001066 }
Scott Michel6e22c652007-12-04 22:23:35 +00001067 } else {
Chris Lattner2104b8d2010-04-07 22:58:41 +00001068 report_fatal_error("LowerGlobalAddress: Relocation model other than static"
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001069 "not supported.");
Scott Michel6e22c652007-12-04 22:23:35 +00001070 /*NOTREACHED*/
1071 }
1072
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001073 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001074}
1075
Nate Begeman4b3210a2008-02-14 18:43:04 +00001076//! Custom lower double precision floating point constants
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001077static SDValue
1078LowerConstantFP(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001079 EVT VT = Op.getValueType();
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001080 // FIXME there is no actual debug info here
1081 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00001082
Owen Anderson9f944592009-08-11 20:47:22 +00001083 if (VT == MVT::f64) {
Scott Michel08a4e202008-12-01 17:56:02 +00001084 ConstantFPSDNode *FP = cast<ConstantFPSDNode>(Op.getNode());
1085
1086 assert((FP != 0) &&
1087 "LowerConstantFP: Node is not ConstantFPSDNode");
Scott Michelb8ee30d2008-12-29 03:23:36 +00001088
Scott Michel098c1132007-12-19 20:15:47 +00001089 uint64_t dbits = DoubleToBits(FP->getValueAPF().convertToDouble());
Owen Anderson9f944592009-08-11 20:47:22 +00001090 SDValue T = DAG.getConstant(dbits, MVT::i64);
1091 SDValue Tvec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64, T, T);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001092 return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
Wesley Peck527da1b2010-11-23 03:31:01 +00001093 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Tvec));
Scott Michel6e22c652007-12-04 22:23:35 +00001094 }
1095
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001096 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001097}
1098
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001099SDValue
1100SPUTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001101 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001102 const SmallVectorImpl<ISD::InputArg>
1103 &Ins,
1104 DebugLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001105 SmallVectorImpl<SDValue> &InVals)
1106 const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001107
Scott Michel6e22c652007-12-04 22:23:35 +00001108 MachineFunction &MF = DAG.getMachineFunction();
1109 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattnera10fff52007-12-31 04:13:23 +00001110 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +00001111 SPUFunctionInfo *FuncInfo = MF.getInfo<SPUFunctionInfo>();
Scott Michel6e22c652007-12-04 22:23:35 +00001112
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001113 unsigned ArgOffset = SPUFrameLowering::minStackSize();
Scott Michel6e22c652007-12-04 22:23:35 +00001114 unsigned ArgRegIdx = 0;
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001115 unsigned StackSlotSize = SPUFrameLowering::stackSlotSize();
Scott Michelfe095082008-07-16 17:17:29 +00001116
Owen Anderson53aa7a92009-08-10 22:56:29 +00001117 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfe095082008-07-16 17:17:29 +00001118
Kalle Raiskilad799ea52010-07-08 21:15:22 +00001119 SmallVector<CCValAssign, 16> ArgLocs;
1120 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1121 *DAG.getContext());
1122 // FIXME: allow for other calling conventions
1123 CCInfo.AnalyzeFormalArguments(Ins, CCC_SPU);
1124
Scott Michel6e22c652007-12-04 22:23:35 +00001125 // Add DAG nodes to load the arguments or copy them out of registers.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001126 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001127 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands13237ac2008-06-06 12:08:01 +00001128 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Scott Michel487c4342008-10-30 01:51:48 +00001129 SDValue ArgVal;
Kalle Raiskilad799ea52010-07-08 21:15:22 +00001130 CCValAssign &VA = ArgLocs[ArgNo];
Scott Michel6e22c652007-12-04 22:23:35 +00001131
Kalle Raiskilad799ea52010-07-08 21:15:22 +00001132 if (VA.isRegLoc()) {
Scott Michel487c4342008-10-30 01:51:48 +00001133 const TargetRegisterClass *ArgRegClass;
Scott Michelfe095082008-07-16 17:17:29 +00001134
Owen Anderson9f944592009-08-11 20:47:22 +00001135 switch (ObjectVT.getSimpleVT().SimpleTy) {
Benjamin Kramera6769262010-04-08 10:44:28 +00001136 default:
1137 report_fatal_error("LowerFormalArguments Unhandled argument type: " +
1138 Twine(ObjectVT.getEVTString()));
Owen Anderson9f944592009-08-11 20:47:22 +00001139 case MVT::i8:
Scott Michelc6918c12008-11-21 02:56:16 +00001140 ArgRegClass = &SPU::R8CRegClass;
1141 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001142 case MVT::i16:
Scott Michelc6918c12008-11-21 02:56:16 +00001143 ArgRegClass = &SPU::R16CRegClass;
1144 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001145 case MVT::i32:
Scott Michelc6918c12008-11-21 02:56:16 +00001146 ArgRegClass = &SPU::R32CRegClass;
1147 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001148 case MVT::i64:
Scott Michelc6918c12008-11-21 02:56:16 +00001149 ArgRegClass = &SPU::R64CRegClass;
1150 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001151 case MVT::i128:
Scott Michel6887caf2009-01-06 03:36:14 +00001152 ArgRegClass = &SPU::GPRCRegClass;
1153 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001154 case MVT::f32:
Scott Michelc6918c12008-11-21 02:56:16 +00001155 ArgRegClass = &SPU::R32FPRegClass;
1156 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001157 case MVT::f64:
Scott Michelc6918c12008-11-21 02:56:16 +00001158 ArgRegClass = &SPU::R64FPRegClass;
1159 break;
Owen Anderson9f944592009-08-11 20:47:22 +00001160 case MVT::v2f64:
1161 case MVT::v4f32:
1162 case MVT::v2i64:
1163 case MVT::v4i32:
1164 case MVT::v8i16:
1165 case MVT::v16i8:
Scott Michelc6918c12008-11-21 02:56:16 +00001166 ArgRegClass = &SPU::VECREGRegClass;
1167 break;
Scott Michel487c4342008-10-30 01:51:48 +00001168 }
1169
1170 unsigned VReg = RegInfo.createVirtualRegister(ArgRegClass);
Kalle Raiskilad799ea52010-07-08 21:15:22 +00001171 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001172 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Scott Michel487c4342008-10-30 01:51:48 +00001173 ++ArgRegIdx;
1174 } else {
1175 // We need to load the argument to a virtual register if we determined
1176 // above that we ran out of physical registers of the appropriate type
1177 // or we're forced to do vararg
Evan Cheng0664a672010-07-03 00:40:23 +00001178 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset, true);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001179 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattner82fd06d2010-09-21 06:22:23 +00001180 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
1181 false, false, 0);
Scott Michel6e22c652007-12-04 22:23:35 +00001182 ArgOffset += StackSlotSize;
1183 }
Scott Michelfe095082008-07-16 17:17:29 +00001184
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001185 InVals.push_back(ArgVal);
Scott Michel487c4342008-10-30 01:51:48 +00001186 // Update the chain
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001187 Chain = ArgVal.getOperand(0);
Scott Michel6e22c652007-12-04 22:23:35 +00001188 }
Scott Michelfe095082008-07-16 17:17:29 +00001189
Scott Michel487c4342008-10-30 01:51:48 +00001190 // vararg handling:
Scott Michel6e22c652007-12-04 22:23:35 +00001191 if (isVarArg) {
Wesley Peck527da1b2010-11-23 03:31:01 +00001192 // FIXME: we should be able to query the argument registers from
1193 // tablegen generated code.
Kalle Raiskilad799ea52010-07-08 21:15:22 +00001194 static const unsigned ArgRegs[] = {
1195 SPU::R3, SPU::R4, SPU::R5, SPU::R6, SPU::R7, SPU::R8, SPU::R9,
1196 SPU::R10, SPU::R11, SPU::R12, SPU::R13, SPU::R14, SPU::R15, SPU::R16,
1197 SPU::R17, SPU::R18, SPU::R19, SPU::R20, SPU::R21, SPU::R22, SPU::R23,
1198 SPU::R24, SPU::R25, SPU::R26, SPU::R27, SPU::R28, SPU::R29, SPU::R30,
1199 SPU::R31, SPU::R32, SPU::R33, SPU::R34, SPU::R35, SPU::R36, SPU::R37,
1200 SPU::R38, SPU::R39, SPU::R40, SPU::R41, SPU::R42, SPU::R43, SPU::R44,
1201 SPU::R45, SPU::R46, SPU::R47, SPU::R48, SPU::R49, SPU::R50, SPU::R51,
1202 SPU::R52, SPU::R53, SPU::R54, SPU::R55, SPU::R56, SPU::R57, SPU::R58,
1203 SPU::R59, SPU::R60, SPU::R61, SPU::R62, SPU::R63, SPU::R64, SPU::R65,
1204 SPU::R66, SPU::R67, SPU::R68, SPU::R69, SPU::R70, SPU::R71, SPU::R72,
1205 SPU::R73, SPU::R74, SPU::R75, SPU::R76, SPU::R77, SPU::R78, SPU::R79
1206 };
1207 // size of ArgRegs array
1208 unsigned NumArgRegs = 77;
1209
Scott Michel487c4342008-10-30 01:51:48 +00001210 // We will spill (79-3)+1 registers to the stack
1211 SmallVector<SDValue, 79-3+1> MemOps;
1212
1213 // Create the frame slot
Scott Michel6e22c652007-12-04 22:23:35 +00001214 for (; ArgRegIdx != NumArgRegs; ++ArgRegIdx) {
Dan Gohman31ae5862010-04-17 14:41:14 +00001215 FuncInfo->setVarArgsFrameIndex(
Evan Cheng0664a672010-07-03 00:40:23 +00001216 MFI->CreateFixedObject(StackSlotSize, ArgOffset, true));
Dan Gohman31ae5862010-04-17 14:41:14 +00001217 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Cameron Zwarich9eb5a412011-05-19 04:44:19 +00001218 unsigned VReg = MF.addLiveIn(ArgRegs[ArgRegIdx], &SPU::VECREGRegClass);
Chris Lattnerf60c5562010-03-29 17:38:47 +00001219 SDValue ArgVal = DAG.getRegister(VReg, MVT::v16i8);
Chris Lattner676c61d2010-09-21 18:41:36 +00001220 SDValue Store = DAG.getStore(Chain, dl, ArgVal, FIN, MachinePointerInfo(),
David Greenecfa68982010-02-15 16:55:58 +00001221 false, false, 0);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001222 Chain = Store.getOperand(0);
Scott Michel6e22c652007-12-04 22:23:35 +00001223 MemOps.push_back(Store);
Scott Michel487c4342008-10-30 01:51:48 +00001224
1225 // Increment address by stack slot size for the next stored argument
1226 ArgOffset += StackSlotSize;
Scott Michel6e22c652007-12-04 22:23:35 +00001227 }
1228 if (!MemOps.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00001229 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001230 &MemOps[0], MemOps.size());
Scott Michel6e22c652007-12-04 22:23:35 +00001231 }
Scott Michelfe095082008-07-16 17:17:29 +00001232
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001233 return Chain;
Scott Michel6e22c652007-12-04 22:23:35 +00001234}
1235
1236/// isLSAAddress - Return the immediate to use if the specified
1237/// value is representable as a LSA address.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001238static SDNode *isLSAAddress(SDValue Op, SelectionDAG &DAG) {
Scott Michelaab89ca2008-11-11 03:06:06 +00001239 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Scott Michel6e22c652007-12-04 22:23:35 +00001240 if (!C) return 0;
Scott Michelfe095082008-07-16 17:17:29 +00001241
Dan Gohmaneffb8942008-09-12 16:56:44 +00001242 int Addr = C->getZExtValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001243 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
1244 (Addr << 14 >> 14) != Addr)
1245 return 0; // Top 14 bits have to be sext of immediate.
Scott Michelfe095082008-07-16 17:17:29 +00001246
Owen Anderson9f944592009-08-11 20:47:22 +00001247 return DAG.getConstant((int)C->getZExtValue() >> 2, MVT::i32).getNode();
Scott Michel6e22c652007-12-04 22:23:35 +00001248}
1249
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001250SDValue
Evan Cheng6f36a082010-02-02 23:55:14 +00001251SPUTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001252 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng67a69dd2010-01-27 00:07:07 +00001253 bool &isTailCall,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001254 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001255 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001256 const SmallVectorImpl<ISD::InputArg> &Ins,
1257 DebugLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001258 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng67a69dd2010-01-27 00:07:07 +00001259 // CellSPU target does not yet support tail call optimization.
1260 isTailCall = false;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001261
1262 const SPUSubtarget *ST = SPUTM.getSubtargetImpl();
1263 unsigned NumOps = Outs.size();
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001264 unsigned StackSlotSize = SPUFrameLowering::stackSlotSize();
Kalle Raiskilad799ea52010-07-08 21:15:22 +00001265
1266 SmallVector<CCValAssign, 16> ArgLocs;
1267 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
Wesley Peck527da1b2010-11-23 03:31:01 +00001268 *DAG.getContext());
Kalle Raiskilad799ea52010-07-08 21:15:22 +00001269 // FIXME: allow for other calling conventions
1270 CCInfo.AnalyzeCallOperands(Outs, CCC_SPU);
Wesley Peck527da1b2010-11-23 03:31:01 +00001271
Kalle Raiskilad799ea52010-07-08 21:15:22 +00001272 const unsigned NumArgRegs = ArgLocs.size();
1273
Scott Michel6e22c652007-12-04 22:23:35 +00001274
1275 // Handy pointer type
Owen Anderson53aa7a92009-08-10 22:56:29 +00001276 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfe095082008-07-16 17:17:29 +00001277
Scott Michel6e22c652007-12-04 22:23:35 +00001278 // Set up a copy of the stack pointer for use loading and storing any
1279 // arguments that may not fit in the registers available for argument
1280 // passing.
Owen Anderson9f944592009-08-11 20:47:22 +00001281 SDValue StackPtr = DAG.getRegister(SPU::R1, MVT::i32);
Scott Michelfe095082008-07-16 17:17:29 +00001282
Scott Michel6e22c652007-12-04 22:23:35 +00001283 // Figure out which arguments are going to go in registers, and which in
1284 // memory.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001285 unsigned ArgOffset = SPUFrameLowering::minStackSize(); // Just below [LR]
Scott Michel6e22c652007-12-04 22:23:35 +00001286 unsigned ArgRegIdx = 0;
1287
1288 // Keep track of registers passing arguments
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001289 std::vector<std::pair<unsigned, SDValue> > RegsToPass;
Scott Michel6e22c652007-12-04 22:23:35 +00001290 // And the arguments passed on the stack
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001291 SmallVector<SDValue, 8> MemOpChains;
Scott Michel6e22c652007-12-04 22:23:35 +00001292
Kalle Raiskilad799ea52010-07-08 21:15:22 +00001293 for (; ArgRegIdx != NumOps; ++ArgRegIdx) {
1294 SDValue Arg = OutVals[ArgRegIdx];
1295 CCValAssign &VA = ArgLocs[ArgRegIdx];
Scott Michelfe095082008-07-16 17:17:29 +00001296
Scott Michel6e22c652007-12-04 22:23:35 +00001297 // PtrOff will be used to store the current argument to the stack if a
1298 // register cannot be found for it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001299 SDValue PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Dale Johannesen021052a2009-02-04 20:06:27 +00001300 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Scott Michel6e22c652007-12-04 22:23:35 +00001301
Owen Anderson9f944592009-08-11 20:47:22 +00001302 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001303 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson9f944592009-08-11 20:47:22 +00001304 case MVT::i8:
1305 case MVT::i16:
1306 case MVT::i32:
1307 case MVT::i64:
1308 case MVT::i128:
Owen Anderson9f944592009-08-11 20:47:22 +00001309 case MVT::f32:
1310 case MVT::f64:
Owen Anderson9f944592009-08-11 20:47:22 +00001311 case MVT::v2i64:
1312 case MVT::v2f64:
1313 case MVT::v4f32:
1314 case MVT::v4i32:
1315 case MVT::v8i16:
1316 case MVT::v16i8:
Scott Michel6e22c652007-12-04 22:23:35 +00001317 if (ArgRegIdx != NumArgRegs) {
Kalle Raiskilad799ea52010-07-08 21:15:22 +00001318 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Scott Michel6e22c652007-12-04 22:23:35 +00001319 } else {
Chris Lattner676c61d2010-09-21 18:41:36 +00001320 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1321 MachinePointerInfo(),
David Greenecfa68982010-02-15 16:55:58 +00001322 false, false, 0));
Scott Michelbb713ae2008-01-30 02:55:46 +00001323 ArgOffset += StackSlotSize;
Scott Michel6e22c652007-12-04 22:23:35 +00001324 }
1325 break;
1326 }
1327 }
1328
Bill Wendling6ff71a12009-12-28 01:31:11 +00001329 // Accumulate how many bytes are to be pushed on the stack, including the
1330 // linkage area, and parameter passing area. According to the SPU ABI,
1331 // we minimally need space for [LR] and [SP].
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001332 unsigned NumStackBytes = ArgOffset - SPUFrameLowering::minStackSize();
Bill Wendling6ff71a12009-12-28 01:31:11 +00001333
1334 // Insert a call sequence start
Chris Lattner27539552008-10-11 22:08:30 +00001335 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumStackBytes,
1336 true));
Scott Michel6e22c652007-12-04 22:23:35 +00001337
1338 if (!MemOpChains.empty()) {
1339 // Adjust the stack pointer for the stack arguments.
Owen Anderson9f944592009-08-11 20:47:22 +00001340 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Scott Michel6e22c652007-12-04 22:23:35 +00001341 &MemOpChains[0], MemOpChains.size());
1342 }
Scott Michelfe095082008-07-16 17:17:29 +00001343
Scott Michel6e22c652007-12-04 22:23:35 +00001344 // Build a sequence of copy-to-reg nodes chained together with token chain
1345 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001346 SDValue InFlag;
Scott Michel6e22c652007-12-04 22:23:35 +00001347 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Micheld1db1ab2009-03-16 18:47:25 +00001348 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen021052a2009-02-04 20:06:27 +00001349 RegsToPass[i].second, InFlag);
Scott Michel6e22c652007-12-04 22:23:35 +00001350 InFlag = Chain.getValue(1);
1351 }
Scott Michelfe095082008-07-16 17:17:29 +00001352
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001353 SmallVector<SDValue, 8> Ops;
Scott Michel6e22c652007-12-04 22:23:35 +00001354 unsigned CallOpc = SPUISD::CALL;
Scott Michelfe095082008-07-16 17:17:29 +00001355
Bill Wendling24c79f22008-09-16 21:48:12 +00001356 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1357 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1358 // node so that legalize doesn't hack it.
Scott Michelaab89ca2008-11-11 03:06:06 +00001359 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001360 const GlobalValue *GV = G->getGlobal();
Owen Anderson53aa7a92009-08-10 22:56:29 +00001361 EVT CalleeVT = Callee.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001362 SDValue Zero = DAG.getConstant(0, PtrVT);
Devang Patela3ca21b2010-07-06 22:08:15 +00001363 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, CalleeVT);
Scott Michel6e22c652007-12-04 22:23:35 +00001364
Scott Michel8d5841a2008-01-11 02:53:15 +00001365 if (!ST->usingLargeMem()) {
1366 // Turn calls to targets that are defined (i.e., have bodies) into BRSL
1367 // style calls, otherwise, external symbols are BRASL calls. This assumes
1368 // that declared/defined symbols are in the same compilation unit and can
1369 // be reached through PC-relative jumps.
1370 //
1371 // NOTE:
1372 // This may be an unsafe assumption for JIT and really large compilation
1373 // units.
1374 if (GV->isDeclaration()) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001375 Callee = DAG.getNode(SPUISD::AFormAddr, dl, CalleeVT, GA, Zero);
Scott Michel8d5841a2008-01-11 02:53:15 +00001376 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001377 Callee = DAG.getNode(SPUISD::PCRelAddr, dl, CalleeVT, GA, Zero);
Scott Michel8d5841a2008-01-11 02:53:15 +00001378 }
Scott Michel6e22c652007-12-04 22:23:35 +00001379 } else {
Scott Michel8d5841a2008-01-11 02:53:15 +00001380 // "Large memory" mode: Turn all calls into indirect calls with a X-form
1381 // address pairs:
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001382 Callee = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, GA, Zero);
Scott Michel6e22c652007-12-04 22:23:35 +00001383 }
Scott Michelb8ee30d2008-12-29 03:23:36 +00001384 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001385 EVT CalleeVT = Callee.getValueType();
Scott Michelb8ee30d2008-12-29 03:23:36 +00001386 SDValue Zero = DAG.getConstant(0, PtrVT);
1387 SDValue ExtSym = DAG.getTargetExternalSymbol(S->getSymbol(),
1388 Callee.getValueType());
1389
1390 if (!ST->usingLargeMem()) {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001391 Callee = DAG.getNode(SPUISD::AFormAddr, dl, CalleeVT, ExtSym, Zero);
Scott Michelb8ee30d2008-12-29 03:23:36 +00001392 } else {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001393 Callee = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, ExtSym, Zero);
Scott Michelb8ee30d2008-12-29 03:23:36 +00001394 }
1395 } else if (SDNode *Dest = isLSAAddress(Callee, DAG)) {
Scott Michel6e22c652007-12-04 22:23:35 +00001396 // If this is an absolute destination address that appears to be a legal
1397 // local store address, use the munged value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001398 Callee = SDValue(Dest, 0);
Scott Michel8d5841a2008-01-11 02:53:15 +00001399 }
Scott Michel6e22c652007-12-04 22:23:35 +00001400
1401 Ops.push_back(Chain);
1402 Ops.push_back(Callee);
Scott Michelfe095082008-07-16 17:17:29 +00001403
Scott Michel6e22c652007-12-04 22:23:35 +00001404 // Add argument registers to the end of the list so that they are known live
1405 // into the call.
1406 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Scott Michelfe095082008-07-16 17:17:29 +00001407 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Scott Michel6e22c652007-12-04 22:23:35 +00001408 RegsToPass[i].second.getValueType()));
Scott Michelfe095082008-07-16 17:17:29 +00001409
Gabor Greiff304a7a2008-08-28 21:40:38 +00001410 if (InFlag.getNode())
Scott Michel6e22c652007-12-04 22:23:35 +00001411 Ops.push_back(InFlag);
Duncan Sands739a0542008-07-02 17:40:58 +00001412 // Returns a chain and a flag for retval copy to use.
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001413 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Glue),
Duncan Sands739a0542008-07-02 17:40:58 +00001414 &Ops[0], Ops.size());
Scott Michel6e22c652007-12-04 22:23:35 +00001415 InFlag = Chain.getValue(1);
1416
Chris Lattner27539552008-10-11 22:08:30 +00001417 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumStackBytes, true),
1418 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001419 if (!Ins.empty())
Evan Cheng0f329162008-02-05 22:44:06 +00001420 InFlag = Chain.getValue(1);
1421
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001422 // If the function returns void, just return the chain.
1423 if (Ins.empty())
1424 return Chain;
Scott Michelfe095082008-07-16 17:17:29 +00001425
Kalle Raiskila7e25bc42010-08-24 11:50:48 +00001426 // Now handle the return value(s)
1427 SmallVector<CCValAssign, 16> RVLocs;
1428 CCState CCRetInfo(CallConv, isVarArg, getTargetMachine(),
1429 RVLocs, *DAG.getContext());
1430 CCRetInfo.AnalyzeCallResult(Ins, CCC_SPU);
1431
1432
Scott Michel6e22c652007-12-04 22:23:35 +00001433 // If the call has results, copy the values out of the ret val registers.
Kalle Raiskila7e25bc42010-08-24 11:50:48 +00001434 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1435 CCValAssign VA = RVLocs[i];
Wesley Peck527da1b2010-11-23 03:31:01 +00001436
Kalle Raiskila7e25bc42010-08-24 11:50:48 +00001437 SDValue Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1438 InFlag);
1439 Chain = Val.getValue(1);
1440 InFlag = Val.getValue(2);
1441 InVals.push_back(Val);
1442 }
Duncan Sands739a0542008-07-02 17:40:58 +00001443
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001444 return Chain;
Scott Michel6e22c652007-12-04 22:23:35 +00001445}
1446
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001447SDValue
1448SPUTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001449 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001450 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001451 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001452 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001453
Scott Michel6e22c652007-12-04 22:23:35 +00001454 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001455 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1456 RVLocs, *DAG.getContext());
1457 CCInfo.AnalyzeReturn(Outs, RetCC_SPU);
Scott Michelfe095082008-07-16 17:17:29 +00001458
Scott Michel6e22c652007-12-04 22:23:35 +00001459 // If this is the first return lowered for this function, add the regs to the
1460 // liveout set for the function.
Chris Lattnera10fff52007-12-31 04:13:23 +00001461 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Scott Michel6e22c652007-12-04 22:23:35 +00001462 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattnera10fff52007-12-31 04:13:23 +00001463 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Scott Michel6e22c652007-12-04 22:23:35 +00001464 }
1465
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001466 SDValue Flag;
Scott Michelfe095082008-07-16 17:17:29 +00001467
Scott Michel6e22c652007-12-04 22:23:35 +00001468 // Copy the result values into the output registers.
1469 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1470 CCValAssign &VA = RVLocs[i];
1471 assert(VA.isRegLoc() && "Can only return in registers!");
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001472 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001473 OutVals[i], Flag);
Scott Michel6e22c652007-12-04 22:23:35 +00001474 Flag = Chain.getValue(1);
1475 }
1476
Gabor Greiff304a7a2008-08-28 21:40:38 +00001477 if (Flag.getNode())
Owen Anderson9f944592009-08-11 20:47:22 +00001478 return DAG.getNode(SPUISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Scott Michel6e22c652007-12-04 22:23:35 +00001479 else
Owen Anderson9f944592009-08-11 20:47:22 +00001480 return DAG.getNode(SPUISD::RET_FLAG, dl, MVT::Other, Chain);
Scott Michel6e22c652007-12-04 22:23:35 +00001481}
1482
1483
1484//===----------------------------------------------------------------------===//
1485// Vector related lowering:
1486//===----------------------------------------------------------------------===//
1487
1488static ConstantSDNode *
1489getVecImm(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001490 SDValue OpVal(0, 0);
Scott Michelfe095082008-07-16 17:17:29 +00001491
Scott Michel6e22c652007-12-04 22:23:35 +00001492 // Check to see if this buildvec has a single non-undef value in its elements.
1493 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1494 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001495 if (OpVal.getNode() == 0)
Scott Michel6e22c652007-12-04 22:23:35 +00001496 OpVal = N->getOperand(i);
1497 else if (OpVal != N->getOperand(i))
1498 return 0;
1499 }
Scott Michelfe095082008-07-16 17:17:29 +00001500
Gabor Greiff304a7a2008-08-28 21:40:38 +00001501 if (OpVal.getNode() != 0) {
Scott Michelaab89ca2008-11-11 03:06:06 +00001502 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Scott Michel6e22c652007-12-04 22:23:35 +00001503 return CN;
1504 }
1505 }
1506
Scott Michel839ad0a2009-03-17 01:15:45 +00001507 return 0;
Scott Michel6e22c652007-12-04 22:23:35 +00001508}
1509
1510/// get_vec_i18imm - Test if this vector is a vector filled with the same value
1511/// and the value fits into an unsigned 18-bit constant, and if so, return the
1512/// constant
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001513SDValue SPU::get_vec_u18imm(SDNode *N, SelectionDAG &DAG,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001514 EVT ValueType) {
Scott Michel6e22c652007-12-04 22:23:35 +00001515 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001516 uint64_t Value = CN->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001517 if (ValueType == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001518 uint64_t UValue = CN->getZExtValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001519 uint32_t upper = uint32_t(UValue >> 32);
1520 uint32_t lower = uint32_t(UValue);
1521 if (upper != lower)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001522 return SDValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001523 Value = Value >> 32;
1524 }
Scott Michel6e22c652007-12-04 22:23:35 +00001525 if (Value <= 0x3ffff)
Dan Gohmanfd820522008-11-05 02:06:09 +00001526 return DAG.getTargetConstant(Value, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001527 }
1528
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001529 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001530}
1531
1532/// get_vec_i16imm - Test if this vector is a vector filled with the same value
1533/// and the value fits into a signed 16-bit constant, and if so, return the
1534/// constant
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001535SDValue SPU::get_vec_i16imm(SDNode *N, SelectionDAG &DAG,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001536 EVT ValueType) {
Scott Michel6e22c652007-12-04 22:23:35 +00001537 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohman6e054832008-09-26 21:54:37 +00001538 int64_t Value = CN->getSExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001539 if (ValueType == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001540 uint64_t UValue = CN->getZExtValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001541 uint32_t upper = uint32_t(UValue >> 32);
1542 uint32_t lower = uint32_t(UValue);
1543 if (upper != lower)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001544 return SDValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001545 Value = Value >> 32;
1546 }
Scott Michel42f56b42008-03-05 23:02:02 +00001547 if (Value >= -(1 << 15) && Value <= ((1 << 15) - 1)) {
Dan Gohmanfd820522008-11-05 02:06:09 +00001548 return DAG.getTargetConstant(Value, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001549 }
1550 }
1551
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001552 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001553}
1554
1555/// get_vec_i10imm - Test if this vector is a vector filled with the same value
1556/// and the value fits into a signed 10-bit constant, and if so, return the
1557/// constant
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001558SDValue SPU::get_vec_i10imm(SDNode *N, SelectionDAG &DAG,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001559 EVT ValueType) {
Scott Michel6e22c652007-12-04 22:23:35 +00001560 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohman6e054832008-09-26 21:54:37 +00001561 int64_t Value = CN->getSExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001562 if (ValueType == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001563 uint64_t UValue = CN->getZExtValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001564 uint32_t upper = uint32_t(UValue >> 32);
1565 uint32_t lower = uint32_t(UValue);
1566 if (upper != lower)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001567 return SDValue();
Scott Michele9b690b2008-03-06 04:02:54 +00001568 Value = Value >> 32;
1569 }
Benjamin Kramerf633ba82010-03-29 19:07:58 +00001570 if (isInt<10>(Value))
Dan Gohmanfd820522008-11-05 02:06:09 +00001571 return DAG.getTargetConstant(Value, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001572 }
1573
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001574 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001575}
1576
1577/// get_vec_i8imm - Test if this vector is a vector filled with the same value
1578/// and the value fits into a signed 8-bit constant, and if so, return the
1579/// constant.
1580///
1581/// @note: The incoming vector is v16i8 because that's the only way we can load
1582/// constant vectors. Thus, we test to see if the upper and lower bytes are the
1583/// same value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001584SDValue SPU::get_vec_i8imm(SDNode *N, SelectionDAG &DAG,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001585 EVT ValueType) {
Scott Michel6e22c652007-12-04 22:23:35 +00001586 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001587 int Value = (int) CN->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001588 if (ValueType == MVT::i16
Scott Michelbb713ae2008-01-30 02:55:46 +00001589 && Value <= 0xffff /* truncated from uint64_t */
1590 && ((short) Value >> 8) == ((short) Value & 0xff))
Dan Gohmanfd820522008-11-05 02:06:09 +00001591 return DAG.getTargetConstant(Value & 0xff, ValueType);
Owen Anderson9f944592009-08-11 20:47:22 +00001592 else if (ValueType == MVT::i8
Scott Michelbb713ae2008-01-30 02:55:46 +00001593 && (Value & 0xff) == Value)
Dan Gohmanfd820522008-11-05 02:06:09 +00001594 return DAG.getTargetConstant(Value, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001595 }
1596
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001597 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001598}
1599
1600/// get_ILHUvec_imm - Test if this vector is a vector filled with the same value
1601/// and the value fits into a signed 16-bit constant, and if so, return the
1602/// constant
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001603SDValue SPU::get_ILHUvec_imm(SDNode *N, SelectionDAG &DAG,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001604 EVT ValueType) {
Scott Michel6e22c652007-12-04 22:23:35 +00001605 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001606 uint64_t Value = CN->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00001607 if ((ValueType == MVT::i32
Scott Michelbb713ae2008-01-30 02:55:46 +00001608 && ((unsigned) Value & 0xffff0000) == (unsigned) Value)
Owen Anderson9f944592009-08-11 20:47:22 +00001609 || (ValueType == MVT::i64 && (Value & 0xffff0000) == Value))
Dan Gohmanfd820522008-11-05 02:06:09 +00001610 return DAG.getTargetConstant(Value >> 16, ValueType);
Scott Michel6e22c652007-12-04 22:23:35 +00001611 }
1612
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001613 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001614}
1615
1616/// get_v4i32_imm - Catch-all for general 32-bit constant vectors
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001617SDValue SPU::get_v4i32_imm(SDNode *N, SelectionDAG &DAG) {
Scott Michel6e22c652007-12-04 22:23:35 +00001618 if (ConstantSDNode *CN = getVecImm(N)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001619 return DAG.getTargetConstant((unsigned) CN->getZExtValue(), MVT::i32);
Scott Michel6e22c652007-12-04 22:23:35 +00001620 }
1621
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001622 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001623}
1624
1625/// get_v4i32_imm - Catch-all for general 64-bit constant vectors
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001626SDValue SPU::get_v2i64_imm(SDNode *N, SelectionDAG &DAG) {
Scott Michel6e22c652007-12-04 22:23:35 +00001627 if (ConstantSDNode *CN = getVecImm(N)) {
Owen Anderson9f944592009-08-11 20:47:22 +00001628 return DAG.getTargetConstant((unsigned) CN->getZExtValue(), MVT::i64);
Scott Michel6e22c652007-12-04 22:23:35 +00001629 }
1630
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001631 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001632}
1633
Scott Micheled7d79f2009-01-21 04:58:48 +00001634//! Lower a BUILD_VECTOR instruction creatively:
Dan Gohmana6d0afc2009-08-07 01:32:21 +00001635static SDValue
Scott Michel9e3e4a92009-01-26 03:31:40 +00001636LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001637 EVT VT = Op.getValueType();
1638 EVT EltVT = VT.getVectorElementType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001639 DebugLoc dl = Op.getDebugLoc();
Scott Michel839ad0a2009-03-17 01:15:45 +00001640 BuildVectorSDNode *BCN = dyn_cast<BuildVectorSDNode>(Op.getNode());
1641 assert(BCN != 0 && "Expected BuildVectorSDNode in SPU LowerBUILD_VECTOR");
1642 unsigned minSplatBits = EltVT.getSizeInBits();
1643
1644 if (minSplatBits < 16)
1645 minSplatBits = 16;
1646
1647 APInt APSplatBits, APSplatUndef;
1648 unsigned SplatBitSize;
1649 bool HasAnyUndefs;
1650
1651 if (!BCN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
1652 HasAnyUndefs, minSplatBits)
1653 || minSplatBits < SplatBitSize)
1654 return SDValue(); // Wasn't a constant vector or splat exceeded min
1655
1656 uint64_t SplatBits = APSplatBits.getZExtValue();
Scott Michelfe095082008-07-16 17:17:29 +00001657
Owen Anderson9f944592009-08-11 20:47:22 +00001658 switch (VT.getSimpleVT().SimpleTy) {
Benjamin Kramera6769262010-04-08 10:44:28 +00001659 default:
1660 report_fatal_error("CellSPU: Unhandled VT in LowerBUILD_VECTOR, VT = " +
1661 Twine(VT.getEVTString()));
Scott Micheled7d79f2009-01-21 04:58:48 +00001662 /*NOTREACHED*/
Owen Anderson9f944592009-08-11 20:47:22 +00001663 case MVT::v4f32: {
Scott Michel9e3e4a92009-01-26 03:31:40 +00001664 uint32_t Value32 = uint32_t(SplatBits);
Chris Lattner78b7cbe2009-03-26 05:29:34 +00001665 assert(SplatBitSize == 32
Scott Michelbb713ae2008-01-30 02:55:46 +00001666 && "LowerBUILD_VECTOR: Unexpected floating point vector element.");
Scott Michel6e22c652007-12-04 22:23:35 +00001667 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
Owen Anderson9f944592009-08-11 20:47:22 +00001668 SDValue T = DAG.getConstant(Value32, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001669 return DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,
Owen Anderson9f944592009-08-11 20:47:22 +00001670 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, T,T,T,T));
Scott Michel6e22c652007-12-04 22:23:35 +00001671 break;
1672 }
Owen Anderson9f944592009-08-11 20:47:22 +00001673 case MVT::v2f64: {
Scott Michel9e3e4a92009-01-26 03:31:40 +00001674 uint64_t f64val = uint64_t(SplatBits);
Chris Lattner78b7cbe2009-03-26 05:29:34 +00001675 assert(SplatBitSize == 64
Scott Michelefc8c7a2008-11-24 17:11:17 +00001676 && "LowerBUILD_VECTOR: 64-bit float vector size > 8 bytes.");
Scott Michel6e22c652007-12-04 22:23:35 +00001677 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
Owen Anderson9f944592009-08-11 20:47:22 +00001678 SDValue T = DAG.getConstant(f64val, MVT::i64);
Wesley Peck527da1b2010-11-23 03:31:01 +00001679 return DAG.getNode(ISD::BITCAST, dl, MVT::v2f64,
Owen Anderson9f944592009-08-11 20:47:22 +00001680 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64, T, T));
Scott Michel6e22c652007-12-04 22:23:35 +00001681 break;
1682 }
Owen Anderson9f944592009-08-11 20:47:22 +00001683 case MVT::v16i8: {
Scott Michel6e22c652007-12-04 22:23:35 +00001684 // 8-bit constants have to be expanded to 16-bits
Scott Michel839ad0a2009-03-17 01:15:45 +00001685 unsigned short Value16 = SplatBits /* | (SplatBits << 8) */;
1686 SmallVector<SDValue, 8> Ops;
1687
Owen Anderson9f944592009-08-11 20:47:22 +00001688 Ops.assign(8, DAG.getConstant(Value16, MVT::i16));
Wesley Peck527da1b2010-11-23 03:31:01 +00001689 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson9f944592009-08-11 20:47:22 +00001690 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i16, &Ops[0], Ops.size()));
Scott Michel6e22c652007-12-04 22:23:35 +00001691 }
Owen Anderson9f944592009-08-11 20:47:22 +00001692 case MVT::v8i16: {
Scott Michel839ad0a2009-03-17 01:15:45 +00001693 unsigned short Value16 = SplatBits;
1694 SDValue T = DAG.getConstant(Value16, EltVT);
1695 SmallVector<SDValue, 8> Ops;
1696
1697 Ops.assign(8, T);
1698 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Scott Michel6e22c652007-12-04 22:23:35 +00001699 }
Owen Anderson9f944592009-08-11 20:47:22 +00001700 case MVT::v4i32: {
Scott Michel839ad0a2009-03-17 01:15:45 +00001701 SDValue T = DAG.getConstant(unsigned(SplatBits), VT.getVectorElementType());
Evan Chenga49de9d2009-02-25 22:49:59 +00001702 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, T, T, T, T);
Scott Michel6e22c652007-12-04 22:23:35 +00001703 }
Owen Anderson9f944592009-08-11 20:47:22 +00001704 case MVT::v2i64: {
Scott Michel839ad0a2009-03-17 01:15:45 +00001705 return SPU::LowerV2I64Splat(VT, DAG, SplatBits, dl);
Scott Michel6e22c652007-12-04 22:23:35 +00001706 }
1707 }
Scott Michelfe095082008-07-16 17:17:29 +00001708
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001709 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001710}
1711
Scott Michel839ad0a2009-03-17 01:15:45 +00001712/*!
1713 */
Scott Michel9e3e4a92009-01-26 03:31:40 +00001714SDValue
Owen Anderson53aa7a92009-08-10 22:56:29 +00001715SPU::LowerV2I64Splat(EVT OpVT, SelectionDAG& DAG, uint64_t SplatVal,
Scott Michel839ad0a2009-03-17 01:15:45 +00001716 DebugLoc dl) {
Scott Michel9e3e4a92009-01-26 03:31:40 +00001717 uint32_t upper = uint32_t(SplatVal >> 32);
1718 uint32_t lower = uint32_t(SplatVal);
1719
1720 if (upper == lower) {
1721 // Magic constant that can be matched by IL, ILA, et. al.
Owen Anderson9f944592009-08-11 20:47:22 +00001722 SDValue Val = DAG.getTargetConstant(upper, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001723 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Owen Anderson9f944592009-08-11 20:47:22 +00001724 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga49de9d2009-02-25 22:49:59 +00001725 Val, Val, Val, Val));
Scott Michel9e3e4a92009-01-26 03:31:40 +00001726 } else {
Scott Michel9e3e4a92009-01-26 03:31:40 +00001727 bool upper_special, lower_special;
1728
1729 // NOTE: This code creates common-case shuffle masks that can be easily
1730 // detected as common expressions. It is not attempting to create highly
1731 // specialized masks to replace any and all 0's, 0xff's and 0x80's.
1732
1733 // Detect if the upper or lower half is a special shuffle mask pattern:
1734 upper_special = (upper == 0 || upper == 0xffffffff || upper == 0x80000000);
1735 lower_special = (lower == 0 || lower == 0xffffffff || lower == 0x80000000);
1736
Scott Michel839ad0a2009-03-17 01:15:45 +00001737 // Both upper and lower are special, lower to a constant pool load:
1738 if (lower_special && upper_special) {
Owen Anderson9f944592009-08-11 20:47:22 +00001739 SDValue SplatValCN = DAG.getConstant(SplatVal, MVT::i64);
1740 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
Scott Michel839ad0a2009-03-17 01:15:45 +00001741 SplatValCN, SplatValCN);
1742 }
1743
1744 SDValue LO32;
1745 SDValue HI32;
1746 SmallVector<SDValue, 16> ShufBytes;
1747 SDValue Result;
1748
Scott Michel9e3e4a92009-01-26 03:31:40 +00001749 // Create lower vector if not a special pattern
1750 if (!lower_special) {
Owen Anderson9f944592009-08-11 20:47:22 +00001751 SDValue LO32C = DAG.getConstant(lower, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001752 LO32 = DAG.getNode(ISD::BITCAST, dl, OpVT,
Owen Anderson9f944592009-08-11 20:47:22 +00001753 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga49de9d2009-02-25 22:49:59 +00001754 LO32C, LO32C, LO32C, LO32C));
Scott Michel9e3e4a92009-01-26 03:31:40 +00001755 }
1756
1757 // Create upper vector if not a special pattern
1758 if (!upper_special) {
Owen Anderson9f944592009-08-11 20:47:22 +00001759 SDValue HI32C = DAG.getConstant(upper, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001760 HI32 = DAG.getNode(ISD::BITCAST, dl, OpVT,
Owen Anderson9f944592009-08-11 20:47:22 +00001761 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga49de9d2009-02-25 22:49:59 +00001762 HI32C, HI32C, HI32C, HI32C));
Scott Michel9e3e4a92009-01-26 03:31:40 +00001763 }
1764
1765 // If either upper or lower are special, then the two input operands are
1766 // the same (basically, one of them is a "don't care")
1767 if (lower_special)
1768 LO32 = HI32;
1769 if (upper_special)
1770 HI32 = LO32;
Scott Michel9e3e4a92009-01-26 03:31:40 +00001771
1772 for (int i = 0; i < 4; ++i) {
1773 uint64_t val = 0;
1774 for (int j = 0; j < 4; ++j) {
1775 SDValue V;
1776 bool process_upper, process_lower;
1777 val <<= 8;
1778 process_upper = (upper_special && (i & 1) == 0);
1779 process_lower = (lower_special && (i & 1) == 1);
1780
1781 if (process_upper || process_lower) {
1782 if ((process_upper && upper == 0)
1783 || (process_lower && lower == 0))
1784 val |= 0x80;
1785 else if ((process_upper && upper == 0xffffffff)
1786 || (process_lower && lower == 0xffffffff))
1787 val |= 0xc0;
1788 else if ((process_upper && upper == 0x80000000)
1789 || (process_lower && lower == 0x80000000))
1790 val |= (j == 0 ? 0xe0 : 0x80);
1791 } else
1792 val |= i * 4 + j + ((i & 1) * 16);
1793 }
1794
Owen Anderson9f944592009-08-11 20:47:22 +00001795 ShufBytes.push_back(DAG.getConstant(val, MVT::i32));
Scott Michel9e3e4a92009-01-26 03:31:40 +00001796 }
1797
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001798 return DAG.getNode(SPUISD::SHUFB, dl, OpVT, HI32, LO32,
Owen Anderson9f944592009-08-11 20:47:22 +00001799 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga49de9d2009-02-25 22:49:59 +00001800 &ShufBytes[0], ShufBytes.size()));
Scott Michel9e3e4a92009-01-26 03:31:40 +00001801 }
1802}
1803
Scott Michel6e22c652007-12-04 22:23:35 +00001804/// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3) to something on
1805/// which the Cell can operate. The code inspects V3 to ascertain whether the
1806/// permutation vector, V3, is monotonically increasing with one "exception"
1807/// element, e.g., (0, 1, _, 3). If this is the case, then generate a
Scott Michel0be03392008-11-22 23:50:42 +00001808/// SHUFFLE_MASK synthetic instruction. Otherwise, spill V3 to the constant pool.
Scott Michel6e22c652007-12-04 22:23:35 +00001809/// In either case, the net result is going to eventually invoke SHUFB to
1810/// permute/shuffle the bytes from V1 and V2.
1811/// \note
Scott Michel0be03392008-11-22 23:50:42 +00001812/// SHUFFLE_MASK is eventually selected as one of the C*D instructions, generate
Scott Michel6e22c652007-12-04 22:23:35 +00001813/// control word for byte/halfword/word insertion. This takes care of a single
1814/// element move from V2 into V1.
1815/// \note
1816/// SPUISD::SHUFB is eventually selected as Cell's <i>shufb</i> instructions.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001817static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001818 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001819 SDValue V1 = Op.getOperand(0);
1820 SDValue V2 = Op.getOperand(1);
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001821 DebugLoc dl = Op.getDebugLoc();
Scott Michelfe095082008-07-16 17:17:29 +00001822
Scott Michel6e22c652007-12-04 22:23:35 +00001823 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfe095082008-07-16 17:17:29 +00001824
Scott Michel6e22c652007-12-04 22:23:35 +00001825 // If we have a single element being moved from V1 to V2, this can be handled
1826 // using the C*[DX] compute mask instructions, but the vector elements have
Kalle Raiskilae60b5162010-08-18 10:20:29 +00001827 // to be monotonically increasing with one exception element, and the source
1828 // slot of the element to move must be the same as the destination.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001829 EVT VecVT = V1.getValueType();
1830 EVT EltVT = VecVT.getVectorElementType();
Scott Michel6e22c652007-12-04 22:23:35 +00001831 unsigned EltsFromV2 = 0;
Kalle Raiskilae60b5162010-08-18 10:20:29 +00001832 unsigned V2EltOffset = 0;
Scott Michel6e22c652007-12-04 22:23:35 +00001833 unsigned V2EltIdx0 = 0;
1834 unsigned CurrElt = 0;
Scott Michelea3c49d2008-12-04 21:01:44 +00001835 unsigned MaxElts = VecVT.getVectorNumElements();
1836 unsigned PrevElt = 0;
Scott Michel6e22c652007-12-04 22:23:35 +00001837 bool monotonic = true;
Scott Michelea3c49d2008-12-04 21:01:44 +00001838 bool rotate = true;
Kalle Raiskila01cda2d2010-09-09 07:30:15 +00001839 int rotamt=0;
Kalle Raiskila6f581902010-06-21 10:17:36 +00001840 EVT maskVT; // which of the c?d instructions to use
Scott Michelea3c49d2008-12-04 21:01:44 +00001841
Owen Anderson9f944592009-08-11 20:47:22 +00001842 if (EltVT == MVT::i8) {
Scott Michel6e22c652007-12-04 22:23:35 +00001843 V2EltIdx0 = 16;
Wesley Peck527da1b2010-11-23 03:31:01 +00001844 maskVT = MVT::v16i8;
Owen Anderson9f944592009-08-11 20:47:22 +00001845 } else if (EltVT == MVT::i16) {
Scott Michel6e22c652007-12-04 22:23:35 +00001846 V2EltIdx0 = 8;
Kalle Raiskila6f581902010-06-21 10:17:36 +00001847 maskVT = MVT::v8i16;
Owen Anderson9f944592009-08-11 20:47:22 +00001848 } else if (EltVT == MVT::i32 || EltVT == MVT::f32) {
Scott Michel6e22c652007-12-04 22:23:35 +00001849 V2EltIdx0 = 4;
Kalle Raiskila6f581902010-06-21 10:17:36 +00001850 maskVT = MVT::v4i32;
Owen Anderson9f944592009-08-11 20:47:22 +00001851 } else if (EltVT == MVT::i64 || EltVT == MVT::f64) {
Scott Michelea3c49d2008-12-04 21:01:44 +00001852 V2EltIdx0 = 2;
Kalle Raiskila6f581902010-06-21 10:17:36 +00001853 maskVT = MVT::v2i64;
Scott Michelea3c49d2008-12-04 21:01:44 +00001854 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001855 llvm_unreachable("Unhandled vector type in LowerVECTOR_SHUFFLE");
Scott Michel6e22c652007-12-04 22:23:35 +00001856
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001857 for (unsigned i = 0; i != MaxElts; ++i) {
1858 if (SVN->getMaskElt(i) < 0)
1859 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00001860
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001861 unsigned SrcElt = SVN->getMaskElt(i);
Scott Michel6e22c652007-12-04 22:23:35 +00001862
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001863 if (monotonic) {
1864 if (SrcElt >= V2EltIdx0) {
Kalle Raiskilae60b5162010-08-18 10:20:29 +00001865 // TODO: optimize for the monotonic case when several consecutive
1866 // elements are taken form V2. Do we ever get such a case?
1867 if (EltsFromV2 == 0 && CurrElt == (SrcElt - V2EltIdx0))
1868 V2EltOffset = (SrcElt - V2EltIdx0) * (EltVT.getSizeInBits()/8);
1869 else
1870 monotonic = false;
1871 ++EltsFromV2;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001872 } else if (CurrElt != SrcElt) {
1873 monotonic = false;
Scott Michelea3c49d2008-12-04 21:01:44 +00001874 }
1875
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001876 ++CurrElt;
1877 }
1878
1879 if (rotate) {
1880 if (PrevElt > 0 && SrcElt < MaxElts) {
1881 if ((PrevElt == SrcElt - 1)
1882 || (PrevElt == MaxElts - 1 && SrcElt == 0)) {
Scott Michelea3c49d2008-12-04 21:01:44 +00001883 PrevElt = SrcElt;
1884 } else {
Scott Michelea3c49d2008-12-04 21:01:44 +00001885 rotate = false;
1886 }
Kalle Raiskilae54297282010-09-08 11:53:38 +00001887 } else if (i == 0 || (PrevElt==0 && SrcElt==1)) {
1888 // First time or after a "wrap around"
Kalle Raiskila77d11d02010-11-22 16:28:26 +00001889 rotamt = SrcElt-i;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001890 PrevElt = SrcElt;
1891 } else {
1892 // This isn't a rotation, takes elements from vector 2
1893 rotate = false;
Scott Michelea3c49d2008-12-04 21:01:44 +00001894 }
Scott Michel6e22c652007-12-04 22:23:35 +00001895 }
Scott Michel6e22c652007-12-04 22:23:35 +00001896 }
1897
1898 if (EltsFromV2 == 1 && monotonic) {
1899 // Compute mask and shuffle
Owen Anderson53aa7a92009-08-10 22:56:29 +00001900 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Kalle Raiskila6f581902010-06-21 10:17:36 +00001901
1902 // As SHUFFLE_MASK becomes a c?d instruction, feed it an address
1903 // R1 ($sp) is used here only as it is guaranteed to have last bits zero
1904 SDValue Pointer = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
1905 DAG.getRegister(SPU::R1, PtrVT),
Kalle Raiskilae60b5162010-08-18 10:20:29 +00001906 DAG.getConstant(V2EltOffset, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00001907 SDValue ShufMaskOp = DAG.getNode(SPUISD::SHUFFLE_MASK, dl,
Kalle Raiskila6f581902010-06-21 10:17:36 +00001908 maskVT, Pointer);
1909
Scott Michel6e22c652007-12-04 22:23:35 +00001910 // Use shuffle mask in SHUFB synthetic instruction:
Scott Micheld1db1ab2009-03-16 18:47:25 +00001911 return DAG.getNode(SPUISD::SHUFB, dl, V1.getValueType(), V2, V1,
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001912 ShufMaskOp);
Scott Michelea3c49d2008-12-04 21:01:44 +00001913 } else if (rotate) {
Kalle Raiskilae54297282010-09-08 11:53:38 +00001914 if (rotamt < 0)
1915 rotamt +=MaxElts;
1916 rotamt *= EltVT.getSizeInBits()/8;
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001917 return DAG.getNode(SPUISD::ROTBYTES_LEFT, dl, V1.getValueType(),
Owen Anderson9f944592009-08-11 20:47:22 +00001918 V1, DAG.getConstant(rotamt, MVT::i16));
Scott Michel6e22c652007-12-04 22:23:35 +00001919 } else {
Gabor Greif81d6a382008-08-31 15:37:04 +00001920 // Convert the SHUFFLE_VECTOR mask's input element units to the
1921 // actual bytes.
Duncan Sands13237ac2008-06-06 12:08:01 +00001922 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfe095082008-07-16 17:17:29 +00001923
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001924 SmallVector<SDValue, 16> ResultMask;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001925 for (unsigned i = 0, e = MaxElts; i != e; ++i) {
1926 unsigned SrcElt = SVN->getMaskElt(i) < 0 ? 0 : SVN->getMaskElt(i);
Scott Michelfe095082008-07-16 17:17:29 +00001927
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001928 for (unsigned j = 0; j < BytesPerElement; ++j)
Owen Anderson9f944592009-08-11 20:47:22 +00001929 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,MVT::i8));
Scott Michel6e22c652007-12-04 22:23:35 +00001930 }
Owen Anderson9f944592009-08-11 20:47:22 +00001931 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga49de9d2009-02-25 22:49:59 +00001932 &ResultMask[0], ResultMask.size());
Dale Johannesenf08a47b2009-02-04 23:02:30 +00001933 return DAG.getNode(SPUISD::SHUFB, dl, V1.getValueType(), V1, V2, VPermMask);
Scott Michel6e22c652007-12-04 22:23:35 +00001934 }
1935}
1936
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001937static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
1938 SDValue Op0 = Op.getOperand(0); // Op0 = the scalar
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001939 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00001940
Gabor Greiff304a7a2008-08-28 21:40:38 +00001941 if (Op0.getNode()->getOpcode() == ISD::Constant) {
Scott Michel6e22c652007-12-04 22:23:35 +00001942 // For a constant, build the appropriate constant vector, which will
1943 // eventually simplify to a vector register load.
1944
Gabor Greiff304a7a2008-08-28 21:40:38 +00001945 ConstantSDNode *CN = cast<ConstantSDNode>(Op0.getNode());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001946 SmallVector<SDValue, 16> ConstVecValues;
Owen Anderson53aa7a92009-08-10 22:56:29 +00001947 EVT VT;
Scott Michel6e22c652007-12-04 22:23:35 +00001948 size_t n_copies;
1949
1950 // Create a constant vector:
Owen Anderson9f944592009-08-11 20:47:22 +00001951 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001952 default: llvm_unreachable("Unexpected constant value type in "
Torok Edwin6cdb8972009-07-14 12:22:58 +00001953 "LowerSCALAR_TO_VECTOR");
Owen Anderson9f944592009-08-11 20:47:22 +00001954 case MVT::v16i8: n_copies = 16; VT = MVT::i8; break;
1955 case MVT::v8i16: n_copies = 8; VT = MVT::i16; break;
1956 case MVT::v4i32: n_copies = 4; VT = MVT::i32; break;
1957 case MVT::v4f32: n_copies = 4; VT = MVT::f32; break;
1958 case MVT::v2i64: n_copies = 2; VT = MVT::i64; break;
1959 case MVT::v2f64: n_copies = 2; VT = MVT::f64; break;
Scott Michel6e22c652007-12-04 22:23:35 +00001960 }
1961
Dan Gohmaneffb8942008-09-12 16:56:44 +00001962 SDValue CValue = DAG.getConstant(CN->getZExtValue(), VT);
Scott Michel6e22c652007-12-04 22:23:35 +00001963 for (size_t j = 0; j < n_copies; ++j)
1964 ConstVecValues.push_back(CValue);
1965
Evan Chenga49de9d2009-02-25 22:49:59 +00001966 return DAG.getNode(ISD::BUILD_VECTOR, dl, Op.getValueType(),
1967 &ConstVecValues[0], ConstVecValues.size());
Scott Michel6e22c652007-12-04 22:23:35 +00001968 } else {
1969 // Otherwise, copy the value from one register to another:
Owen Anderson9f944592009-08-11 20:47:22 +00001970 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001971 default: llvm_unreachable("Unexpected value type in LowerSCALAR_TO_VECTOR");
Owen Anderson9f944592009-08-11 20:47:22 +00001972 case MVT::i8:
1973 case MVT::i16:
1974 case MVT::i32:
1975 case MVT::i64:
1976 case MVT::f32:
1977 case MVT::f64:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001978 return DAG.getNode(SPUISD::PREFSLOT2VEC, dl, Op.getValueType(), Op0, Op0);
Scott Michel6e22c652007-12-04 22:23:35 +00001979 }
1980 }
1981
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001982 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001983}
1984
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001985static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001986 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001987 SDValue N = Op.getOperand(0);
1988 SDValue Elt = Op.getOperand(1);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00001989 DebugLoc dl = Op.getDebugLoc();
Scott Michel0be03392008-11-22 23:50:42 +00001990 SDValue retval;
Scott Michel6e22c652007-12-04 22:23:35 +00001991
Scott Michel0be03392008-11-22 23:50:42 +00001992 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
1993 // Constant argument:
1994 int EltNo = (int) C->getZExtValue();
Scott Michel6e22c652007-12-04 22:23:35 +00001995
Scott Michel0be03392008-11-22 23:50:42 +00001996 // sanity checks:
Owen Anderson9f944592009-08-11 20:47:22 +00001997 if (VT == MVT::i8 && EltNo >= 16)
Torok Edwinfbcc6632009-07-14 16:55:14 +00001998 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i8 extraction slot > 15");
Owen Anderson9f944592009-08-11 20:47:22 +00001999 else if (VT == MVT::i16 && EltNo >= 8)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002000 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i16 extraction slot > 7");
Owen Anderson9f944592009-08-11 20:47:22 +00002001 else if (VT == MVT::i32 && EltNo >= 4)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002002 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i32 extraction slot > 4");
Owen Anderson9f944592009-08-11 20:47:22 +00002003 else if (VT == MVT::i64 && EltNo >= 2)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002004 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i64 extraction slot > 2");
Scott Michel6e22c652007-12-04 22:23:35 +00002005
Owen Anderson9f944592009-08-11 20:47:22 +00002006 if (EltNo == 0 && (VT == MVT::i32 || VT == MVT::i64)) {
Scott Michel0be03392008-11-22 23:50:42 +00002007 // i32 and i64: Element 0 is the preferred slot
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002008 return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT, N);
Scott Michel0be03392008-11-22 23:50:42 +00002009 }
Scott Michel6e22c652007-12-04 22:23:35 +00002010
Scott Michel0be03392008-11-22 23:50:42 +00002011 // Need to generate shuffle mask and extract:
2012 int prefslot_begin = -1, prefslot_end = -1;
2013 int elt_byte = EltNo * VT.getSizeInBits() / 8;
2014
Owen Anderson9f944592009-08-11 20:47:22 +00002015 switch (VT.getSimpleVT().SimpleTy) {
Scott Michel0be03392008-11-22 23:50:42 +00002016 default:
2017 assert(false && "Invalid value type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002018 case MVT::i8: {
Scott Michel0be03392008-11-22 23:50:42 +00002019 prefslot_begin = prefslot_end = 3;
2020 break;
2021 }
Owen Anderson9f944592009-08-11 20:47:22 +00002022 case MVT::i16: {
Scott Michel0be03392008-11-22 23:50:42 +00002023 prefslot_begin = 2; prefslot_end = 3;
2024 break;
2025 }
Owen Anderson9f944592009-08-11 20:47:22 +00002026 case MVT::i32:
2027 case MVT::f32: {
Scott Michel0be03392008-11-22 23:50:42 +00002028 prefslot_begin = 0; prefslot_end = 3;
2029 break;
2030 }
Owen Anderson9f944592009-08-11 20:47:22 +00002031 case MVT::i64:
2032 case MVT::f64: {
Scott Michel0be03392008-11-22 23:50:42 +00002033 prefslot_begin = 0; prefslot_end = 7;
2034 break;
2035 }
2036 }
2037
2038 assert(prefslot_begin != -1 && prefslot_end != -1 &&
2039 "LowerEXTRACT_VECTOR_ELT: preferred slots uninitialized");
2040
Scott Michelb54075e2009-08-24 21:53:27 +00002041 unsigned int ShufBytes[16] = {
2042 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2043 };
Scott Michel0be03392008-11-22 23:50:42 +00002044 for (int i = 0; i < 16; ++i) {
2045 // zero fill uppper part of preferred slot, don't care about the
2046 // other slots:
2047 unsigned int mask_val;
2048 if (i <= prefslot_end) {
2049 mask_val =
2050 ((i < prefslot_begin)
2051 ? 0x80
2052 : elt_byte + (i - prefslot_begin));
2053
2054 ShufBytes[i] = mask_val;
2055 } else
2056 ShufBytes[i] = ShufBytes[i % (prefslot_end + 1)];
2057 }
2058
2059 SDValue ShufMask[4];
2060 for (unsigned i = 0; i < sizeof(ShufMask)/sizeof(ShufMask[0]); ++i) {
Scott Michelea3c49d2008-12-04 21:01:44 +00002061 unsigned bidx = i * 4;
Scott Michel0be03392008-11-22 23:50:42 +00002062 unsigned int bits = ((ShufBytes[bidx] << 24) |
2063 (ShufBytes[bidx+1] << 16) |
2064 (ShufBytes[bidx+2] << 8) |
2065 ShufBytes[bidx+3]);
Owen Anderson9f944592009-08-11 20:47:22 +00002066 ShufMask[i] = DAG.getConstant(bits, MVT::i32);
Scott Michel0be03392008-11-22 23:50:42 +00002067 }
2068
Scott Michel839ad0a2009-03-17 01:15:45 +00002069 SDValue ShufMaskVec =
Owen Anderson9f944592009-08-11 20:47:22 +00002070 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel839ad0a2009-03-17 01:15:45 +00002071 &ShufMask[0], sizeof(ShufMask)/sizeof(ShufMask[0]));
Scott Michel0be03392008-11-22 23:50:42 +00002072
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002073 retval = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
2074 DAG.getNode(SPUISD::SHUFB, dl, N.getValueType(),
Scott Michel0be03392008-11-22 23:50:42 +00002075 N, N, ShufMaskVec));
2076 } else {
2077 // Variable index: Rotate the requested element into slot 0, then replicate
2078 // slot 0 across the vector
Owen Anderson53aa7a92009-08-10 22:56:29 +00002079 EVT VecVT = N.getValueType();
Kalle Raiskila622f8eb2010-08-02 08:54:39 +00002080 if (!VecVT.isSimple() || !VecVT.isVector()) {
Chris Lattner2104b8d2010-04-07 22:58:41 +00002081 report_fatal_error("LowerEXTRACT_VECTOR_ELT: Must have a simple, 128-bit"
Torok Edwinfb8d6d52009-07-08 20:53:28 +00002082 "vector type!");
Scott Michel0be03392008-11-22 23:50:42 +00002083 }
2084
2085 // Make life easier by making sure the index is zero-extended to i32
Owen Anderson9f944592009-08-11 20:47:22 +00002086 if (Elt.getValueType() != MVT::i32)
2087 Elt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Elt);
Scott Michel0be03392008-11-22 23:50:42 +00002088
2089 // Scale the index to a bit/byte shift quantity
2090 APInt scaleFactor =
Scott Michelefc8c7a2008-11-24 17:11:17 +00002091 APInt(32, uint64_t(16 / N.getValueType().getVectorNumElements()), false);
2092 unsigned scaleShift = scaleFactor.logBase2();
Scott Michel0be03392008-11-22 23:50:42 +00002093 SDValue vecShift;
Scott Michel0be03392008-11-22 23:50:42 +00002094
Scott Michelefc8c7a2008-11-24 17:11:17 +00002095 if (scaleShift > 0) {
2096 // Scale the shift factor:
Owen Anderson9f944592009-08-11 20:47:22 +00002097 Elt = DAG.getNode(ISD::SHL, dl, MVT::i32, Elt,
2098 DAG.getConstant(scaleShift, MVT::i32));
Scott Michel0be03392008-11-22 23:50:42 +00002099 }
2100
Kalle Raiskila0a9dd402010-11-12 10:14:03 +00002101 vecShift = DAG.getNode(SPUISD::SHL_BYTES, dl, VecVT, N, Elt);
Scott Michelefc8c7a2008-11-24 17:11:17 +00002102
2103 // Replicate the bytes starting at byte 0 across the entire vector (for
2104 // consistency with the notion of a unified register set)
Scott Michel0be03392008-11-22 23:50:42 +00002105 SDValue replicate;
2106
Owen Anderson9f944592009-08-11 20:47:22 +00002107 switch (VT.getSimpleVT().SimpleTy) {
Scott Michel0be03392008-11-22 23:50:42 +00002108 default:
Chris Lattner2104b8d2010-04-07 22:58:41 +00002109 report_fatal_error("LowerEXTRACT_VECTOR_ELT(varable): Unhandled vector"
Torok Edwinfb8d6d52009-07-08 20:53:28 +00002110 "type");
Scott Michel0be03392008-11-22 23:50:42 +00002111 /*NOTREACHED*/
Owen Anderson9f944592009-08-11 20:47:22 +00002112 case MVT::i8: {
2113 SDValue factor = DAG.getConstant(0x00000000, MVT::i32);
2114 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel839ad0a2009-03-17 01:15:45 +00002115 factor, factor, factor, factor);
Scott Michel0be03392008-11-22 23:50:42 +00002116 break;
2117 }
Owen Anderson9f944592009-08-11 20:47:22 +00002118 case MVT::i16: {
2119 SDValue factor = DAG.getConstant(0x00010001, MVT::i32);
2120 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel839ad0a2009-03-17 01:15:45 +00002121 factor, factor, factor, factor);
Scott Michel0be03392008-11-22 23:50:42 +00002122 break;
2123 }
Owen Anderson9f944592009-08-11 20:47:22 +00002124 case MVT::i32:
2125 case MVT::f32: {
2126 SDValue factor = DAG.getConstant(0x00010203, MVT::i32);
2127 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel839ad0a2009-03-17 01:15:45 +00002128 factor, factor, factor, factor);
Scott Michel0be03392008-11-22 23:50:42 +00002129 break;
2130 }
Owen Anderson9f944592009-08-11 20:47:22 +00002131 case MVT::i64:
2132 case MVT::f64: {
2133 SDValue loFactor = DAG.getConstant(0x00010203, MVT::i32);
2134 SDValue hiFactor = DAG.getConstant(0x04050607, MVT::i32);
2135 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga49de9d2009-02-25 22:49:59 +00002136 loFactor, hiFactor, loFactor, hiFactor);
Scott Michel0be03392008-11-22 23:50:42 +00002137 break;
2138 }
2139 }
2140
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002141 retval = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
2142 DAG.getNode(SPUISD::SHUFB, dl, VecVT,
Scott Michel08a4e202008-12-01 17:56:02 +00002143 vecShift, vecShift, replicate));
Scott Michel6e22c652007-12-04 22:23:35 +00002144 }
2145
Scott Michel0be03392008-11-22 23:50:42 +00002146 return retval;
Scott Michel6e22c652007-12-04 22:23:35 +00002147}
2148
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002149static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
2150 SDValue VecOp = Op.getOperand(0);
2151 SDValue ValOp = Op.getOperand(1);
2152 SDValue IdxOp = Op.getOperand(2);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002153 DebugLoc dl = Op.getDebugLoc();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002154 EVT VT = Op.getValueType();
Kalle Raiskila1e616572010-08-29 12:41:50 +00002155 EVT eltVT = ValOp.getValueType();
Scott Michel6e22c652007-12-04 22:23:35 +00002156
Kalle Raiskila5e0862f2010-06-09 09:58:17 +00002157 // use 0 when the lane to insert to is 'undef'
Kalle Raiskila1e616572010-08-29 12:41:50 +00002158 int64_t Offset=0;
Kalle Raiskila5e0862f2010-06-09 09:58:17 +00002159 if (IdxOp.getOpcode() != ISD::UNDEF) {
2160 ConstantSDNode *CN = cast<ConstantSDNode>(IdxOp);
2161 assert(CN != 0 && "LowerINSERT_VECTOR_ELT: Index is not constant!");
Kalle Raiskila1e616572010-08-29 12:41:50 +00002162 Offset = (CN->getSExtValue()) * eltVT.getSizeInBits()/8;
Kalle Raiskila5e0862f2010-06-09 09:58:17 +00002163 }
Scott Michel6e22c652007-12-04 22:23:35 +00002164
Owen Anderson53aa7a92009-08-10 22:56:29 +00002165 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel08a4e202008-12-01 17:56:02 +00002166 // Use $sp ($1) because it's always 16-byte aligned and it's available:
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002167 SDValue Pointer = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel08a4e202008-12-01 17:56:02 +00002168 DAG.getRegister(SPU::R1, PtrVT),
Kalle Raiskila1e616572010-08-29 12:41:50 +00002169 DAG.getConstant(Offset, PtrVT));
Kalle Raiskila8b2f7012010-08-04 13:59:48 +00002170 // widen the mask when dealing with half vectors
Wesley Peck527da1b2010-11-23 03:31:01 +00002171 EVT maskVT = EVT::getVectorVT(*(DAG.getContext()), VT.getVectorElementType(),
Kalle Raiskila8b2f7012010-08-04 13:59:48 +00002172 128/ VT.getVectorElementType().getSizeInBits());
2173 SDValue ShufMask = DAG.getNode(SPUISD::SHUFFLE_MASK, dl, maskVT, Pointer);
Scott Michel6e22c652007-12-04 22:23:35 +00002174
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002175 SDValue result =
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002176 DAG.getNode(SPUISD::SHUFB, dl, VT,
2177 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, ValOp),
Scott Michelb8ee30d2008-12-29 03:23:36 +00002178 VecOp,
Wesley Peck527da1b2010-11-23 03:31:01 +00002179 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ShufMask));
Scott Michel6e22c652007-12-04 22:23:35 +00002180
2181 return result;
2182}
2183
Scott Michel82335272008-12-27 04:51:36 +00002184static SDValue LowerI8Math(SDValue Op, SelectionDAG &DAG, unsigned Opc,
2185 const TargetLowering &TLI)
Scott Michel7d5eaec2008-02-23 18:41:37 +00002186{
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002187 SDValue N0 = Op.getOperand(0); // Everything has at least one operand
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002188 DebugLoc dl = Op.getDebugLoc();
Owen Andersonb2c80da2011-02-25 21:41:48 +00002189 EVT ShiftVT = TLI.getShiftAmountTy(N0.getValueType());
Scott Michel6e22c652007-12-04 22:23:35 +00002190
Owen Anderson9f944592009-08-11 20:47:22 +00002191 assert(Op.getValueType() == MVT::i8);
Scott Michel6e22c652007-12-04 22:23:35 +00002192 switch (Opc) {
2193 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002194 llvm_unreachable("Unhandled i8 math operator");
Scott Michel6e22c652007-12-04 22:23:35 +00002195 /*NOTREACHED*/
2196 break;
Scott Michel41236c02008-12-30 23:28:25 +00002197 case ISD::ADD: {
2198 // 8-bit addition: Promote the arguments up to 16-bits and truncate
2199 // the result:
2200 SDValue N1 = Op.getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00002201 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
2202 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
2203 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2204 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel41236c02008-12-30 23:28:25 +00002205
2206 }
2207
Scott Michel6e22c652007-12-04 22:23:35 +00002208 case ISD::SUB: {
2209 // 8-bit subtraction: Promote the arguments up to 16-bits and truncate
2210 // the result:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002211 SDValue N1 = Op.getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00002212 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
2213 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
2214 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2215 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michelfe095082008-07-16 17:17:29 +00002216 }
Scott Michel6e22c652007-12-04 22:23:35 +00002217 case ISD::ROTR:
2218 case ISD::ROTL: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002219 SDValue N1 = Op.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002220 EVT N1VT = N1.getValueType();
Scott Michel839ad0a2009-03-17 01:15:45 +00002221
Owen Anderson9f944592009-08-11 20:47:22 +00002222 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, N0);
Scott Michel839ad0a2009-03-17 01:15:45 +00002223 if (!N1VT.bitsEq(ShiftVT)) {
2224 unsigned N1Opc = N1.getValueType().bitsLT(ShiftVT)
2225 ? ISD::ZERO_EXTEND
2226 : ISD::TRUNCATE;
2227 N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
2228 }
2229
2230 // Replicate lower 8-bits into upper 8:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002231 SDValue ExpandArg =
Owen Anderson9f944592009-08-11 20:47:22 +00002232 DAG.getNode(ISD::OR, dl, MVT::i16, N0,
2233 DAG.getNode(ISD::SHL, dl, MVT::i16,
2234 N0, DAG.getConstant(8, MVT::i32)));
Scott Michel839ad0a2009-03-17 01:15:45 +00002235
2236 // Truncate back down to i8
Owen Anderson9f944592009-08-11 20:47:22 +00002237 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2238 DAG.getNode(Opc, dl, MVT::i16, ExpandArg, N1));
Scott Michel6e22c652007-12-04 22:23:35 +00002239 }
2240 case ISD::SRL:
2241 case ISD::SHL: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002242 SDValue N1 = Op.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002243 EVT N1VT = N1.getValueType();
Scott Michel839ad0a2009-03-17 01:15:45 +00002244
Owen Anderson9f944592009-08-11 20:47:22 +00002245 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, N0);
Scott Michel839ad0a2009-03-17 01:15:45 +00002246 if (!N1VT.bitsEq(ShiftVT)) {
2247 unsigned N1Opc = ISD::ZERO_EXTEND;
2248
2249 if (N1.getValueType().bitsGT(ShiftVT))
2250 N1Opc = ISD::TRUNCATE;
2251
2252 N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
2253 }
2254
Owen Anderson9f944592009-08-11 20:47:22 +00002255 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2256 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel6e22c652007-12-04 22:23:35 +00002257 }
2258 case ISD::SRA: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002259 SDValue N1 = Op.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002260 EVT N1VT = N1.getValueType();
Scott Michel839ad0a2009-03-17 01:15:45 +00002261
Owen Anderson9f944592009-08-11 20:47:22 +00002262 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
Scott Michel839ad0a2009-03-17 01:15:45 +00002263 if (!N1VT.bitsEq(ShiftVT)) {
2264 unsigned N1Opc = ISD::SIGN_EXTEND;
2265
2266 if (N1VT.bitsGT(ShiftVT))
2267 N1Opc = ISD::TRUNCATE;
2268 N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
2269 }
2270
Owen Anderson9f944592009-08-11 20:47:22 +00002271 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2272 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel6e22c652007-12-04 22:23:35 +00002273 }
2274 case ISD::MUL: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002275 SDValue N1 = Op.getOperand(1);
Scott Michel839ad0a2009-03-17 01:15:45 +00002276
Owen Anderson9f944592009-08-11 20:47:22 +00002277 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
2278 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
2279 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2280 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel6e22c652007-12-04 22:23:35 +00002281 break;
2282 }
2283 }
2284
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002285 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00002286}
2287
2288//! Lower byte immediate operations for v16i8 vectors:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002289static SDValue
2290LowerByteImmed(SDValue Op, SelectionDAG &DAG) {
2291 SDValue ConstVec;
2292 SDValue Arg;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002293 EVT VT = Op.getValueType();
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002294 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00002295
2296 ConstVec = Op.getOperand(0);
2297 Arg = Op.getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002298 if (ConstVec.getNode()->getOpcode() != ISD::BUILD_VECTOR) {
Wesley Peck527da1b2010-11-23 03:31:01 +00002299 if (ConstVec.getNode()->getOpcode() == ISD::BITCAST) {
Scott Michel6e22c652007-12-04 22:23:35 +00002300 ConstVec = ConstVec.getOperand(0);
2301 } else {
2302 ConstVec = Op.getOperand(1);
2303 Arg = Op.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00002304 if (ConstVec.getNode()->getOpcode() == ISD::BITCAST) {
Scott Michelbb713ae2008-01-30 02:55:46 +00002305 ConstVec = ConstVec.getOperand(0);
Scott Michel6e22c652007-12-04 22:23:35 +00002306 }
2307 }
2308 }
2309
Gabor Greiff304a7a2008-08-28 21:40:38 +00002310 if (ConstVec.getNode()->getOpcode() == ISD::BUILD_VECTOR) {
Scott Michel839ad0a2009-03-17 01:15:45 +00002311 BuildVectorSDNode *BCN = dyn_cast<BuildVectorSDNode>(ConstVec.getNode());
2312 assert(BCN != 0 && "Expected BuildVectorSDNode in SPU LowerByteImmed");
Scott Michel6e22c652007-12-04 22:23:35 +00002313
Scott Michel839ad0a2009-03-17 01:15:45 +00002314 APInt APSplatBits, APSplatUndef;
2315 unsigned SplatBitSize;
2316 bool HasAnyUndefs;
2317 unsigned minSplatBits = VT.getVectorElementType().getSizeInBits();
2318
2319 if (BCN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2320 HasAnyUndefs, minSplatBits)
2321 && minSplatBits <= SplatBitSize) {
2322 uint64_t SplatBits = APSplatBits.getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00002323 SDValue tc = DAG.getTargetConstant(SplatBits & 0xff, MVT::i8);
Scott Michel6e22c652007-12-04 22:23:35 +00002324
Scott Michel839ad0a2009-03-17 01:15:45 +00002325 SmallVector<SDValue, 16> tcVec;
2326 tcVec.assign(16, tc);
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002327 return DAG.getNode(Op.getNode()->getOpcode(), dl, VT, Arg,
Scott Michel839ad0a2009-03-17 01:15:45 +00002328 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &tcVec[0], tcVec.size()));
Scott Michel6e22c652007-12-04 22:23:35 +00002329 }
2330 }
Scott Michel49483182009-01-26 22:33:37 +00002331
Nate Begeman82f19252008-07-29 19:07:27 +00002332 // These operations (AND, OR, XOR) are legal, they just couldn't be custom
2333 // lowered. Return the operation, rather than a null SDValue.
2334 return Op;
Scott Michel6e22c652007-12-04 22:23:35 +00002335}
2336
Scott Michel6e22c652007-12-04 22:23:35 +00002337//! Custom lowering for CTPOP (count population)
2338/*!
2339 Custom lowering code that counts the number ones in the input
2340 operand. SPU has such an instruction, but it counts the number of
2341 ones per byte, which then have to be accumulated.
2342*/
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002343static SDValue LowerCTPOP(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002344 EVT VT = Op.getValueType();
Wesley Peck527da1b2010-11-23 03:31:01 +00002345 EVT vecVT = EVT::getVectorVT(*DAG.getContext(),
Owen Anderson117c9e82009-08-12 00:36:31 +00002346 VT, (128 / VT.getSizeInBits()));
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002347 DebugLoc dl = Op.getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00002348
Owen Anderson9f944592009-08-11 20:47:22 +00002349 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands13237ac2008-06-06 12:08:01 +00002350 default:
2351 assert(false && "Invalid value type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002352 case MVT::i8: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002353 SDValue N = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00002354 SDValue Elt0 = DAG.getConstant(0, MVT::i32);
Scott Michel6e22c652007-12-04 22:23:35 +00002355
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002356 SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
2357 SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +00002358
Owen Anderson9f944592009-08-11 20:47:22 +00002359 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i8, CNTB, Elt0);
Scott Michel6e22c652007-12-04 22:23:35 +00002360 }
2361
Owen Anderson9f944592009-08-11 20:47:22 +00002362 case MVT::i16: {
Scott Michel6e22c652007-12-04 22:23:35 +00002363 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnera10fff52007-12-31 04:13:23 +00002364 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel6e22c652007-12-04 22:23:35 +00002365
Chris Lattnera10fff52007-12-31 04:13:23 +00002366 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R16CRegClass);
Scott Michel6e22c652007-12-04 22:23:35 +00002367
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002368 SDValue N = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00002369 SDValue Elt0 = DAG.getConstant(0, MVT::i16);
2370 SDValue Mask0 = DAG.getConstant(0x0f, MVT::i16);
2371 SDValue Shift1 = DAG.getConstant(8, MVT::i32);
Scott Michel6e22c652007-12-04 22:23:35 +00002372
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002373 SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
2374 SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +00002375
2376 // CNTB_result becomes the chain to which all of the virtual registers
2377 // CNTB_reg, SUM1_reg become associated:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002378 SDValue CNTB_result =
Owen Anderson9f944592009-08-11 20:47:22 +00002379 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, CNTB, Elt0);
Scott Michelfe095082008-07-16 17:17:29 +00002380
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002381 SDValue CNTB_rescopy =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002382 DAG.getCopyToReg(CNTB_result, dl, CNTB_reg, CNTB_result);
Scott Michel6e22c652007-12-04 22:23:35 +00002383
Owen Anderson9f944592009-08-11 20:47:22 +00002384 SDValue Tmp1 = DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i16);
Scott Michel6e22c652007-12-04 22:23:35 +00002385
Owen Anderson9f944592009-08-11 20:47:22 +00002386 return DAG.getNode(ISD::AND, dl, MVT::i16,
2387 DAG.getNode(ISD::ADD, dl, MVT::i16,
2388 DAG.getNode(ISD::SRL, dl, MVT::i16,
Scott Michelbb713ae2008-01-30 02:55:46 +00002389 Tmp1, Shift1),
2390 Tmp1),
2391 Mask0);
Scott Michel6e22c652007-12-04 22:23:35 +00002392 }
2393
Owen Anderson9f944592009-08-11 20:47:22 +00002394 case MVT::i32: {
Scott Michel6e22c652007-12-04 22:23:35 +00002395 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnera10fff52007-12-31 04:13:23 +00002396 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel6e22c652007-12-04 22:23:35 +00002397
Chris Lattnera10fff52007-12-31 04:13:23 +00002398 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
2399 unsigned SUM1_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
Scott Michel6e22c652007-12-04 22:23:35 +00002400
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002401 SDValue N = Op.getOperand(0);
Owen Anderson9f944592009-08-11 20:47:22 +00002402 SDValue Elt0 = DAG.getConstant(0, MVT::i32);
2403 SDValue Mask0 = DAG.getConstant(0xff, MVT::i32);
2404 SDValue Shift1 = DAG.getConstant(16, MVT::i32);
2405 SDValue Shift2 = DAG.getConstant(8, MVT::i32);
Scott Michel6e22c652007-12-04 22:23:35 +00002406
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002407 SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
2408 SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
Scott Michel6e22c652007-12-04 22:23:35 +00002409
2410 // CNTB_result becomes the chain to which all of the virtual registers
2411 // CNTB_reg, SUM1_reg become associated:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002412 SDValue CNTB_result =
Owen Anderson9f944592009-08-11 20:47:22 +00002413 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, CNTB, Elt0);
Scott Michelfe095082008-07-16 17:17:29 +00002414
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002415 SDValue CNTB_rescopy =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002416 DAG.getCopyToReg(CNTB_result, dl, CNTB_reg, CNTB_result);
Scott Michel6e22c652007-12-04 22:23:35 +00002417
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002418 SDValue Comp1 =
Owen Anderson9f944592009-08-11 20:47:22 +00002419 DAG.getNode(ISD::SRL, dl, MVT::i32,
2420 DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i32),
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002421 Shift1);
Scott Michel6e22c652007-12-04 22:23:35 +00002422
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002423 SDValue Sum1 =
Owen Anderson9f944592009-08-11 20:47:22 +00002424 DAG.getNode(ISD::ADD, dl, MVT::i32, Comp1,
2425 DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i32));
Scott Michel6e22c652007-12-04 22:23:35 +00002426
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002427 SDValue Sum1_rescopy =
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002428 DAG.getCopyToReg(CNTB_result, dl, SUM1_reg, Sum1);
Scott Michel6e22c652007-12-04 22:23:35 +00002429
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002430 SDValue Comp2 =
Owen Anderson9f944592009-08-11 20:47:22 +00002431 DAG.getNode(ISD::SRL, dl, MVT::i32,
2432 DAG.getCopyFromReg(Sum1_rescopy, dl, SUM1_reg, MVT::i32),
Scott Michelbb713ae2008-01-30 02:55:46 +00002433 Shift2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002434 SDValue Sum2 =
Owen Anderson9f944592009-08-11 20:47:22 +00002435 DAG.getNode(ISD::ADD, dl, MVT::i32, Comp2,
2436 DAG.getCopyFromReg(Sum1_rescopy, dl, SUM1_reg, MVT::i32));
Scott Michel6e22c652007-12-04 22:23:35 +00002437
Owen Anderson9f944592009-08-11 20:47:22 +00002438 return DAG.getNode(ISD::AND, dl, MVT::i32, Sum2, Mask0);
Scott Michel6e22c652007-12-04 22:23:35 +00002439 }
2440
Owen Anderson9f944592009-08-11 20:47:22 +00002441 case MVT::i64:
Scott Michel6e22c652007-12-04 22:23:35 +00002442 break;
2443 }
2444
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002445 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00002446}
2447
Scott Michel9e3e4a92009-01-26 03:31:40 +00002448//! Lower ISD::FP_TO_SINT, ISD::FP_TO_UINT for i32
Scott Micheled7d79f2009-01-21 04:58:48 +00002449/*!
Scott Michel9e3e4a92009-01-26 03:31:40 +00002450 f32->i32 passes through unchanged, whereas f64->i32 expands to a libcall.
2451 All conversions to i64 are expanded to a libcall.
Scott Micheled7d79f2009-01-21 04:58:48 +00002452 */
Scott Michel9e3e4a92009-01-26 03:31:40 +00002453static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002454 const SPUTargetLowering &TLI) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002455 EVT OpVT = Op.getValueType();
Scott Micheled7d79f2009-01-21 04:58:48 +00002456 SDValue Op0 = Op.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002457 EVT Op0VT = Op0.getValueType();
Scott Micheled7d79f2009-01-21 04:58:48 +00002458
Owen Anderson9f944592009-08-11 20:47:22 +00002459 if ((OpVT == MVT::i32 && Op0VT == MVT::f64)
2460 || OpVT == MVT::i64) {
Scott Michel9e3e4a92009-01-26 03:31:40 +00002461 // Convert f32 / f64 to i32 / i64 via libcall.
2462 RTLIB::Libcall LC =
2463 (Op.getOpcode() == ISD::FP_TO_SINT)
2464 ? RTLIB::getFPTOSINT(Op0VT, OpVT)
2465 : RTLIB::getFPTOUINT(Op0VT, OpVT);
2466 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
2467 SDValue Dummy;
2468 return ExpandLibCall(LC, Op, DAG, false, Dummy, TLI);
2469 }
Scott Micheled7d79f2009-01-21 04:58:48 +00002470
Eli Friedmanacb851a2009-05-27 00:47:34 +00002471 return Op;
Scott Michel9e3e4a92009-01-26 03:31:40 +00002472}
Scott Micheled7d79f2009-01-21 04:58:48 +00002473
Scott Michel9e3e4a92009-01-26 03:31:40 +00002474//! Lower ISD::SINT_TO_FP, ISD::UINT_TO_FP for i32
2475/*!
2476 i32->f32 passes through unchanged, whereas i32->f64 is expanded to a libcall.
2477 All conversions from i64 are expanded to a libcall.
2478 */
2479static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002480 const SPUTargetLowering &TLI) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002481 EVT OpVT = Op.getValueType();
Scott Michel9e3e4a92009-01-26 03:31:40 +00002482 SDValue Op0 = Op.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002483 EVT Op0VT = Op0.getValueType();
Scott Michel9e3e4a92009-01-26 03:31:40 +00002484
Owen Anderson9f944592009-08-11 20:47:22 +00002485 if ((OpVT == MVT::f64 && Op0VT == MVT::i32)
2486 || Op0VT == MVT::i64) {
Scott Michel9e3e4a92009-01-26 03:31:40 +00002487 // Convert i32, i64 to f64 via libcall:
2488 RTLIB::Libcall LC =
2489 (Op.getOpcode() == ISD::SINT_TO_FP)
2490 ? RTLIB::getSINTTOFP(Op0VT, OpVT)
2491 : RTLIB::getUINTTOFP(Op0VT, OpVT);
2492 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd int-to-fp conversion!");
2493 SDValue Dummy;
2494 return ExpandLibCall(LC, Op, DAG, false, Dummy, TLI);
2495 }
2496
Eli Friedmanacb851a2009-05-27 00:47:34 +00002497 return Op;
Scott Micheled7d79f2009-01-21 04:58:48 +00002498}
2499
2500//! Lower ISD::SETCC
2501/*!
Owen Anderson9f944592009-08-11 20:47:22 +00002502 This handles MVT::f64 (double floating point) condition lowering
Scott Micheled7d79f2009-01-21 04:58:48 +00002503 */
Scott Micheled7d79f2009-01-21 04:58:48 +00002504static SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG,
2505 const TargetLowering &TLI) {
Scott Michel9e3e4a92009-01-26 03:31:40 +00002506 CondCodeSDNode *CC = dyn_cast<CondCodeSDNode>(Op.getOperand(2));
Dale Johannesen9c310712009-02-07 19:59:05 +00002507 DebugLoc dl = Op.getDebugLoc();
Scott Michel9e3e4a92009-01-26 03:31:40 +00002508 assert(CC != 0 && "LowerSETCC: CondCodeSDNode should not be null here!\n");
2509
Scott Micheled7d79f2009-01-21 04:58:48 +00002510 SDValue lhs = Op.getOperand(0);
2511 SDValue rhs = Op.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002512 EVT lhsVT = lhs.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00002513 assert(lhsVT == MVT::f64 && "LowerSETCC: type other than MVT::64\n");
Scott Micheled7d79f2009-01-21 04:58:48 +00002514
Owen Anderson53aa7a92009-08-10 22:56:29 +00002515 EVT ccResultVT = TLI.getSetCCResultType(lhs.getValueType());
Scott Michel9e3e4a92009-01-26 03:31:40 +00002516 APInt ccResultOnes = APInt::getAllOnesValue(ccResultVT.getSizeInBits());
Owen Anderson9f944592009-08-11 20:47:22 +00002517 EVT IntVT(MVT::i64);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002518
2519 // Take advantage of the fact that (truncate (sra arg, 32)) is efficiently
2520 // selected to a NOP:
Wesley Peck527da1b2010-11-23 03:31:01 +00002521 SDValue i64lhs = DAG.getNode(ISD::BITCAST, dl, IntVT, lhs);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002522 SDValue lhsHi32 =
Owen Anderson9f944592009-08-11 20:47:22 +00002523 DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002524 DAG.getNode(ISD::SRL, dl, IntVT,
Owen Anderson9f944592009-08-11 20:47:22 +00002525 i64lhs, DAG.getConstant(32, MVT::i32)));
Scott Michel9e3e4a92009-01-26 03:31:40 +00002526 SDValue lhsHi32abs =
Owen Anderson9f944592009-08-11 20:47:22 +00002527 DAG.getNode(ISD::AND, dl, MVT::i32,
2528 lhsHi32, DAG.getConstant(0x7fffffff, MVT::i32));
Scott Michel9e3e4a92009-01-26 03:31:40 +00002529 SDValue lhsLo32 =
Owen Anderson9f944592009-08-11 20:47:22 +00002530 DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, i64lhs);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002531
2532 // SETO and SETUO only use the lhs operand:
2533 if (CC->get() == ISD::SETO) {
2534 // Evaluates to true if Op0 is not [SQ]NaN - lowers to the inverse of
2535 // SETUO
2536 APInt ccResultAllOnes = APInt::getAllOnesValue(ccResultVT.getSizeInBits());
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002537 return DAG.getNode(ISD::XOR, dl, ccResultVT,
2538 DAG.getSetCC(dl, ccResultVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002539 lhs, DAG.getConstantFP(0.0, lhsVT),
2540 ISD::SETUO),
2541 DAG.getConstant(ccResultAllOnes, ccResultVT));
2542 } else if (CC->get() == ISD::SETUO) {
2543 // Evaluates to true if Op0 is [SQ]NaN
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002544 return DAG.getNode(ISD::AND, dl, ccResultVT,
2545 DAG.getSetCC(dl, ccResultVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002546 lhsHi32abs,
Owen Anderson9f944592009-08-11 20:47:22 +00002547 DAG.getConstant(0x7ff00000, MVT::i32),
Scott Michel9e3e4a92009-01-26 03:31:40 +00002548 ISD::SETGE),
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002549 DAG.getSetCC(dl, ccResultVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002550 lhsLo32,
Owen Anderson9f944592009-08-11 20:47:22 +00002551 DAG.getConstant(0, MVT::i32),
Scott Michel9e3e4a92009-01-26 03:31:40 +00002552 ISD::SETGT));
2553 }
2554
Wesley Peck527da1b2010-11-23 03:31:01 +00002555 SDValue i64rhs = DAG.getNode(ISD::BITCAST, dl, IntVT, rhs);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002556 SDValue rhsHi32 =
Owen Anderson9f944592009-08-11 20:47:22 +00002557 DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002558 DAG.getNode(ISD::SRL, dl, IntVT,
Owen Anderson9f944592009-08-11 20:47:22 +00002559 i64rhs, DAG.getConstant(32, MVT::i32)));
Scott Michel9e3e4a92009-01-26 03:31:40 +00002560
2561 // If a value is negative, subtract from the sign magnitude constant:
2562 SDValue signMag2TC = DAG.getConstant(0x8000000000000000ULL, IntVT);
2563
2564 // Convert the sign-magnitude representation into 2's complement:
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002565 SDValue lhsSelectMask = DAG.getNode(ISD::SRA, dl, ccResultVT,
Owen Anderson9f944592009-08-11 20:47:22 +00002566 lhsHi32, DAG.getConstant(31, MVT::i32));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002567 SDValue lhsSignMag2TC = DAG.getNode(ISD::SUB, dl, IntVT, signMag2TC, i64lhs);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002568 SDValue lhsSelect =
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002569 DAG.getNode(ISD::SELECT, dl, IntVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002570 lhsSelectMask, lhsSignMag2TC, i64lhs);
2571
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002572 SDValue rhsSelectMask = DAG.getNode(ISD::SRA, dl, ccResultVT,
Owen Anderson9f944592009-08-11 20:47:22 +00002573 rhsHi32, DAG.getConstant(31, MVT::i32));
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002574 SDValue rhsSignMag2TC = DAG.getNode(ISD::SUB, dl, IntVT, signMag2TC, i64rhs);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002575 SDValue rhsSelect =
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002576 DAG.getNode(ISD::SELECT, dl, IntVT,
Scott Michel9e3e4a92009-01-26 03:31:40 +00002577 rhsSelectMask, rhsSignMag2TC, i64rhs);
2578
2579 unsigned compareOp;
2580
Scott Micheled7d79f2009-01-21 04:58:48 +00002581 switch (CC->get()) {
2582 case ISD::SETOEQ:
Scott Micheled7d79f2009-01-21 04:58:48 +00002583 case ISD::SETUEQ:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002584 compareOp = ISD::SETEQ; break;
2585 case ISD::SETOGT:
Scott Micheled7d79f2009-01-21 04:58:48 +00002586 case ISD::SETUGT:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002587 compareOp = ISD::SETGT; break;
2588 case ISD::SETOGE:
Scott Micheled7d79f2009-01-21 04:58:48 +00002589 case ISD::SETUGE:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002590 compareOp = ISD::SETGE; break;
2591 case ISD::SETOLT:
Scott Micheled7d79f2009-01-21 04:58:48 +00002592 case ISD::SETULT:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002593 compareOp = ISD::SETLT; break;
2594 case ISD::SETOLE:
Scott Micheled7d79f2009-01-21 04:58:48 +00002595 case ISD::SETULE:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002596 compareOp = ISD::SETLE; break;
Scott Micheled7d79f2009-01-21 04:58:48 +00002597 case ISD::SETUNE:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002598 case ISD::SETONE:
2599 compareOp = ISD::SETNE; break;
Scott Micheled7d79f2009-01-21 04:58:48 +00002600 default:
Chris Lattner2104b8d2010-04-07 22:58:41 +00002601 report_fatal_error("CellSPU ISel Select: unimplemented f64 condition");
Scott Micheled7d79f2009-01-21 04:58:48 +00002602 }
2603
Scott Michel9e3e4a92009-01-26 03:31:40 +00002604 SDValue result =
Scott Micheld1db1ab2009-03-16 18:47:25 +00002605 DAG.getSetCC(dl, ccResultVT, lhsSelect, rhsSelect,
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002606 (ISD::CondCode) compareOp);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002607
2608 if ((CC->get() & 0x8) == 0) {
2609 // Ordered comparison:
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002610 SDValue lhsNaN = DAG.getSetCC(dl, ccResultVT,
Owen Anderson9f944592009-08-11 20:47:22 +00002611 lhs, DAG.getConstantFP(0.0, MVT::f64),
Scott Michel9e3e4a92009-01-26 03:31:40 +00002612 ISD::SETO);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002613 SDValue rhsNaN = DAG.getSetCC(dl, ccResultVT,
Owen Anderson9f944592009-08-11 20:47:22 +00002614 rhs, DAG.getConstantFP(0.0, MVT::f64),
Scott Michel9e3e4a92009-01-26 03:31:40 +00002615 ISD::SETO);
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002616 SDValue ordered = DAG.getNode(ISD::AND, dl, ccResultVT, lhsNaN, rhsNaN);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002617
Dale Johannesenf2bb6f02009-02-04 01:48:28 +00002618 result = DAG.getNode(ISD::AND, dl, ccResultVT, ordered, result);
Scott Michel9e3e4a92009-01-26 03:31:40 +00002619 }
2620
2621 return result;
Scott Micheled7d79f2009-01-21 04:58:48 +00002622}
2623
Scott Michel0be03392008-11-22 23:50:42 +00002624//! Lower ISD::SELECT_CC
2625/*!
2626 ISD::SELECT_CC can (generally) be implemented directly on the SPU using the
2627 SELB instruction.
2628
2629 \note Need to revisit this in the future: if the code path through the true
2630 and false value computations is longer than the latency of a branch (6
2631 cycles), then it would be more advantageous to branch and insert a new basic
2632 block and branch on the condition. However, this code does not make that
2633 assumption, given the simplisitc uses so far.
2634 */
2635
Scott Michel82335272008-12-27 04:51:36 +00002636static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2637 const TargetLowering &TLI) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002638 EVT VT = Op.getValueType();
Scott Michel0be03392008-11-22 23:50:42 +00002639 SDValue lhs = Op.getOperand(0);
2640 SDValue rhs = Op.getOperand(1);
2641 SDValue trueval = Op.getOperand(2);
2642 SDValue falseval = Op.getOperand(3);
2643 SDValue condition = Op.getOperand(4);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002644 DebugLoc dl = Op.getDebugLoc();
Scott Michel0be03392008-11-22 23:50:42 +00002645
Scott Michel82335272008-12-27 04:51:36 +00002646 // NOTE: SELB's arguments: $rA, $rB, $mask
2647 //
2648 // SELB selects bits from $rA where bits in $mask are 0, bits from $rB
2649 // where bits in $mask are 1. CCond will be inverted, having 1s where the
2650 // condition was true and 0s where the condition was false. Hence, the
2651 // arguments to SELB get reversed.
2652
Scott Michel0be03392008-11-22 23:50:42 +00002653 // Note: Really should be ISD::SELECT instead of SPUISD::SELB, but LLVM's
2654 // legalizer insists on combining SETCC/SELECT into SELECT_CC, so we end up
2655 // with another "cannot select select_cc" assert:
2656
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002657 SDValue compare = DAG.getNode(ISD::SETCC, dl,
Duncan Sands8feb6942009-01-01 15:52:00 +00002658 TLI.getSetCCResultType(Op.getValueType()),
Scott Michel82335272008-12-27 04:51:36 +00002659 lhs, rhs, condition);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002660 return DAG.getNode(SPUISD::SELB, dl, VT, falseval, trueval, compare);
Scott Michel0be03392008-11-22 23:50:42 +00002661}
2662
Scott Michel73640252008-12-02 19:53:53 +00002663//! Custom lower ISD::TRUNCATE
2664static SDValue LowerTRUNCATE(SDValue Op, SelectionDAG &DAG)
2665{
Scott Micheld1db1ab2009-03-16 18:47:25 +00002666 // Type to truncate to
Owen Anderson53aa7a92009-08-10 22:56:29 +00002667 EVT VT = Op.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00002668 MVT simpleVT = VT.getSimpleVT();
Wesley Peck527da1b2010-11-23 03:31:01 +00002669 EVT VecVT = EVT::getVectorVT(*DAG.getContext(),
Owen Anderson117c9e82009-08-12 00:36:31 +00002670 VT, (128 / VT.getSizeInBits()));
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002671 DebugLoc dl = Op.getDebugLoc();
Scott Michel73640252008-12-02 19:53:53 +00002672
Scott Micheld1db1ab2009-03-16 18:47:25 +00002673 // Type to truncate from
Scott Michel73640252008-12-02 19:53:53 +00002674 SDValue Op0 = Op.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002675 EVT Op0VT = Op0.getValueType();
Scott Michel73640252008-12-02 19:53:53 +00002676
Duncan Sands14627772010-11-03 12:17:33 +00002677 if (Op0VT == MVT::i128 && simpleVT == MVT::i64) {
Scott Michel6a1f6272009-01-03 00:27:53 +00002678 // Create shuffle mask, least significant doubleword of quadword
Scott Michel82335272008-12-27 04:51:36 +00002679 unsigned maskHigh = 0x08090a0b;
2680 unsigned maskLow = 0x0c0d0e0f;
2681 // Use a shuffle to perform the truncation
Owen Anderson9f944592009-08-11 20:47:22 +00002682 SDValue shufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
2683 DAG.getConstant(maskHigh, MVT::i32),
2684 DAG.getConstant(maskLow, MVT::i32),
2685 DAG.getConstant(maskHigh, MVT::i32),
2686 DAG.getConstant(maskLow, MVT::i32));
Scott Michel82335272008-12-27 04:51:36 +00002687
Scott Micheld1db1ab2009-03-16 18:47:25 +00002688 SDValue truncShuffle = DAG.getNode(SPUISD::SHUFB, dl, VecVT,
2689 Op0, Op0, shufMask);
Scott Michel82335272008-12-27 04:51:36 +00002690
Scott Micheld1db1ab2009-03-16 18:47:25 +00002691 return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT, truncShuffle);
Scott Michel73640252008-12-02 19:53:53 +00002692 }
2693
Scott Michel82335272008-12-27 04:51:36 +00002694 return SDValue(); // Leave the truncate unmolested
Scott Michel73640252008-12-02 19:53:53 +00002695}
2696
Scott Michelc5dd8bd2009-08-25 22:37:34 +00002697/*!
2698 * Emit the instruction sequence for i64/i32 -> i128 sign extend. The basic
2699 * algorithm is to duplicate the sign bit using rotmai to generate at
2700 * least one byte full of sign bits. Then propagate the "sign-byte" into
2701 * the leftmost words and the i64/i32 into the rightmost words using shufb.
2702 *
2703 * @param Op The sext operand
2704 * @param DAG The current DAG
2705 * @return The SDValue with the entire instruction sequence
2706 */
Scott Michel8d1602a2009-08-24 22:28:53 +00002707static SDValue LowerSIGN_EXTEND(SDValue Op, SelectionDAG &DAG)
2708{
Scott Michel8d1602a2009-08-24 22:28:53 +00002709 DebugLoc dl = Op.getDebugLoc();
2710
Scott Michelc5dd8bd2009-08-25 22:37:34 +00002711 // Type to extend to
2712 MVT OpVT = Op.getValueType().getSimpleVT();
Scott Michelc5dd8bd2009-08-25 22:37:34 +00002713
Scott Michel8d1602a2009-08-24 22:28:53 +00002714 // Type to extend from
2715 SDValue Op0 = Op.getOperand(0);
Scott Michelc5dd8bd2009-08-25 22:37:34 +00002716 MVT Op0VT = Op0.getValueType().getSimpleVT();
Scott Michel8d1602a2009-08-24 22:28:53 +00002717
Kalle Raiskila6e5a54b2011-01-20 15:49:06 +00002718 // extend i8 & i16 via i32
2719 if (Op0VT == MVT::i8 || Op0VT == MVT::i16) {
2720 Op0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, Op0);
2721 Op0VT = MVT::i32;
2722 }
2723
Scott Michelc5dd8bd2009-08-25 22:37:34 +00002724 // The type to extend to needs to be a i128 and
2725 // the type to extend from needs to be i64 or i32.
2726 assert((OpVT == MVT::i128 && (Op0VT == MVT::i64 || Op0VT == MVT::i32)) &&
Scott Michel8d1602a2009-08-24 22:28:53 +00002727 "LowerSIGN_EXTEND: input and/or output operand have wrong size");
2728
2729 // Create shuffle mask
Scott Michelc5dd8bd2009-08-25 22:37:34 +00002730 unsigned mask1 = 0x10101010; // byte 0 - 3 and 4 - 7
2731 unsigned mask2 = Op0VT == MVT::i64 ? 0x00010203 : 0x10101010; // byte 8 - 11
2732 unsigned mask3 = Op0VT == MVT::i64 ? 0x04050607 : 0x00010203; // byte 12 - 15
Scott Michel8d1602a2009-08-24 22:28:53 +00002733 SDValue shufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
2734 DAG.getConstant(mask1, MVT::i32),
2735 DAG.getConstant(mask1, MVT::i32),
2736 DAG.getConstant(mask2, MVT::i32),
2737 DAG.getConstant(mask3, MVT::i32));
2738
Scott Michelc5dd8bd2009-08-25 22:37:34 +00002739 // Word wise arithmetic right shift to generate at least one byte
2740 // that contains sign bits.
2741 MVT mvt = Op0VT == MVT::i64 ? MVT::v2i64 : MVT::v4i32;
Scott Michel8d1602a2009-08-24 22:28:53 +00002742 SDValue sraVal = DAG.getNode(ISD::SRA,
2743 dl,
Scott Michelc5dd8bd2009-08-25 22:37:34 +00002744 mvt,
2745 DAG.getNode(SPUISD::PREFSLOT2VEC, dl, mvt, Op0, Op0),
Scott Michel8d1602a2009-08-24 22:28:53 +00002746 DAG.getConstant(31, MVT::i32));
2747
Kalle Raiskila5f2034c2010-10-18 09:34:19 +00002748 // reinterpret as a i128 (SHUFB requires it). This gets lowered away.
Wesley Peck527da1b2010-11-23 03:31:01 +00002749 SDValue extended = SDValue(DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Kalle Raiskila5f2034c2010-10-18 09:34:19 +00002750 dl, Op0VT, Op0,
2751 DAG.getTargetConstant(
Wesley Peck527da1b2010-11-23 03:31:01 +00002752 SPU::GPRCRegClass.getID(),
Kalle Raiskila5f2034c2010-10-18 09:34:19 +00002753 MVT::i32)), 0);
Scott Michelc5dd8bd2009-08-25 22:37:34 +00002754 // Shuffle bytes - Copy the sign bits into the upper 64 bits
2755 // and the input value into the lower 64 bits.
2756 SDValue extShuffle = DAG.getNode(SPUISD::SHUFB, dl, mvt,
Kalle Raiskila5f2034c2010-10-18 09:34:19 +00002757 extended, sraVal, shufMask);
Wesley Peck527da1b2010-11-23 03:31:01 +00002758 return DAG.getNode(ISD::BITCAST, dl, MVT::i128, extShuffle);
Scott Michel8d1602a2009-08-24 22:28:53 +00002759}
2760
Scott Michel0be03392008-11-22 23:50:42 +00002761//! Custom (target-specific) lowering entry point
2762/*!
2763 This is where LLVM's DAG selection process calls to do target-specific
2764 lowering of nodes.
2765 */
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002766SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002767SPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const
Scott Michel6e22c652007-12-04 22:23:35 +00002768{
Scott Michel7d5eaec2008-02-23 18:41:37 +00002769 unsigned Opc = (unsigned) Op.getOpcode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002770 EVT VT = Op.getValueType();
Scott Michel7d5eaec2008-02-23 18:41:37 +00002771
2772 switch (Opc) {
Scott Michel6e22c652007-12-04 22:23:35 +00002773 default: {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00002774#ifndef NDEBUG
Chris Lattner317dbbc2009-08-23 07:05:07 +00002775 errs() << "SPUTargetLowering::LowerOperation(): need to lower this!\n";
2776 errs() << "Op.getOpcode() = " << Opc << "\n";
2777 errs() << "*Op.getNode():\n";
Gabor Greiff304a7a2008-08-28 21:40:38 +00002778 Op.getNode()->dump();
Torok Edwinfb8d6d52009-07-08 20:53:28 +00002779#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00002780 llvm_unreachable(0);
Scott Michel6e22c652007-12-04 22:23:35 +00002781 }
2782 case ISD::LOAD:
Scott Michel73640252008-12-02 19:53:53 +00002783 case ISD::EXTLOAD:
Scott Michel6e22c652007-12-04 22:23:35 +00002784 case ISD::SEXTLOAD:
2785 case ISD::ZEXTLOAD:
2786 return LowerLOAD(Op, DAG, SPUTM.getSubtargetImpl());
2787 case ISD::STORE:
2788 return LowerSTORE(Op, DAG, SPUTM.getSubtargetImpl());
2789 case ISD::ConstantPool:
2790 return LowerConstantPool(Op, DAG, SPUTM.getSubtargetImpl());
2791 case ISD::GlobalAddress:
2792 return LowerGlobalAddress(Op, DAG, SPUTM.getSubtargetImpl());
2793 case ISD::JumpTable:
2794 return LowerJumpTable(Op, DAG, SPUTM.getSubtargetImpl());
Scott Michel6e22c652007-12-04 22:23:35 +00002795 case ISD::ConstantFP:
2796 return LowerConstantFP(Op, DAG);
Scott Michel6e22c652007-12-04 22:23:35 +00002797
Scott Michel41236c02008-12-30 23:28:25 +00002798 // i8, i64 math ops:
Scott Micheld831cc42008-06-02 22:18:03 +00002799 case ISD::ADD:
Scott Michel6e22c652007-12-04 22:23:35 +00002800 case ISD::SUB:
2801 case ISD::ROTR:
2802 case ISD::ROTL:
2803 case ISD::SRL:
2804 case ISD::SHL:
Scott Micheld831cc42008-06-02 22:18:03 +00002805 case ISD::SRA: {
Owen Anderson9f944592009-08-11 20:47:22 +00002806 if (VT == MVT::i8)
Scott Michel82335272008-12-27 04:51:36 +00002807 return LowerI8Math(Op, DAG, Opc, *this);
Scott Michel7d5eaec2008-02-23 18:41:37 +00002808 break;
Scott Micheld831cc42008-06-02 22:18:03 +00002809 }
Scott Michel6e22c652007-12-04 22:23:35 +00002810
Scott Michel9e3e4a92009-01-26 03:31:40 +00002811 case ISD::FP_TO_SINT:
2812 case ISD::FP_TO_UINT:
2813 return LowerFP_TO_INT(Op, DAG, *this);
2814
2815 case ISD::SINT_TO_FP:
2816 case ISD::UINT_TO_FP:
2817 return LowerINT_TO_FP(Op, DAG, *this);
Scott Micheled7d79f2009-01-21 04:58:48 +00002818
Scott Michel6e22c652007-12-04 22:23:35 +00002819 // Vector-related lowering.
2820 case ISD::BUILD_VECTOR:
Scott Michel9e3e4a92009-01-26 03:31:40 +00002821 return LowerBUILD_VECTOR(Op, DAG);
Scott Michel6e22c652007-12-04 22:23:35 +00002822 case ISD::SCALAR_TO_VECTOR:
2823 return LowerSCALAR_TO_VECTOR(Op, DAG);
2824 case ISD::VECTOR_SHUFFLE:
2825 return LowerVECTOR_SHUFFLE(Op, DAG);
2826 case ISD::EXTRACT_VECTOR_ELT:
2827 return LowerEXTRACT_VECTOR_ELT(Op, DAG);
2828 case ISD::INSERT_VECTOR_ELT:
2829 return LowerINSERT_VECTOR_ELT(Op, DAG);
2830
2831 // Look for ANDBI, ORBI and XORBI opportunities and lower appropriately:
2832 case ISD::AND:
2833 case ISD::OR:
2834 case ISD::XOR:
2835 return LowerByteImmed(Op, DAG);
2836
2837 // Vector and i8 multiply:
2838 case ISD::MUL:
Owen Anderson9f944592009-08-11 20:47:22 +00002839 if (VT == MVT::i8)
Scott Michel82335272008-12-27 04:51:36 +00002840 return LowerI8Math(Op, DAG, Opc, *this);
Scott Michel6e22c652007-12-04 22:23:35 +00002841
Scott Michel6e22c652007-12-04 22:23:35 +00002842 case ISD::CTPOP:
2843 return LowerCTPOP(Op, DAG);
Scott Michel0be03392008-11-22 23:50:42 +00002844
2845 case ISD::SELECT_CC:
Scott Michel82335272008-12-27 04:51:36 +00002846 return LowerSELECT_CC(Op, DAG, *this);
Scott Michel73640252008-12-02 19:53:53 +00002847
Scott Micheled7d79f2009-01-21 04:58:48 +00002848 case ISD::SETCC:
2849 return LowerSETCC(Op, DAG, *this);
2850
Scott Michel73640252008-12-02 19:53:53 +00002851 case ISD::TRUNCATE:
2852 return LowerTRUNCATE(Op, DAG);
Scott Michel8d1602a2009-08-24 22:28:53 +00002853
2854 case ISD::SIGN_EXTEND:
2855 return LowerSIGN_EXTEND(Op, DAG);
Scott Michel6e22c652007-12-04 22:23:35 +00002856 }
2857
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002858 return SDValue();
Scott Michel6e22c652007-12-04 22:23:35 +00002859}
2860
Duncan Sands6ed40142008-12-01 11:39:25 +00002861void SPUTargetLowering::ReplaceNodeResults(SDNode *N,
2862 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002863 SelectionDAG &DAG) const
Scott Michelabad22c2008-11-10 23:43:06 +00002864{
2865#if 0
2866 unsigned Opc = (unsigned) N->getOpcode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002867 EVT OpVT = N->getValueType(0);
Scott Michelabad22c2008-11-10 23:43:06 +00002868
2869 switch (Opc) {
2870 default: {
Chris Lattner317dbbc2009-08-23 07:05:07 +00002871 errs() << "SPUTargetLowering::ReplaceNodeResults(): need to fix this!\n";
2872 errs() << "Op.getOpcode() = " << Opc << "\n";
2873 errs() << "*Op.getNode():\n";
Scott Michelabad22c2008-11-10 23:43:06 +00002874 N->dump();
2875 abort();
2876 /*NOTREACHED*/
2877 }
2878 }
2879#endif
2880
2881 /* Otherwise, return unchanged */
Scott Michelabad22c2008-11-10 23:43:06 +00002882}
2883
Scott Michel6e22c652007-12-04 22:23:35 +00002884//===----------------------------------------------------------------------===//
Scott Michel6e22c652007-12-04 22:23:35 +00002885// Target Optimization Hooks
2886//===----------------------------------------------------------------------===//
2887
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002888SDValue
Scott Michel6e22c652007-12-04 22:23:35 +00002889SPUTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const
2890{
2891#if 0
2892 TargetMachine &TM = getTargetMachine();
Scott Michelceae3bb2008-01-29 02:16:57 +00002893#endif
2894 const SPUSubtarget *ST = SPUTM.getSubtargetImpl();
Scott Michel6e22c652007-12-04 22:23:35 +00002895 SelectionDAG &DAG = DCI.DAG;
Scott Michel08a4e202008-12-01 17:56:02 +00002896 SDValue Op0 = N->getOperand(0); // everything has at least one operand
Owen Anderson53aa7a92009-08-10 22:56:29 +00002897 EVT NodeVT = N->getValueType(0); // The node's value type
2898 EVT Op0VT = Op0.getValueType(); // The first operand's result
Scott Michel08a4e202008-12-01 17:56:02 +00002899 SDValue Result; // Initially, empty result
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002900 DebugLoc dl = N->getDebugLoc();
Scott Michel6e22c652007-12-04 22:23:35 +00002901
2902 switch (N->getOpcode()) {
2903 default: break;
Scott Michelceae3bb2008-01-29 02:16:57 +00002904 case ISD::ADD: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002905 SDValue Op1 = N->getOperand(1);
Scott Michelceae3bb2008-01-29 02:16:57 +00002906
Scott Michel82335272008-12-27 04:51:36 +00002907 if (Op0.getOpcode() == SPUISD::IndirectAddr
2908 || Op1.getOpcode() == SPUISD::IndirectAddr) {
2909 // Normalize the operands to reduce repeated code
2910 SDValue IndirectArg = Op0, AddArg = Op1;
Scott Michelb8ee30d2008-12-29 03:23:36 +00002911
Scott Michel82335272008-12-27 04:51:36 +00002912 if (Op1.getOpcode() == SPUISD::IndirectAddr) {
2913 IndirectArg = Op1;
2914 AddArg = Op0;
2915 }
2916
2917 if (isa<ConstantSDNode>(AddArg)) {
2918 ConstantSDNode *CN0 = cast<ConstantSDNode > (AddArg);
2919 SDValue IndOp1 = IndirectArg.getOperand(1);
2920
2921 if (CN0->isNullValue()) {
2922 // (add (SPUindirect <arg>, <arg>), 0) ->
2923 // (SPUindirect <arg>, <arg>)
Scott Michelceae3bb2008-01-29 02:16:57 +00002924
Scott Michel187250b2008-12-04 17:16:59 +00002925#if !defined(NDEBUG)
Scott Michel82335272008-12-27 04:51:36 +00002926 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Chris Lattner317dbbc2009-08-23 07:05:07 +00002927 errs() << "\n"
Scott Michel82335272008-12-27 04:51:36 +00002928 << "Replace: (add (SPUindirect <arg>, <arg>), 0)\n"
2929 << "With: (SPUindirect <arg>, <arg>)\n";
2930 }
Scott Michel40f54d22008-12-04 03:02:42 +00002931#endif
2932
Scott Michel82335272008-12-27 04:51:36 +00002933 return IndirectArg;
2934 } else if (isa<ConstantSDNode>(IndOp1)) {
2935 // (add (SPUindirect <arg>, <const>), <const>) ->
2936 // (SPUindirect <arg>, <const + const>)
2937 ConstantSDNode *CN1 = cast<ConstantSDNode > (IndOp1);
2938 int64_t combinedConst = CN0->getSExtValue() + CN1->getSExtValue();
2939 SDValue combinedValue = DAG.getConstant(combinedConst, Op0VT);
Scott Michelceae3bb2008-01-29 02:16:57 +00002940
Scott Michel82335272008-12-27 04:51:36 +00002941#if !defined(NDEBUG)
2942 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Chris Lattner317dbbc2009-08-23 07:05:07 +00002943 errs() << "\n"
Scott Michel82335272008-12-27 04:51:36 +00002944 << "Replace: (add (SPUindirect <arg>, " << CN1->getSExtValue()
2945 << "), " << CN0->getSExtValue() << ")\n"
2946 << "With: (SPUindirect <arg>, "
2947 << combinedConst << ")\n";
2948 }
2949#endif
Scott Michelceae3bb2008-01-29 02:16:57 +00002950
Dale Johannesen400dc2e2009-02-06 21:50:26 +00002951 return DAG.getNode(SPUISD::IndirectAddr, dl, Op0VT,
Scott Michel82335272008-12-27 04:51:36 +00002952 IndirectArg, combinedValue);
2953 }
Scott Michelceae3bb2008-01-29 02:16:57 +00002954 }
2955 }
Scott Michel7d5eaec2008-02-23 18:41:37 +00002956 break;
2957 }
2958 case ISD::SIGN_EXTEND:
2959 case ISD::ZERO_EXTEND:
2960 case ISD::ANY_EXTEND: {
Scott Michel08a4e202008-12-01 17:56:02 +00002961 if (Op0.getOpcode() == SPUISD::VEC2PREFSLOT && NodeVT == Op0VT) {
Scott Michel7d5eaec2008-02-23 18:41:37 +00002962 // (any_extend (SPUextract_elt0 <arg>)) ->
2963 // (SPUextract_elt0 <arg>)
2964 // Types must match, however...
Scott Michel187250b2008-12-04 17:16:59 +00002965#if !defined(NDEBUG)
2966 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Chris Lattner317dbbc2009-08-23 07:05:07 +00002967 errs() << "\nReplace: ";
Scott Michel40f54d22008-12-04 03:02:42 +00002968 N->dump(&DAG);
Chris Lattner317dbbc2009-08-23 07:05:07 +00002969 errs() << "\nWith: ";
Scott Michel40f54d22008-12-04 03:02:42 +00002970 Op0.getNode()->dump(&DAG);
Chris Lattner317dbbc2009-08-23 07:05:07 +00002971 errs() << "\n";
Scott Michel187250b2008-12-04 17:16:59 +00002972 }
Scott Michel40f54d22008-12-04 03:02:42 +00002973#endif
Scott Michel7d5eaec2008-02-23 18:41:37 +00002974
2975 return Op0;
2976 }
2977 break;
2978 }
2979 case SPUISD::IndirectAddr: {
2980 if (!ST->usingLargeMem() && Op0.getOpcode() == SPUISD::AFormAddr) {
Scott Micheled7d79f2009-01-21 04:58:48 +00002981 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1));
Dan Gohmanf1d83042010-06-18 14:22:04 +00002982 if (CN != 0 && CN->isNullValue()) {
Scott Michel7d5eaec2008-02-23 18:41:37 +00002983 // (SPUindirect (SPUaform <addr>, 0), 0) ->
2984 // (SPUaform <addr>, 0)
2985
Chris Lattner317dbbc2009-08-23 07:05:07 +00002986 DEBUG(errs() << "Replace: ");
Scott Michel7d5eaec2008-02-23 18:41:37 +00002987 DEBUG(N->dump(&DAG));
Chris Lattner317dbbc2009-08-23 07:05:07 +00002988 DEBUG(errs() << "\nWith: ");
Gabor Greiff304a7a2008-08-28 21:40:38 +00002989 DEBUG(Op0.getNode()->dump(&DAG));
Chris Lattner317dbbc2009-08-23 07:05:07 +00002990 DEBUG(errs() << "\n");
Scott Michel7d5eaec2008-02-23 18:41:37 +00002991
2992 return Op0;
2993 }
Scott Michel82335272008-12-27 04:51:36 +00002994 } else if (Op0.getOpcode() == ISD::ADD) {
2995 SDValue Op1 = N->getOperand(1);
2996 if (ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(Op1)) {
2997 // (SPUindirect (add <arg>, <arg>), 0) ->
2998 // (SPUindirect <arg>, <arg>)
2999 if (CN1->isNullValue()) {
3000
3001#if !defined(NDEBUG)
3002 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Chris Lattner317dbbc2009-08-23 07:05:07 +00003003 errs() << "\n"
Scott Michel82335272008-12-27 04:51:36 +00003004 << "Replace: (SPUindirect (add <arg>, <arg>), 0)\n"
3005 << "With: (SPUindirect <arg>, <arg>)\n";
3006 }
3007#endif
3008
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003009 return DAG.getNode(SPUISD::IndirectAddr, dl, Op0VT,
Scott Michel82335272008-12-27 04:51:36 +00003010 Op0.getOperand(0), Op0.getOperand(1));
3011 }
3012 }
Scott Michel7d5eaec2008-02-23 18:41:37 +00003013 }
3014 break;
3015 }
Kalle Raiskila0a9dd402010-11-12 10:14:03 +00003016 case SPUISD::SHL_BITS:
3017 case SPUISD::SHL_BYTES:
Scott Michel82335272008-12-27 04:51:36 +00003018 case SPUISD::ROTBYTES_LEFT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003019 SDValue Op1 = N->getOperand(1);
Scott Michel7d5eaec2008-02-23 18:41:37 +00003020
Scott Michel82335272008-12-27 04:51:36 +00003021 // Kill degenerate vector shifts:
3022 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3023 if (CN->isNullValue()) {
Scott Michel7d5eaec2008-02-23 18:41:37 +00003024 Result = Op0;
3025 }
3026 }
3027 break;
3028 }
Scott Michel82335272008-12-27 04:51:36 +00003029 case SPUISD::PREFSLOT2VEC: {
Scott Michel7d5eaec2008-02-23 18:41:37 +00003030 switch (Op0.getOpcode()) {
3031 default:
3032 break;
3033 case ISD::ANY_EXTEND:
3034 case ISD::ZERO_EXTEND:
3035 case ISD::SIGN_EXTEND: {
Scott Michelb8ee30d2008-12-29 03:23:36 +00003036 // (SPUprefslot2vec (any|zero|sign_extend (SPUvec2prefslot <arg>))) ->
Scott Michel7d5eaec2008-02-23 18:41:37 +00003037 // <arg>
Scott Michelb8ee30d2008-12-29 03:23:36 +00003038 // but only if the SPUprefslot2vec and <arg> types match.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003039 SDValue Op00 = Op0.getOperand(0);
Scott Michelefc8c7a2008-11-24 17:11:17 +00003040 if (Op00.getOpcode() == SPUISD::VEC2PREFSLOT) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003041 SDValue Op000 = Op00.getOperand(0);
Scott Michel08a4e202008-12-01 17:56:02 +00003042 if (Op000.getValueType() == NodeVT) {
Scott Michel7d5eaec2008-02-23 18:41:37 +00003043 Result = Op000;
3044 }
3045 }
3046 break;
3047 }
Scott Michelefc8c7a2008-11-24 17:11:17 +00003048 case SPUISD::VEC2PREFSLOT: {
Scott Michelb8ee30d2008-12-29 03:23:36 +00003049 // (SPUprefslot2vec (SPUvec2prefslot <arg>)) ->
Scott Michel7d5eaec2008-02-23 18:41:37 +00003050 // <arg>
3051 Result = Op0.getOperand(0);
3052 break;
Scott Michelfe095082008-07-16 17:17:29 +00003053 }
Scott Michel7d5eaec2008-02-23 18:41:37 +00003054 }
3055 break;
Scott Michelceae3bb2008-01-29 02:16:57 +00003056 }
3057 }
Scott Micheled7d79f2009-01-21 04:58:48 +00003058
Scott Michele4d3e3c2008-01-17 20:38:41 +00003059 // Otherwise, return unchanged.
Scott Michel08a4e202008-12-01 17:56:02 +00003060#ifndef NDEBUG
Gabor Greiff304a7a2008-08-28 21:40:38 +00003061 if (Result.getNode()) {
Chris Lattner317dbbc2009-08-23 07:05:07 +00003062 DEBUG(errs() << "\nReplace.SPU: ");
Scott Michel7d5eaec2008-02-23 18:41:37 +00003063 DEBUG(N->dump(&DAG));
Chris Lattner317dbbc2009-08-23 07:05:07 +00003064 DEBUG(errs() << "\nWith: ");
Gabor Greiff304a7a2008-08-28 21:40:38 +00003065 DEBUG(Result.getNode()->dump(&DAG));
Chris Lattner317dbbc2009-08-23 07:05:07 +00003066 DEBUG(errs() << "\n");
Scott Michel7d5eaec2008-02-23 18:41:37 +00003067 }
3068#endif
3069
3070 return Result;
Scott Michel6e22c652007-12-04 22:23:35 +00003071}
3072
3073//===----------------------------------------------------------------------===//
3074// Inline Assembly Support
3075//===----------------------------------------------------------------------===//
3076
3077/// getConstraintType - Given a constraint letter, return the type of
3078/// constraint it is for this target.
Scott Michelfe095082008-07-16 17:17:29 +00003079SPUTargetLowering::ConstraintType
Scott Michel6e22c652007-12-04 22:23:35 +00003080SPUTargetLowering::getConstraintType(const std::string &ConstraintLetter) const {
3081 if (ConstraintLetter.size() == 1) {
3082 switch (ConstraintLetter[0]) {
3083 default: break;
3084 case 'b':
3085 case 'r':
3086 case 'f':
3087 case 'v':
3088 case 'y':
3089 return C_RegisterClass;
Scott Michelfe095082008-07-16 17:17:29 +00003090 }
Scott Michel6e22c652007-12-04 22:23:35 +00003091 }
3092 return TargetLowering::getConstraintType(ConstraintLetter);
3093}
3094
John Thompsone8360b72010-10-29 17:29:13 +00003095/// Examine constraint type and operand type and determine a weight value.
3096/// This object must already have been set up with the operand type
3097/// and the current alternative constraint selected.
3098TargetLowering::ConstraintWeight
3099SPUTargetLowering::getSingleConstraintMatchWeight(
3100 AsmOperandInfo &info, const char *constraint) const {
3101 ConstraintWeight weight = CW_Invalid;
3102 Value *CallOperandVal = info.CallOperandVal;
3103 // If we don't have a value, we can't do a match,
3104 // but allow it at the lowest weight.
3105 if (CallOperandVal == NULL)
3106 return CW_Default;
3107 // Look at the constraint type.
3108 switch (*constraint) {
3109 default:
3110 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
Owen Andersonb2c80da2011-02-25 21:41:48 +00003111 break;
John Thompsone8360b72010-10-29 17:29:13 +00003112 //FIXME: Seems like the supported constraint letters were just copied
3113 // from PPC, as the following doesn't correspond to the GCC docs.
3114 // I'm leaving it so until someone adds the corresponding lowering support.
3115 case 'b':
3116 case 'r':
3117 case 'f':
3118 case 'd':
3119 case 'v':
3120 case 'y':
3121 weight = CW_Register;
3122 break;
3123 }
3124 return weight;
3125}
3126
Scott Michelfe095082008-07-16 17:17:29 +00003127std::pair<unsigned, const TargetRegisterClass*>
Scott Michel6e22c652007-12-04 22:23:35 +00003128SPUTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Anderson53aa7a92009-08-10 22:56:29 +00003129 EVT VT) const
Scott Michel6e22c652007-12-04 22:23:35 +00003130{
3131 if (Constraint.size() == 1) {
3132 // GCC RS6000 Constraint Letters
3133 switch (Constraint[0]) {
3134 case 'b': // R1-R31
3135 case 'r': // R0-R31
Owen Anderson9f944592009-08-11 20:47:22 +00003136 if (VT == MVT::i64)
Scott Michel6e22c652007-12-04 22:23:35 +00003137 return std::make_pair(0U, SPU::R64CRegisterClass);
3138 return std::make_pair(0U, SPU::R32CRegisterClass);
3139 case 'f':
Owen Anderson9f944592009-08-11 20:47:22 +00003140 if (VT == MVT::f32)
Scott Michel6e22c652007-12-04 22:23:35 +00003141 return std::make_pair(0U, SPU::R32FPRegisterClass);
Owen Anderson9f944592009-08-11 20:47:22 +00003142 else if (VT == MVT::f64)
Scott Michel6e22c652007-12-04 22:23:35 +00003143 return std::make_pair(0U, SPU::R64FPRegisterClass);
3144 break;
Scott Michelfe095082008-07-16 17:17:29 +00003145 case 'v':
Scott Michel6e22c652007-12-04 22:23:35 +00003146 return std::make_pair(0U, SPU::GPRCRegisterClass);
3147 }
3148 }
Scott Michelfe095082008-07-16 17:17:29 +00003149
Scott Michel6e22c652007-12-04 22:23:35 +00003150 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3151}
3152
Scott Michel7d5eaec2008-02-23 18:41:37 +00003153//! Compute used/known bits for a SPU operand
Scott Michel6e22c652007-12-04 22:23:35 +00003154void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003155SPUTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmane1d9ee62008-02-13 22:28:48 +00003156 const APInt &Mask,
Scott Michelfe095082008-07-16 17:17:29 +00003157 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00003158 APInt &KnownOne,
Scott Michelbb713ae2008-01-30 02:55:46 +00003159 const SelectionDAG &DAG,
3160 unsigned Depth ) const {
Scott Michelc3a19102008-04-30 00:30:08 +00003161#if 0
Dan Gohmancff69532009-04-01 18:45:54 +00003162 const uint64_t uint64_sizebits = sizeof(uint64_t) * CHAR_BIT;
Scott Michel7d5eaec2008-02-23 18:41:37 +00003163
3164 switch (Op.getOpcode()) {
3165 default:
3166 // KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
3167 break;
Scott Michel7d5eaec2008-02-23 18:41:37 +00003168 case CALL:
3169 case SHUFB:
Scott Michel0be03392008-11-22 23:50:42 +00003170 case SHUFFLE_MASK:
Scott Michel7d5eaec2008-02-23 18:41:37 +00003171 case CNTB:
Scott Micheled7d79f2009-01-21 04:58:48 +00003172 case SPUISD::PREFSLOT2VEC:
Scott Michel7d5eaec2008-02-23 18:41:37 +00003173 case SPUISD::LDRESULT:
Scott Micheled7d79f2009-01-21 04:58:48 +00003174 case SPUISD::VEC2PREFSLOT:
Scott Michelc3a19102008-04-30 00:30:08 +00003175 case SPUISD::SHLQUAD_L_BITS:
3176 case SPUISD::SHLQUAD_L_BYTES:
Scott Michelc3a19102008-04-30 00:30:08 +00003177 case SPUISD::VEC_ROTL:
3178 case SPUISD::VEC_ROTR:
Scott Michelc3a19102008-04-30 00:30:08 +00003179 case SPUISD::ROTBYTES_LEFT:
Scott Micheld831cc42008-06-02 22:18:03 +00003180 case SPUISD::SELECT_MASK:
3181 case SPUISD::SELB:
Scott Michel7d5eaec2008-02-23 18:41:37 +00003182 }
Scott Micheled7d79f2009-01-21 04:58:48 +00003183#endif
Scott Michel6e22c652007-12-04 22:23:35 +00003184}
Scott Michel41236c02008-12-30 23:28:25 +00003185
Scott Michel82335272008-12-27 04:51:36 +00003186unsigned
3187SPUTargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
3188 unsigned Depth) const {
3189 switch (Op.getOpcode()) {
3190 default:
3191 return 1;
Scott Michel6e22c652007-12-04 22:23:35 +00003192
Scott Michel82335272008-12-27 04:51:36 +00003193 case ISD::SETCC: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003194 EVT VT = Op.getValueType();
Scott Michel82335272008-12-27 04:51:36 +00003195
Owen Anderson9f944592009-08-11 20:47:22 +00003196 if (VT != MVT::i8 && VT != MVT::i16 && VT != MVT::i32) {
3197 VT = MVT::i32;
Scott Michel82335272008-12-27 04:51:36 +00003198 }
3199 return VT.getSizeInBits();
3200 }
3201 }
3202}
Scott Michelb8ee30d2008-12-29 03:23:36 +00003203
Scott Michelc3a19102008-04-30 00:30:08 +00003204// LowerAsmOperandForConstraint
3205void
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003206SPUTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +00003207 std::string &Constraint,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003208 std::vector<SDValue> &Ops,
Scott Michelc3a19102008-04-30 00:30:08 +00003209 SelectionDAG &DAG) const {
3210 // Default, for the time being, to the base class handler
Eric Christopherde9399b2011-06-02 23:16:42 +00003211 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Scott Michelc3a19102008-04-30 00:30:08 +00003212}
3213
Scott Michel6e22c652007-12-04 22:23:35 +00003214/// isLegalAddressImmediate - Return true if the integer value can be used
3215/// as the offset of the target addressing mode.
Gabor Greif81d6a382008-08-31 15:37:04 +00003216bool SPUTargetLowering::isLegalAddressImmediate(int64_t V,
3217 const Type *Ty) const {
Scott Michel6e22c652007-12-04 22:23:35 +00003218 // SPU's addresses are 256K:
3219 return (V > -(1 << 18) && V < (1 << 18) - 1);
3220}
3221
3222bool SPUTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Scott Michelfe095082008-07-16 17:17:29 +00003223 return false;
Scott Michel6e22c652007-12-04 22:23:35 +00003224}
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003225
3226bool
3227SPUTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3228 // The SPU target isn't yet aware of offsets.
3229 return false;
3230}
Kalle Raiskilaa8450222010-10-07 16:24:35 +00003231
3232// can we compare to Imm without writing it into a register?
3233bool SPUTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3234 //ceqi, cgti, etc. all take s10 operand
3235 return isInt<10>(Imm);
3236}
3237
Wesley Peck527da1b2010-11-23 03:31:01 +00003238bool
3239SPUTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Kalle Raiskilaa8450222010-10-07 16:24:35 +00003240 const Type * ) const{
3241
Wesley Peck527da1b2010-11-23 03:31:01 +00003242 // A-form: 18bit absolute address.
Kalle Raiskilaa8450222010-10-07 16:24:35 +00003243 if (AM.BaseGV && !AM.HasBaseReg && AM.Scale == 0 && AM.BaseOffs == 0)
3244 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +00003245
Kalle Raiskilaa8450222010-10-07 16:24:35 +00003246 // D-form: reg + 14bit offset
3247 if (AM.BaseGV ==0 && AM.HasBaseReg && AM.Scale == 0 && isInt<14>(AM.BaseOffs))
3248 return true;
3249
3250 // X-form: reg+reg
3251 if (AM.BaseGV == 0 && AM.HasBaseReg && AM.Scale == 1 && AM.BaseOffs ==0)
3252 return true;
3253
3254 return false;
3255}