blob: 4e9fcd1bc7655ce95181bdfde635e291114f714d [file] [log] [blame]
Scott Michel7ea02ff2009-03-17 01:15:45 +00001//===-- SPUISelLowering.cpp - Cell SPU DAG Lowering Implementation --------===//
Scott Michel266bc8f2007-12-04 22:23:35 +00002// The LLVM Compiler Infrastructure
3//
Chris Lattner4ee451d2007-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 Michel266bc8f2007-12-04 22:23:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the SPUTargetLowering class.
10//
11//===----------------------------------------------------------------------===//
12
Scott Michel266bc8f2007-12-04 22:23:35 +000013#include "SPUISelLowering.h"
14#include "SPUTargetMachine.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000015#include "SPUFrameLowering.h"
Dan Gohman1e93df62010-04-17 14:41:14 +000016#include "SPUMachineFunction.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000017#include "llvm/Constants.h"
18#include "llvm/Function.h"
19#include "llvm/Intrinsics.h"
Scott Michelc9c8b2a2009-01-26 03:31:40 +000020#include "llvm/CallingConv.h"
John Thompson44ab89e2010-10-29 17:29:13 +000021#include "llvm/Type.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000022#include "llvm/CodeGen/CallingConvLower.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000027#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000028#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000029#include "llvm/Target/TargetOptions.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000030#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000031#include "llvm/Support/ErrorHandling.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000032#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000033#include "llvm/Support/raw_ostream.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000034
35using namespace llvm;
36
Scott Michel266bc8f2007-12-04 22:23:35 +000037namespace {
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +000038 // Byte offset of the preferred slot (counted from the MSB)
39 int prefslotOffset(EVT VT) {
40 int retval=0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000041 if (VT==MVT::i1) retval=3;
42 if (VT==MVT::i8) retval=3;
43 if (VT==MVT::i16) retval=2;
Scott Michel266bc8f2007-12-04 22:23:35 +000044
45 return retval;
46 }
Scott Michel94bd57e2009-01-15 04:41:47 +000047
Scott Michelc9c8b2a2009-01-26 03:31:40 +000048 //! Expand a library call into an actual call DAG node
49 /*!
50 \note
51 This code is taken from SelectionDAGLegalize, since it is not exposed as
52 part of the LLVM SelectionDAG API.
53 */
54
55 SDValue
56 ExpandLibCall(RTLIB::Libcall LC, SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +000057 bool isSigned, SDValue &Hi, const SPUTargetLowering &TLI) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +000058 // The input chain to this libcall is the entry node of the function.
59 // Legalizing the call will automatically add the previous call to the
60 // dependence.
61 SDValue InChain = DAG.getEntryNode();
62
63 TargetLowering::ArgListTy Args;
64 TargetLowering::ArgListEntry Entry;
65 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +000066 EVT ArgVT = Op.getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +000067 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelc9c8b2a2009-01-26 03:31:40 +000068 Entry.Node = Op.getOperand(i);
69 Entry.Ty = ArgTy;
70 Entry.isSExt = isSigned;
71 Entry.isZExt = !isSigned;
72 Args.push_back(Entry);
73 }
74 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
75 TLI.getPointerTy());
76
77 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000078 Type *RetTy =
Owen Anderson23b9b192009-08-12 00:36:31 +000079 Op.getNode()->getValueType(0).getTypeForEVT(*DAG.getContext());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +000080 TargetLowering::CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned,
81 false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +000082 0, TLI.getLibcallCallingConv(LC),
83 /*isTailCall=*/false,
Justin Holewinskid2ea0e12012-05-25 16:35:28 +000084 /*doesNotRet=*/false,
85 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +000086 Callee, Args, DAG, Op.getDebugLoc());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +000087 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
Scott Michelc9c8b2a2009-01-26 03:31:40 +000088
89 return CallInfo.first;
90 }
Scott Michel266bc8f2007-12-04 22:23:35 +000091}
92
93SPUTargetLowering::SPUTargetLowering(SPUTargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000094 : TargetLowering(TM, new TargetLoweringObjectFileELF()),
95 SPUTM(TM) {
Scott Michel266bc8f2007-12-04 22:23:35 +000096
97 // Use _setjmp/_longjmp instead of setjmp/longjmp.
98 setUseUnderscoreSetJmp(true);
99 setUseUnderscoreLongJmp(true);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000100
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000101 // Set RTLIB libcall names as used by SPU:
102 setLibcallName(RTLIB::DIV_F64, "__fast_divdf3");
103
Scott Michel266bc8f2007-12-04 22:23:35 +0000104 // Set up the SPU's register classes:
Craig Topper420761a2012-04-20 07:30:17 +0000105 addRegisterClass(MVT::i8, &SPU::R8CRegClass);
106 addRegisterClass(MVT::i16, &SPU::R16CRegClass);
107 addRegisterClass(MVT::i32, &SPU::R32CRegClass);
108 addRegisterClass(MVT::i64, &SPU::R64CRegClass);
109 addRegisterClass(MVT::f32, &SPU::R32FPRegClass);
110 addRegisterClass(MVT::f64, &SPU::R64FPRegClass);
111 addRegisterClass(MVT::i128, &SPU::GPRCRegClass);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000112
Scott Michel266bc8f2007-12-04 22:23:35 +0000113 // SPU has no sign or zero extended loads for i1, i8, i16:
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
115 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
116 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
Scott Michel266bc8f2007-12-04 22:23:35 +0000117
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
119 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Scott Michelb30e8f62008-12-02 19:53:53 +0000120
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 setTruncStoreAction(MVT::i128, MVT::i64, Expand);
122 setTruncStoreAction(MVT::i128, MVT::i32, Expand);
123 setTruncStoreAction(MVT::i128, MVT::i16, Expand);
124 setTruncStoreAction(MVT::i128, MVT::i8, Expand);
Eli Friedman5427d712009-07-17 06:36:24 +0000125
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman5427d712009-07-17 06:36:24 +0000127
Scott Michel266bc8f2007-12-04 22:23:35 +0000128 // SPU constant load actions are custom lowered:
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
130 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Scott Michel266bc8f2007-12-04 22:23:35 +0000131
132 // SPU's loads and stores have to be custom lowered:
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 for (unsigned sctype = (unsigned) MVT::i8; sctype < (unsigned) MVT::i128;
Scott Michel266bc8f2007-12-04 22:23:35 +0000134 ++sctype) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 MVT::SimpleValueType VT = (MVT::SimpleValueType)sctype;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000136
Scott Michelf0569be2008-12-27 04:51:36 +0000137 setOperationAction(ISD::LOAD, VT, Custom);
138 setOperationAction(ISD::STORE, VT, Custom);
139 setLoadExtAction(ISD::EXTLOAD, VT, Custom);
140 setLoadExtAction(ISD::ZEXTLOAD, VT, Custom);
141 setLoadExtAction(ISD::SEXTLOAD, VT, Custom);
142
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 for (unsigned stype = sctype - 1; stype >= (unsigned) MVT::i8; --stype) {
144 MVT::SimpleValueType StoreVT = (MVT::SimpleValueType) stype;
Scott Michelf0569be2008-12-27 04:51:36 +0000145 setTruncStoreAction(VT, StoreVT, Expand);
146 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000147 }
148
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 for (unsigned sctype = (unsigned) MVT::f32; sctype < (unsigned) MVT::f64;
Scott Michelf0569be2008-12-27 04:51:36 +0000150 ++sctype) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 MVT::SimpleValueType VT = (MVT::SimpleValueType) sctype;
Scott Michelf0569be2008-12-27 04:51:36 +0000152
153 setOperationAction(ISD::LOAD, VT, Custom);
154 setOperationAction(ISD::STORE, VT, Custom);
155
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 for (unsigned stype = sctype - 1; stype >= (unsigned) MVT::f32; --stype) {
157 MVT::SimpleValueType StoreVT = (MVT::SimpleValueType) stype;
Scott Michelf0569be2008-12-27 04:51:36 +0000158 setTruncStoreAction(VT, StoreVT, Expand);
159 }
160 }
161
Scott Michel266bc8f2007-12-04 22:23:35 +0000162 // Expand the jumptable branches
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
164 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Scott Michel7a1c9e92008-11-22 23:50:42 +0000165
166 // Custom lower SELECT_CC for most cases, but expand by default
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
168 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
169 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
170 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
171 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Scott Michel266bc8f2007-12-04 22:23:35 +0000172
173 // SPU has no intrinsics for these particular operations:
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000175 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000176
Eli Friedman5427d712009-07-17 06:36:24 +0000177 // SPU has no division/remainder instructions
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::SREM, MVT::i8, Expand);
179 setOperationAction(ISD::UREM, MVT::i8, Expand);
180 setOperationAction(ISD::SDIV, MVT::i8, Expand);
181 setOperationAction(ISD::UDIV, MVT::i8, Expand);
182 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
183 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
184 setOperationAction(ISD::SREM, MVT::i16, Expand);
185 setOperationAction(ISD::UREM, MVT::i16, Expand);
186 setOperationAction(ISD::SDIV, MVT::i16, Expand);
187 setOperationAction(ISD::UDIV, MVT::i16, Expand);
188 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
189 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
190 setOperationAction(ISD::SREM, MVT::i32, Expand);
191 setOperationAction(ISD::UREM, MVT::i32, Expand);
192 setOperationAction(ISD::SDIV, MVT::i32, Expand);
193 setOperationAction(ISD::UDIV, MVT::i32, Expand);
194 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
195 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
196 setOperationAction(ISD::SREM, MVT::i64, Expand);
197 setOperationAction(ISD::UREM, MVT::i64, Expand);
198 setOperationAction(ISD::SDIV, MVT::i64, Expand);
199 setOperationAction(ISD::UDIV, MVT::i64, Expand);
200 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
201 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
202 setOperationAction(ISD::SREM, MVT::i128, Expand);
203 setOperationAction(ISD::UREM, MVT::i128, Expand);
204 setOperationAction(ISD::SDIV, MVT::i128, Expand);
205 setOperationAction(ISD::UDIV, MVT::i128, Expand);
206 setOperationAction(ISD::SDIVREM, MVT::i128, Expand);
207 setOperationAction(ISD::UDIVREM, MVT::i128, Expand);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000208
Scott Michel266bc8f2007-12-04 22:23:35 +0000209 // We don't support sin/cos/sqrt/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::FSIN , MVT::f64, Expand);
211 setOperationAction(ISD::FCOS , MVT::f64, Expand);
212 setOperationAction(ISD::FREM , MVT::f64, Expand);
213 setOperationAction(ISD::FSIN , MVT::f32, Expand);
214 setOperationAction(ISD::FCOS , MVT::f32, Expand);
215 setOperationAction(ISD::FREM , MVT::f32, Expand);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000216
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000217 // Expand fsqrt to the appropriate libcall (NOTE: should use h/w fsqrt
218 // for f32!)
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
220 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000221
Cameron Zwarich33390842011-07-08 21:39:21 +0000222 setOperationAction(ISD::FMA, MVT::f64, Expand);
223 setOperationAction(ISD::FMA, MVT::f32, Expand);
224
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
226 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Scott Michel266bc8f2007-12-04 22:23:35 +0000227
228 // SPU can do rotate right and left, so legalize it... but customize for i8
229 // because instructions don't exist.
Bill Wendling9440e352008-08-31 02:59:23 +0000230
231 // FIXME: Change from "expand" to appropriate type once ROTR is supported in
232 // .td files.
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setOperationAction(ISD::ROTR, MVT::i32, Expand /*Legal*/);
234 setOperationAction(ISD::ROTR, MVT::i16, Expand /*Legal*/);
235 setOperationAction(ISD::ROTR, MVT::i8, Expand /*Custom*/);
Bill Wendling9440e352008-08-31 02:59:23 +0000236
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setOperationAction(ISD::ROTL, MVT::i32, Legal);
238 setOperationAction(ISD::ROTL, MVT::i16, Legal);
239 setOperationAction(ISD::ROTL, MVT::i8, Custom);
Scott Micheldc91bea2008-11-20 16:36:33 +0000240
Scott Michel266bc8f2007-12-04 22:23:35 +0000241 // SPU has no native version of shift left/right for i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 setOperationAction(ISD::SHL, MVT::i8, Custom);
243 setOperationAction(ISD::SRL, MVT::i8, Custom);
244 setOperationAction(ISD::SRA, MVT::i8, Custom);
Scott Michel9c0c6b22008-11-21 02:56:16 +0000245
Scott Michel02d711b2008-12-30 23:28:25 +0000246 // Make these operations legal and handle them during instruction selection:
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 setOperationAction(ISD::SHL, MVT::i64, Legal);
248 setOperationAction(ISD::SRL, MVT::i64, Legal);
249 setOperationAction(ISD::SRA, MVT::i64, Legal);
Scott Michel266bc8f2007-12-04 22:23:35 +0000250
Scott Michel5af8f0e2008-07-16 17:17:29 +0000251 // Custom lower i8, i32 and i64 multiplications
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::MUL, MVT::i8, Custom);
253 setOperationAction(ISD::MUL, MVT::i32, Legal);
254 setOperationAction(ISD::MUL, MVT::i64, Legal);
Scott Michel9c0c6b22008-11-21 02:56:16 +0000255
Eli Friedman6314ac22009-06-16 06:40:59 +0000256 // Expand double-width multiplication
257 // FIXME: It would probably be reasonable to support some of these operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
259 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
260 setOperationAction(ISD::MULHU, MVT::i8, Expand);
261 setOperationAction(ISD::MULHS, MVT::i8, Expand);
262 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
263 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
264 setOperationAction(ISD::MULHU, MVT::i16, Expand);
265 setOperationAction(ISD::MULHS, MVT::i16, Expand);
266 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
267 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
268 setOperationAction(ISD::MULHU, MVT::i32, Expand);
269 setOperationAction(ISD::MULHS, MVT::i32, Expand);
270 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
271 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
272 setOperationAction(ISD::MULHU, MVT::i64, Expand);
273 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Eli Friedman6314ac22009-06-16 06:40:59 +0000274
Scott Michel8bf61e82008-06-02 22:18:03 +0000275 // Need to custom handle (some) common i8, i64 math ops
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::ADD, MVT::i8, Custom);
277 setOperationAction(ISD::ADD, MVT::i64, Legal);
278 setOperationAction(ISD::SUB, MVT::i8, Custom);
279 setOperationAction(ISD::SUB, MVT::i64, Legal);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000280
Scott Michel266bc8f2007-12-04 22:23:35 +0000281 // SPU does not have BSWAP. It does have i32 support CTLZ.
282 // CTPOP has to be custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
284 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Scott Michel266bc8f2007-12-04 22:23:35 +0000285
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::CTPOP, MVT::i8, Custom);
287 setOperationAction(ISD::CTPOP, MVT::i16, Custom);
288 setOperationAction(ISD::CTPOP, MVT::i32, Custom);
289 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
290 setOperationAction(ISD::CTPOP, MVT::i128, Expand);
Scott Michel266bc8f2007-12-04 22:23:35 +0000291
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::CTTZ , MVT::i8, Expand);
293 setOperationAction(ISD::CTTZ , MVT::i16, Expand);
294 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
295 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
296 setOperationAction(ISD::CTTZ , MVT::i128, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000297 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Expand);
298 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Expand);
299 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
300 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
301 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i128, Expand);
Scott Michel266bc8f2007-12-04 22:23:35 +0000302
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::CTLZ , MVT::i8, Promote);
304 setOperationAction(ISD::CTLZ , MVT::i16, Promote);
305 setOperationAction(ISD::CTLZ , MVT::i32, Legal);
306 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
307 setOperationAction(ISD::CTLZ , MVT::i128, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000308 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Expand);
309 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Expand);
310 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
311 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
312 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i128, Expand);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000313
Scott Michel8bf61e82008-06-02 22:18:03 +0000314 // SPU has a version of select that implements (a&~c)|(b&c), just like
Scott Michel405fba12008-03-10 23:49:09 +0000315 // select ought to work:
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::SELECT, MVT::i8, Legal);
317 setOperationAction(ISD::SELECT, MVT::i16, Legal);
318 setOperationAction(ISD::SELECT, MVT::i32, Legal);
319 setOperationAction(ISD::SELECT, MVT::i64, Legal);
Scott Michel266bc8f2007-12-04 22:23:35 +0000320
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::SETCC, MVT::i8, Legal);
322 setOperationAction(ISD::SETCC, MVT::i16, Legal);
323 setOperationAction(ISD::SETCC, MVT::i32, Legal);
324 setOperationAction(ISD::SETCC, MVT::i64, Legal);
325 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Scott Michelad2715e2008-03-05 23:02:02 +0000326
Scott Michelf0569be2008-12-27 04:51:36 +0000327 // Custom lower i128 -> i64 truncates
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::TRUNCATE, MVT::i64, Custom);
Scott Michelb30e8f62008-12-02 19:53:53 +0000329
Scott Michel77f452d2009-08-25 22:37:34 +0000330 // Custom lower i32/i64 -> i128 sign extend
Scott Michelf1fa4fd2009-08-24 22:28:53 +0000331 setOperationAction(ISD::SIGN_EXTEND, MVT::i128, Custom);
332
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
334 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
335 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
336 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000337 // SPU has a legal FP -> signed INT instruction for f32, but for f64, need
338 // to expand to a libcall, hence the custom lowering:
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
340 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
341 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Expand);
342 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
343 setOperationAction(ISD::FP_TO_SINT, MVT::i128, Expand);
344 setOperationAction(ISD::FP_TO_UINT, MVT::i128, Expand);
Scott Michel266bc8f2007-12-04 22:23:35 +0000345
346 // FDIV on SPU requires custom lowering
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::FDIV, MVT::f64, Expand); // to libcall
Scott Michel266bc8f2007-12-04 22:23:35 +0000348
Scott Michel9de57a92009-01-26 22:33:37 +0000349 // SPU has [U|S]INT_TO_FP for f32->i32, but not for f64->i32, f64->i64:
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
351 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
352 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
353 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
354 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
355 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
356 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
357 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Scott Michel266bc8f2007-12-04 22:23:35 +0000358
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000359 setOperationAction(ISD::BITCAST, MVT::i32, Legal);
360 setOperationAction(ISD::BITCAST, MVT::f32, Legal);
361 setOperationAction(ISD::BITCAST, MVT::i64, Legal);
362 setOperationAction(ISD::BITCAST, MVT::f64, Legal);
Scott Michel266bc8f2007-12-04 22:23:35 +0000363
364 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000365 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000366
Scott Michel5af8f0e2008-07-16 17:17:29 +0000367 // We want to legalize GlobalAddress and ConstantPool nodes into the
Scott Michel266bc8f2007-12-04 22:23:35 +0000368 // appropriate instructions to materialize the address.
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 for (unsigned sctype = (unsigned) MVT::i8; sctype < (unsigned) MVT::f128;
Scott Michel053c1da2008-01-29 02:16:57 +0000370 ++sctype) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 MVT::SimpleValueType VT = (MVT::SimpleValueType)sctype;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000372
Scott Michel1df30c42008-12-29 03:23:36 +0000373 setOperationAction(ISD::GlobalAddress, VT, Custom);
374 setOperationAction(ISD::ConstantPool, VT, Custom);
375 setOperationAction(ISD::JumpTable, VT, Custom);
Scott Michel053c1da2008-01-29 02:16:57 +0000376 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000377
Scott Michel266bc8f2007-12-04 22:23:35 +0000378 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000380
Scott Michel266bc8f2007-12-04 22:23:35 +0000381 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::VAARG , MVT::Other, Expand);
383 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
384 setOperationAction(ISD::VAEND , MVT::Other, Expand);
385 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
386 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
387 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
388 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Expand);
Scott Michel266bc8f2007-12-04 22:23:35 +0000389
390 // Cell SPU has instructions for converting between i64 and fp.
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
392 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000393
Scott Michel266bc8f2007-12-04 22:23:35 +0000394 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
Scott Michel266bc8f2007-12-04 22:23:35 +0000396
397 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Scott Michel266bc8f2007-12-04 22:23:35 +0000399
400 // First set operation action for all vector types to expand. Then we
401 // will selectively turn on ones that can be effectively codegen'd.
Craig Topper420761a2012-04-20 07:30:17 +0000402 addRegisterClass(MVT::v16i8, &SPU::VECREGRegClass);
403 addRegisterClass(MVT::v8i16, &SPU::VECREGRegClass);
404 addRegisterClass(MVT::v4i32, &SPU::VECREGRegClass);
405 addRegisterClass(MVT::v2i64, &SPU::VECREGRegClass);
406 addRegisterClass(MVT::v4f32, &SPU::VECREGRegClass);
407 addRegisterClass(MVT::v2f64, &SPU::VECREGRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +0000408
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
410 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
411 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Scott Michel266bc8f2007-12-04 22:23:35 +0000412
Nadav Rotem34804c42011-10-04 12:05:35 +0000413 // Set operation actions to legal types only.
414 if (!isTypeLegal(VT)) continue;
415
Duncan Sands83ec4b62008-06-06 12:08:01 +0000416 // add/sub are legal for all supported vector VT's.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000417 setOperationAction(ISD::ADD, VT, Legal);
418 setOperationAction(ISD::SUB, VT, Legal);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000419 // mul has to be custom lowered.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000420 setOperationAction(ISD::MUL, VT, Legal);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000421
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000422 setOperationAction(ISD::AND, VT, Legal);
423 setOperationAction(ISD::OR, VT, Legal);
424 setOperationAction(ISD::XOR, VT, Legal);
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000425 setOperationAction(ISD::LOAD, VT, Custom);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000426 setOperationAction(ISD::SELECT, VT, Legal);
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000427 setOperationAction(ISD::STORE, VT, Custom);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000428
Scott Michel266bc8f2007-12-04 22:23:35 +0000429 // These operations need to be expanded:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000430 setOperationAction(ISD::SDIV, VT, Expand);
431 setOperationAction(ISD::SREM, VT, Expand);
432 setOperationAction(ISD::UDIV, VT, Expand);
433 setOperationAction(ISD::UREM, VT, Expand);
Scott Michel266bc8f2007-12-04 22:23:35 +0000434
Nadav Rotem4d83b792011-10-15 20:05:17 +0000435 // Expand all trunc stores
436 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
437 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
438 MVT::SimpleValueType TargetVT = (MVT::SimpleValueType)j;
439 setTruncStoreAction(VT, TargetVT, Expand);
440 }
441
Scott Michel266bc8f2007-12-04 22:23:35 +0000442 // Custom lower build_vector, constant pool spills, insert and
443 // extract vector elements:
Nadav Rotem34804c42011-10-04 12:05:35 +0000444 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
445 setOperationAction(ISD::ConstantPool, VT, Custom);
446 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
447 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
448 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
449 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
Scott Michel266bc8f2007-12-04 22:23:35 +0000450 }
451
Nadav Rotem4d83b792011-10-15 20:05:17 +0000452 setOperationAction(ISD::SHL, MVT::v2i64, Expand);
453
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::AND, MVT::v16i8, Custom);
455 setOperationAction(ISD::OR, MVT::v16i8, Custom);
456 setOperationAction(ISD::XOR, MVT::v16i8, Custom);
457 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000458
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Scott Michel1df30c42008-12-29 03:23:36 +0000460
Scott Michelf0569be2008-12-27 04:51:36 +0000461 setBooleanContents(ZeroOrNegativeOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000462 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); // FIXME: Is this correct?
Scott Michel5af8f0e2008-07-16 17:17:29 +0000463
Scott Michel266bc8f2007-12-04 22:23:35 +0000464 setStackPointerRegisterToSaveRestore(SPU::R1);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000465
Scott Michel266bc8f2007-12-04 22:23:35 +0000466 // We have target-specific dag combine patterns for the following nodes:
Scott Michel053c1da2008-01-29 02:16:57 +0000467 setTargetDAGCombine(ISD::ADD);
Scott Michela59d4692008-02-23 18:41:37 +0000468 setTargetDAGCombine(ISD::ZERO_EXTEND);
469 setTargetDAGCombine(ISD::SIGN_EXTEND);
470 setTargetDAGCombine(ISD::ANY_EXTEND);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000471
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000472 setMinFunctionAlignment(3);
473
Scott Michel266bc8f2007-12-04 22:23:35 +0000474 computeRegisterProperties();
Scott Michel7a1c9e92008-11-22 23:50:42 +0000475
Scott Michele07d3de2008-12-09 03:37:19 +0000476 // Set pre-RA register scheduler default to BURR, which produces slightly
477 // better code than the default (could also be TDRR, but TargetLowering.h
478 // needs a mod to support that model):
Evan Cheng211ffa12010-05-19 20:19:50 +0000479 setSchedulingPreference(Sched::RegPressure);
Scott Michel266bc8f2007-12-04 22:23:35 +0000480}
481
Benjamin Kramer57a76602012-03-11 18:12:04 +0000482const char *SPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
483 switch (Opcode) {
484 default: return 0;
485 case SPUISD::RET_FLAG: return "SPUISD::RET_FLAG";
486 case SPUISD::Hi: return "SPUISD::Hi";
487 case SPUISD::Lo: return "SPUISD::Lo";
488 case SPUISD::PCRelAddr: return "SPUISD::PCRelAddr";
489 case SPUISD::AFormAddr: return "SPUISD::AFormAddr";
490 case SPUISD::IndirectAddr: return "SPUISD::IndirectAddr";
491 case SPUISD::LDRESULT: return "SPUISD::LDRESULT";
492 case SPUISD::CALL: return "SPUISD::CALL";
493 case SPUISD::SHUFB: return "SPUISD::SHUFB";
494 case SPUISD::SHUFFLE_MASK: return "SPUISD::SHUFFLE_MASK";
495 case SPUISD::CNTB: return "SPUISD::CNTB";
496 case SPUISD::PREFSLOT2VEC: return "SPUISD::PREFSLOT2VEC";
497 case SPUISD::VEC2PREFSLOT: return "SPUISD::VEC2PREFSLOT";
498 case SPUISD::SHL_BITS: return "SPUISD::SHL_BITS";
499 case SPUISD::SHL_BYTES: return "SPUISD::SHL_BYTES";
500 case SPUISD::VEC_ROTL: return "SPUISD::VEC_ROTL";
501 case SPUISD::VEC_ROTR: return "SPUISD::VEC_ROTR";
502 case SPUISD::ROTBYTES_LEFT: return "SPUISD::ROTBYTES_LEFT";
503 case SPUISD::ROTBYTES_LEFT_BITS: return "SPUISD::ROTBYTES_LEFT_BITS";
504 case SPUISD::SELECT_MASK: return "SPUISD::SELECT_MASK";
505 case SPUISD::SELB: return "SPUISD::SELB";
506 case SPUISD::ADD64_MARKER: return "SPUISD::ADD64_MARKER";
507 case SPUISD::SUB64_MARKER: return "SPUISD::SUB64_MARKER";
508 case SPUISD::MUL64_MARKER: return "SPUISD::MUL64_MARKER";
Scott Michel266bc8f2007-12-04 22:23:35 +0000509 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000510}
511
Scott Michelf0569be2008-12-27 04:51:36 +0000512//===----------------------------------------------------------------------===//
513// Return the Cell SPU's SETCC result type
514//===----------------------------------------------------------------------===//
515
Duncan Sands28b77e92011-09-06 19:07:46 +0000516EVT SPUTargetLowering::getSetCCResultType(EVT VT) const {
Kalle Raiskila7de81012010-11-24 12:59:16 +0000517 // i8, i16 and i32 are valid SETCC result types
518 MVT::SimpleValueType retval;
519
520 switch(VT.getSimpleVT().SimpleTy){
521 case MVT::i1:
522 case MVT::i8:
523 retval = MVT::i8; break;
524 case MVT::i16:
525 retval = MVT::i16; break;
526 case MVT::i32:
527 default:
528 retval = MVT::i32;
529 }
530 return retval;
Scott Michel78c47fa2008-03-10 16:58:52 +0000531}
532
Scott Michel266bc8f2007-12-04 22:23:35 +0000533//===----------------------------------------------------------------------===//
534// Calling convention code:
535//===----------------------------------------------------------------------===//
536
537#include "SPUGenCallingConv.inc"
538
539//===----------------------------------------------------------------------===//
540// LowerOperation implementation
541//===----------------------------------------------------------------------===//
542
543/// Custom lower loads for CellSPU
544/*!
545 All CellSPU loads and stores are aligned to 16-byte boundaries, so for elements
546 within a 16-byte block, we have to rotate to extract the requested element.
Scott Michel30ee7df2008-12-04 03:02:42 +0000547
548 For extending loads, we also want to ensure that the following sequence is
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 emitted, e.g. for MVT::f32 extending load to MVT::f64:
Scott Michel30ee7df2008-12-04 03:02:42 +0000550
551\verbatim
Scott Michel1df30c42008-12-29 03:23:36 +0000552%1 v16i8,ch = load
Scott Michel30ee7df2008-12-04 03:02:42 +0000553%2 v16i8,ch = rotate %1
Scott Michel1df30c42008-12-29 03:23:36 +0000554%3 v4f8, ch = bitconvert %2
Scott Michel30ee7df2008-12-04 03:02:42 +0000555%4 f32 = vec2perfslot %3
556%5 f64 = fp_extend %4
557\endverbatim
558*/
Dan Gohman475871a2008-07-27 21:46:04 +0000559static SDValue
560LowerLOAD(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000561 LoadSDNode *LN = cast<LoadSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000562 SDValue the_chain = LN->getChain();
Owen Andersone50ed302009-08-10 22:56:29 +0000563 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
564 EVT InVT = LN->getMemoryVT();
565 EVT OutVT = Op.getValueType();
Scott Michel266bc8f2007-12-04 22:23:35 +0000566 ISD::LoadExtType ExtType = LN->getExtensionType();
567 unsigned alignment = LN->getAlignment();
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000568 int pso = prefslotOffset(InVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000569 DebugLoc dl = Op.getDebugLoc();
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000570 EVT vecVT = InVT.isVector()? InVT: EVT::getVectorVT(*DAG.getContext(), InVT,
571 (128 / InVT.getSizeInBits()));
Scott Michel266bc8f2007-12-04 22:23:35 +0000572
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000573 // two sanity checks
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000574 assert( LN->getAddressingMode() == ISD::UNINDEXED
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000575 && "we should get only UNINDEXED adresses");
576 // clean aligned loads can be selected as-is
Kalle Raiskila8702e8b2011-01-17 11:59:20 +0000577 if (InVT.getSizeInBits() == 128 && (alignment%16) == 0)
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000578 return SDValue();
579
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000580 // Get pointerinfos to the memory chunk(s) that contain the data to load
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000581 uint64_t mpi_offset = LN->getPointerInfo().Offset;
582 mpi_offset -= mpi_offset%16;
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000583 MachinePointerInfo lowMemPtr(LN->getPointerInfo().V, mpi_offset);
584 MachinePointerInfo highMemPtr(LN->getPointerInfo().V, mpi_offset+16);
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000585
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000586 SDValue result;
587 SDValue basePtr = LN->getBasePtr();
588 SDValue rotate;
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000589
Kalle Raiskila8702e8b2011-01-17 11:59:20 +0000590 if ((alignment%16) == 0) {
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000591 ConstantSDNode *CN;
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000592
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000593 // Special cases for a known aligned load to simplify the base pointer
594 // and the rotation amount:
595 if (basePtr.getOpcode() == ISD::ADD
596 && (CN = dyn_cast<ConstantSDNode > (basePtr.getOperand(1))) != 0) {
597 // Known offset into basePtr
598 int64_t offset = CN->getSExtValue();
599 int64_t rotamt = int64_t((offset & 0xf) - pso);
Scott Michel266bc8f2007-12-04 22:23:35 +0000600
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000601 if (rotamt < 0)
602 rotamt += 16;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000603
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000604 rotate = DAG.getConstant(rotamt, MVT::i16);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000605
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000606 // Simplify the base pointer for this case:
607 basePtr = basePtr.getOperand(0);
608 if ((offset & ~0xf) > 0) {
Dale Johannesende064702009-02-06 21:50:26 +0000609 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michelf0569be2008-12-27 04:51:36 +0000610 basePtr,
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000611 DAG.getConstant((offset & ~0xf), PtrVT));
Scott Michelf0569be2008-12-27 04:51:36 +0000612 }
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000613 } else if ((basePtr.getOpcode() == SPUISD::AFormAddr)
614 || (basePtr.getOpcode() == SPUISD::IndirectAddr
615 && basePtr.getOperand(0).getOpcode() == SPUISD::Hi
616 && basePtr.getOperand(1).getOpcode() == SPUISD::Lo)) {
617 // Plain aligned a-form address: rotate into preferred slot
618 // Same for (SPUindirect (SPUhi ...), (SPUlo ...))
619 int64_t rotamt = -pso;
620 if (rotamt < 0)
621 rotamt += 16;
622 rotate = DAG.getConstant(rotamt, MVT::i16);
623 } else {
Scott Michelf0569be2008-12-27 04:51:36 +0000624 // Offset the rotate amount by the basePtr and the preferred slot
625 // byte offset
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000626 int64_t rotamt = -pso;
627 if (rotamt < 0)
628 rotamt += 16;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000629 rotate = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michelf0569be2008-12-27 04:51:36 +0000630 basePtr,
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000631 DAG.getConstant(rotamt, PtrVT));
Scott Michel266bc8f2007-12-04 22:23:35 +0000632 }
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000633 } else {
634 // Unaligned load: must be more pessimistic about addressing modes:
635 if (basePtr.getOpcode() == ISD::ADD) {
636 MachineFunction &MF = DAG.getMachineFunction();
637 MachineRegisterInfo &RegInfo = MF.getRegInfo();
638 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
639 SDValue Flag;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000640
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000641 SDValue Op0 = basePtr.getOperand(0);
642 SDValue Op1 = basePtr.getOperand(1);
643
644 if (isa<ConstantSDNode>(Op1)) {
645 // Convert the (add <ptr>, <const>) to an indirect address contained
646 // in a register. Note that this is done because we need to avoid
647 // creating a 0(reg) d-form address due to the SPU's block loads.
648 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
649 the_chain = DAG.getCopyToReg(the_chain, dl, VReg, basePtr, Flag);
650 basePtr = DAG.getCopyFromReg(the_chain, dl, VReg, PtrVT);
651 } else {
652 // Convert the (add <arg1>, <arg2>) to an indirect address, which
653 // will likely be lowered as a reg(reg) x-form address.
654 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
655 }
656 } else {
657 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
658 basePtr,
659 DAG.getConstant(0, PtrVT));
660 }
661
662 // Offset the rotate amount by the basePtr and the preferred slot
663 // byte offset
664 rotate = DAG.getNode(ISD::ADD, dl, PtrVT,
665 basePtr,
666 DAG.getConstant(-pso, PtrVT));
667 }
668
669 // Do the load as a i128 to allow possible shifting
670 SDValue low = DAG.getLoad(MVT::i128, dl, the_chain, basePtr,
671 lowMemPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000672 LN->isVolatile(), LN->isNonTemporal(), false, 16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000673
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000674 // When the size is not greater than alignment we get all data with just
675 // one load
676 if (alignment >= InVT.getSizeInBits()/8) {
Scott Michelf0569be2008-12-27 04:51:36 +0000677 // Update the chain
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000678 the_chain = low.getValue(1);
Scott Michelf0569be2008-12-27 04:51:36 +0000679
680 // Rotate into the preferred slot:
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000681 result = DAG.getNode(SPUISD::ROTBYTES_LEFT, dl, MVT::i128,
682 low.getValue(0), rotate);
Scott Michelf0569be2008-12-27 04:51:36 +0000683
Scott Michel30ee7df2008-12-04 03:02:42 +0000684 // Convert the loaded v16i8 vector to the appropriate vector type
685 // specified by the operand:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000686 EVT vecVT = EVT::getVectorVT(*DAG.getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +0000687 InVT, (128 / InVT.getSizeInBits()));
Dale Johannesen33c960f2009-02-04 20:06:27 +0000688 result = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, InVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000689 DAG.getNode(ISD::BITCAST, dl, vecVT, result));
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000690 }
691 // When alignment is less than the size, we might need (known only at
692 // run-time) two loads
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000693 // TODO: if the memory address is composed only from constants, we have
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000694 // extra kowledge, and might avoid the second load
695 else {
696 // storage position offset from lower 16 byte aligned memory chunk
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000697 SDValue offset = DAG.getNode(ISD::AND, dl, MVT::i32,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000698 basePtr, DAG.getConstant( 0xf, MVT::i32 ) );
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000699 // get a registerfull of ones. (this implementation is a workaround: LLVM
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000700 // cannot handle 128 bit signed int constants)
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000701 SDValue ones = DAG.getConstant(-1, MVT::v4i32 );
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000702 ones = DAG.getNode(ISD::BITCAST, dl, MVT::i128, ones);
Scott Michel5af8f0e2008-07-16 17:17:29 +0000703
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000704 SDValue high = DAG.getLoad(MVT::i128, dl, the_chain,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000705 DAG.getNode(ISD::ADD, dl, PtrVT,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000706 basePtr,
707 DAG.getConstant(16, PtrVT)),
708 highMemPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000709 LN->isVolatile(), LN->isNonTemporal(), false,
710 16);
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000711
712 the_chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, low.getValue(1),
713 high.getValue(1));
714
715 // Shift the (possible) high part right to compensate the misalignemnt.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000716 // if there is no highpart (i.e. value is i64 and offset is 4), this
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000717 // will zero out the high value.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000718 high = DAG.getNode(SPUISD::SRL_BYTES, dl, MVT::i128, high,
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000719 DAG.getNode(ISD::SUB, dl, MVT::i32,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000720 DAG.getConstant( 16, MVT::i32),
721 offset
722 ));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000723
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000724 // Shift the low similarly
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000725 // TODO: add SPUISD::SHL_BYTES
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000726 low = DAG.getNode(SPUISD::SHL_BYTES, dl, MVT::i128, low, offset );
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000727
728 // Merge the two parts
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000729 result = DAG.getNode(ISD::BITCAST, dl, vecVT,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000730 DAG.getNode(ISD::OR, dl, MVT::i128, low, high));
731
732 if (!InVT.isVector()) {
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000733 result = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, InVT, result );
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000734 }
735
736 }
Scott Michel30ee7df2008-12-04 03:02:42 +0000737 // Handle extending loads by extending the scalar result:
738 if (ExtType == ISD::SEXTLOAD) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000739 result = DAG.getNode(ISD::SIGN_EXTEND, dl, OutVT, result);
Scott Michel30ee7df2008-12-04 03:02:42 +0000740 } else if (ExtType == ISD::ZEXTLOAD) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000741 result = DAG.getNode(ISD::ZERO_EXTEND, dl, OutVT, result);
Scott Michel30ee7df2008-12-04 03:02:42 +0000742 } else if (ExtType == ISD::EXTLOAD) {
743 unsigned NewOpc = ISD::ANY_EXTEND;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000744
Scott Michel30ee7df2008-12-04 03:02:42 +0000745 if (OutVT.isFloatingPoint())
Scott Michel19c10e62009-01-26 03:37:41 +0000746 NewOpc = ISD::FP_EXTEND;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000747
Dale Johannesen33c960f2009-02-04 20:06:27 +0000748 result = DAG.getNode(NewOpc, dl, OutVT, result);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000749 }
750
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 SDVTList retvts = DAG.getVTList(OutVT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +0000752 SDValue retops[2] = {
Scott Michel58c58182008-01-17 20:38:41 +0000753 result,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000754 the_chain
Scott Michel58c58182008-01-17 20:38:41 +0000755 };
Scott Michel9de5d0d2008-01-11 02:53:15 +0000756
Dale Johannesen33c960f2009-02-04 20:06:27 +0000757 result = DAG.getNode(SPUISD::LDRESULT, dl, retvts,
Scott Michel58c58182008-01-17 20:38:41 +0000758 retops, sizeof(retops) / sizeof(retops[0]));
Scott Michel9de5d0d2008-01-11 02:53:15 +0000759 return result;
Scott Michel266bc8f2007-12-04 22:23:35 +0000760}
761
762/// Custom lower stores for CellSPU
763/*!
764 All CellSPU stores are aligned to 16-byte boundaries, so for elements
765 within a 16-byte block, we have to generate a shuffle to insert the
766 requested element into its place, then store the resulting block.
767 */
Dan Gohman475871a2008-07-27 21:46:04 +0000768static SDValue
769LowerSTORE(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000770 StoreSDNode *SN = cast<StoreSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000771 SDValue Value = SN->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000772 EVT VT = Value.getValueType();
773 EVT StVT = (!SN->isTruncatingStore() ? VT : SN->getMemoryVT());
774 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000775 DebugLoc dl = Op.getDebugLoc();
Scott Michel9de5d0d2008-01-11 02:53:15 +0000776 unsigned alignment = SN->getAlignment();
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000777 SDValue result;
778 EVT vecVT = StVT.isVector()? StVT: EVT::getVectorVT(*DAG.getContext(), StVT,
779 (128 / StVT.getSizeInBits()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000780 // Get pointerinfos to the memory chunk(s) that contain the data to load
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000781 uint64_t mpi_offset = SN->getPointerInfo().Offset;
782 mpi_offset -= mpi_offset%16;
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000783 MachinePointerInfo lowMemPtr(SN->getPointerInfo().V, mpi_offset);
784 MachinePointerInfo highMemPtr(SN->getPointerInfo().V, mpi_offset+16);
Scott Michel266bc8f2007-12-04 22:23:35 +0000785
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000786
787 // two sanity checks
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000788 assert( SN->getAddressingMode() == ISD::UNINDEXED
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000789 && "we should get only UNINDEXED adresses");
790 // clean aligned loads can be selected as-is
Kalle Raiskila8702e8b2011-01-17 11:59:20 +0000791 if (StVT.getSizeInBits() == 128 && (alignment%16) == 0)
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000792 return SDValue();
793
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000794 SDValue alignLoadVec;
795 SDValue basePtr = SN->getBasePtr();
796 SDValue the_chain = SN->getChain();
797 SDValue insertEltOffs;
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000798
Kalle Raiskila8702e8b2011-01-17 11:59:20 +0000799 if ((alignment%16) == 0) {
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000800 ConstantSDNode *CN;
801 // Special cases for a known aligned load to simplify the base pointer
802 // and insertion byte:
803 if (basePtr.getOpcode() == ISD::ADD
804 && (CN = dyn_cast<ConstantSDNode>(basePtr.getOperand(1))) != 0) {
805 // Known offset into basePtr
806 int64_t offset = CN->getSExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000807
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000808 // Simplify the base pointer for this case:
809 basePtr = basePtr.getOperand(0);
810 insertEltOffs = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
811 basePtr,
812 DAG.getConstant((offset & 0xf), PtrVT));
Scott Michel266bc8f2007-12-04 22:23:35 +0000813
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000814 if ((offset & ~0xf) > 0) {
Dale Johannesende064702009-02-06 21:50:26 +0000815 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michelf0569be2008-12-27 04:51:36 +0000816 basePtr,
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000817 DAG.getConstant((offset & ~0xf), PtrVT));
Scott Michelf0569be2008-12-27 04:51:36 +0000818 }
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000819 } else {
820 // Otherwise, assume it's at byte 0 of basePtr
821 insertEltOffs = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
822 basePtr,
823 DAG.getConstant(0, PtrVT));
824 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michelf0569be2008-12-27 04:51:36 +0000825 basePtr,
826 DAG.getConstant(0, PtrVT));
827 }
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000828 } else {
829 // Unaligned load: must be more pessimistic about addressing modes:
830 if (basePtr.getOpcode() == ISD::ADD) {
831 MachineFunction &MF = DAG.getMachineFunction();
832 MachineRegisterInfo &RegInfo = MF.getRegInfo();
833 unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
834 SDValue Flag;
Scott Michelf0569be2008-12-27 04:51:36 +0000835
Kalle Raiskila38e0c9b2010-11-15 10:12:32 +0000836 SDValue Op0 = basePtr.getOperand(0);
837 SDValue Op1 = basePtr.getOperand(1);
838
839 if (isa<ConstantSDNode>(Op1)) {
840 // Convert the (add <ptr>, <const>) to an indirect address contained
841 // in a register. Note that this is done because we need to avoid
842 // creating a 0(reg) d-form address due to the SPU's block loads.
843 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
844 the_chain = DAG.getCopyToReg(the_chain, dl, VReg, basePtr, Flag);
845 basePtr = DAG.getCopyFromReg(the_chain, dl, VReg, PtrVT);
846 } else {
847 // Convert the (add <arg1>, <arg2>) to an indirect address, which
848 // will likely be lowered as a reg(reg) x-form address.
849 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
850 }
851 } else {
852 basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
853 basePtr,
854 DAG.getConstant(0, PtrVT));
855 }
856
857 // Insertion point is solely determined by basePtr's contents
858 insertEltOffs = DAG.getNode(ISD::ADD, dl, PtrVT,
859 basePtr,
860 DAG.getConstant(0, PtrVT));
861 }
862
863 // Load the lower part of the memory to which to store.
864 SDValue low = DAG.getLoad(vecVT, dl, the_chain, basePtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000865 lowMemPtr, SN->isVolatile(), SN->isNonTemporal(),
866 false, 16);
Scott Michelf0569be2008-12-27 04:51:36 +0000867
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000868 // if we don't need to store over the 16 byte boundary, one store suffices
869 if (alignment >= StVT.getSizeInBits()/8) {
Scott Michelf0569be2008-12-27 04:51:36 +0000870 // Update the chain
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000871 the_chain = low.getValue(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000872
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000873 LoadSDNode *LN = cast<LoadSDNode>(low);
Dan Gohman475871a2008-07-27 21:46:04 +0000874 SDValue theValue = SN->getValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000875
876 if (StVT != VT
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000877 && (theValue.getOpcode() == ISD::AssertZext
878 || theValue.getOpcode() == ISD::AssertSext)) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000879 // Drill down and get the value for zero- and sign-extended
880 // quantities
Scott Michel5af8f0e2008-07-16 17:17:29 +0000881 theValue = theValue.getOperand(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000882 }
883
Scott Michel9de5d0d2008-01-11 02:53:15 +0000884 // If the base pointer is already a D-form address, then just create
885 // a new D-form address with a slot offset and the orignal base pointer.
886 // Otherwise generate a D-form address with the slot offset relative
887 // to the stack pointer, which is always aligned.
Scott Michelf0569be2008-12-27 04:51:36 +0000888#if !defined(NDEBUG)
889 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Chris Lattner4437ae22009-08-23 07:05:07 +0000890 errs() << "CellSPU LowerSTORE: basePtr = ";
Scott Michelf0569be2008-12-27 04:51:36 +0000891 basePtr.getNode()->dump(&DAG);
Chris Lattner4437ae22009-08-23 07:05:07 +0000892 errs() << "\n";
Scott Michelf0569be2008-12-27 04:51:36 +0000893 }
894#endif
Scott Michel9de5d0d2008-01-11 02:53:15 +0000895
Kalle Raiskilaf53fdc22010-08-24 11:05:51 +0000896 SDValue insertEltOp = DAG.getNode(SPUISD::SHUFFLE_MASK, dl, vecVT,
897 insertEltOffs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000898 SDValue vectorizeOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, vecVT,
Kalle Raiskilaf53fdc22010-08-24 11:05:51 +0000899 theValue);
900
Dale Johannesen33c960f2009-02-04 20:06:27 +0000901 result = DAG.getNode(SPUISD::SHUFB, dl, vecVT,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000902 vectorizeOp, low,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000903 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 MVT::v4i32, insertEltOp));
Scott Michel266bc8f2007-12-04 22:23:35 +0000905
Dale Johannesen33c960f2009-02-04 20:06:27 +0000906 result = DAG.getStore(the_chain, dl, result, basePtr,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000907 lowMemPtr,
David Greene73657df2010-02-15 16:55:58 +0000908 LN->isVolatile(), LN->isNonTemporal(),
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000909 16);
Scott Michel266bc8f2007-12-04 22:23:35 +0000910
Scott Michel266bc8f2007-12-04 22:23:35 +0000911 }
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000912 // do the store when it might cross the 16 byte memory access boundary.
913 else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000914 // TODO issue a warning if SN->isVolatile()== true? This is likely not
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000915 // what the user wanted.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000916
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000917 // address offset from nearest lower 16byte alinged address
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000918 SDValue offset = DAG.getNode(ISD::AND, dl, MVT::i32,
919 SN->getBasePtr(),
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000920 DAG.getConstant(0xf, MVT::i32));
921 // 16 - offset
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000922 SDValue offset_compl = DAG.getNode(ISD::SUB, dl, MVT::i32,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000923 DAG.getConstant( 16, MVT::i32),
924 offset);
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000925 // 16 - sizeof(Value)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000926 SDValue surplus = DAG.getNode(ISD::SUB, dl, MVT::i32,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000927 DAG.getConstant( 16, MVT::i32),
928 DAG.getConstant( VT.getSizeInBits()/8,
929 MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000930 // get a registerfull of ones
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000931 SDValue ones = DAG.getConstant(-1, MVT::v4i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000932 ones = DAG.getNode(ISD::BITCAST, dl, MVT::i128, ones);
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000933
934 // Create the 128 bit masks that have ones where the data to store is
935 // located.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000936 SDValue lowmask, himask;
937 // if the value to store don't fill up the an entire 128 bits, zero
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000938 // out the last bits of the mask so that only the value we want to store
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000939 // is masked.
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000940 // this is e.g. in the case of store i32, align 2
941 if (!VT.isVector()){
942 Value = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, Value);
943 lowmask = DAG.getNode(SPUISD::SRL_BYTES, dl, MVT::i128, ones, surplus);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000944 lowmask = DAG.getNode(SPUISD::SHL_BYTES, dl, MVT::i128, lowmask,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000945 surplus);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000946 Value = DAG.getNode(ISD::BITCAST, dl, MVT::i128, Value);
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000947 Value = DAG.getNode(ISD::AND, dl, MVT::i128, Value, lowmask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000948
Torok Edwindac237e2009-07-08 20:53:28 +0000949 }
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000950 else {
951 lowmask = ones;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000952 Value = DAG.getNode(ISD::BITCAST, dl, MVT::i128, Value);
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000953 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000954 // this will zero, if there are no data that goes to the high quad
955 himask = DAG.getNode(SPUISD::SHL_BYTES, dl, MVT::i128, lowmask,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000956 offset_compl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000957 lowmask = DAG.getNode(SPUISD::SRL_BYTES, dl, MVT::i128, lowmask,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000958 offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000959
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000960 // Load in the old data and zero out the parts that will be overwritten with
961 // the new data to store.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000962 SDValue hi = DAG.getLoad(MVT::i128, dl, the_chain,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000963 DAG.getNode(ISD::ADD, dl, PtrVT, basePtr,
964 DAG.getConstant( 16, PtrVT)),
965 highMemPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000966 SN->isVolatile(), SN->isNonTemporal(),
967 false, 16);
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000968 the_chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, low.getValue(1),
969 hi.getValue(1));
Scott Michel266bc8f2007-12-04 22:23:35 +0000970
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000971 low = DAG.getNode(ISD::AND, dl, MVT::i128,
972 DAG.getNode( ISD::BITCAST, dl, MVT::i128, low),
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000973 DAG.getNode( ISD::XOR, dl, MVT::i128, lowmask, ones));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000974 hi = DAG.getNode(ISD::AND, dl, MVT::i128,
975 DAG.getNode( ISD::BITCAST, dl, MVT::i128, hi),
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000976 DAG.getNode( ISD::XOR, dl, MVT::i128, himask, ones));
977
978 // Shift the Value to store into place. rlow contains the parts that go to
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000979 // the lower memory chunk, rhi has the parts that go to the upper one.
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000980 SDValue rlow = DAG.getNode(SPUISD::SRL_BYTES, dl, MVT::i128, Value, offset);
981 rlow = DAG.getNode(ISD::AND, dl, MVT::i128, rlow, lowmask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000982 SDValue rhi = DAG.getNode(SPUISD::SHL_BYTES, dl, MVT::i128, Value,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000983 offset_compl);
984
985 // Merge the old data and the new data and store the results
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000986 // Need to convert vectors here to integer as 'OR'ing floats assert
987 rlow = DAG.getNode(ISD::OR, dl, MVT::i128,
988 DAG.getNode(ISD::BITCAST, dl, MVT::i128, low),
989 DAG.getNode(ISD::BITCAST, dl, MVT::i128, rlow));
990 rhi = DAG.getNode(ISD::OR, dl, MVT::i128,
991 DAG.getNode(ISD::BITCAST, dl, MVT::i128, hi),
992 DAG.getNode(ISD::BITCAST, dl, MVT::i128, rhi));
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000993
994 low = DAG.getStore(the_chain, dl, rlow, basePtr,
995 lowMemPtr,
996 SN->isVolatile(), SN->isNonTemporal(), 16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000997 hi = DAG.getStore(the_chain, dl, rhi,
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +0000998 DAG.getNode(ISD::ADD, dl, PtrVT, basePtr,
999 DAG.getConstant( 16, PtrVT)),
1000 highMemPtr,
1001 SN->isVolatile(), SN->isNonTemporal(), 16);
1002 result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, low.getValue(0),
1003 hi.getValue(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001004 }
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +00001005
1006 return result;
Scott Michel266bc8f2007-12-04 22:23:35 +00001007}
1008
Scott Michel94bd57e2009-01-15 04:41:47 +00001009//! Generate the address of a constant pool entry.
Dan Gohman7db949d2009-08-07 01:32:21 +00001010static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001011LowerConstantPool(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001012 EVT PtrVT = Op.getValueType();
Scott Michel266bc8f2007-12-04 22:23:35 +00001013 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001014 const Constant *C = CP->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +00001015 SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
1016 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Michel9de5d0d2008-01-11 02:53:15 +00001017 const TargetMachine &TM = DAG.getTarget();
Dale Johannesende064702009-02-06 21:50:26 +00001018 // FIXME there is no actual debug info here
1019 DebugLoc dl = Op.getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +00001020
1021 if (TM.getRelocationModel() == Reloc::Static) {
1022 if (!ST->usingLargeMem()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001023 // Just return the SDValue with the constant pool address in it.
Dale Johannesende064702009-02-06 21:50:26 +00001024 return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, CPI, Zero);
Scott Michel266bc8f2007-12-04 22:23:35 +00001025 } else {
Dale Johannesende064702009-02-06 21:50:26 +00001026 SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, CPI, Zero);
1027 SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, CPI, Zero);
1028 return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
Scott Michel266bc8f2007-12-04 22:23:35 +00001029 }
1030 }
1031
Torok Edwinc23197a2009-07-14 16:55:14 +00001032 llvm_unreachable("LowerConstantPool: Relocation model other than static"
Torok Edwin481d15a2009-07-14 12:22:58 +00001033 " not supported.");
Scott Michel266bc8f2007-12-04 22:23:35 +00001034}
1035
Scott Michel94bd57e2009-01-15 04:41:47 +00001036//! Alternate entry point for generating the address of a constant pool entry
1037SDValue
1038SPU::LowerConstantPool(SDValue Op, SelectionDAG &DAG, const SPUTargetMachine &TM) {
1039 return ::LowerConstantPool(Op, DAG, TM.getSubtargetImpl());
1040}
1041
Dan Gohman475871a2008-07-27 21:46:04 +00001042static SDValue
1043LowerJumpTable(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001044 EVT PtrVT = Op.getValueType();
Scott Michel266bc8f2007-12-04 22:23:35 +00001045 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001046 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1047 SDValue Zero = DAG.getConstant(0, PtrVT);
Scott Michel266bc8f2007-12-04 22:23:35 +00001048 const TargetMachine &TM = DAG.getTarget();
Dale Johannesende064702009-02-06 21:50:26 +00001049 // FIXME there is no actual debug info here
1050 DebugLoc dl = Op.getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +00001051
1052 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michela59d4692008-02-23 18:41:37 +00001053 if (!ST->usingLargeMem()) {
Dale Johannesende064702009-02-06 21:50:26 +00001054 return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, JTI, Zero);
Scott Michela59d4692008-02-23 18:41:37 +00001055 } else {
Dale Johannesende064702009-02-06 21:50:26 +00001056 SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, JTI, Zero);
1057 SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, JTI, Zero);
1058 return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
Scott Michela59d4692008-02-23 18:41:37 +00001059 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001060 }
1061
Torok Edwinc23197a2009-07-14 16:55:14 +00001062 llvm_unreachable("LowerJumpTable: Relocation model other than static"
Torok Edwin481d15a2009-07-14 12:22:58 +00001063 " not supported.");
Scott Michel266bc8f2007-12-04 22:23:35 +00001064}
1065
Dan Gohman475871a2008-07-27 21:46:04 +00001066static SDValue
1067LowerGlobalAddress(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00001068 EVT PtrVT = Op.getValueType();
Scott Michel266bc8f2007-12-04 22:23:35 +00001069 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001070 const GlobalValue *GV = GSDN->getGlobal();
Devang Patel0d881da2010-07-06 22:08:15 +00001071 SDValue GA = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
1072 PtrVT, GSDN->getOffset());
Scott Michel266bc8f2007-12-04 22:23:35 +00001073 const TargetMachine &TM = DAG.getTarget();
Dan Gohman475871a2008-07-27 21:46:04 +00001074 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesende064702009-02-06 21:50:26 +00001075 // FIXME there is no actual debug info here
1076 DebugLoc dl = Op.getDebugLoc();
Scott Michel5af8f0e2008-07-16 17:17:29 +00001077
Scott Michel266bc8f2007-12-04 22:23:35 +00001078 if (TM.getRelocationModel() == Reloc::Static) {
Scott Michel053c1da2008-01-29 02:16:57 +00001079 if (!ST->usingLargeMem()) {
Dale Johannesende064702009-02-06 21:50:26 +00001080 return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, GA, Zero);
Scott Michel053c1da2008-01-29 02:16:57 +00001081 } else {
Dale Johannesende064702009-02-06 21:50:26 +00001082 SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, GA, Zero);
1083 SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, GA, Zero);
1084 return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
Scott Michel053c1da2008-01-29 02:16:57 +00001085 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001086 } else {
Chris Lattner75361b62010-04-07 22:58:41 +00001087 report_fatal_error("LowerGlobalAddress: Relocation model other than static"
Torok Edwindac237e2009-07-08 20:53:28 +00001088 "not supported.");
Scott Michel266bc8f2007-12-04 22:23:35 +00001089 /*NOTREACHED*/
1090 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001091}
1092
Nate Begemanccef5802008-02-14 18:43:04 +00001093//! Custom lower double precision floating point constants
Dan Gohman475871a2008-07-27 21:46:04 +00001094static SDValue
1095LowerConstantFP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001096 EVT VT = Op.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00001097 // FIXME there is no actual debug info here
1098 DebugLoc dl = Op.getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +00001099
Owen Anderson825b72b2009-08-11 20:47:22 +00001100 if (VT == MVT::f64) {
Scott Michel1a6cdb62008-12-01 17:56:02 +00001101 ConstantFPSDNode *FP = cast<ConstantFPSDNode>(Op.getNode());
1102
1103 assert((FP != 0) &&
1104 "LowerConstantFP: Node is not ConstantFPSDNode");
Scott Michel1df30c42008-12-29 03:23:36 +00001105
Scott Michel170783a2007-12-19 20:15:47 +00001106 uint64_t dbits = DoubleToBits(FP->getValueAPF().convertToDouble());
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 SDValue T = DAG.getConstant(dbits, MVT::i64);
1108 SDValue Tvec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64, T, T);
Dale Johannesende064702009-02-06 21:50:26 +00001109 return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001110 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Tvec));
Scott Michel266bc8f2007-12-04 22:23:35 +00001111 }
1112
Dan Gohman475871a2008-07-27 21:46:04 +00001113 return SDValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00001114}
1115
Dan Gohman98ca4f22009-08-05 01:29:28 +00001116SDValue
1117SPUTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001118 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001119 const SmallVectorImpl<ISD::InputArg>
1120 &Ins,
1121 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001122 SmallVectorImpl<SDValue> &InVals)
1123 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001124
Scott Michel266bc8f2007-12-04 22:23:35 +00001125 MachineFunction &MF = DAG.getMachineFunction();
1126 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001127 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00001128 SPUFunctionInfo *FuncInfo = MF.getInfo<SPUFunctionInfo>();
Scott Michel266bc8f2007-12-04 22:23:35 +00001129
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001130 unsigned ArgOffset = SPUFrameLowering::minStackSize();
Scott Michel266bc8f2007-12-04 22:23:35 +00001131 unsigned ArgRegIdx = 0;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001132 unsigned StackSlotSize = SPUFrameLowering::stackSlotSize();
Scott Michel5af8f0e2008-07-16 17:17:29 +00001133
Owen Andersone50ed302009-08-10 22:56:29 +00001134 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel5af8f0e2008-07-16 17:17:29 +00001135
Kalle Raiskilad258c492010-07-08 21:15:22 +00001136 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001137 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001138 getTargetMachine(), ArgLocs, *DAG.getContext());
Kalle Raiskilad258c492010-07-08 21:15:22 +00001139 // FIXME: allow for other calling conventions
1140 CCInfo.AnalyzeFormalArguments(Ins, CCC_SPU);
1141
Scott Michel266bc8f2007-12-04 22:23:35 +00001142 // Add DAG nodes to load the arguments or copy them out of registers.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001143 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Owen Andersone50ed302009-08-10 22:56:29 +00001144 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001145 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Scott Micheld976c212008-10-30 01:51:48 +00001146 SDValue ArgVal;
Kalle Raiskilad258c492010-07-08 21:15:22 +00001147 CCValAssign &VA = ArgLocs[ArgNo];
Scott Michel266bc8f2007-12-04 22:23:35 +00001148
Kalle Raiskilad258c492010-07-08 21:15:22 +00001149 if (VA.isRegLoc()) {
Scott Micheld976c212008-10-30 01:51:48 +00001150 const TargetRegisterClass *ArgRegClass;
Scott Michel5af8f0e2008-07-16 17:17:29 +00001151
Owen Anderson825b72b2009-08-11 20:47:22 +00001152 switch (ObjectVT.getSimpleVT().SimpleTy) {
Benjamin Kramer1bd73352010-04-08 10:44:28 +00001153 default:
1154 report_fatal_error("LowerFormalArguments Unhandled argument type: " +
1155 Twine(ObjectVT.getEVTString()));
Owen Anderson825b72b2009-08-11 20:47:22 +00001156 case MVT::i8:
Scott Michel9c0c6b22008-11-21 02:56:16 +00001157 ArgRegClass = &SPU::R8CRegClass;
1158 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001159 case MVT::i16:
Scott Michel9c0c6b22008-11-21 02:56:16 +00001160 ArgRegClass = &SPU::R16CRegClass;
1161 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001162 case MVT::i32:
Scott Michel9c0c6b22008-11-21 02:56:16 +00001163 ArgRegClass = &SPU::R32CRegClass;
1164 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001165 case MVT::i64:
Scott Michel9c0c6b22008-11-21 02:56:16 +00001166 ArgRegClass = &SPU::R64CRegClass;
1167 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001168 case MVT::i128:
Scott Micheldd950092009-01-06 03:36:14 +00001169 ArgRegClass = &SPU::GPRCRegClass;
1170 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001171 case MVT::f32:
Scott Michel9c0c6b22008-11-21 02:56:16 +00001172 ArgRegClass = &SPU::R32FPRegClass;
1173 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001174 case MVT::f64:
Scott Michel9c0c6b22008-11-21 02:56:16 +00001175 ArgRegClass = &SPU::R64FPRegClass;
1176 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001177 case MVT::v2f64:
1178 case MVT::v4f32:
1179 case MVT::v2i64:
1180 case MVT::v4i32:
1181 case MVT::v8i16:
1182 case MVT::v16i8:
Scott Michel9c0c6b22008-11-21 02:56:16 +00001183 ArgRegClass = &SPU::VECREGRegClass;
1184 break;
Scott Micheld976c212008-10-30 01:51:48 +00001185 }
1186
1187 unsigned VReg = RegInfo.createVirtualRegister(ArgRegClass);
Kalle Raiskilad258c492010-07-08 21:15:22 +00001188 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Scott Micheld976c212008-10-30 01:51:48 +00001190 ++ArgRegIdx;
1191 } else {
1192 // We need to load the argument to a virtual register if we determined
1193 // above that we ran out of physical registers of the appropriate type
1194 // or we're forced to do vararg
Evan Chenged2ae132010-07-03 00:40:23 +00001195 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00001196 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnere8639032010-09-21 06:22:23 +00001197 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001198 false, false, false, 0);
Scott Michel266bc8f2007-12-04 22:23:35 +00001199 ArgOffset += StackSlotSize;
1200 }
Scott Michel5af8f0e2008-07-16 17:17:29 +00001201
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 InVals.push_back(ArgVal);
Scott Micheld976c212008-10-30 01:51:48 +00001203 // Update the chain
Dan Gohman98ca4f22009-08-05 01:29:28 +00001204 Chain = ArgVal.getOperand(0);
Scott Michel266bc8f2007-12-04 22:23:35 +00001205 }
Scott Michel5af8f0e2008-07-16 17:17:29 +00001206
Scott Micheld976c212008-10-30 01:51:48 +00001207 // vararg handling:
Scott Michel266bc8f2007-12-04 22:23:35 +00001208 if (isVarArg) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001209 // FIXME: we should be able to query the argument registers from
1210 // tablegen generated code.
Craig Topperb78ca422012-03-11 07:16:55 +00001211 static const uint16_t ArgRegs[] = {
Kalle Raiskilad258c492010-07-08 21:15:22 +00001212 SPU::R3, SPU::R4, SPU::R5, SPU::R6, SPU::R7, SPU::R8, SPU::R9,
1213 SPU::R10, SPU::R11, SPU::R12, SPU::R13, SPU::R14, SPU::R15, SPU::R16,
1214 SPU::R17, SPU::R18, SPU::R19, SPU::R20, SPU::R21, SPU::R22, SPU::R23,
1215 SPU::R24, SPU::R25, SPU::R26, SPU::R27, SPU::R28, SPU::R29, SPU::R30,
1216 SPU::R31, SPU::R32, SPU::R33, SPU::R34, SPU::R35, SPU::R36, SPU::R37,
1217 SPU::R38, SPU::R39, SPU::R40, SPU::R41, SPU::R42, SPU::R43, SPU::R44,
1218 SPU::R45, SPU::R46, SPU::R47, SPU::R48, SPU::R49, SPU::R50, SPU::R51,
1219 SPU::R52, SPU::R53, SPU::R54, SPU::R55, SPU::R56, SPU::R57, SPU::R58,
1220 SPU::R59, SPU::R60, SPU::R61, SPU::R62, SPU::R63, SPU::R64, SPU::R65,
1221 SPU::R66, SPU::R67, SPU::R68, SPU::R69, SPU::R70, SPU::R71, SPU::R72,
1222 SPU::R73, SPU::R74, SPU::R75, SPU::R76, SPU::R77, SPU::R78, SPU::R79
1223 };
1224 // size of ArgRegs array
Craig Topperb78ca422012-03-11 07:16:55 +00001225 const unsigned NumArgRegs = 77;
Kalle Raiskilad258c492010-07-08 21:15:22 +00001226
Scott Micheld976c212008-10-30 01:51:48 +00001227 // We will spill (79-3)+1 registers to the stack
1228 SmallVector<SDValue, 79-3+1> MemOps;
1229
1230 // Create the frame slot
Scott Michel266bc8f2007-12-04 22:23:35 +00001231 for (; ArgRegIdx != NumArgRegs; ++ArgRegIdx) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001232 FuncInfo->setVarArgsFrameIndex(
Evan Chenged2ae132010-07-03 00:40:23 +00001233 MFI->CreateFixedObject(StackSlotSize, ArgOffset, true));
Dan Gohman1e93df62010-04-17 14:41:14 +00001234 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Cameron Zwarich055cdfc2011-05-19 04:44:19 +00001235 unsigned VReg = MF.addLiveIn(ArgRegs[ArgRegIdx], &SPU::VECREGRegClass);
Chris Lattnere27e02b2010-03-29 17:38:47 +00001236 SDValue ArgVal = DAG.getRegister(VReg, MVT::v16i8);
Chris Lattner6229d0a2010-09-21 18:41:36 +00001237 SDValue Store = DAG.getStore(Chain, dl, ArgVal, FIN, MachinePointerInfo(),
David Greene73657df2010-02-15 16:55:58 +00001238 false, false, 0);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001239 Chain = Store.getOperand(0);
Scott Michel266bc8f2007-12-04 22:23:35 +00001240 MemOps.push_back(Store);
Scott Micheld976c212008-10-30 01:51:48 +00001241
1242 // Increment address by stack slot size for the next stored argument
1243 ArgOffset += StackSlotSize;
Scott Michel266bc8f2007-12-04 22:23:35 +00001244 }
1245 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001246 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001247 &MemOps[0], MemOps.size());
Scott Michel266bc8f2007-12-04 22:23:35 +00001248 }
Scott Michel5af8f0e2008-07-16 17:17:29 +00001249
Dan Gohman98ca4f22009-08-05 01:29:28 +00001250 return Chain;
Scott Michel266bc8f2007-12-04 22:23:35 +00001251}
1252
1253/// isLSAAddress - Return the immediate to use if the specified
1254/// value is representable as a LSA address.
Dan Gohman475871a2008-07-27 21:46:04 +00001255static SDNode *isLSAAddress(SDValue Op, SelectionDAG &DAG) {
Scott Michel19fd42a2008-11-11 03:06:06 +00001256 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +00001257 if (!C) return 0;
Scott Michel5af8f0e2008-07-16 17:17:29 +00001258
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001259 int Addr = C->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00001260 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
1261 (Addr << 14 >> 14) != Addr)
1262 return 0; // Top 14 bits have to be sext of immediate.
Scott Michel5af8f0e2008-07-16 17:17:29 +00001263
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 return DAG.getConstant((int)C->getZExtValue() >> 2, MVT::i32).getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +00001265}
1266
Dan Gohman98ca4f22009-08-05 01:29:28 +00001267SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001268SPUTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001269 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001270 SelectionDAG &DAG = CLI.DAG;
1271 DebugLoc &dl = CLI.DL;
1272 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1273 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1274 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1275 SDValue Chain = CLI.Chain;
1276 SDValue Callee = CLI.Callee;
1277 bool &isTailCall = CLI.IsTailCall;
1278 CallingConv::ID CallConv = CLI.CallConv;
1279 bool isVarArg = CLI.IsVarArg;
1280
Evan Cheng0c439eb2010-01-27 00:07:07 +00001281 // CellSPU target does not yet support tail call optimization.
1282 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283
1284 const SPUSubtarget *ST = SPUTM.getSubtargetImpl();
1285 unsigned NumOps = Outs.size();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001286 unsigned StackSlotSize = SPUFrameLowering::stackSlotSize();
Kalle Raiskilad258c492010-07-08 21:15:22 +00001287
1288 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001289 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001290 getTargetMachine(), ArgLocs, *DAG.getContext());
Kalle Raiskilad258c492010-07-08 21:15:22 +00001291 // FIXME: allow for other calling conventions
1292 CCInfo.AnalyzeCallOperands(Outs, CCC_SPU);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001293
Kalle Raiskilad258c492010-07-08 21:15:22 +00001294 const unsigned NumArgRegs = ArgLocs.size();
1295
Scott Michel266bc8f2007-12-04 22:23:35 +00001296
1297 // Handy pointer type
Owen Andersone50ed302009-08-10 22:56:29 +00001298 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel5af8f0e2008-07-16 17:17:29 +00001299
Scott Michel266bc8f2007-12-04 22:23:35 +00001300 // Set up a copy of the stack pointer for use loading and storing any
1301 // arguments that may not fit in the registers available for argument
1302 // passing.
Owen Anderson825b72b2009-08-11 20:47:22 +00001303 SDValue StackPtr = DAG.getRegister(SPU::R1, MVT::i32);
Scott Michel5af8f0e2008-07-16 17:17:29 +00001304
Scott Michel266bc8f2007-12-04 22:23:35 +00001305 // Figure out which arguments are going to go in registers, and which in
1306 // memory.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001307 unsigned ArgOffset = SPUFrameLowering::minStackSize(); // Just below [LR]
Scott Michel266bc8f2007-12-04 22:23:35 +00001308 unsigned ArgRegIdx = 0;
1309
1310 // Keep track of registers passing arguments
Dan Gohman475871a2008-07-27 21:46:04 +00001311 std::vector<std::pair<unsigned, SDValue> > RegsToPass;
Scott Michel266bc8f2007-12-04 22:23:35 +00001312 // And the arguments passed on the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001313 SmallVector<SDValue, 8> MemOpChains;
Scott Michel266bc8f2007-12-04 22:23:35 +00001314
Kalle Raiskilad258c492010-07-08 21:15:22 +00001315 for (; ArgRegIdx != NumOps; ++ArgRegIdx) {
1316 SDValue Arg = OutVals[ArgRegIdx];
1317 CCValAssign &VA = ArgLocs[ArgRegIdx];
Scott Michel5af8f0e2008-07-16 17:17:29 +00001318
Scott Michel266bc8f2007-12-04 22:23:35 +00001319 // PtrOff will be used to store the current argument to the stack if a
1320 // register cannot be found for it.
Dan Gohman475871a2008-07-27 21:46:04 +00001321 SDValue PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Dale Johannesen33c960f2009-02-04 20:06:27 +00001322 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Scott Michel266bc8f2007-12-04 22:23:35 +00001323
Owen Anderson825b72b2009-08-11 20:47:22 +00001324 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001325 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001326 case MVT::i8:
1327 case MVT::i16:
1328 case MVT::i32:
1329 case MVT::i64:
1330 case MVT::i128:
Owen Anderson825b72b2009-08-11 20:47:22 +00001331 case MVT::f32:
1332 case MVT::f64:
Owen Anderson825b72b2009-08-11 20:47:22 +00001333 case MVT::v2i64:
1334 case MVT::v2f64:
1335 case MVT::v4f32:
1336 case MVT::v4i32:
1337 case MVT::v8i16:
1338 case MVT::v16i8:
Scott Michel266bc8f2007-12-04 22:23:35 +00001339 if (ArgRegIdx != NumArgRegs) {
Kalle Raiskilad258c492010-07-08 21:15:22 +00001340 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Scott Michel266bc8f2007-12-04 22:23:35 +00001341 } else {
Chris Lattner6229d0a2010-09-21 18:41:36 +00001342 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1343 MachinePointerInfo(),
David Greene73657df2010-02-15 16:55:58 +00001344 false, false, 0));
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001345 ArgOffset += StackSlotSize;
Scott Michel266bc8f2007-12-04 22:23:35 +00001346 }
1347 break;
1348 }
1349 }
1350
Bill Wendlingce90c242009-12-28 01:31:11 +00001351 // Accumulate how many bytes are to be pushed on the stack, including the
1352 // linkage area, and parameter passing area. According to the SPU ABI,
1353 // we minimally need space for [LR] and [SP].
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001354 unsigned NumStackBytes = ArgOffset - SPUFrameLowering::minStackSize();
Bill Wendlingce90c242009-12-28 01:31:11 +00001355
1356 // Insert a call sequence start
Chris Lattnere563bbc2008-10-11 22:08:30 +00001357 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumStackBytes,
1358 true));
Scott Michel266bc8f2007-12-04 22:23:35 +00001359
1360 if (!MemOpChains.empty()) {
1361 // Adjust the stack pointer for the stack arguments.
Owen Anderson825b72b2009-08-11 20:47:22 +00001362 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Scott Michel266bc8f2007-12-04 22:23:35 +00001363 &MemOpChains[0], MemOpChains.size());
1364 }
Scott Michel5af8f0e2008-07-16 17:17:29 +00001365
Scott Michel266bc8f2007-12-04 22:23:35 +00001366 // Build a sequence of copy-to-reg nodes chained together with token chain
1367 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001368 SDValue InFlag;
Scott Michel266bc8f2007-12-04 22:23:35 +00001369 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michel6e1d1472009-03-16 18:47:25 +00001370 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001371 RegsToPass[i].second, InFlag);
Scott Michel266bc8f2007-12-04 22:23:35 +00001372 InFlag = Chain.getValue(1);
1373 }
Scott Michel5af8f0e2008-07-16 17:17:29 +00001374
Dan Gohman475871a2008-07-27 21:46:04 +00001375 SmallVector<SDValue, 8> Ops;
Scott Michel266bc8f2007-12-04 22:23:35 +00001376 unsigned CallOpc = SPUISD::CALL;
Scott Michel5af8f0e2008-07-16 17:17:29 +00001377
Bill Wendling056292f2008-09-16 21:48:12 +00001378 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1379 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1380 // node so that legalize doesn't hack it.
Scott Michel19fd42a2008-11-11 03:06:06 +00001381 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001382 const GlobalValue *GV = G->getGlobal();
Owen Andersone50ed302009-08-10 22:56:29 +00001383 EVT CalleeVT = Callee.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001384 SDValue Zero = DAG.getConstant(0, PtrVT);
Devang Patel0d881da2010-07-06 22:08:15 +00001385 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, CalleeVT);
Scott Michel266bc8f2007-12-04 22:23:35 +00001386
Scott Michel9de5d0d2008-01-11 02:53:15 +00001387 if (!ST->usingLargeMem()) {
1388 // Turn calls to targets that are defined (i.e., have bodies) into BRSL
1389 // style calls, otherwise, external symbols are BRASL calls. This assumes
1390 // that declared/defined symbols are in the same compilation unit and can
1391 // be reached through PC-relative jumps.
1392 //
1393 // NOTE:
1394 // This may be an unsafe assumption for JIT and really large compilation
1395 // units.
1396 if (GV->isDeclaration()) {
Dale Johannesende064702009-02-06 21:50:26 +00001397 Callee = DAG.getNode(SPUISD::AFormAddr, dl, CalleeVT, GA, Zero);
Scott Michel9de5d0d2008-01-11 02:53:15 +00001398 } else {
Dale Johannesende064702009-02-06 21:50:26 +00001399 Callee = DAG.getNode(SPUISD::PCRelAddr, dl, CalleeVT, GA, Zero);
Scott Michel9de5d0d2008-01-11 02:53:15 +00001400 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001401 } else {
Scott Michel9de5d0d2008-01-11 02:53:15 +00001402 // "Large memory" mode: Turn all calls into indirect calls with a X-form
1403 // address pairs:
Dale Johannesende064702009-02-06 21:50:26 +00001404 Callee = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, GA, Zero);
Scott Michel266bc8f2007-12-04 22:23:35 +00001405 }
Scott Michel1df30c42008-12-29 03:23:36 +00001406 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001407 EVT CalleeVT = Callee.getValueType();
Scott Michel1df30c42008-12-29 03:23:36 +00001408 SDValue Zero = DAG.getConstant(0, PtrVT);
1409 SDValue ExtSym = DAG.getTargetExternalSymbol(S->getSymbol(),
1410 Callee.getValueType());
1411
1412 if (!ST->usingLargeMem()) {
Dale Johannesende064702009-02-06 21:50:26 +00001413 Callee = DAG.getNode(SPUISD::AFormAddr, dl, CalleeVT, ExtSym, Zero);
Scott Michel1df30c42008-12-29 03:23:36 +00001414 } else {
Dale Johannesende064702009-02-06 21:50:26 +00001415 Callee = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, ExtSym, Zero);
Scott Michel1df30c42008-12-29 03:23:36 +00001416 }
1417 } else if (SDNode *Dest = isLSAAddress(Callee, DAG)) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001418 // If this is an absolute destination address that appears to be a legal
1419 // local store address, use the munged value.
Dan Gohman475871a2008-07-27 21:46:04 +00001420 Callee = SDValue(Dest, 0);
Scott Michel9de5d0d2008-01-11 02:53:15 +00001421 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001422
1423 Ops.push_back(Chain);
1424 Ops.push_back(Callee);
Scott Michel5af8f0e2008-07-16 17:17:29 +00001425
Scott Michel266bc8f2007-12-04 22:23:35 +00001426 // Add argument registers to the end of the list so that they are known live
1427 // into the call.
1428 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Scott Michel5af8f0e2008-07-16 17:17:29 +00001429 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Scott Michel266bc8f2007-12-04 22:23:35 +00001430 RegsToPass[i].second.getValueType()));
Scott Michel5af8f0e2008-07-16 17:17:29 +00001431
Gabor Greifba36cb52008-08-28 21:40:38 +00001432 if (InFlag.getNode())
Scott Michel266bc8f2007-12-04 22:23:35 +00001433 Ops.push_back(InFlag);
Duncan Sands4bdcb612008-07-02 17:40:58 +00001434 // Returns a chain and a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001435 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Glue),
Duncan Sands4bdcb612008-07-02 17:40:58 +00001436 &Ops[0], Ops.size());
Scott Michel266bc8f2007-12-04 22:23:35 +00001437 InFlag = Chain.getValue(1);
1438
Chris Lattnere563bbc2008-10-11 22:08:30 +00001439 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumStackBytes, true),
1440 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001441 if (!Ins.empty())
Evan Chengebaaa912008-02-05 22:44:06 +00001442 InFlag = Chain.getValue(1);
1443
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444 // If the function returns void, just return the chain.
1445 if (Ins.empty())
1446 return Chain;
Scott Michel5af8f0e2008-07-16 17:17:29 +00001447
Kalle Raiskila55aebef2010-08-24 11:50:48 +00001448 // Now handle the return value(s)
1449 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001450 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001451 getTargetMachine(), RVLocs, *DAG.getContext());
Kalle Raiskila55aebef2010-08-24 11:50:48 +00001452 CCRetInfo.AnalyzeCallResult(Ins, CCC_SPU);
1453
1454
Scott Michel266bc8f2007-12-04 22:23:35 +00001455 // If the call has results, copy the values out of the ret val registers.
Kalle Raiskila55aebef2010-08-24 11:50:48 +00001456 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1457 CCValAssign VA = RVLocs[i];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001458
Kalle Raiskila55aebef2010-08-24 11:50:48 +00001459 SDValue Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1460 InFlag);
1461 Chain = Val.getValue(1);
1462 InFlag = Val.getValue(2);
1463 InVals.push_back(Val);
1464 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001465
Dan Gohman98ca4f22009-08-05 01:29:28 +00001466 return Chain;
Scott Michel266bc8f2007-12-04 22:23:35 +00001467}
1468
Dan Gohman98ca4f22009-08-05 01:29:28 +00001469SDValue
1470SPUTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001471 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001472 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001473 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001474 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001475
Scott Michel266bc8f2007-12-04 22:23:35 +00001476 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001477 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001478 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001479 CCInfo.AnalyzeReturn(Outs, RetCC_SPU);
Scott Michel5af8f0e2008-07-16 17:17:29 +00001480
Scott Michel266bc8f2007-12-04 22:23:35 +00001481 // If this is the first return lowered for this function, add the regs to the
1482 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001483 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001484 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner84bc5422007-12-31 04:13:23 +00001485 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Scott Michel266bc8f2007-12-04 22:23:35 +00001486 }
1487
Dan Gohman475871a2008-07-27 21:46:04 +00001488 SDValue Flag;
Scott Michel5af8f0e2008-07-16 17:17:29 +00001489
Scott Michel266bc8f2007-12-04 22:23:35 +00001490 // Copy the result values into the output registers.
1491 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1492 CCValAssign &VA = RVLocs[i];
1493 assert(VA.isRegLoc() && "Can only return in registers!");
Dale Johannesena05dca42009-02-04 23:02:30 +00001494 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001495 OutVals[i], Flag);
Scott Michel266bc8f2007-12-04 22:23:35 +00001496 Flag = Chain.getValue(1);
1497 }
1498
Gabor Greifba36cb52008-08-28 21:40:38 +00001499 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001500 return DAG.getNode(SPUISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Scott Michel266bc8f2007-12-04 22:23:35 +00001501 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001502 return DAG.getNode(SPUISD::RET_FLAG, dl, MVT::Other, Chain);
Scott Michel266bc8f2007-12-04 22:23:35 +00001503}
1504
1505
1506//===----------------------------------------------------------------------===//
1507// Vector related lowering:
1508//===----------------------------------------------------------------------===//
1509
1510static ConstantSDNode *
1511getVecImm(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001512 SDValue OpVal(0, 0);
Scott Michel5af8f0e2008-07-16 17:17:29 +00001513
Scott Michel266bc8f2007-12-04 22:23:35 +00001514 // Check to see if this buildvec has a single non-undef value in its elements.
1515 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1516 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greifba36cb52008-08-28 21:40:38 +00001517 if (OpVal.getNode() == 0)
Scott Michel266bc8f2007-12-04 22:23:35 +00001518 OpVal = N->getOperand(i);
1519 else if (OpVal != N->getOperand(i))
1520 return 0;
1521 }
Scott Michel5af8f0e2008-07-16 17:17:29 +00001522
Gabor Greifba36cb52008-08-28 21:40:38 +00001523 if (OpVal.getNode() != 0) {
Scott Michel19fd42a2008-11-11 03:06:06 +00001524 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001525 return CN;
1526 }
1527 }
1528
Scott Michel7ea02ff2009-03-17 01:15:45 +00001529 return 0;
Scott Michel266bc8f2007-12-04 22:23:35 +00001530}
1531
1532/// get_vec_i18imm - Test if this vector is a vector filled with the same value
1533/// and the value fits into an unsigned 18-bit constant, and if so, return the
1534/// constant
Dan Gohman475871a2008-07-27 21:46:04 +00001535SDValue SPU::get_vec_u18imm(SDNode *N, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00001536 EVT ValueType) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001537 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001538 uint64_t Value = CN->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00001539 if (ValueType == MVT::i64) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001540 uint64_t UValue = CN->getZExtValue();
Scott Michel4cb8bd82008-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 Gohman475871a2008-07-27 21:46:04 +00001544 return SDValue();
Scott Michel4cb8bd82008-03-06 04:02:54 +00001545 Value = Value >> 32;
1546 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001547 if (Value <= 0x3ffff)
Dan Gohmanfa210d82008-11-05 02:06:09 +00001548 return DAG.getTargetConstant(Value, ValueType);
Scott Michel266bc8f2007-12-04 22:23:35 +00001549 }
1550
Dan Gohman475871a2008-07-27 21:46:04 +00001551 return SDValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00001552}
1553
1554/// get_vec_i16imm - Test if this vector is a vector filled with the same value
1555/// and the value fits into a signed 16-bit constant, and if so, return the
1556/// constant
Dan Gohman475871a2008-07-27 21:46:04 +00001557SDValue SPU::get_vec_i16imm(SDNode *N, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00001558 EVT ValueType) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001559 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohman7810bfe2008-09-26 21:54:37 +00001560 int64_t Value = CN->getSExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00001561 if (ValueType == MVT::i64) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001562 uint64_t UValue = CN->getZExtValue();
Scott Michel4cb8bd82008-03-06 04:02:54 +00001563 uint32_t upper = uint32_t(UValue >> 32);
1564 uint32_t lower = uint32_t(UValue);
1565 if (upper != lower)
Dan Gohman475871a2008-07-27 21:46:04 +00001566 return SDValue();
Scott Michel4cb8bd82008-03-06 04:02:54 +00001567 Value = Value >> 32;
1568 }
Scott Michelad2715e2008-03-05 23:02:02 +00001569 if (Value >= -(1 << 15) && Value <= ((1 << 15) - 1)) {
Dan Gohmanfa210d82008-11-05 02:06:09 +00001570 return DAG.getTargetConstant(Value, ValueType);
Scott Michel266bc8f2007-12-04 22:23:35 +00001571 }
1572 }
1573
Dan Gohman475871a2008-07-27 21:46:04 +00001574 return SDValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00001575}
1576
1577/// get_vec_i10imm - Test if this vector is a vector filled with the same value
1578/// and the value fits into a signed 10-bit constant, and if so, return the
1579/// constant
Dan Gohman475871a2008-07-27 21:46:04 +00001580SDValue SPU::get_vec_i10imm(SDNode *N, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00001581 EVT ValueType) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001582 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohman7810bfe2008-09-26 21:54:37 +00001583 int64_t Value = CN->getSExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 if (ValueType == MVT::i64) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001585 uint64_t UValue = CN->getZExtValue();
Scott Michel4cb8bd82008-03-06 04:02:54 +00001586 uint32_t upper = uint32_t(UValue >> 32);
1587 uint32_t lower = uint32_t(UValue);
1588 if (upper != lower)
Dan Gohman475871a2008-07-27 21:46:04 +00001589 return SDValue();
Scott Michel4cb8bd82008-03-06 04:02:54 +00001590 Value = Value >> 32;
1591 }
Benjamin Kramer7e09deb2010-03-29 19:07:58 +00001592 if (isInt<10>(Value))
Dan Gohmanfa210d82008-11-05 02:06:09 +00001593 return DAG.getTargetConstant(Value, ValueType);
Scott Michel266bc8f2007-12-04 22:23:35 +00001594 }
1595
Dan Gohman475871a2008-07-27 21:46:04 +00001596 return SDValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00001597}
1598
1599/// get_vec_i8imm - Test if this vector is a vector filled with the same value
1600/// and the value fits into a signed 8-bit constant, and if so, return the
1601/// constant.
1602///
1603/// @note: The incoming vector is v16i8 because that's the only way we can load
1604/// constant vectors. Thus, we test to see if the upper and lower bytes are the
1605/// same value.
Dan Gohman475871a2008-07-27 21:46:04 +00001606SDValue SPU::get_vec_i8imm(SDNode *N, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00001607 EVT ValueType) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001608 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001609 int Value = (int) CN->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00001610 if (ValueType == MVT::i16
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001611 && Value <= 0xffff /* truncated from uint64_t */
1612 && ((short) Value >> 8) == ((short) Value & 0xff))
Dan Gohmanfa210d82008-11-05 02:06:09 +00001613 return DAG.getTargetConstant(Value & 0xff, ValueType);
Owen Anderson825b72b2009-08-11 20:47:22 +00001614 else if (ValueType == MVT::i8
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001615 && (Value & 0xff) == Value)
Dan Gohmanfa210d82008-11-05 02:06:09 +00001616 return DAG.getTargetConstant(Value, ValueType);
Scott Michel266bc8f2007-12-04 22:23:35 +00001617 }
1618
Dan Gohman475871a2008-07-27 21:46:04 +00001619 return SDValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00001620}
1621
1622/// get_ILHUvec_imm - Test if this vector is a vector filled with the same value
1623/// and the value fits into a signed 16-bit constant, and if so, return the
1624/// constant
Dan Gohman475871a2008-07-27 21:46:04 +00001625SDValue SPU::get_ILHUvec_imm(SDNode *N, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00001626 EVT ValueType) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001627 if (ConstantSDNode *CN = getVecImm(N)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001628 uint64_t Value = CN->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00001629 if ((ValueType == MVT::i32
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001630 && ((unsigned) Value & 0xffff0000) == (unsigned) Value)
Owen Anderson825b72b2009-08-11 20:47:22 +00001631 || (ValueType == MVT::i64 && (Value & 0xffff0000) == Value))
Dan Gohmanfa210d82008-11-05 02:06:09 +00001632 return DAG.getTargetConstant(Value >> 16, ValueType);
Scott Michel266bc8f2007-12-04 22:23:35 +00001633 }
1634
Dan Gohman475871a2008-07-27 21:46:04 +00001635 return SDValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00001636}
1637
1638/// get_v4i32_imm - Catch-all for general 32-bit constant vectors
Dan Gohman475871a2008-07-27 21:46:04 +00001639SDValue SPU::get_v4i32_imm(SDNode *N, SelectionDAG &DAG) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001640 if (ConstantSDNode *CN = getVecImm(N)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001641 return DAG.getTargetConstant((unsigned) CN->getZExtValue(), MVT::i32);
Scott Michel266bc8f2007-12-04 22:23:35 +00001642 }
1643
Dan Gohman475871a2008-07-27 21:46:04 +00001644 return SDValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00001645}
1646
1647/// get_v4i32_imm - Catch-all for general 64-bit constant vectors
Dan Gohman475871a2008-07-27 21:46:04 +00001648SDValue SPU::get_v2i64_imm(SDNode *N, SelectionDAG &DAG) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001649 if (ConstantSDNode *CN = getVecImm(N)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001650 return DAG.getTargetConstant((unsigned) CN->getZExtValue(), MVT::i64);
Scott Michel266bc8f2007-12-04 22:23:35 +00001651 }
1652
Dan Gohman475871a2008-07-27 21:46:04 +00001653 return SDValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00001654}
1655
Scott Micheld1e8d9c2009-01-21 04:58:48 +00001656//! Lower a BUILD_VECTOR instruction creatively:
Dan Gohman7db949d2009-08-07 01:32:21 +00001657static SDValue
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001658LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001659 EVT VT = Op.getValueType();
1660 EVT EltVT = VT.getVectorElementType();
Dale Johannesened2eee62009-02-06 01:31:28 +00001661 DebugLoc dl = Op.getDebugLoc();
Scott Michel7ea02ff2009-03-17 01:15:45 +00001662 BuildVectorSDNode *BCN = dyn_cast<BuildVectorSDNode>(Op.getNode());
1663 assert(BCN != 0 && "Expected BuildVectorSDNode in SPU LowerBUILD_VECTOR");
1664 unsigned minSplatBits = EltVT.getSizeInBits();
1665
1666 if (minSplatBits < 16)
1667 minSplatBits = 16;
1668
1669 APInt APSplatBits, APSplatUndef;
1670 unsigned SplatBitSize;
1671 bool HasAnyUndefs;
1672
1673 if (!BCN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
1674 HasAnyUndefs, minSplatBits)
1675 || minSplatBits < SplatBitSize)
1676 return SDValue(); // Wasn't a constant vector or splat exceeded min
1677
1678 uint64_t SplatBits = APSplatBits.getZExtValue();
Scott Michel5af8f0e2008-07-16 17:17:29 +00001679
Owen Anderson825b72b2009-08-11 20:47:22 +00001680 switch (VT.getSimpleVT().SimpleTy) {
Benjamin Kramer1bd73352010-04-08 10:44:28 +00001681 default:
1682 report_fatal_error("CellSPU: Unhandled VT in LowerBUILD_VECTOR, VT = " +
1683 Twine(VT.getEVTString()));
Scott Micheld1e8d9c2009-01-21 04:58:48 +00001684 /*NOTREACHED*/
Owen Anderson825b72b2009-08-11 20:47:22 +00001685 case MVT::v4f32: {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001686 uint32_t Value32 = uint32_t(SplatBits);
Chris Lattnere7fa1f22009-03-26 05:29:34 +00001687 assert(SplatBitSize == 32
Scott Michel7f9ba9b2008-01-30 02:55:46 +00001688 && "LowerBUILD_VECTOR: Unexpected floating point vector element.");
Scott Michel266bc8f2007-12-04 22:23:35 +00001689 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
Owen Anderson825b72b2009-08-11 20:47:22 +00001690 SDValue T = DAG.getConstant(Value32, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001691 return DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,
Owen Anderson825b72b2009-08-11 20:47:22 +00001692 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, T,T,T,T));
Scott Michel266bc8f2007-12-04 22:23:35 +00001693 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 case MVT::v2f64: {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001695 uint64_t f64val = uint64_t(SplatBits);
Chris Lattnere7fa1f22009-03-26 05:29:34 +00001696 assert(SplatBitSize == 64
Scott Michel104de432008-11-24 17:11:17 +00001697 && "LowerBUILD_VECTOR: 64-bit float vector size > 8 bytes.");
Scott Michel266bc8f2007-12-04 22:23:35 +00001698 // NOTE: pretend the constant is an integer. LLVM won't load FP constants
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 SDValue T = DAG.getConstant(f64val, MVT::i64);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001700 return DAG.getNode(ISD::BITCAST, dl, MVT::v2f64,
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64, T, T));
Scott Michel266bc8f2007-12-04 22:23:35 +00001702 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 case MVT::v16i8: {
Scott Michel266bc8f2007-12-04 22:23:35 +00001704 // 8-bit constants have to be expanded to 16-bits
Scott Michel7ea02ff2009-03-17 01:15:45 +00001705 unsigned short Value16 = SplatBits /* | (SplatBits << 8) */;
1706 SmallVector<SDValue, 8> Ops;
1707
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 Ops.assign(8, DAG.getConstant(Value16, MVT::i16));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001709 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00001710 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i16, &Ops[0], Ops.size()));
Scott Michel266bc8f2007-12-04 22:23:35 +00001711 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 case MVT::v8i16: {
Scott Michel7ea02ff2009-03-17 01:15:45 +00001713 unsigned short Value16 = SplatBits;
1714 SDValue T = DAG.getConstant(Value16, EltVT);
1715 SmallVector<SDValue, 8> Ops;
1716
1717 Ops.assign(8, T);
1718 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Scott Michel266bc8f2007-12-04 22:23:35 +00001719 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001720 case MVT::v4i32: {
Scott Michel7ea02ff2009-03-17 01:15:45 +00001721 SDValue T = DAG.getConstant(unsigned(SplatBits), VT.getVectorElementType());
Evan Chenga87008d2009-02-25 22:49:59 +00001722 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, T, T, T, T);
Scott Michel266bc8f2007-12-04 22:23:35 +00001723 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001724 case MVT::v2i64: {
Scott Michel7ea02ff2009-03-17 01:15:45 +00001725 return SPU::LowerV2I64Splat(VT, DAG, SplatBits, dl);
Scott Michel266bc8f2007-12-04 22:23:35 +00001726 }
1727 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001728}
1729
Scott Michel7ea02ff2009-03-17 01:15:45 +00001730/*!
1731 */
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001732SDValue
Owen Andersone50ed302009-08-10 22:56:29 +00001733SPU::LowerV2I64Splat(EVT OpVT, SelectionDAG& DAG, uint64_t SplatVal,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001734 DebugLoc dl) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001735 uint32_t upper = uint32_t(SplatVal >> 32);
1736 uint32_t lower = uint32_t(SplatVal);
1737
1738 if (upper == lower) {
1739 // Magic constant that can be matched by IL, ILA, et. al.
Owen Anderson825b72b2009-08-11 20:47:22 +00001740 SDValue Val = DAG.getTargetConstant(upper, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001741 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00001742 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga87008d2009-02-25 22:49:59 +00001743 Val, Val, Val, Val));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001744 } else {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001745 bool upper_special, lower_special;
1746
1747 // NOTE: This code creates common-case shuffle masks that can be easily
1748 // detected as common expressions. It is not attempting to create highly
1749 // specialized masks to replace any and all 0's, 0xff's and 0x80's.
1750
1751 // Detect if the upper or lower half is a special shuffle mask pattern:
1752 upper_special = (upper == 0 || upper == 0xffffffff || upper == 0x80000000);
1753 lower_special = (lower == 0 || lower == 0xffffffff || lower == 0x80000000);
1754
Scott Michel7ea02ff2009-03-17 01:15:45 +00001755 // Both upper and lower are special, lower to a constant pool load:
1756 if (lower_special && upper_special) {
Nadav Rotemc32a8c92011-10-16 10:02:06 +00001757 SDValue UpperVal = DAG.getConstant(upper, MVT::i32);
1758 SDValue LowerVal = DAG.getConstant(lower, MVT::i32);
1759 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
1760 UpperVal, LowerVal, UpperVal, LowerVal);
1761 return DAG.getNode(ISD::BITCAST, dl, OpVT, BV);
Scott Michel7ea02ff2009-03-17 01:15:45 +00001762 }
1763
1764 SDValue LO32;
1765 SDValue HI32;
1766 SmallVector<SDValue, 16> ShufBytes;
1767 SDValue Result;
1768
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001769 // Create lower vector if not a special pattern
1770 if (!lower_special) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 SDValue LO32C = DAG.getConstant(lower, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001772 LO32 = DAG.getNode(ISD::BITCAST, dl, OpVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00001773 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga87008d2009-02-25 22:49:59 +00001774 LO32C, LO32C, LO32C, LO32C));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001775 }
1776
1777 // Create upper vector if not a special pattern
1778 if (!upper_special) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 SDValue HI32C = DAG.getConstant(upper, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001780 HI32 = DAG.getNode(ISD::BITCAST, dl, OpVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00001781 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga87008d2009-02-25 22:49:59 +00001782 HI32C, HI32C, HI32C, HI32C));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001783 }
1784
1785 // If either upper or lower are special, then the two input operands are
1786 // the same (basically, one of them is a "don't care")
1787 if (lower_special)
1788 LO32 = HI32;
1789 if (upper_special)
1790 HI32 = LO32;
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001791
1792 for (int i = 0; i < 4; ++i) {
1793 uint64_t val = 0;
1794 for (int j = 0; j < 4; ++j) {
1795 SDValue V;
1796 bool process_upper, process_lower;
1797 val <<= 8;
1798 process_upper = (upper_special && (i & 1) == 0);
1799 process_lower = (lower_special && (i & 1) == 1);
1800
1801 if (process_upper || process_lower) {
1802 if ((process_upper && upper == 0)
1803 || (process_lower && lower == 0))
1804 val |= 0x80;
1805 else if ((process_upper && upper == 0xffffffff)
1806 || (process_lower && lower == 0xffffffff))
1807 val |= 0xc0;
1808 else if ((process_upper && upper == 0x80000000)
1809 || (process_lower && lower == 0x80000000))
1810 val |= (j == 0 ? 0xe0 : 0x80);
1811 } else
1812 val |= i * 4 + j + ((i & 1) * 16);
1813 }
1814
Owen Anderson825b72b2009-08-11 20:47:22 +00001815 ShufBytes.push_back(DAG.getConstant(val, MVT::i32));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001816 }
1817
Dale Johannesened2eee62009-02-06 01:31:28 +00001818 return DAG.getNode(SPUISD::SHUFB, dl, OpVT, HI32, LO32,
Owen Anderson825b72b2009-08-11 20:47:22 +00001819 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga87008d2009-02-25 22:49:59 +00001820 &ShufBytes[0], ShufBytes.size()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001821 }
1822}
1823
Scott Michel266bc8f2007-12-04 22:23:35 +00001824/// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3) to something on
1825/// which the Cell can operate. The code inspects V3 to ascertain whether the
1826/// permutation vector, V3, is monotonically increasing with one "exception"
1827/// element, e.g., (0, 1, _, 3). If this is the case, then generate a
Scott Michel7a1c9e92008-11-22 23:50:42 +00001828/// SHUFFLE_MASK synthetic instruction. Otherwise, spill V3 to the constant pool.
Scott Michel266bc8f2007-12-04 22:23:35 +00001829/// In either case, the net result is going to eventually invoke SHUFB to
1830/// permute/shuffle the bytes from V1 and V2.
1831/// \note
Scott Michel7a1c9e92008-11-22 23:50:42 +00001832/// SHUFFLE_MASK is eventually selected as one of the C*D instructions, generate
Scott Michel266bc8f2007-12-04 22:23:35 +00001833/// control word for byte/halfword/word insertion. This takes care of a single
1834/// element move from V2 into V1.
1835/// \note
1836/// SPUISD::SHUFB is eventually selected as Cell's <i>shufb</i> instructions.
Dan Gohman475871a2008-07-27 21:46:04 +00001837static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001838 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue V1 = Op.getOperand(0);
1840 SDValue V2 = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001841 DebugLoc dl = Op.getDebugLoc();
Scott Michel5af8f0e2008-07-16 17:17:29 +00001842
Scott Michel266bc8f2007-12-04 22:23:35 +00001843 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michel5af8f0e2008-07-16 17:17:29 +00001844
Scott Michel266bc8f2007-12-04 22:23:35 +00001845 // If we have a single element being moved from V1 to V2, this can be handled
1846 // using the C*[DX] compute mask instructions, but the vector elements have
Kalle Raiskilaca9460f2010-08-18 10:20:29 +00001847 // to be monotonically increasing with one exception element, and the source
1848 // slot of the element to move must be the same as the destination.
Owen Andersone50ed302009-08-10 22:56:29 +00001849 EVT VecVT = V1.getValueType();
1850 EVT EltVT = VecVT.getVectorElementType();
Scott Michel266bc8f2007-12-04 22:23:35 +00001851 unsigned EltsFromV2 = 0;
Kalle Raiskilaca9460f2010-08-18 10:20:29 +00001852 unsigned V2EltOffset = 0;
Scott Michel266bc8f2007-12-04 22:23:35 +00001853 unsigned V2EltIdx0 = 0;
1854 unsigned CurrElt = 0;
Scott Michelcc188272008-12-04 21:01:44 +00001855 unsigned MaxElts = VecVT.getVectorNumElements();
1856 unsigned PrevElt = 0;
Scott Michel266bc8f2007-12-04 22:23:35 +00001857 bool monotonic = true;
Scott Michelcc188272008-12-04 21:01:44 +00001858 bool rotate = true;
Kalle Raiskilabb7d33a2010-09-09 07:30:15 +00001859 int rotamt=0;
Kalle Raiskila47948072010-06-21 10:17:36 +00001860 EVT maskVT; // which of the c?d instructions to use
Scott Michelcc188272008-12-04 21:01:44 +00001861
Owen Anderson825b72b2009-08-11 20:47:22 +00001862 if (EltVT == MVT::i8) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001863 V2EltIdx0 = 16;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001864 maskVT = MVT::v16i8;
Owen Anderson825b72b2009-08-11 20:47:22 +00001865 } else if (EltVT == MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001866 V2EltIdx0 = 8;
Kalle Raiskila47948072010-06-21 10:17:36 +00001867 maskVT = MVT::v8i16;
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 } else if (EltVT == MVT::i32 || EltVT == MVT::f32) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001869 V2EltIdx0 = 4;
Kalle Raiskila47948072010-06-21 10:17:36 +00001870 maskVT = MVT::v4i32;
Owen Anderson825b72b2009-08-11 20:47:22 +00001871 } else if (EltVT == MVT::i64 || EltVT == MVT::f64) {
Scott Michelcc188272008-12-04 21:01:44 +00001872 V2EltIdx0 = 2;
Kalle Raiskila47948072010-06-21 10:17:36 +00001873 maskVT = MVT::v2i64;
Scott Michelcc188272008-12-04 21:01:44 +00001874 } else
Torok Edwinc23197a2009-07-14 16:55:14 +00001875 llvm_unreachable("Unhandled vector type in LowerVECTOR_SHUFFLE");
Scott Michel266bc8f2007-12-04 22:23:35 +00001876
Nate Begeman9008ca62009-04-27 18:41:29 +00001877 for (unsigned i = 0; i != MaxElts; ++i) {
1878 if (SVN->getMaskElt(i) < 0)
1879 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001880
Nate Begeman9008ca62009-04-27 18:41:29 +00001881 unsigned SrcElt = SVN->getMaskElt(i);
Scott Michel266bc8f2007-12-04 22:23:35 +00001882
Nate Begeman9008ca62009-04-27 18:41:29 +00001883 if (monotonic) {
1884 if (SrcElt >= V2EltIdx0) {
Kalle Raiskilaca9460f2010-08-18 10:20:29 +00001885 // TODO: optimize for the monotonic case when several consecutive
1886 // elements are taken form V2. Do we ever get such a case?
1887 if (EltsFromV2 == 0 && CurrElt == (SrcElt - V2EltIdx0))
1888 V2EltOffset = (SrcElt - V2EltIdx0) * (EltVT.getSizeInBits()/8);
1889 else
1890 monotonic = false;
1891 ++EltsFromV2;
Nate Begeman9008ca62009-04-27 18:41:29 +00001892 } else if (CurrElt != SrcElt) {
1893 monotonic = false;
Scott Michelcc188272008-12-04 21:01:44 +00001894 }
1895
Nate Begeman9008ca62009-04-27 18:41:29 +00001896 ++CurrElt;
1897 }
1898
1899 if (rotate) {
1900 if (PrevElt > 0 && SrcElt < MaxElts) {
1901 if ((PrevElt == SrcElt - 1)
1902 || (PrevElt == MaxElts - 1 && SrcElt == 0)) {
Scott Michelcc188272008-12-04 21:01:44 +00001903 PrevElt = SrcElt;
1904 } else {
Scott Michelcc188272008-12-04 21:01:44 +00001905 rotate = false;
1906 }
Kalle Raiskila0b4ab0c2010-09-08 11:53:38 +00001907 } else if (i == 0 || (PrevElt==0 && SrcElt==1)) {
1908 // First time or after a "wrap around"
Kalle Raiskilad87e5712010-11-22 16:28:26 +00001909 rotamt = SrcElt-i;
Nate Begeman9008ca62009-04-27 18:41:29 +00001910 PrevElt = SrcElt;
1911 } else {
1912 // This isn't a rotation, takes elements from vector 2
1913 rotate = false;
Scott Michelcc188272008-12-04 21:01:44 +00001914 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001915 }
Scott Michel266bc8f2007-12-04 22:23:35 +00001916 }
1917
1918 if (EltsFromV2 == 1 && monotonic) {
1919 // Compute mask and shuffle
Owen Andersone50ed302009-08-10 22:56:29 +00001920 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Kalle Raiskila47948072010-06-21 10:17:36 +00001921
1922 // As SHUFFLE_MASK becomes a c?d instruction, feed it an address
1923 // R1 ($sp) is used here only as it is guaranteed to have last bits zero
1924 SDValue Pointer = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
1925 DAG.getRegister(SPU::R1, PtrVT),
Kalle Raiskilaca9460f2010-08-18 10:20:29 +00001926 DAG.getConstant(V2EltOffset, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001927 SDValue ShufMaskOp = DAG.getNode(SPUISD::SHUFFLE_MASK, dl,
Kalle Raiskila47948072010-06-21 10:17:36 +00001928 maskVT, Pointer);
1929
Scott Michel266bc8f2007-12-04 22:23:35 +00001930 // Use shuffle mask in SHUFB synthetic instruction:
Scott Michel6e1d1472009-03-16 18:47:25 +00001931 return DAG.getNode(SPUISD::SHUFB, dl, V1.getValueType(), V2, V1,
Dale Johannesena05dca42009-02-04 23:02:30 +00001932 ShufMaskOp);
Scott Michelcc188272008-12-04 21:01:44 +00001933 } else if (rotate) {
Kalle Raiskila0b4ab0c2010-09-08 11:53:38 +00001934 if (rotamt < 0)
1935 rotamt +=MaxElts;
1936 rotamt *= EltVT.getSizeInBits()/8;
Dale Johannesena05dca42009-02-04 23:02:30 +00001937 return DAG.getNode(SPUISD::ROTBYTES_LEFT, dl, V1.getValueType(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 V1, DAG.getConstant(rotamt, MVT::i16));
Scott Michel266bc8f2007-12-04 22:23:35 +00001939 } else {
Gabor Greif93c53e52008-08-31 15:37:04 +00001940 // Convert the SHUFFLE_VECTOR mask's input element units to the
1941 // actual bytes.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001942 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michel5af8f0e2008-07-16 17:17:29 +00001943
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SmallVector<SDValue, 16> ResultMask;
Nate Begeman9008ca62009-04-27 18:41:29 +00001945 for (unsigned i = 0, e = MaxElts; i != e; ++i) {
1946 unsigned SrcElt = SVN->getMaskElt(i) < 0 ? 0 : SVN->getMaskElt(i);
Scott Michel5af8f0e2008-07-16 17:17:29 +00001947
Nate Begeman9008ca62009-04-27 18:41:29 +00001948 for (unsigned j = 0; j < BytesPerElement; ++j)
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,MVT::i8));
Scott Michel266bc8f2007-12-04 22:23:35 +00001950 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga87008d2009-02-25 22:49:59 +00001952 &ResultMask[0], ResultMask.size());
Dale Johannesena05dca42009-02-04 23:02:30 +00001953 return DAG.getNode(SPUISD::SHUFB, dl, V1.getValueType(), V1, V2, VPermMask);
Scott Michel266bc8f2007-12-04 22:23:35 +00001954 }
1955}
1956
Dan Gohman475871a2008-07-27 21:46:04 +00001957static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
1958 SDValue Op0 = Op.getOperand(0); // Op0 = the scalar
Dale Johannesened2eee62009-02-06 01:31:28 +00001959 DebugLoc dl = Op.getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +00001960
Gabor Greifba36cb52008-08-28 21:40:38 +00001961 if (Op0.getNode()->getOpcode() == ISD::Constant) {
Scott Michel266bc8f2007-12-04 22:23:35 +00001962 // For a constant, build the appropriate constant vector, which will
1963 // eventually simplify to a vector register load.
1964
Gabor Greifba36cb52008-08-28 21:40:38 +00001965 ConstantSDNode *CN = cast<ConstantSDNode>(Op0.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001966 SmallVector<SDValue, 16> ConstVecValues;
Owen Andersone50ed302009-08-10 22:56:29 +00001967 EVT VT;
Scott Michel266bc8f2007-12-04 22:23:35 +00001968 size_t n_copies;
1969
1970 // Create a constant vector:
Owen Anderson825b72b2009-08-11 20:47:22 +00001971 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001972 default: llvm_unreachable("Unexpected constant value type in "
Torok Edwin481d15a2009-07-14 12:22:58 +00001973 "LowerSCALAR_TO_VECTOR");
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 case MVT::v16i8: n_copies = 16; VT = MVT::i8; break;
1975 case MVT::v8i16: n_copies = 8; VT = MVT::i16; break;
1976 case MVT::v4i32: n_copies = 4; VT = MVT::i32; break;
1977 case MVT::v4f32: n_copies = 4; VT = MVT::f32; break;
1978 case MVT::v2i64: n_copies = 2; VT = MVT::i64; break;
1979 case MVT::v2f64: n_copies = 2; VT = MVT::f64; break;
Scott Michel266bc8f2007-12-04 22:23:35 +00001980 }
1981
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001982 SDValue CValue = DAG.getConstant(CN->getZExtValue(), VT);
Scott Michel266bc8f2007-12-04 22:23:35 +00001983 for (size_t j = 0; j < n_copies; ++j)
1984 ConstVecValues.push_back(CValue);
1985
Evan Chenga87008d2009-02-25 22:49:59 +00001986 return DAG.getNode(ISD::BUILD_VECTOR, dl, Op.getValueType(),
1987 &ConstVecValues[0], ConstVecValues.size());
Scott Michel266bc8f2007-12-04 22:23:35 +00001988 } else {
1989 // Otherwise, copy the value from one register to another:
Owen Anderson825b72b2009-08-11 20:47:22 +00001990 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001991 default: llvm_unreachable("Unexpected value type in LowerSCALAR_TO_VECTOR");
Owen Anderson825b72b2009-08-11 20:47:22 +00001992 case MVT::i8:
1993 case MVT::i16:
1994 case MVT::i32:
1995 case MVT::i64:
1996 case MVT::f32:
1997 case MVT::f64:
Dale Johannesened2eee62009-02-06 01:31:28 +00001998 return DAG.getNode(SPUISD::PREFSLOT2VEC, dl, Op.getValueType(), Op0, Op0);
Scott Michel266bc8f2007-12-04 22:23:35 +00001999 }
2000 }
Scott Michel266bc8f2007-12-04 22:23:35 +00002001}
2002
Dan Gohman475871a2008-07-27 21:46:04 +00002003static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002004 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002005 SDValue N = Op.getOperand(0);
2006 SDValue Elt = Op.getOperand(1);
Dale Johannesened2eee62009-02-06 01:31:28 +00002007 DebugLoc dl = Op.getDebugLoc();
Scott Michel7a1c9e92008-11-22 23:50:42 +00002008 SDValue retval;
Scott Michel266bc8f2007-12-04 22:23:35 +00002009
Scott Michel7a1c9e92008-11-22 23:50:42 +00002010 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
2011 // Constant argument:
2012 int EltNo = (int) C->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00002013
Scott Michel7a1c9e92008-11-22 23:50:42 +00002014 // sanity checks:
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 if (VT == MVT::i8 && EltNo >= 16)
Torok Edwinc23197a2009-07-14 16:55:14 +00002016 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i8 extraction slot > 15");
Owen Anderson825b72b2009-08-11 20:47:22 +00002017 else if (VT == MVT::i16 && EltNo >= 8)
Torok Edwinc23197a2009-07-14 16:55:14 +00002018 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i16 extraction slot > 7");
Owen Anderson825b72b2009-08-11 20:47:22 +00002019 else if (VT == MVT::i32 && EltNo >= 4)
Torok Edwinc23197a2009-07-14 16:55:14 +00002020 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i32 extraction slot > 4");
Owen Anderson825b72b2009-08-11 20:47:22 +00002021 else if (VT == MVT::i64 && EltNo >= 2)
Torok Edwinc23197a2009-07-14 16:55:14 +00002022 llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i64 extraction slot > 2");
Scott Michel266bc8f2007-12-04 22:23:35 +00002023
Owen Anderson825b72b2009-08-11 20:47:22 +00002024 if (EltNo == 0 && (VT == MVT::i32 || VT == MVT::i64)) {
Scott Michel7a1c9e92008-11-22 23:50:42 +00002025 // i32 and i64: Element 0 is the preferred slot
Dale Johannesened2eee62009-02-06 01:31:28 +00002026 return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT, N);
Scott Michel7a1c9e92008-11-22 23:50:42 +00002027 }
Scott Michel266bc8f2007-12-04 22:23:35 +00002028
Scott Michel7a1c9e92008-11-22 23:50:42 +00002029 // Need to generate shuffle mask and extract:
2030 int prefslot_begin = -1, prefslot_end = -1;
2031 int elt_byte = EltNo * VT.getSizeInBits() / 8;
2032
Owen Anderson825b72b2009-08-11 20:47:22 +00002033 switch (VT.getSimpleVT().SimpleTy) {
Craig Topperbc219812012-02-07 02:50:20 +00002034 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002035 case MVT::i8: {
Scott Michel7a1c9e92008-11-22 23:50:42 +00002036 prefslot_begin = prefslot_end = 3;
2037 break;
2038 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002039 case MVT::i16: {
Scott Michel7a1c9e92008-11-22 23:50:42 +00002040 prefslot_begin = 2; prefslot_end = 3;
2041 break;
2042 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002043 case MVT::i32:
2044 case MVT::f32: {
Scott Michel7a1c9e92008-11-22 23:50:42 +00002045 prefslot_begin = 0; prefslot_end = 3;
2046 break;
2047 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 case MVT::i64:
2049 case MVT::f64: {
Scott Michel7a1c9e92008-11-22 23:50:42 +00002050 prefslot_begin = 0; prefslot_end = 7;
2051 break;
2052 }
2053 }
2054
2055 assert(prefslot_begin != -1 && prefslot_end != -1 &&
2056 "LowerEXTRACT_VECTOR_ELT: preferred slots uninitialized");
2057
Scott Michel9b2420d2009-08-24 21:53:27 +00002058 unsigned int ShufBytes[16] = {
2059 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2060 };
Scott Michel7a1c9e92008-11-22 23:50:42 +00002061 for (int i = 0; i < 16; ++i) {
2062 // zero fill uppper part of preferred slot, don't care about the
2063 // other slots:
2064 unsigned int mask_val;
2065 if (i <= prefslot_end) {
2066 mask_val =
2067 ((i < prefslot_begin)
2068 ? 0x80
2069 : elt_byte + (i - prefslot_begin));
2070
2071 ShufBytes[i] = mask_val;
2072 } else
2073 ShufBytes[i] = ShufBytes[i % (prefslot_end + 1)];
2074 }
2075
2076 SDValue ShufMask[4];
2077 for (unsigned i = 0; i < sizeof(ShufMask)/sizeof(ShufMask[0]); ++i) {
Scott Michelcc188272008-12-04 21:01:44 +00002078 unsigned bidx = i * 4;
Scott Michel7a1c9e92008-11-22 23:50:42 +00002079 unsigned int bits = ((ShufBytes[bidx] << 24) |
2080 (ShufBytes[bidx+1] << 16) |
2081 (ShufBytes[bidx+2] << 8) |
2082 ShufBytes[bidx+3]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002083 ShufMask[i] = DAG.getConstant(bits, MVT::i32);
Scott Michel7a1c9e92008-11-22 23:50:42 +00002084 }
2085
Scott Michel7ea02ff2009-03-17 01:15:45 +00002086 SDValue ShufMaskVec =
Owen Anderson825b72b2009-08-11 20:47:22 +00002087 DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +00002088 &ShufMask[0], sizeof(ShufMask)/sizeof(ShufMask[0]));
Scott Michel7a1c9e92008-11-22 23:50:42 +00002089
Dale Johannesened2eee62009-02-06 01:31:28 +00002090 retval = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
2091 DAG.getNode(SPUISD::SHUFB, dl, N.getValueType(),
Scott Michel7a1c9e92008-11-22 23:50:42 +00002092 N, N, ShufMaskVec));
2093 } else {
2094 // Variable index: Rotate the requested element into slot 0, then replicate
2095 // slot 0 across the vector
Owen Andersone50ed302009-08-10 22:56:29 +00002096 EVT VecVT = N.getValueType();
Kalle Raiskila82fe4672010-08-02 08:54:39 +00002097 if (!VecVT.isSimple() || !VecVT.isVector()) {
Chris Lattner75361b62010-04-07 22:58:41 +00002098 report_fatal_error("LowerEXTRACT_VECTOR_ELT: Must have a simple, 128-bit"
Torok Edwindac237e2009-07-08 20:53:28 +00002099 "vector type!");
Scott Michel7a1c9e92008-11-22 23:50:42 +00002100 }
2101
2102 // Make life easier by making sure the index is zero-extended to i32
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 if (Elt.getValueType() != MVT::i32)
2104 Elt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Elt);
Scott Michel7a1c9e92008-11-22 23:50:42 +00002105
2106 // Scale the index to a bit/byte shift quantity
2107 APInt scaleFactor =
Scott Michel104de432008-11-24 17:11:17 +00002108 APInt(32, uint64_t(16 / N.getValueType().getVectorNumElements()), false);
2109 unsigned scaleShift = scaleFactor.logBase2();
Scott Michel7a1c9e92008-11-22 23:50:42 +00002110 SDValue vecShift;
Scott Michel7a1c9e92008-11-22 23:50:42 +00002111
Scott Michel104de432008-11-24 17:11:17 +00002112 if (scaleShift > 0) {
2113 // Scale the shift factor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002114 Elt = DAG.getNode(ISD::SHL, dl, MVT::i32, Elt,
2115 DAG.getConstant(scaleShift, MVT::i32));
Scott Michel7a1c9e92008-11-22 23:50:42 +00002116 }
2117
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +00002118 vecShift = DAG.getNode(SPUISD::SHL_BYTES, dl, VecVT, N, Elt);
Scott Michel104de432008-11-24 17:11:17 +00002119
2120 // Replicate the bytes starting at byte 0 across the entire vector (for
2121 // consistency with the notion of a unified register set)
Scott Michel7a1c9e92008-11-22 23:50:42 +00002122 SDValue replicate;
2123
Owen Anderson825b72b2009-08-11 20:47:22 +00002124 switch (VT.getSimpleVT().SimpleTy) {
Scott Michel7a1c9e92008-11-22 23:50:42 +00002125 default:
Chris Lattner75361b62010-04-07 22:58:41 +00002126 report_fatal_error("LowerEXTRACT_VECTOR_ELT(varable): Unhandled vector"
Torok Edwindac237e2009-07-08 20:53:28 +00002127 "type");
Scott Michel7a1c9e92008-11-22 23:50:42 +00002128 /*NOTREACHED*/
Owen Anderson825b72b2009-08-11 20:47:22 +00002129 case MVT::i8: {
2130 SDValue factor = DAG.getConstant(0x00000000, MVT::i32);
2131 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +00002132 factor, factor, factor, factor);
Scott Michel7a1c9e92008-11-22 23:50:42 +00002133 break;
2134 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 case MVT::i16: {
2136 SDValue factor = DAG.getConstant(0x00010001, MVT::i32);
2137 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +00002138 factor, factor, factor, factor);
Scott Michel7a1c9e92008-11-22 23:50:42 +00002139 break;
2140 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002141 case MVT::i32:
2142 case MVT::f32: {
2143 SDValue factor = DAG.getConstant(0x00010203, MVT::i32);
2144 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +00002145 factor, factor, factor, factor);
Scott Michel7a1c9e92008-11-22 23:50:42 +00002146 break;
2147 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002148 case MVT::i64:
2149 case MVT::f64: {
2150 SDValue loFactor = DAG.getConstant(0x00010203, MVT::i32);
2151 SDValue hiFactor = DAG.getConstant(0x04050607, MVT::i32);
2152 replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Evan Chenga87008d2009-02-25 22:49:59 +00002153 loFactor, hiFactor, loFactor, hiFactor);
Scott Michel7a1c9e92008-11-22 23:50:42 +00002154 break;
2155 }
2156 }
2157
Dale Johannesened2eee62009-02-06 01:31:28 +00002158 retval = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
2159 DAG.getNode(SPUISD::SHUFB, dl, VecVT,
Scott Michel1a6cdb62008-12-01 17:56:02 +00002160 vecShift, vecShift, replicate));
Scott Michel266bc8f2007-12-04 22:23:35 +00002161 }
2162
Scott Michel7a1c9e92008-11-22 23:50:42 +00002163 return retval;
Scott Michel266bc8f2007-12-04 22:23:35 +00002164}
2165
Dan Gohman475871a2008-07-27 21:46:04 +00002166static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
2167 SDValue VecOp = Op.getOperand(0);
2168 SDValue ValOp = Op.getOperand(1);
2169 SDValue IdxOp = Op.getOperand(2);
Dale Johannesened2eee62009-02-06 01:31:28 +00002170 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002171 EVT VT = Op.getValueType();
Kalle Raiskilabd887df2010-08-29 12:41:50 +00002172 EVT eltVT = ValOp.getValueType();
Scott Michel266bc8f2007-12-04 22:23:35 +00002173
Kalle Raiskila43d225d2010-06-09 09:58:17 +00002174 // use 0 when the lane to insert to is 'undef'
Kalle Raiskilabd887df2010-08-29 12:41:50 +00002175 int64_t Offset=0;
Kalle Raiskila43d225d2010-06-09 09:58:17 +00002176 if (IdxOp.getOpcode() != ISD::UNDEF) {
2177 ConstantSDNode *CN = cast<ConstantSDNode>(IdxOp);
2178 assert(CN != 0 && "LowerINSERT_VECTOR_ELT: Index is not constant!");
Kalle Raiskilabd887df2010-08-29 12:41:50 +00002179 Offset = (CN->getSExtValue()) * eltVT.getSizeInBits()/8;
Kalle Raiskila43d225d2010-06-09 09:58:17 +00002180 }
Scott Michel266bc8f2007-12-04 22:23:35 +00002181
Owen Andersone50ed302009-08-10 22:56:29 +00002182 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michel1a6cdb62008-12-01 17:56:02 +00002183 // Use $sp ($1) because it's always 16-byte aligned and it's available:
Dale Johannesened2eee62009-02-06 01:31:28 +00002184 SDValue Pointer = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
Scott Michel1a6cdb62008-12-01 17:56:02 +00002185 DAG.getRegister(SPU::R1, PtrVT),
Kalle Raiskilabd887df2010-08-29 12:41:50 +00002186 DAG.getConstant(Offset, PtrVT));
Kalle Raiskilabc2697c2010-08-04 13:59:48 +00002187 // widen the mask when dealing with half vectors
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002188 EVT maskVT = EVT::getVectorVT(*(DAG.getContext()), VT.getVectorElementType(),
Kalle Raiskilabc2697c2010-08-04 13:59:48 +00002189 128/ VT.getVectorElementType().getSizeInBits());
2190 SDValue ShufMask = DAG.getNode(SPUISD::SHUFFLE_MASK, dl, maskVT, Pointer);
Scott Michel266bc8f2007-12-04 22:23:35 +00002191
Dan Gohman475871a2008-07-27 21:46:04 +00002192 SDValue result =
Dale Johannesened2eee62009-02-06 01:31:28 +00002193 DAG.getNode(SPUISD::SHUFB, dl, VT,
2194 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, ValOp),
Scott Michel1df30c42008-12-29 03:23:36 +00002195 VecOp,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002196 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ShufMask));
Scott Michel266bc8f2007-12-04 22:23:35 +00002197
2198 return result;
2199}
2200
Scott Michelf0569be2008-12-27 04:51:36 +00002201static SDValue LowerI8Math(SDValue Op, SelectionDAG &DAG, unsigned Opc,
2202 const TargetLowering &TLI)
Scott Michela59d4692008-02-23 18:41:37 +00002203{
Dan Gohman475871a2008-07-27 21:46:04 +00002204 SDValue N0 = Op.getOperand(0); // Everything has at least one operand
Dale Johannesened2eee62009-02-06 01:31:28 +00002205 DebugLoc dl = Op.getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002206 EVT ShiftVT = TLI.getShiftAmountTy(N0.getValueType());
Scott Michel266bc8f2007-12-04 22:23:35 +00002207
Owen Anderson825b72b2009-08-11 20:47:22 +00002208 assert(Op.getValueType() == MVT::i8);
Scott Michel266bc8f2007-12-04 22:23:35 +00002209 switch (Opc) {
2210 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00002211 llvm_unreachable("Unhandled i8 math operator");
Scott Michel02d711b2008-12-30 23:28:25 +00002212 case ISD::ADD: {
2213 // 8-bit addition: Promote the arguments up to 16-bits and truncate
2214 // the result:
2215 SDValue N1 = Op.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002216 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
2217 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
2218 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2219 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel02d711b2008-12-30 23:28:25 +00002220
2221 }
2222
Scott Michel266bc8f2007-12-04 22:23:35 +00002223 case ISD::SUB: {
2224 // 8-bit subtraction: Promote the arguments up to 16-bits and truncate
2225 // the result:
Dan Gohman475871a2008-07-27 21:46:04 +00002226 SDValue N1 = Op.getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002227 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
2228 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
2229 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2230 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel5af8f0e2008-07-16 17:17:29 +00002231 }
Scott Michel266bc8f2007-12-04 22:23:35 +00002232 case ISD::ROTR:
2233 case ISD::ROTL: {
Dan Gohman475871a2008-07-27 21:46:04 +00002234 SDValue N1 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002235 EVT N1VT = N1.getValueType();
Scott Michel7ea02ff2009-03-17 01:15:45 +00002236
Owen Anderson825b72b2009-08-11 20:47:22 +00002237 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, N0);
Scott Michel7ea02ff2009-03-17 01:15:45 +00002238 if (!N1VT.bitsEq(ShiftVT)) {
2239 unsigned N1Opc = N1.getValueType().bitsLT(ShiftVT)
2240 ? ISD::ZERO_EXTEND
2241 : ISD::TRUNCATE;
2242 N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
2243 }
2244
2245 // Replicate lower 8-bits into upper 8:
Dan Gohman475871a2008-07-27 21:46:04 +00002246 SDValue ExpandArg =
Owen Anderson825b72b2009-08-11 20:47:22 +00002247 DAG.getNode(ISD::OR, dl, MVT::i16, N0,
2248 DAG.getNode(ISD::SHL, dl, MVT::i16,
2249 N0, DAG.getConstant(8, MVT::i32)));
Scott Michel7ea02ff2009-03-17 01:15:45 +00002250
2251 // Truncate back down to i8
Owen Anderson825b72b2009-08-11 20:47:22 +00002252 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2253 DAG.getNode(Opc, dl, MVT::i16, ExpandArg, N1));
Scott Michel266bc8f2007-12-04 22:23:35 +00002254 }
2255 case ISD::SRL:
2256 case ISD::SHL: {
Dan Gohman475871a2008-07-27 21:46:04 +00002257 SDValue N1 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002258 EVT N1VT = N1.getValueType();
Scott Michel7ea02ff2009-03-17 01:15:45 +00002259
Owen Anderson825b72b2009-08-11 20:47:22 +00002260 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, N0);
Scott Michel7ea02ff2009-03-17 01:15:45 +00002261 if (!N1VT.bitsEq(ShiftVT)) {
2262 unsigned N1Opc = ISD::ZERO_EXTEND;
2263
2264 if (N1.getValueType().bitsGT(ShiftVT))
2265 N1Opc = ISD::TRUNCATE;
2266
2267 N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
2268 }
2269
Owen Anderson825b72b2009-08-11 20:47:22 +00002270 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2271 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel266bc8f2007-12-04 22:23:35 +00002272 }
2273 case ISD::SRA: {
Dan Gohman475871a2008-07-27 21:46:04 +00002274 SDValue N1 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002275 EVT N1VT = N1.getValueType();
Scott Michel7ea02ff2009-03-17 01:15:45 +00002276
Owen Anderson825b72b2009-08-11 20:47:22 +00002277 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
Scott Michel7ea02ff2009-03-17 01:15:45 +00002278 if (!N1VT.bitsEq(ShiftVT)) {
2279 unsigned N1Opc = ISD::SIGN_EXTEND;
2280
2281 if (N1VT.bitsGT(ShiftVT))
2282 N1Opc = ISD::TRUNCATE;
2283 N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
2284 }
2285
Owen Anderson825b72b2009-08-11 20:47:22 +00002286 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2287 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel266bc8f2007-12-04 22:23:35 +00002288 }
2289 case ISD::MUL: {
Dan Gohman475871a2008-07-27 21:46:04 +00002290 SDValue N1 = Op.getOperand(1);
Scott Michel7ea02ff2009-03-17 01:15:45 +00002291
Owen Anderson825b72b2009-08-11 20:47:22 +00002292 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
2293 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
2294 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
2295 DAG.getNode(Opc, dl, MVT::i16, N0, N1));
Scott Michel266bc8f2007-12-04 22:23:35 +00002296 }
2297 }
Scott Michel266bc8f2007-12-04 22:23:35 +00002298}
2299
2300//! Lower byte immediate operations for v16i8 vectors:
Dan Gohman475871a2008-07-27 21:46:04 +00002301static SDValue
2302LowerByteImmed(SDValue Op, SelectionDAG &DAG) {
2303 SDValue ConstVec;
2304 SDValue Arg;
Owen Andersone50ed302009-08-10 22:56:29 +00002305 EVT VT = Op.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00002306 DebugLoc dl = Op.getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +00002307
2308 ConstVec = Op.getOperand(0);
2309 Arg = Op.getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002310 if (ConstVec.getNode()->getOpcode() != ISD::BUILD_VECTOR) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002311 if (ConstVec.getNode()->getOpcode() == ISD::BITCAST) {
Scott Michel266bc8f2007-12-04 22:23:35 +00002312 ConstVec = ConstVec.getOperand(0);
2313 } else {
2314 ConstVec = Op.getOperand(1);
2315 Arg = Op.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002316 if (ConstVec.getNode()->getOpcode() == ISD::BITCAST) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002317 ConstVec = ConstVec.getOperand(0);
Scott Michel266bc8f2007-12-04 22:23:35 +00002318 }
2319 }
2320 }
2321
Gabor Greifba36cb52008-08-28 21:40:38 +00002322 if (ConstVec.getNode()->getOpcode() == ISD::BUILD_VECTOR) {
Scott Michel7ea02ff2009-03-17 01:15:45 +00002323 BuildVectorSDNode *BCN = dyn_cast<BuildVectorSDNode>(ConstVec.getNode());
2324 assert(BCN != 0 && "Expected BuildVectorSDNode in SPU LowerByteImmed");
Scott Michel266bc8f2007-12-04 22:23:35 +00002325
Scott Michel7ea02ff2009-03-17 01:15:45 +00002326 APInt APSplatBits, APSplatUndef;
2327 unsigned SplatBitSize;
2328 bool HasAnyUndefs;
2329 unsigned minSplatBits = VT.getVectorElementType().getSizeInBits();
2330
2331 if (BCN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
2332 HasAnyUndefs, minSplatBits)
2333 && minSplatBits <= SplatBitSize) {
2334 uint64_t SplatBits = APSplatBits.getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00002335 SDValue tc = DAG.getTargetConstant(SplatBits & 0xff, MVT::i8);
Scott Michel266bc8f2007-12-04 22:23:35 +00002336
Scott Michel7ea02ff2009-03-17 01:15:45 +00002337 SmallVector<SDValue, 16> tcVec;
2338 tcVec.assign(16, tc);
Dale Johannesened2eee62009-02-06 01:31:28 +00002339 return DAG.getNode(Op.getNode()->getOpcode(), dl, VT, Arg,
Scott Michel7ea02ff2009-03-17 01:15:45 +00002340 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &tcVec[0], tcVec.size()));
Scott Michel266bc8f2007-12-04 22:23:35 +00002341 }
2342 }
Scott Michel9de57a92009-01-26 22:33:37 +00002343
Nate Begeman24dc3462008-07-29 19:07:27 +00002344 // These operations (AND, OR, XOR) are legal, they just couldn't be custom
2345 // lowered. Return the operation, rather than a null SDValue.
2346 return Op;
Scott Michel266bc8f2007-12-04 22:23:35 +00002347}
2348
Scott Michel266bc8f2007-12-04 22:23:35 +00002349//! Custom lowering for CTPOP (count population)
2350/*!
2351 Custom lowering code that counts the number ones in the input
2352 operand. SPU has such an instruction, but it counts the number of
2353 ones per byte, which then have to be accumulated.
2354*/
Dan Gohman475871a2008-07-27 21:46:04 +00002355static SDValue LowerCTPOP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002356 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002357 EVT vecVT = EVT::getVectorVT(*DAG.getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +00002358 VT, (128 / VT.getSizeInBits()));
Dale Johannesena05dca42009-02-04 23:02:30 +00002359 DebugLoc dl = Op.getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +00002360
Owen Anderson825b72b2009-08-11 20:47:22 +00002361 switch (VT.getSimpleVT().SimpleTy) {
Craig Topperbc219812012-02-07 02:50:20 +00002362 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002363 case MVT::i8: {
Dan Gohman475871a2008-07-27 21:46:04 +00002364 SDValue N = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00002365 SDValue Elt0 = DAG.getConstant(0, MVT::i32);
Scott Michel266bc8f2007-12-04 22:23:35 +00002366
Dale Johannesena05dca42009-02-04 23:02:30 +00002367 SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
2368 SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
Scott Michel266bc8f2007-12-04 22:23:35 +00002369
Owen Anderson825b72b2009-08-11 20:47:22 +00002370 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i8, CNTB, Elt0);
Scott Michel266bc8f2007-12-04 22:23:35 +00002371 }
2372
Owen Anderson825b72b2009-08-11 20:47:22 +00002373 case MVT::i16: {
Scott Michel266bc8f2007-12-04 22:23:35 +00002374 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00002375 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +00002376
Chris Lattner84bc5422007-12-31 04:13:23 +00002377 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R16CRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00002378
Dan Gohman475871a2008-07-27 21:46:04 +00002379 SDValue N = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00002380 SDValue Elt0 = DAG.getConstant(0, MVT::i16);
2381 SDValue Mask0 = DAG.getConstant(0x0f, MVT::i16);
2382 SDValue Shift1 = DAG.getConstant(8, MVT::i32);
Scott Michel266bc8f2007-12-04 22:23:35 +00002383
Dale Johannesena05dca42009-02-04 23:02:30 +00002384 SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
2385 SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
Scott Michel266bc8f2007-12-04 22:23:35 +00002386
2387 // CNTB_result becomes the chain to which all of the virtual registers
2388 // CNTB_reg, SUM1_reg become associated:
Dan Gohman475871a2008-07-27 21:46:04 +00002389 SDValue CNTB_result =
Owen Anderson825b72b2009-08-11 20:47:22 +00002390 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, CNTB, Elt0);
Scott Michel5af8f0e2008-07-16 17:17:29 +00002391
Dan Gohman475871a2008-07-27 21:46:04 +00002392 SDValue CNTB_rescopy =
Dale Johannesena05dca42009-02-04 23:02:30 +00002393 DAG.getCopyToReg(CNTB_result, dl, CNTB_reg, CNTB_result);
Scott Michel266bc8f2007-12-04 22:23:35 +00002394
Owen Anderson825b72b2009-08-11 20:47:22 +00002395 SDValue Tmp1 = DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i16);
Scott Michel266bc8f2007-12-04 22:23:35 +00002396
Owen Anderson825b72b2009-08-11 20:47:22 +00002397 return DAG.getNode(ISD::AND, dl, MVT::i16,
2398 DAG.getNode(ISD::ADD, dl, MVT::i16,
2399 DAG.getNode(ISD::SRL, dl, MVT::i16,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002400 Tmp1, Shift1),
2401 Tmp1),
2402 Mask0);
Scott Michel266bc8f2007-12-04 22:23:35 +00002403 }
2404
Owen Anderson825b72b2009-08-11 20:47:22 +00002405 case MVT::i32: {
Scott Michel266bc8f2007-12-04 22:23:35 +00002406 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner84bc5422007-12-31 04:13:23 +00002407 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +00002408
Chris Lattner84bc5422007-12-31 04:13:23 +00002409 unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
2410 unsigned SUM1_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00002411
Dan Gohman475871a2008-07-27 21:46:04 +00002412 SDValue N = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00002413 SDValue Elt0 = DAG.getConstant(0, MVT::i32);
2414 SDValue Mask0 = DAG.getConstant(0xff, MVT::i32);
2415 SDValue Shift1 = DAG.getConstant(16, MVT::i32);
2416 SDValue Shift2 = DAG.getConstant(8, MVT::i32);
Scott Michel266bc8f2007-12-04 22:23:35 +00002417
Dale Johannesena05dca42009-02-04 23:02:30 +00002418 SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
2419 SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
Scott Michel266bc8f2007-12-04 22:23:35 +00002420
2421 // CNTB_result becomes the chain to which all of the virtual registers
2422 // CNTB_reg, SUM1_reg become associated:
Dan Gohman475871a2008-07-27 21:46:04 +00002423 SDValue CNTB_result =
Owen Anderson825b72b2009-08-11 20:47:22 +00002424 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, CNTB, Elt0);
Scott Michel5af8f0e2008-07-16 17:17:29 +00002425
Dan Gohman475871a2008-07-27 21:46:04 +00002426 SDValue CNTB_rescopy =
Dale Johannesena05dca42009-02-04 23:02:30 +00002427 DAG.getCopyToReg(CNTB_result, dl, CNTB_reg, CNTB_result);
Scott Michel266bc8f2007-12-04 22:23:35 +00002428
Dan Gohman475871a2008-07-27 21:46:04 +00002429 SDValue Comp1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00002430 DAG.getNode(ISD::SRL, dl, MVT::i32,
2431 DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i32),
Dale Johannesena05dca42009-02-04 23:02:30 +00002432 Shift1);
Scott Michel266bc8f2007-12-04 22:23:35 +00002433
Dan Gohman475871a2008-07-27 21:46:04 +00002434 SDValue Sum1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00002435 DAG.getNode(ISD::ADD, dl, MVT::i32, Comp1,
2436 DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +00002437
Dan Gohman475871a2008-07-27 21:46:04 +00002438 SDValue Sum1_rescopy =
Dale Johannesena05dca42009-02-04 23:02:30 +00002439 DAG.getCopyToReg(CNTB_result, dl, SUM1_reg, Sum1);
Scott Michel266bc8f2007-12-04 22:23:35 +00002440
Dan Gohman475871a2008-07-27 21:46:04 +00002441 SDValue Comp2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00002442 DAG.getNode(ISD::SRL, dl, MVT::i32,
2443 DAG.getCopyFromReg(Sum1_rescopy, dl, SUM1_reg, MVT::i32),
Scott Michel7f9ba9b2008-01-30 02:55:46 +00002444 Shift2);
Dan Gohman475871a2008-07-27 21:46:04 +00002445 SDValue Sum2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00002446 DAG.getNode(ISD::ADD, dl, MVT::i32, Comp2,
2447 DAG.getCopyFromReg(Sum1_rescopy, dl, SUM1_reg, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +00002448
Owen Anderson825b72b2009-08-11 20:47:22 +00002449 return DAG.getNode(ISD::AND, dl, MVT::i32, Sum2, Mask0);
Scott Michel266bc8f2007-12-04 22:23:35 +00002450 }
2451
Owen Anderson825b72b2009-08-11 20:47:22 +00002452 case MVT::i64:
Scott Michel266bc8f2007-12-04 22:23:35 +00002453 break;
2454 }
2455
Dan Gohman475871a2008-07-27 21:46:04 +00002456 return SDValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00002457}
2458
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002459//! Lower ISD::FP_TO_SINT, ISD::FP_TO_UINT for i32
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002460/*!
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002461 f32->i32 passes through unchanged, whereas f64->i32 expands to a libcall.
2462 All conversions to i64 are expanded to a libcall.
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002463 */
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002464static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002465 const SPUTargetLowering &TLI) {
Owen Andersone50ed302009-08-10 22:56:29 +00002466 EVT OpVT = Op.getValueType();
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002467 SDValue Op0 = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002468 EVT Op0VT = Op0.getValueType();
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002469
Owen Anderson825b72b2009-08-11 20:47:22 +00002470 if ((OpVT == MVT::i32 && Op0VT == MVT::f64)
2471 || OpVT == MVT::i64) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002472 // Convert f32 / f64 to i32 / i64 via libcall.
2473 RTLIB::Libcall LC =
2474 (Op.getOpcode() == ISD::FP_TO_SINT)
2475 ? RTLIB::getFPTOSINT(Op0VT, OpVT)
2476 : RTLIB::getFPTOUINT(Op0VT, OpVT);
2477 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
2478 SDValue Dummy;
2479 return ExpandLibCall(LC, Op, DAG, false, Dummy, TLI);
2480 }
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002481
Eli Friedman36df4992009-05-27 00:47:34 +00002482 return Op;
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002483}
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002484
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002485//! Lower ISD::SINT_TO_FP, ISD::UINT_TO_FP for i32
2486/*!
2487 i32->f32 passes through unchanged, whereas i32->f64 is expanded to a libcall.
2488 All conversions from i64 are expanded to a libcall.
2489 */
2490static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002491 const SPUTargetLowering &TLI) {
Owen Andersone50ed302009-08-10 22:56:29 +00002492 EVT OpVT = Op.getValueType();
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002493 SDValue Op0 = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002494 EVT Op0VT = Op0.getValueType();
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002495
Owen Anderson825b72b2009-08-11 20:47:22 +00002496 if ((OpVT == MVT::f64 && Op0VT == MVT::i32)
2497 || Op0VT == MVT::i64) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002498 // Convert i32, i64 to f64 via libcall:
2499 RTLIB::Libcall LC =
2500 (Op.getOpcode() == ISD::SINT_TO_FP)
2501 ? RTLIB::getSINTTOFP(Op0VT, OpVT)
2502 : RTLIB::getUINTTOFP(Op0VT, OpVT);
2503 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd int-to-fp conversion!");
2504 SDValue Dummy;
2505 return ExpandLibCall(LC, Op, DAG, false, Dummy, TLI);
2506 }
2507
Eli Friedman36df4992009-05-27 00:47:34 +00002508 return Op;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002509}
2510
2511//! Lower ISD::SETCC
2512/*!
Owen Anderson825b72b2009-08-11 20:47:22 +00002513 This handles MVT::f64 (double floating point) condition lowering
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002514 */
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002515static SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG,
2516 const TargetLowering &TLI) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002517 CondCodeSDNode *CC = dyn_cast<CondCodeSDNode>(Op.getOperand(2));
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002518 DebugLoc dl = Op.getDebugLoc();
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002519 assert(CC != 0 && "LowerSETCC: CondCodeSDNode should not be null here!\n");
2520
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002521 SDValue lhs = Op.getOperand(0);
2522 SDValue rhs = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002523 EVT lhsVT = lhs.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00002524 assert(lhsVT == MVT::f64 && "LowerSETCC: type other than MVT::64\n");
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002525
Owen Andersone50ed302009-08-10 22:56:29 +00002526 EVT ccResultVT = TLI.getSetCCResultType(lhs.getValueType());
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002527 APInt ccResultOnes = APInt::getAllOnesValue(ccResultVT.getSizeInBits());
Owen Anderson825b72b2009-08-11 20:47:22 +00002528 EVT IntVT(MVT::i64);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002529
2530 // Take advantage of the fact that (truncate (sra arg, 32)) is efficiently
2531 // selected to a NOP:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002532 SDValue i64lhs = DAG.getNode(ISD::BITCAST, dl, IntVT, lhs);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002533 SDValue lhsHi32 =
Owen Anderson825b72b2009-08-11 20:47:22 +00002534 DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
Dale Johannesenf5d97892009-02-04 01:48:28 +00002535 DAG.getNode(ISD::SRL, dl, IntVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002536 i64lhs, DAG.getConstant(32, MVT::i32)));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002537 SDValue lhsHi32abs =
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 DAG.getNode(ISD::AND, dl, MVT::i32,
2539 lhsHi32, DAG.getConstant(0x7fffffff, MVT::i32));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002540 SDValue lhsLo32 =
Owen Anderson825b72b2009-08-11 20:47:22 +00002541 DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, i64lhs);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002542
2543 // SETO and SETUO only use the lhs operand:
2544 if (CC->get() == ISD::SETO) {
2545 // Evaluates to true if Op0 is not [SQ]NaN - lowers to the inverse of
2546 // SETUO
2547 APInt ccResultAllOnes = APInt::getAllOnesValue(ccResultVT.getSizeInBits());
Dale Johannesenf5d97892009-02-04 01:48:28 +00002548 return DAG.getNode(ISD::XOR, dl, ccResultVT,
2549 DAG.getSetCC(dl, ccResultVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002550 lhs, DAG.getConstantFP(0.0, lhsVT),
2551 ISD::SETUO),
2552 DAG.getConstant(ccResultAllOnes, ccResultVT));
2553 } else if (CC->get() == ISD::SETUO) {
2554 // Evaluates to true if Op0 is [SQ]NaN
Dale Johannesenf5d97892009-02-04 01:48:28 +00002555 return DAG.getNode(ISD::AND, dl, ccResultVT,
2556 DAG.getSetCC(dl, ccResultVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002557 lhsHi32abs,
Owen Anderson825b72b2009-08-11 20:47:22 +00002558 DAG.getConstant(0x7ff00000, MVT::i32),
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002559 ISD::SETGE),
Dale Johannesenf5d97892009-02-04 01:48:28 +00002560 DAG.getSetCC(dl, ccResultVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002561 lhsLo32,
Owen Anderson825b72b2009-08-11 20:47:22 +00002562 DAG.getConstant(0, MVT::i32),
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002563 ISD::SETGT));
2564 }
2565
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002566 SDValue i64rhs = DAG.getNode(ISD::BITCAST, dl, IntVT, rhs);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002567 SDValue rhsHi32 =
Owen Anderson825b72b2009-08-11 20:47:22 +00002568 DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
Dale Johannesenf5d97892009-02-04 01:48:28 +00002569 DAG.getNode(ISD::SRL, dl, IntVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002570 i64rhs, DAG.getConstant(32, MVT::i32)));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002571
2572 // If a value is negative, subtract from the sign magnitude constant:
2573 SDValue signMag2TC = DAG.getConstant(0x8000000000000000ULL, IntVT);
2574
2575 // Convert the sign-magnitude representation into 2's complement:
Dale Johannesenf5d97892009-02-04 01:48:28 +00002576 SDValue lhsSelectMask = DAG.getNode(ISD::SRA, dl, ccResultVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002577 lhsHi32, DAG.getConstant(31, MVT::i32));
Dale Johannesenf5d97892009-02-04 01:48:28 +00002578 SDValue lhsSignMag2TC = DAG.getNode(ISD::SUB, dl, IntVT, signMag2TC, i64lhs);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002579 SDValue lhsSelect =
Dale Johannesenf5d97892009-02-04 01:48:28 +00002580 DAG.getNode(ISD::SELECT, dl, IntVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002581 lhsSelectMask, lhsSignMag2TC, i64lhs);
2582
Dale Johannesenf5d97892009-02-04 01:48:28 +00002583 SDValue rhsSelectMask = DAG.getNode(ISD::SRA, dl, ccResultVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002584 rhsHi32, DAG.getConstant(31, MVT::i32));
Dale Johannesenf5d97892009-02-04 01:48:28 +00002585 SDValue rhsSignMag2TC = DAG.getNode(ISD::SUB, dl, IntVT, signMag2TC, i64rhs);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002586 SDValue rhsSelect =
Dale Johannesenf5d97892009-02-04 01:48:28 +00002587 DAG.getNode(ISD::SELECT, dl, IntVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002588 rhsSelectMask, rhsSignMag2TC, i64rhs);
2589
2590 unsigned compareOp;
2591
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002592 switch (CC->get()) {
2593 case ISD::SETOEQ:
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002594 case ISD::SETUEQ:
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002595 compareOp = ISD::SETEQ; break;
2596 case ISD::SETOGT:
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002597 case ISD::SETUGT:
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002598 compareOp = ISD::SETGT; break;
2599 case ISD::SETOGE:
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002600 case ISD::SETUGE:
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002601 compareOp = ISD::SETGE; break;
2602 case ISD::SETOLT:
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002603 case ISD::SETULT:
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002604 compareOp = ISD::SETLT; break;
2605 case ISD::SETOLE:
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002606 case ISD::SETULE:
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002607 compareOp = ISD::SETLE; break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002608 case ISD::SETUNE:
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002609 case ISD::SETONE:
2610 compareOp = ISD::SETNE; break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002611 default:
Chris Lattner75361b62010-04-07 22:58:41 +00002612 report_fatal_error("CellSPU ISel Select: unimplemented f64 condition");
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002613 }
2614
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002615 SDValue result =
Scott Michel6e1d1472009-03-16 18:47:25 +00002616 DAG.getSetCC(dl, ccResultVT, lhsSelect, rhsSelect,
Dale Johannesenf5d97892009-02-04 01:48:28 +00002617 (ISD::CondCode) compareOp);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002618
2619 if ((CC->get() & 0x8) == 0) {
2620 // Ordered comparison:
Dale Johannesenf5d97892009-02-04 01:48:28 +00002621 SDValue lhsNaN = DAG.getSetCC(dl, ccResultVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002622 lhs, DAG.getConstantFP(0.0, MVT::f64),
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002623 ISD::SETO);
Dale Johannesenf5d97892009-02-04 01:48:28 +00002624 SDValue rhsNaN = DAG.getSetCC(dl, ccResultVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00002625 rhs, DAG.getConstantFP(0.0, MVT::f64),
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002626 ISD::SETO);
Dale Johannesenf5d97892009-02-04 01:48:28 +00002627 SDValue ordered = DAG.getNode(ISD::AND, dl, ccResultVT, lhsNaN, rhsNaN);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002628
Dale Johannesenf5d97892009-02-04 01:48:28 +00002629 result = DAG.getNode(ISD::AND, dl, ccResultVT, ordered, result);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002630 }
2631
2632 return result;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002633}
2634
Scott Michel7a1c9e92008-11-22 23:50:42 +00002635//! Lower ISD::SELECT_CC
2636/*!
2637 ISD::SELECT_CC can (generally) be implemented directly on the SPU using the
2638 SELB instruction.
2639
2640 \note Need to revisit this in the future: if the code path through the true
2641 and false value computations is longer than the latency of a branch (6
2642 cycles), then it would be more advantageous to branch and insert a new basic
2643 block and branch on the condition. However, this code does not make that
2644 assumption, given the simplisitc uses so far.
2645 */
2646
Scott Michelf0569be2008-12-27 04:51:36 +00002647static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2648 const TargetLowering &TLI) {
Owen Andersone50ed302009-08-10 22:56:29 +00002649 EVT VT = Op.getValueType();
Scott Michel7a1c9e92008-11-22 23:50:42 +00002650 SDValue lhs = Op.getOperand(0);
2651 SDValue rhs = Op.getOperand(1);
2652 SDValue trueval = Op.getOperand(2);
2653 SDValue falseval = Op.getOperand(3);
2654 SDValue condition = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002655 DebugLoc dl = Op.getDebugLoc();
Scott Michel7a1c9e92008-11-22 23:50:42 +00002656
Scott Michelf0569be2008-12-27 04:51:36 +00002657 // NOTE: SELB's arguments: $rA, $rB, $mask
2658 //
2659 // SELB selects bits from $rA where bits in $mask are 0, bits from $rB
2660 // where bits in $mask are 1. CCond will be inverted, having 1s where the
2661 // condition was true and 0s where the condition was false. Hence, the
2662 // arguments to SELB get reversed.
2663
Scott Michel7a1c9e92008-11-22 23:50:42 +00002664 // Note: Really should be ISD::SELECT instead of SPUISD::SELB, but LLVM's
2665 // legalizer insists on combining SETCC/SELECT into SELECT_CC, so we end up
2666 // with another "cannot select select_cc" assert:
2667
Dale Johannesende064702009-02-06 21:50:26 +00002668 SDValue compare = DAG.getNode(ISD::SETCC, dl,
Duncan Sands5480c042009-01-01 15:52:00 +00002669 TLI.getSetCCResultType(Op.getValueType()),
Scott Michelf0569be2008-12-27 04:51:36 +00002670 lhs, rhs, condition);
Dale Johannesende064702009-02-06 21:50:26 +00002671 return DAG.getNode(SPUISD::SELB, dl, VT, falseval, trueval, compare);
Scott Michel7a1c9e92008-11-22 23:50:42 +00002672}
2673
Scott Michelb30e8f62008-12-02 19:53:53 +00002674//! Custom lower ISD::TRUNCATE
2675static SDValue LowerTRUNCATE(SDValue Op, SelectionDAG &DAG)
2676{
Scott Michel6e1d1472009-03-16 18:47:25 +00002677 // Type to truncate to
Owen Andersone50ed302009-08-10 22:56:29 +00002678 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00002679 MVT simpleVT = VT.getSimpleVT();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002680 EVT VecVT = EVT::getVectorVT(*DAG.getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +00002681 VT, (128 / VT.getSizeInBits()));
Dale Johannesende064702009-02-06 21:50:26 +00002682 DebugLoc dl = Op.getDebugLoc();
Scott Michelb30e8f62008-12-02 19:53:53 +00002683
Scott Michel6e1d1472009-03-16 18:47:25 +00002684 // Type to truncate from
Scott Michelb30e8f62008-12-02 19:53:53 +00002685 SDValue Op0 = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002686 EVT Op0VT = Op0.getValueType();
Scott Michelb30e8f62008-12-02 19:53:53 +00002687
Duncan Sandscdfad362010-11-03 12:17:33 +00002688 if (Op0VT == MVT::i128 && simpleVT == MVT::i64) {
Scott Michel52d00012009-01-03 00:27:53 +00002689 // Create shuffle mask, least significant doubleword of quadword
Scott Michelf0569be2008-12-27 04:51:36 +00002690 unsigned maskHigh = 0x08090a0b;
2691 unsigned maskLow = 0x0c0d0e0f;
2692 // Use a shuffle to perform the truncation
Owen Anderson825b72b2009-08-11 20:47:22 +00002693 SDValue shufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
2694 DAG.getConstant(maskHigh, MVT::i32),
2695 DAG.getConstant(maskLow, MVT::i32),
2696 DAG.getConstant(maskHigh, MVT::i32),
2697 DAG.getConstant(maskLow, MVT::i32));
Scott Michelf0569be2008-12-27 04:51:36 +00002698
Scott Michel6e1d1472009-03-16 18:47:25 +00002699 SDValue truncShuffle = DAG.getNode(SPUISD::SHUFB, dl, VecVT,
2700 Op0, Op0, shufMask);
Scott Michelf0569be2008-12-27 04:51:36 +00002701
Scott Michel6e1d1472009-03-16 18:47:25 +00002702 return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT, truncShuffle);
Scott Michelb30e8f62008-12-02 19:53:53 +00002703 }
2704
Scott Michelf0569be2008-12-27 04:51:36 +00002705 return SDValue(); // Leave the truncate unmolested
Scott Michelb30e8f62008-12-02 19:53:53 +00002706}
2707
Scott Michel77f452d2009-08-25 22:37:34 +00002708/*!
2709 * Emit the instruction sequence for i64/i32 -> i128 sign extend. The basic
2710 * algorithm is to duplicate the sign bit using rotmai to generate at
2711 * least one byte full of sign bits. Then propagate the "sign-byte" into
2712 * the leftmost words and the i64/i32 into the rightmost words using shufb.
2713 *
2714 * @param Op The sext operand
2715 * @param DAG The current DAG
2716 * @return The SDValue with the entire instruction sequence
2717 */
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002718static SDValue LowerSIGN_EXTEND(SDValue Op, SelectionDAG &DAG)
2719{
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002720 DebugLoc dl = Op.getDebugLoc();
2721
Scott Michel77f452d2009-08-25 22:37:34 +00002722 // Type to extend to
2723 MVT OpVT = Op.getValueType().getSimpleVT();
Scott Michel77f452d2009-08-25 22:37:34 +00002724
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002725 // Type to extend from
2726 SDValue Op0 = Op.getOperand(0);
Scott Michel77f452d2009-08-25 22:37:34 +00002727 MVT Op0VT = Op0.getValueType().getSimpleVT();
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002728
Kalle Raiskila5106b842011-01-20 15:49:06 +00002729 // extend i8 & i16 via i32
2730 if (Op0VT == MVT::i8 || Op0VT == MVT::i16) {
2731 Op0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, Op0);
2732 Op0VT = MVT::i32;
2733 }
2734
Scott Michel77f452d2009-08-25 22:37:34 +00002735 // The type to extend to needs to be a i128 and
2736 // the type to extend from needs to be i64 or i32.
2737 assert((OpVT == MVT::i128 && (Op0VT == MVT::i64 || Op0VT == MVT::i32)) &&
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002738 "LowerSIGN_EXTEND: input and/or output operand have wrong size");
Duncan Sands1f6a3292011-08-12 14:54:45 +00002739 (void)OpVT;
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002740
2741 // Create shuffle mask
Scott Michel77f452d2009-08-25 22:37:34 +00002742 unsigned mask1 = 0x10101010; // byte 0 - 3 and 4 - 7
2743 unsigned mask2 = Op0VT == MVT::i64 ? 0x00010203 : 0x10101010; // byte 8 - 11
2744 unsigned mask3 = Op0VT == MVT::i64 ? 0x04050607 : 0x00010203; // byte 12 - 15
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002745 SDValue shufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
2746 DAG.getConstant(mask1, MVT::i32),
2747 DAG.getConstant(mask1, MVT::i32),
2748 DAG.getConstant(mask2, MVT::i32),
2749 DAG.getConstant(mask3, MVT::i32));
2750
Scott Michel77f452d2009-08-25 22:37:34 +00002751 // Word wise arithmetic right shift to generate at least one byte
2752 // that contains sign bits.
2753 MVT mvt = Op0VT == MVT::i64 ? MVT::v2i64 : MVT::v4i32;
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002754 SDValue sraVal = DAG.getNode(ISD::SRA,
2755 dl,
Scott Michel77f452d2009-08-25 22:37:34 +00002756 mvt,
2757 DAG.getNode(SPUISD::PREFSLOT2VEC, dl, mvt, Op0, Op0),
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002758 DAG.getConstant(31, MVT::i32));
2759
Kalle Raiskila940e7962010-10-18 09:34:19 +00002760 // reinterpret as a i128 (SHUFB requires it). This gets lowered away.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002761 SDValue extended = SDValue(DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Kalle Raiskila940e7962010-10-18 09:34:19 +00002762 dl, Op0VT, Op0,
2763 DAG.getTargetConstant(
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002764 SPU::GPRCRegClass.getID(),
Kalle Raiskila940e7962010-10-18 09:34:19 +00002765 MVT::i32)), 0);
Scott Michel77f452d2009-08-25 22:37:34 +00002766 // Shuffle bytes - Copy the sign bits into the upper 64 bits
2767 // and the input value into the lower 64 bits.
2768 SDValue extShuffle = DAG.getNode(SPUISD::SHUFB, dl, mvt,
Kalle Raiskila940e7962010-10-18 09:34:19 +00002769 extended, sraVal, shufMask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002770 return DAG.getNode(ISD::BITCAST, dl, MVT::i128, extShuffle);
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002771}
2772
Scott Michel7a1c9e92008-11-22 23:50:42 +00002773//! Custom (target-specific) lowering entry point
2774/*!
2775 This is where LLVM's DAG selection process calls to do target-specific
2776 lowering of nodes.
2777 */
Dan Gohman475871a2008-07-27 21:46:04 +00002778SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002779SPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const
Scott Michel266bc8f2007-12-04 22:23:35 +00002780{
Scott Michela59d4692008-02-23 18:41:37 +00002781 unsigned Opc = (unsigned) Op.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00002782 EVT VT = Op.getValueType();
Scott Michela59d4692008-02-23 18:41:37 +00002783
2784 switch (Opc) {
Scott Michel266bc8f2007-12-04 22:23:35 +00002785 default: {
Torok Edwindac237e2009-07-08 20:53:28 +00002786#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +00002787 errs() << "SPUTargetLowering::LowerOperation(): need to lower this!\n";
2788 errs() << "Op.getOpcode() = " << Opc << "\n";
2789 errs() << "*Op.getNode():\n";
Gabor Greifba36cb52008-08-28 21:40:38 +00002790 Op.getNode()->dump();
Torok Edwindac237e2009-07-08 20:53:28 +00002791#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00002792 llvm_unreachable(0);
Scott Michel266bc8f2007-12-04 22:23:35 +00002793 }
2794 case ISD::LOAD:
Scott Michelb30e8f62008-12-02 19:53:53 +00002795 case ISD::EXTLOAD:
Scott Michel266bc8f2007-12-04 22:23:35 +00002796 case ISD::SEXTLOAD:
2797 case ISD::ZEXTLOAD:
2798 return LowerLOAD(Op, DAG, SPUTM.getSubtargetImpl());
2799 case ISD::STORE:
2800 return LowerSTORE(Op, DAG, SPUTM.getSubtargetImpl());
2801 case ISD::ConstantPool:
2802 return LowerConstantPool(Op, DAG, SPUTM.getSubtargetImpl());
2803 case ISD::GlobalAddress:
2804 return LowerGlobalAddress(Op, DAG, SPUTM.getSubtargetImpl());
2805 case ISD::JumpTable:
2806 return LowerJumpTable(Op, DAG, SPUTM.getSubtargetImpl());
Scott Michel266bc8f2007-12-04 22:23:35 +00002807 case ISD::ConstantFP:
2808 return LowerConstantFP(Op, DAG);
Scott Michel266bc8f2007-12-04 22:23:35 +00002809
Scott Michel02d711b2008-12-30 23:28:25 +00002810 // i8, i64 math ops:
Scott Michel8bf61e82008-06-02 22:18:03 +00002811 case ISD::ADD:
Scott Michel266bc8f2007-12-04 22:23:35 +00002812 case ISD::SUB:
2813 case ISD::ROTR:
2814 case ISD::ROTL:
2815 case ISD::SRL:
2816 case ISD::SHL:
Scott Michel8bf61e82008-06-02 22:18:03 +00002817 case ISD::SRA: {
Owen Anderson825b72b2009-08-11 20:47:22 +00002818 if (VT == MVT::i8)
Scott Michelf0569be2008-12-27 04:51:36 +00002819 return LowerI8Math(Op, DAG, Opc, *this);
Scott Michela59d4692008-02-23 18:41:37 +00002820 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00002821 }
Scott Michel266bc8f2007-12-04 22:23:35 +00002822
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002823 case ISD::FP_TO_SINT:
2824 case ISD::FP_TO_UINT:
2825 return LowerFP_TO_INT(Op, DAG, *this);
2826
2827 case ISD::SINT_TO_FP:
2828 case ISD::UINT_TO_FP:
2829 return LowerINT_TO_FP(Op, DAG, *this);
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002830
Scott Michel266bc8f2007-12-04 22:23:35 +00002831 // Vector-related lowering.
2832 case ISD::BUILD_VECTOR:
Scott Michelc9c8b2a2009-01-26 03:31:40 +00002833 return LowerBUILD_VECTOR(Op, DAG);
Scott Michel266bc8f2007-12-04 22:23:35 +00002834 case ISD::SCALAR_TO_VECTOR:
2835 return LowerSCALAR_TO_VECTOR(Op, DAG);
2836 case ISD::VECTOR_SHUFFLE:
2837 return LowerVECTOR_SHUFFLE(Op, DAG);
2838 case ISD::EXTRACT_VECTOR_ELT:
2839 return LowerEXTRACT_VECTOR_ELT(Op, DAG);
2840 case ISD::INSERT_VECTOR_ELT:
2841 return LowerINSERT_VECTOR_ELT(Op, DAG);
2842
2843 // Look for ANDBI, ORBI and XORBI opportunities and lower appropriately:
2844 case ISD::AND:
2845 case ISD::OR:
2846 case ISD::XOR:
2847 return LowerByteImmed(Op, DAG);
2848
2849 // Vector and i8 multiply:
2850 case ISD::MUL:
Owen Anderson825b72b2009-08-11 20:47:22 +00002851 if (VT == MVT::i8)
Scott Michelf0569be2008-12-27 04:51:36 +00002852 return LowerI8Math(Op, DAG, Opc, *this);
Scott Michel266bc8f2007-12-04 22:23:35 +00002853
Scott Michel266bc8f2007-12-04 22:23:35 +00002854 case ISD::CTPOP:
2855 return LowerCTPOP(Op, DAG);
Scott Michel7a1c9e92008-11-22 23:50:42 +00002856
2857 case ISD::SELECT_CC:
Scott Michelf0569be2008-12-27 04:51:36 +00002858 return LowerSELECT_CC(Op, DAG, *this);
Scott Michelb30e8f62008-12-02 19:53:53 +00002859
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002860 case ISD::SETCC:
2861 return LowerSETCC(Op, DAG, *this);
2862
Scott Michelb30e8f62008-12-02 19:53:53 +00002863 case ISD::TRUNCATE:
2864 return LowerTRUNCATE(Op, DAG);
Scott Michelf1fa4fd2009-08-24 22:28:53 +00002865
2866 case ISD::SIGN_EXTEND:
2867 return LowerSIGN_EXTEND(Op, DAG);
Scott Michel266bc8f2007-12-04 22:23:35 +00002868 }
2869
Dan Gohman475871a2008-07-27 21:46:04 +00002870 return SDValue();
Scott Michel266bc8f2007-12-04 22:23:35 +00002871}
2872
Duncan Sands1607f052008-12-01 11:39:25 +00002873void SPUTargetLowering::ReplaceNodeResults(SDNode *N,
2874 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00002875 SelectionDAG &DAG) const
Scott Michel73ce1c52008-11-10 23:43:06 +00002876{
2877#if 0
2878 unsigned Opc = (unsigned) N->getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00002879 EVT OpVT = N->getValueType(0);
Scott Michel73ce1c52008-11-10 23:43:06 +00002880
2881 switch (Opc) {
2882 default: {
Chris Lattner4437ae22009-08-23 07:05:07 +00002883 errs() << "SPUTargetLowering::ReplaceNodeResults(): need to fix this!\n";
2884 errs() << "Op.getOpcode() = " << Opc << "\n";
2885 errs() << "*Op.getNode():\n";
Scott Michel73ce1c52008-11-10 23:43:06 +00002886 N->dump();
2887 abort();
2888 /*NOTREACHED*/
2889 }
2890 }
2891#endif
2892
2893 /* Otherwise, return unchanged */
Scott Michel73ce1c52008-11-10 23:43:06 +00002894}
2895
Scott Michel266bc8f2007-12-04 22:23:35 +00002896//===----------------------------------------------------------------------===//
Scott Michel266bc8f2007-12-04 22:23:35 +00002897// Target Optimization Hooks
2898//===----------------------------------------------------------------------===//
2899
Dan Gohman475871a2008-07-27 21:46:04 +00002900SDValue
Scott Michel266bc8f2007-12-04 22:23:35 +00002901SPUTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const
2902{
2903#if 0
2904 TargetMachine &TM = getTargetMachine();
Scott Michel053c1da2008-01-29 02:16:57 +00002905#endif
2906 const SPUSubtarget *ST = SPUTM.getSubtargetImpl();
Scott Michel266bc8f2007-12-04 22:23:35 +00002907 SelectionDAG &DAG = DCI.DAG;
Scott Michel1a6cdb62008-12-01 17:56:02 +00002908 SDValue Op0 = N->getOperand(0); // everything has at least one operand
Owen Andersone50ed302009-08-10 22:56:29 +00002909 EVT NodeVT = N->getValueType(0); // The node's value type
2910 EVT Op0VT = Op0.getValueType(); // The first operand's result
Scott Michel1a6cdb62008-12-01 17:56:02 +00002911 SDValue Result; // Initially, empty result
Dale Johannesende064702009-02-06 21:50:26 +00002912 DebugLoc dl = N->getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +00002913
2914 switch (N->getOpcode()) {
2915 default: break;
Scott Michel053c1da2008-01-29 02:16:57 +00002916 case ISD::ADD: {
Dan Gohman475871a2008-07-27 21:46:04 +00002917 SDValue Op1 = N->getOperand(1);
Scott Michel053c1da2008-01-29 02:16:57 +00002918
Scott Michelf0569be2008-12-27 04:51:36 +00002919 if (Op0.getOpcode() == SPUISD::IndirectAddr
2920 || Op1.getOpcode() == SPUISD::IndirectAddr) {
2921 // Normalize the operands to reduce repeated code
2922 SDValue IndirectArg = Op0, AddArg = Op1;
Scott Michel1df30c42008-12-29 03:23:36 +00002923
Scott Michelf0569be2008-12-27 04:51:36 +00002924 if (Op1.getOpcode() == SPUISD::IndirectAddr) {
2925 IndirectArg = Op1;
2926 AddArg = Op0;
2927 }
2928
2929 if (isa<ConstantSDNode>(AddArg)) {
2930 ConstantSDNode *CN0 = cast<ConstantSDNode > (AddArg);
2931 SDValue IndOp1 = IndirectArg.getOperand(1);
2932
2933 if (CN0->isNullValue()) {
2934 // (add (SPUindirect <arg>, <arg>), 0) ->
2935 // (SPUindirect <arg>, <arg>)
Scott Michel053c1da2008-01-29 02:16:57 +00002936
Scott Michel23f2ff72008-12-04 17:16:59 +00002937#if !defined(NDEBUG)
Scott Michelf0569be2008-12-27 04:51:36 +00002938 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Chris Lattner4437ae22009-08-23 07:05:07 +00002939 errs() << "\n"
Scott Michelf0569be2008-12-27 04:51:36 +00002940 << "Replace: (add (SPUindirect <arg>, <arg>), 0)\n"
2941 << "With: (SPUindirect <arg>, <arg>)\n";
2942 }
Scott Michel30ee7df2008-12-04 03:02:42 +00002943#endif
2944
Scott Michelf0569be2008-12-27 04:51:36 +00002945 return IndirectArg;
2946 } else if (isa<ConstantSDNode>(IndOp1)) {
2947 // (add (SPUindirect <arg>, <const>), <const>) ->
2948 // (SPUindirect <arg>, <const + const>)
2949 ConstantSDNode *CN1 = cast<ConstantSDNode > (IndOp1);
2950 int64_t combinedConst = CN0->getSExtValue() + CN1->getSExtValue();
2951 SDValue combinedValue = DAG.getConstant(combinedConst, Op0VT);
Scott Michel053c1da2008-01-29 02:16:57 +00002952
Scott Michelf0569be2008-12-27 04:51:36 +00002953#if !defined(NDEBUG)
2954 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Chris Lattner4437ae22009-08-23 07:05:07 +00002955 errs() << "\n"
Scott Michelf0569be2008-12-27 04:51:36 +00002956 << "Replace: (add (SPUindirect <arg>, " << CN1->getSExtValue()
2957 << "), " << CN0->getSExtValue() << ")\n"
2958 << "With: (SPUindirect <arg>, "
2959 << combinedConst << ")\n";
2960 }
2961#endif
Scott Michel053c1da2008-01-29 02:16:57 +00002962
Dale Johannesende064702009-02-06 21:50:26 +00002963 return DAG.getNode(SPUISD::IndirectAddr, dl, Op0VT,
Scott Michelf0569be2008-12-27 04:51:36 +00002964 IndirectArg, combinedValue);
2965 }
Scott Michel053c1da2008-01-29 02:16:57 +00002966 }
2967 }
Scott Michela59d4692008-02-23 18:41:37 +00002968 break;
2969 }
2970 case ISD::SIGN_EXTEND:
2971 case ISD::ZERO_EXTEND:
2972 case ISD::ANY_EXTEND: {
Scott Michel1a6cdb62008-12-01 17:56:02 +00002973 if (Op0.getOpcode() == SPUISD::VEC2PREFSLOT && NodeVT == Op0VT) {
Scott Michela59d4692008-02-23 18:41:37 +00002974 // (any_extend (SPUextract_elt0 <arg>)) ->
2975 // (SPUextract_elt0 <arg>)
2976 // Types must match, however...
Scott Michel23f2ff72008-12-04 17:16:59 +00002977#if !defined(NDEBUG)
2978 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Chris Lattner4437ae22009-08-23 07:05:07 +00002979 errs() << "\nReplace: ";
Scott Michel30ee7df2008-12-04 03:02:42 +00002980 N->dump(&DAG);
Chris Lattner4437ae22009-08-23 07:05:07 +00002981 errs() << "\nWith: ";
Scott Michel30ee7df2008-12-04 03:02:42 +00002982 Op0.getNode()->dump(&DAG);
Chris Lattner4437ae22009-08-23 07:05:07 +00002983 errs() << "\n";
Scott Michel23f2ff72008-12-04 17:16:59 +00002984 }
Scott Michel30ee7df2008-12-04 03:02:42 +00002985#endif
Scott Michela59d4692008-02-23 18:41:37 +00002986
2987 return Op0;
2988 }
2989 break;
2990 }
2991 case SPUISD::IndirectAddr: {
2992 if (!ST->usingLargeMem() && Op0.getOpcode() == SPUISD::AFormAddr) {
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002993 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1));
Dan Gohmane368b462010-06-18 14:22:04 +00002994 if (CN != 0 && CN->isNullValue()) {
Scott Michela59d4692008-02-23 18:41:37 +00002995 // (SPUindirect (SPUaform <addr>, 0), 0) ->
2996 // (SPUaform <addr>, 0)
2997
Chris Lattner4437ae22009-08-23 07:05:07 +00002998 DEBUG(errs() << "Replace: ");
Scott Michela59d4692008-02-23 18:41:37 +00002999 DEBUG(N->dump(&DAG));
Chris Lattner4437ae22009-08-23 07:05:07 +00003000 DEBUG(errs() << "\nWith: ");
Gabor Greifba36cb52008-08-28 21:40:38 +00003001 DEBUG(Op0.getNode()->dump(&DAG));
Chris Lattner4437ae22009-08-23 07:05:07 +00003002 DEBUG(errs() << "\n");
Scott Michela59d4692008-02-23 18:41:37 +00003003
3004 return Op0;
3005 }
Scott Michelf0569be2008-12-27 04:51:36 +00003006 } else if (Op0.getOpcode() == ISD::ADD) {
3007 SDValue Op1 = N->getOperand(1);
3008 if (ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(Op1)) {
3009 // (SPUindirect (add <arg>, <arg>), 0) ->
3010 // (SPUindirect <arg>, <arg>)
3011 if (CN1->isNullValue()) {
3012
3013#if !defined(NDEBUG)
3014 if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
Chris Lattner4437ae22009-08-23 07:05:07 +00003015 errs() << "\n"
Scott Michelf0569be2008-12-27 04:51:36 +00003016 << "Replace: (SPUindirect (add <arg>, <arg>), 0)\n"
3017 << "With: (SPUindirect <arg>, <arg>)\n";
3018 }
3019#endif
3020
Dale Johannesende064702009-02-06 21:50:26 +00003021 return DAG.getNode(SPUISD::IndirectAddr, dl, Op0VT,
Scott Michelf0569be2008-12-27 04:51:36 +00003022 Op0.getOperand(0), Op0.getOperand(1));
3023 }
3024 }
Scott Michela59d4692008-02-23 18:41:37 +00003025 }
3026 break;
3027 }
Kalle Raiskila7ea1ab52010-11-12 10:14:03 +00003028 case SPUISD::SHL_BITS:
3029 case SPUISD::SHL_BYTES:
Scott Michelf0569be2008-12-27 04:51:36 +00003030 case SPUISD::ROTBYTES_LEFT: {
Dan Gohman475871a2008-07-27 21:46:04 +00003031 SDValue Op1 = N->getOperand(1);
Scott Michela59d4692008-02-23 18:41:37 +00003032
Scott Michelf0569be2008-12-27 04:51:36 +00003033 // Kill degenerate vector shifts:
3034 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3035 if (CN->isNullValue()) {
Scott Michela59d4692008-02-23 18:41:37 +00003036 Result = Op0;
3037 }
3038 }
3039 break;
3040 }
Scott Michelf0569be2008-12-27 04:51:36 +00003041 case SPUISD::PREFSLOT2VEC: {
Scott Michela59d4692008-02-23 18:41:37 +00003042 switch (Op0.getOpcode()) {
3043 default:
3044 break;
3045 case ISD::ANY_EXTEND:
3046 case ISD::ZERO_EXTEND:
3047 case ISD::SIGN_EXTEND: {
Scott Michel1df30c42008-12-29 03:23:36 +00003048 // (SPUprefslot2vec (any|zero|sign_extend (SPUvec2prefslot <arg>))) ->
Scott Michela59d4692008-02-23 18:41:37 +00003049 // <arg>
Scott Michel1df30c42008-12-29 03:23:36 +00003050 // but only if the SPUprefslot2vec and <arg> types match.
Dan Gohman475871a2008-07-27 21:46:04 +00003051 SDValue Op00 = Op0.getOperand(0);
Scott Michel104de432008-11-24 17:11:17 +00003052 if (Op00.getOpcode() == SPUISD::VEC2PREFSLOT) {
Dan Gohman475871a2008-07-27 21:46:04 +00003053 SDValue Op000 = Op00.getOperand(0);
Scott Michel1a6cdb62008-12-01 17:56:02 +00003054 if (Op000.getValueType() == NodeVT) {
Scott Michela59d4692008-02-23 18:41:37 +00003055 Result = Op000;
3056 }
3057 }
3058 break;
3059 }
Scott Michel104de432008-11-24 17:11:17 +00003060 case SPUISD::VEC2PREFSLOT: {
Scott Michel1df30c42008-12-29 03:23:36 +00003061 // (SPUprefslot2vec (SPUvec2prefslot <arg>)) ->
Scott Michela59d4692008-02-23 18:41:37 +00003062 // <arg>
3063 Result = Op0.getOperand(0);
3064 break;
Scott Michel5af8f0e2008-07-16 17:17:29 +00003065 }
Scott Michela59d4692008-02-23 18:41:37 +00003066 }
3067 break;
Scott Michel053c1da2008-01-29 02:16:57 +00003068 }
3069 }
Scott Micheld1e8d9c2009-01-21 04:58:48 +00003070
Scott Michel58c58182008-01-17 20:38:41 +00003071 // Otherwise, return unchanged.
Scott Michel1a6cdb62008-12-01 17:56:02 +00003072#ifndef NDEBUG
Gabor Greifba36cb52008-08-28 21:40:38 +00003073 if (Result.getNode()) {
Chris Lattner4437ae22009-08-23 07:05:07 +00003074 DEBUG(errs() << "\nReplace.SPU: ");
Scott Michela59d4692008-02-23 18:41:37 +00003075 DEBUG(N->dump(&DAG));
Chris Lattner4437ae22009-08-23 07:05:07 +00003076 DEBUG(errs() << "\nWith: ");
Gabor Greifba36cb52008-08-28 21:40:38 +00003077 DEBUG(Result.getNode()->dump(&DAG));
Chris Lattner4437ae22009-08-23 07:05:07 +00003078 DEBUG(errs() << "\n");
Scott Michela59d4692008-02-23 18:41:37 +00003079 }
3080#endif
3081
3082 return Result;
Scott Michel266bc8f2007-12-04 22:23:35 +00003083}
3084
3085//===----------------------------------------------------------------------===//
3086// Inline Assembly Support
3087//===----------------------------------------------------------------------===//
3088
3089/// getConstraintType - Given a constraint letter, return the type of
3090/// constraint it is for this target.
Scott Michel5af8f0e2008-07-16 17:17:29 +00003091SPUTargetLowering::ConstraintType
Scott Michel266bc8f2007-12-04 22:23:35 +00003092SPUTargetLowering::getConstraintType(const std::string &ConstraintLetter) const {
3093 if (ConstraintLetter.size() == 1) {
3094 switch (ConstraintLetter[0]) {
3095 default: break;
3096 case 'b':
3097 case 'r':
3098 case 'f':
3099 case 'v':
3100 case 'y':
3101 return C_RegisterClass;
Scott Michel5af8f0e2008-07-16 17:17:29 +00003102 }
Scott Michel266bc8f2007-12-04 22:23:35 +00003103 }
3104 return TargetLowering::getConstraintType(ConstraintLetter);
3105}
3106
John Thompson44ab89e2010-10-29 17:29:13 +00003107/// Examine constraint type and operand type and determine a weight value.
3108/// This object must already have been set up with the operand type
3109/// and the current alternative constraint selected.
3110TargetLowering::ConstraintWeight
3111SPUTargetLowering::getSingleConstraintMatchWeight(
3112 AsmOperandInfo &info, const char *constraint) const {
3113 ConstraintWeight weight = CW_Invalid;
3114 Value *CallOperandVal = info.CallOperandVal;
3115 // If we don't have a value, we can't do a match,
3116 // but allow it at the lowest weight.
3117 if (CallOperandVal == NULL)
3118 return CW_Default;
3119 // Look at the constraint type.
3120 switch (*constraint) {
3121 default:
3122 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
Owen Anderson95771af2011-02-25 21:41:48 +00003123 break;
John Thompson44ab89e2010-10-29 17:29:13 +00003124 //FIXME: Seems like the supported constraint letters were just copied
3125 // from PPC, as the following doesn't correspond to the GCC docs.
3126 // I'm leaving it so until someone adds the corresponding lowering support.
3127 case 'b':
3128 case 'r':
3129 case 'f':
3130 case 'd':
3131 case 'v':
3132 case 'y':
3133 weight = CW_Register;
3134 break;
3135 }
3136 return weight;
3137}
3138
Scott Michel5af8f0e2008-07-16 17:17:29 +00003139std::pair<unsigned, const TargetRegisterClass*>
Scott Michel266bc8f2007-12-04 22:23:35 +00003140SPUTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00003141 EVT VT) const
Scott Michel266bc8f2007-12-04 22:23:35 +00003142{
3143 if (Constraint.size() == 1) {
3144 // GCC RS6000 Constraint Letters
3145 switch (Constraint[0]) {
3146 case 'b': // R1-R31
3147 case 'r': // R0-R31
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 if (VT == MVT::i64)
Craig Topper420761a2012-04-20 07:30:17 +00003149 return std::make_pair(0U, &SPU::R64CRegClass);
3150 return std::make_pair(0U, &SPU::R32CRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00003151 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00003153 return std::make_pair(0U, &SPU::R32FPRegClass);
3154 if (VT == MVT::f64)
3155 return std::make_pair(0U, &SPU::R64FPRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00003156 break;
Scott Michel5af8f0e2008-07-16 17:17:29 +00003157 case 'v':
Craig Topper420761a2012-04-20 07:30:17 +00003158 return std::make_pair(0U, &SPU::GPRCRegClass);
Scott Michel266bc8f2007-12-04 22:23:35 +00003159 }
3160 }
Scott Michel5af8f0e2008-07-16 17:17:29 +00003161
Scott Michel266bc8f2007-12-04 22:23:35 +00003162 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3163}
3164
Scott Michela59d4692008-02-23 18:41:37 +00003165//! Compute used/known bits for a SPU operand
Scott Michel266bc8f2007-12-04 22:23:35 +00003166void
Dan Gohman475871a2008-07-27 21:46:04 +00003167SPUTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michel5af8f0e2008-07-16 17:17:29 +00003168 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00003169 APInt &KnownOne,
Scott Michel7f9ba9b2008-01-30 02:55:46 +00003170 const SelectionDAG &DAG,
3171 unsigned Depth ) const {
Scott Michel203b2d62008-04-30 00:30:08 +00003172#if 0
Dan Gohmande551f92009-04-01 18:45:54 +00003173 const uint64_t uint64_sizebits = sizeof(uint64_t) * CHAR_BIT;
Scott Michela59d4692008-02-23 18:41:37 +00003174
3175 switch (Op.getOpcode()) {
3176 default:
3177 // KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
3178 break;
Scott Michela59d4692008-02-23 18:41:37 +00003179 case CALL:
3180 case SHUFB:
Scott Michel7a1c9e92008-11-22 23:50:42 +00003181 case SHUFFLE_MASK:
Scott Michela59d4692008-02-23 18:41:37 +00003182 case CNTB:
Scott Micheld1e8d9c2009-01-21 04:58:48 +00003183 case SPUISD::PREFSLOT2VEC:
Scott Michela59d4692008-02-23 18:41:37 +00003184 case SPUISD::LDRESULT:
Scott Micheld1e8d9c2009-01-21 04:58:48 +00003185 case SPUISD::VEC2PREFSLOT:
Scott Michel203b2d62008-04-30 00:30:08 +00003186 case SPUISD::SHLQUAD_L_BITS:
3187 case SPUISD::SHLQUAD_L_BYTES:
Scott Michel203b2d62008-04-30 00:30:08 +00003188 case SPUISD::VEC_ROTL:
3189 case SPUISD::VEC_ROTR:
Scott Michel203b2d62008-04-30 00:30:08 +00003190 case SPUISD::ROTBYTES_LEFT:
Scott Michel8bf61e82008-06-02 22:18:03 +00003191 case SPUISD::SELECT_MASK:
3192 case SPUISD::SELB:
Scott Michela59d4692008-02-23 18:41:37 +00003193 }
Scott Micheld1e8d9c2009-01-21 04:58:48 +00003194#endif
Scott Michel266bc8f2007-12-04 22:23:35 +00003195}
Scott Michel02d711b2008-12-30 23:28:25 +00003196
Scott Michelf0569be2008-12-27 04:51:36 +00003197unsigned
3198SPUTargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
3199 unsigned Depth) const {
3200 switch (Op.getOpcode()) {
3201 default:
3202 return 1;
Scott Michel266bc8f2007-12-04 22:23:35 +00003203
Scott Michelf0569be2008-12-27 04:51:36 +00003204 case ISD::SETCC: {
Owen Andersone50ed302009-08-10 22:56:29 +00003205 EVT VT = Op.getValueType();
Scott Michelf0569be2008-12-27 04:51:36 +00003206
Owen Anderson825b72b2009-08-11 20:47:22 +00003207 if (VT != MVT::i8 && VT != MVT::i16 && VT != MVT::i32) {
3208 VT = MVT::i32;
Scott Michelf0569be2008-12-27 04:51:36 +00003209 }
3210 return VT.getSizeInBits();
3211 }
3212 }
3213}
Scott Michel1df30c42008-12-29 03:23:36 +00003214
Scott Michel203b2d62008-04-30 00:30:08 +00003215// LowerAsmOperandForConstraint
3216void
Dan Gohman475871a2008-07-27 21:46:04 +00003217SPUTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00003218 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00003219 std::vector<SDValue> &Ops,
Scott Michel203b2d62008-04-30 00:30:08 +00003220 SelectionDAG &DAG) const {
3221 // Default, for the time being, to the base class handler
Eric Christopher100c8332011-06-02 23:16:42 +00003222 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Scott Michel203b2d62008-04-30 00:30:08 +00003223}
3224
Scott Michel266bc8f2007-12-04 22:23:35 +00003225/// isLegalAddressImmediate - Return true if the integer value can be used
3226/// as the offset of the target addressing mode.
Gabor Greif93c53e52008-08-31 15:37:04 +00003227bool SPUTargetLowering::isLegalAddressImmediate(int64_t V,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003228 Type *Ty) const {
Scott Michel266bc8f2007-12-04 22:23:35 +00003229 // SPU's addresses are 256K:
3230 return (V > -(1 << 18) && V < (1 << 18) - 1);
3231}
3232
Craig Topperc89c7442012-03-27 07:21:54 +00003233bool SPUTargetLowering::isLegalAddressImmediate(GlobalValue* GV) const {
Scott Michel5af8f0e2008-07-16 17:17:29 +00003234 return false;
Scott Michel266bc8f2007-12-04 22:23:35 +00003235}
Dan Gohman6520e202008-10-18 02:06:02 +00003236
3237bool
3238SPUTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3239 // The SPU target isn't yet aware of offsets.
3240 return false;
3241}
Kalle Raiskila8a52fa62010-10-07 16:24:35 +00003242
3243// can we compare to Imm without writing it into a register?
3244bool SPUTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3245 //ceqi, cgti, etc. all take s10 operand
3246 return isInt<10>(Imm);
3247}
3248
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003249bool
3250SPUTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003251 Type * ) const{
Kalle Raiskila8a52fa62010-10-07 16:24:35 +00003252
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003253 // A-form: 18bit absolute address.
Kalle Raiskila8a52fa62010-10-07 16:24:35 +00003254 if (AM.BaseGV && !AM.HasBaseReg && AM.Scale == 0 && AM.BaseOffs == 0)
3255 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003256
Kalle Raiskila8a52fa62010-10-07 16:24:35 +00003257 // D-form: reg + 14bit offset
3258 if (AM.BaseGV ==0 && AM.HasBaseReg && AM.Scale == 0 && isInt<14>(AM.BaseOffs))
3259 return true;
3260
3261 // X-form: reg+reg
3262 if (AM.BaseGV == 0 && AM.HasBaseReg && AM.Scale == 1 && AM.BaseOffs ==0)
3263 return true;
3264
3265 return false;
3266}