blob: a851be384fe4c17fcb19e6735bf3b4623dd96e42 [file] [log] [blame]
Chris Lattner4ee451d2007-12-29 20:36:04 +00001//===-- SPUISelDAGToDAG.cpp - CellSPU pattern matching inst selector ------===//
Scott Michel266bc8f2007-12-04 22:23:35 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Scott Michel266bc8f2007-12-04 22:23:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for the Cell SPU,
11// converting from a legalized dag to a SPU-target dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SPU.h"
16#include "SPUTargetMachine.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000017#include "SPUHazardRecognizers.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000018#include "SPUFrameLowering.h"
Scott Michel94bd57e2009-01-15 04:41:47 +000019#include "SPUTargetMachine.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000020#include "llvm/CodeGen/MachineConstantPool.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineFunction.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000023#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/Target/TargetOptions.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/Constants.h"
28#include "llvm/GlobalValue.h"
29#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000031#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000032#include "llvm/Support/ErrorHandling.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000033#include "llvm/Support/MathExtras.h"
34#include "llvm/Support/Compiler.h"
Torok Edwindac237e2009-07-08 20:53:28 +000035#include "llvm/Support/raw_ostream.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000036
37using namespace llvm;
38
39namespace {
40 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
41 bool
Scott Michel266bc8f2007-12-04 22:23:35 +000042 isI32IntS10Immediate(ConstantSDNode *CN)
43 {
Benjamin Kramer7e09deb2010-03-29 19:07:58 +000044 return isInt<10>(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000045 }
46
Scott Michel504c3692007-12-17 22:32:34 +000047 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
48 bool
49 isI32IntU10Immediate(ConstantSDNode *CN)
50 {
Benjamin Kramer34247a02010-03-29 21:13:41 +000051 return isUInt<10>(CN->getSExtValue());
Scott Michel504c3692007-12-17 22:32:34 +000052 }
53
Scott Michel266bc8f2007-12-04 22:23:35 +000054 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
55 bool
56 isI16IntS10Immediate(ConstantSDNode *CN)
57 {
Benjamin Kramer7e09deb2010-03-29 19:07:58 +000058 return isInt<10>(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000059 }
60
Scott Michelec2a08f2007-12-15 00:38:50 +000061 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
62 bool
63 isI16IntU10Immediate(ConstantSDNode *CN)
64 {
Benjamin Kramer34247a02010-03-29 21:13:41 +000065 return isUInt<10>((short) CN->getZExtValue());
Scott Michelec2a08f2007-12-15 00:38:50 +000066 }
67
Scott Michel266bc8f2007-12-04 22:23:35 +000068 //! ConstantSDNode predicate for signed 16-bit values
69 /*!
70 \arg CN The constant SelectionDAG node holding the value
71 \arg Imm The returned 16-bit value, if returning true
72
73 This predicate tests the value in \a CN to see whether it can be
74 represented as a 16-bit, sign-extended quantity. Returns true if
75 this is the case.
76 */
77 bool
78 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
79 {
Owen Andersone50ed302009-08-10 22:56:29 +000080 EVT vt = CN->getValueType(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000081 Imm = (short) CN->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000082 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +000083 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +000084 } else if (vt == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000085 int32_t i_val = (int32_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +000086 short s_val = (short) i_val;
87 return i_val == s_val;
88 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000089 int64_t i_val = (int64_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +000090 short s_val = (short) i_val;
91 return i_val == s_val;
92 }
93
94 return false;
95 }
96
Scott Michel266bc8f2007-12-04 22:23:35 +000097 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
98 static bool
99 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
100 {
Owen Andersone50ed302009-08-10 22:56:29 +0000101 EVT vt = FPN->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000102 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000103 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000104 int sval = (int) ((val << 16) >> 16);
105 Imm = (short) val;
106 return val == sval;
107 }
108
109 return false;
110 }
111
Scott Michel7ea02ff2009-03-17 01:15:45 +0000112 //! Generate the carry-generate shuffle mask.
113 SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
114 SmallVector<SDValue, 16 > ShufBytes;
Dan Gohman844731a2008-05-13 00:00:25 +0000115
Scott Michel7ea02ff2009-03-17 01:15:45 +0000116 // Create the shuffle mask for "rotating" the borrow up one register slot
117 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
119 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
120 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
121 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +0000122
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000124 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000125 }
Scott Michel02d711b2008-12-30 23:28:25 +0000126
Scott Michel7ea02ff2009-03-17 01:15:45 +0000127 //! Generate the borrow-generate shuffle mask
128 SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
129 SmallVector<SDValue, 16 > ShufBytes;
130
131 // Create the shuffle mask for "rotating" the borrow up one register slot
132 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
134 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
135 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
136 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000137
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000139 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000140 }
141
Scott Michel7ea02ff2009-03-17 01:15:45 +0000142 //===------------------------------------------------------------------===//
143 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
144 /// instructions for SelectionDAG operations.
145 ///
146 class SPUDAGToDAGISel :
147 public SelectionDAGISel
148 {
Dan Gohmand858e902010-04-17 15:26:15 +0000149 const SPUTargetMachine &TM;
150 const SPUTargetLowering &SPUtli;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000151 unsigned GlobalBaseReg;
Scott Michel02d711b2008-12-30 23:28:25 +0000152
Scott Michel7ea02ff2009-03-17 01:15:45 +0000153 public:
154 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
155 SelectionDAGISel(tm),
156 TM(tm),
157 SPUtli(*tm.getTargetLowering())
158 { }
159
Dan Gohmanad2afc22009-07-31 18:16:33 +0000160 virtual bool runOnMachineFunction(MachineFunction &MF) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000161 // Make sure we re-emit a set of the global base reg if necessary
162 GlobalBaseReg = 0;
Dan Gohmanad2afc22009-07-31 18:16:33 +0000163 SelectionDAGISel::runOnMachineFunction(MF);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000164 return true;
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000165 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000166
Scott Michel7ea02ff2009-03-17 01:15:45 +0000167 /// getI32Imm - Return a target constant with the specified value, of type
168 /// i32.
169 inline SDValue getI32Imm(uint32_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 return CurDAG->getTargetConstant(Imm, MVT::i32);
Scott Michel94bd57e2009-01-15 04:41:47 +0000171 }
172
Scott Michel7ea02ff2009-03-17 01:15:45 +0000173 /// getSmallIPtrImm - Return a target constant of pointer type.
174 inline SDValue getSmallIPtrImm(unsigned Imm) {
175 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Chris Lattner17aa6802010-09-04 18:12:00 +0000176 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000177
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000178 SDNode *emitBuildVector(SDNode *bvNode) {
179 EVT vecVT = bvNode->getValueType(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000180 DebugLoc dl = bvNode->getDebugLoc();
181
182 // Check to see if this vector can be represented as a CellSPU immediate
183 // constant by invoking all of the instruction selection predicates:
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 if (((vecVT == MVT::v8i16) &&
185 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
186 ((vecVT == MVT::v4i32) &&
187 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
188 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
189 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
Scott Michel7ea02ff2009-03-17 01:15:45 +0000190 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 ((vecVT == MVT::v2i64) &&
192 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
193 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
Chris Lattnera8e76142010-02-23 05:30:43 +0000194 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)))) {
195 HandleSDNode Dummy(SDValue(bvNode, 0));
196 if (SDNode *N = Select(bvNode))
197 return N;
198 return Dummy.getValue().getNode();
199 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000200
201 // No, need to emit a constant pool spill:
202 std::vector<Constant*> CV;
203
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000204 for (size_t i = 0; i < bvNode->getNumOperands(); ++i) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000205 ConstantSDNode *V = cast<ConstantSDNode > (bvNode->getOperand(i));
Chris Lattnera8e76142010-02-23 05:30:43 +0000206 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000207 }
208
Dan Gohman46510a72010-04-15 01:51:59 +0000209 const Constant *CP = ConstantVector::get(CV);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000210 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
211 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
212 SDValue CGPoolOffset =
Dan Gohmand858e902010-04-17 15:26:15 +0000213 SPU::LowerConstantPool(CPIdx, *CurDAG, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000214
Chris Lattnera8e76142010-02-23 05:30:43 +0000215 HandleSDNode Dummy(CurDAG->getLoad(vecVT, dl,
216 CurDAG->getEntryNode(), CGPoolOffset,
Chris Lattnere8639032010-09-21 06:22:23 +0000217 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000218 false, false, false, Alignment));
Chris Lattnera8e76142010-02-23 05:30:43 +0000219 CurDAG->ReplaceAllUsesWith(SDValue(bvNode, 0), Dummy.getValue());
220 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
221 return N;
222 return Dummy.getValue().getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000223 }
Scott Michel02d711b2008-12-30 23:28:25 +0000224
Scott Michel7ea02ff2009-03-17 01:15:45 +0000225 /// Select - Convert the specified operand from a target-independent to a
226 /// target-specific node if it hasn't already been changed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000227 SDNode *Select(SDNode *N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000228
Scott Michel7ea02ff2009-03-17 01:15:45 +0000229 //! Emit the instruction sequence for i64 shl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000230 SDNode *SelectSHLi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000231
Scott Michel7ea02ff2009-03-17 01:15:45 +0000232 //! Emit the instruction sequence for i64 srl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000233 SDNode *SelectSRLi64(SDNode *N, EVT OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000234
Scott Michel7ea02ff2009-03-17 01:15:45 +0000235 //! Emit the instruction sequence for i64 sra
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000236 SDNode *SelectSRAi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000237
Scott Michel7ea02ff2009-03-17 01:15:45 +0000238 //! Emit the necessary sequence for loading i64 constants:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000239 SDNode *SelectI64Constant(SDNode *N, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000240
241 //! Alternate instruction emit sequence for loading i64 constants
Owen Andersone50ed302009-08-10 22:56:29 +0000242 SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000243
244 //! Returns true if the address N is an A-form (local store) address
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000245 bool SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000246 SDValue &Index);
247
248 //! D-form address predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000249 bool SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000250 SDValue &Index);
251
252 /// Alternate D-form address using i7 offset predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000253 bool SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000254 SDValue &Base);
255
256 /// D-form address selection workhorse
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000257 bool DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000258 SDValue &Base, int minOffset, int maxOffset);
259
260 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000261 bool SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000262 SDValue &Index);
263
264 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
265 /// inline asm expressions.
266 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
267 char ConstraintCode,
268 std::vector<SDValue> &OutOps) {
269 SDValue Op0, Op1;
270 switch (ConstraintCode) {
271 default: return true;
272 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000273 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
274 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1))
275 SelectXFormAddr(Op.getNode(), Op, Op0, Op1);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000276 break;
277 case 'o': // offsetable
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000278 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
279 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1)) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000280 Op0 = Op;
281 Op1 = getSmallIPtrImm(0);
282 }
283 break;
284 case 'v': // not offsetable
285#if 1
Torok Edwinc23197a2009-07-14 16:55:14 +0000286 llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
Scott Michel7ea02ff2009-03-17 01:15:45 +0000287#else
288 SelectAddrIdxOnly(Op, Op, Op0, Op1);
289#endif
290 break;
291 }
292
293 OutOps.push_back(Op0);
294 OutOps.push_back(Op1);
295 return false;
296 }
297
Scott Michel7ea02ff2009-03-17 01:15:45 +0000298 virtual const char *getPassName() const {
299 return "Cell SPU DAG->DAG Pattern Instruction Selection";
300 }
301
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000302 private:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000303 SDValue getRC( MVT );
Scott Michel7ea02ff2009-03-17 01:15:45 +0000304
305 // Include the pieces autogenerated from the target description.
Scott Michel266bc8f2007-12-04 22:23:35 +0000306#include "SPUGenDAGISel.inc"
Scott Michel7ea02ff2009-03-17 01:15:45 +0000307 };
Dan Gohman844731a2008-05-13 00:00:25 +0000308}
309
Scott Michel266bc8f2007-12-04 22:23:35 +0000310/*!
Scott Michel9de57a92009-01-26 22:33:37 +0000311 \arg Op The ISD instruction operand
Scott Michel266bc8f2007-12-04 22:23:35 +0000312 \arg N The address to be tested
313 \arg Base The base address
314 \arg Index The base address index
315 */
316bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000317SPUDAGToDAGISel::SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000318 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000319 // These match the addr256k operand type:
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 EVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000321 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000322 int64_t val;
Scott Michel266bc8f2007-12-04 22:23:35 +0000323
324 switch (N.getOpcode()) {
325 case ISD::Constant:
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000326 val = dyn_cast<ConstantSDNode>(N.getNode())->getSExtValue();
327 Base = CurDAG->getTargetConstant( val , MVT::i32);
328 Index = Zero;
329 return true; break;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000330 case ISD::ConstantPool:
331 case ISD::GlobalAddress:
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000332 report_fatal_error("SPU SelectAFormAddr: Pool/Global not lowered.");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000333 /*NOTREACHED*/
334
Scott Michel053c1da2008-01-29 02:16:57 +0000335 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000336 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000337 case ISD::TargetJumpTable:
Chris Lattner75361b62010-04-07 22:58:41 +0000338 report_fatal_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
Torok Edwindac237e2009-07-08 20:53:28 +0000339 "not wrapped as A-form address.");
Scott Michel053c1da2008-01-29 02:16:57 +0000340 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000341
Scott Michel02d711b2008-12-30 23:28:25 +0000342 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000343 // Just load from memory if there's only a single use of the location,
344 // otherwise, this will get handled below with D-form offset addresses
345 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000346 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000347 switch (Op0.getOpcode()) {
348 case ISD::TargetConstantPool:
349 case ISD::TargetJumpTable:
350 Base = Op0;
351 Index = Zero;
352 return true;
353
354 case ISD::TargetGlobalAddress: {
355 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
Dan Gohman46510a72010-04-15 01:51:59 +0000356 const GlobalValue *GV = GSDN->getGlobal();
Scott Michel053c1da2008-01-29 02:16:57 +0000357 if (GV->getAlignment() == 16) {
358 Base = Op0;
359 Index = Zero;
360 return true;
361 }
362 break;
363 }
364 }
365 }
366 break;
367 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000368 return false;
369}
370
Scott Michel02d711b2008-12-30 23:28:25 +0000371bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000372SPUDAGToDAGISel::SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Dan Gohman475871a2008-07-27 21:46:04 +0000373 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000374 const int minDForm2Offset = -(1 << 7);
375 const int maxDForm2Offset = (1 << 7) - 1;
376 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
377 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000378}
379
Scott Michel266bc8f2007-12-04 22:23:35 +0000380/*!
381 \arg Op The ISD instruction (ignored)
382 \arg N The address to be tested
383 \arg Base Base address register/pointer
384 \arg Index Base address index
385
386 Examine the input address by a base register plus a signed 10-bit
387 displacement, [r+I10] (D-form address).
388
389 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000390 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000391*/
392bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000393SPUDAGToDAGISel::SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000394 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000395 return DFormAddressPredicate(Op, N, Base, Index,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000396 SPUFrameLowering::minFrameOffset(),
397 SPUFrameLowering::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000398}
399
400bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000401SPUDAGToDAGISel::DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000402 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000403 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000404 unsigned Opc = N.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +0000405 EVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000406
Scott Michel053c1da2008-01-29 02:16:57 +0000407 if (Opc == ISD::FrameIndex) {
408 // Stack frame index must be less than 512 (divided by 16):
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000409 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(N);
Scott Michel203b2d62008-04-30 00:30:08 +0000410 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000411 DEBUG(errs() << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000412 << FI << "\n");
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000413 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000414 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000415 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000416 return true;
417 }
418 } else if (Opc == ISD::ADD) {
419 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000420 const SDValue Op0 = N.getOperand(0);
421 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000422
Scott Michel053c1da2008-01-29 02:16:57 +0000423 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
424 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
425 Base = CurDAG->getTargetConstant(0, PtrTy);
426 Index = N;
427 return true;
428 } else if (Op1.getOpcode() == ISD::Constant
429 || Op1.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000430 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000431 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000432
Scott Michel053c1da2008-01-29 02:16:57 +0000433 if (Op0.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000434 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op0);
Scott Michel203b2d62008-04-30 00:30:08 +0000435 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000436 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000437 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000438
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000439 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000440 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000441 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000442 return true;
443 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000444 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000445 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000446 Index = Op0;
447 return true;
448 }
449 } else if (Op0.getOpcode() == ISD::Constant
450 || Op0.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000451 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000452 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000453
454 if (Op1.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000455 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op1);
Scott Michel203b2d62008-04-30 00:30:08 +0000456 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000457 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000458 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000459
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000460 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000461 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000462 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000463 return true;
464 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000465 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000466 Base = CurDAG->getTargetConstant(offset, PtrTy);
467 Index = Op1;
468 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000469 }
Scott Michel053c1da2008-01-29 02:16:57 +0000470 }
471 } else if (Opc == SPUISD::IndirectAddr) {
472 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000473 const SDValue Op0 = N.getOperand(0);
474 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000475
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000476 if (Op0.getOpcode() == SPUISD::Hi
477 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000478 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000479 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000480 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000481 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000482 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
483 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000484 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000485
486 if (isa<ConstantSDNode>(Op1)) {
487 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000488 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000489 idxOp = Op0;
490 } else if (isa<ConstantSDNode>(Op0)) {
491 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000492 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000493 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000494 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000495
496 if (offset >= minOffset && offset <= maxOffset) {
497 Base = CurDAG->getTargetConstant(offset, PtrTy);
498 Index = idxOp;
499 return true;
500 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000501 }
Scott Michel053c1da2008-01-29 02:16:57 +0000502 } else if (Opc == SPUISD::AFormAddr) {
503 Base = CurDAG->getTargetConstant(0, N.getValueType());
504 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000505 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000506 } else if (Opc == SPUISD::LDRESULT) {
507 Base = CurDAG->getTargetConstant(0, N.getValueType());
508 Index = N;
509 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000510 } else if (Opc == ISD::Register
511 ||Opc == ISD::CopyFromReg
Kalle Raiskilabc2697c2010-08-04 13:59:48 +0000512 ||Opc == ISD::UNDEF
513 ||Opc == ISD::Constant) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000514 unsigned OpOpc = Op->getOpcode();
Scott Michel9c0c6b22008-11-21 02:56:16 +0000515
516 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
517 // Direct load/store without getelementptr
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000518 SDValue Offs;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000519
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000520 Offs = ((OpOpc == ISD::STORE) ? Op->getOperand(3) : Op->getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000521
522 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
523 if (Offs.getOpcode() == ISD::UNDEF)
524 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
525
526 Base = Offs;
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000527 Index = N;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000528 return true;
529 }
Scott Michelaedc6372008-12-10 00:15:19 +0000530 } else {
531 /* If otherwise unadorned, default to D-form address with 0 offset: */
532 if (Opc == ISD::CopyFromReg) {
Scott Michel19c10e62009-01-26 03:37:41 +0000533 Index = N.getOperand(1);
Scott Michelaedc6372008-12-10 00:15:19 +0000534 } else {
Scott Michel19c10e62009-01-26 03:37:41 +0000535 Index = N;
Scott Michelaedc6372008-12-10 00:15:19 +0000536 }
537
538 Base = CurDAG->getTargetConstant(0, Index.getValueType());
539 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000540 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000541 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000542
Scott Michel266bc8f2007-12-04 22:23:35 +0000543 return false;
544}
545
546/*!
547 \arg Op The ISD instruction operand
548 \arg N The address operand
549 \arg Base The base pointer operand
550 \arg Index The offset/index operand
551
Scott Michel9c0c6b22008-11-21 02:56:16 +0000552 If the address \a N can be expressed as an A-form or D-form address, returns
553 false. Otherwise, creates two operands, Base and Index that will become the
554 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000555*/
556bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000557SPUDAGToDAGISel::SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000558 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000559 if (!SelectAFormAddr(Op, N, Base, Index)
560 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000561 // If the address is neither A-form or D-form, punt and use an X-form
562 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000563 Base = N.getOperand(1);
564 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000565 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000566 }
567
568 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000569}
570
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000571/*!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000572 Utility function to use with COPY_TO_REGCLASS instructions. Returns a SDValue
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000573 to be used as the last parameter of a
574CurDAG->getMachineNode(COPY_TO_REGCLASS,..., ) function call
575 \arg VT the value type for which we want a register class
576*/
577SDValue SPUDAGToDAGISel::getRC( MVT VT ) {
578 switch( VT.SimpleTy ) {
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000579 case MVT::i8:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000580 return CurDAG->getTargetConstant(SPU::R8CRegClass.getID(), MVT::i32);
581 break;
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000582 case MVT::i16:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000583 return CurDAG->getTargetConstant(SPU::R16CRegClass.getID(), MVT::i32);
584 break;
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000585 case MVT::i32:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000586 return CurDAG->getTargetConstant(SPU::R32CRegClass.getID(), MVT::i32);
587 break;
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000588 case MVT::f32:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000589 return CurDAG->getTargetConstant(SPU::R32FPRegClass.getID(), MVT::i32);
590 break;
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000591 case MVT::i64:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000592 return CurDAG->getTargetConstant(SPU::R64CRegClass.getID(), MVT::i32);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000593 break;
Kalle Raiskila11edd0c2010-11-29 09:36:26 +0000594 case MVT::i128:
595 return CurDAG->getTargetConstant(SPU::GPRCRegClass.getID(), MVT::i32);
596 break;
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000597 case MVT::v16i8:
598 case MVT::v8i16:
599 case MVT::v4i32:
600 case MVT::v4f32:
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000601 case MVT::v2i64:
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000602 case MVT::v2f64:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000603 return CurDAG->getTargetConstant(SPU::VECREGRegClass.getID(), MVT::i32);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000604 break;
605 default:
606 assert( false && "add a new case here" );
607 }
608 return SDValue();
609}
610
Scott Michel266bc8f2007-12-04 22:23:35 +0000611//! Convert the operand from a target-independent to a target-specific node
612/*!
613 */
614SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000615SPUDAGToDAGISel::Select(SDNode *N) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000616 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000617 int n_ops = -1;
Ted Kremenek584520e2011-01-23 17:05:06 +0000618 unsigned NewOpc = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000619 EVT OpVT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000620 SDValue Ops[8];
Dale Johannesened2eee62009-02-06 01:31:28 +0000621 DebugLoc dl = N->getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +0000622
Chris Lattnera8e76142010-02-23 05:30:43 +0000623 if (N->isMachineOpcode())
Scott Michel266bc8f2007-12-04 22:23:35 +0000624 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000625
626 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000627 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000628 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
629 SDValue Imm0 = CurDAG->getTargetConstant(0, N->getValueType(0));
Scott Michel266bc8f2007-12-04 22:23:35 +0000630
Scott Michel02d711b2008-12-30 23:28:25 +0000631 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000632 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000633 Ops[0] = TFI;
634 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000635 n_ops = 2;
636 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000637 NewOpc = SPU::Ar32;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000638 Ops[0] = CurDAG->getRegister(SPU::R1, N->getValueType(0));
Dan Gohman602b0c82009-09-25 18:54:59 +0000639 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILAr32, dl,
Kalle Raiskila7d170972010-12-09 16:17:31 +0000640 N->getValueType(0), TFI),
Dan Gohman602b0c82009-09-25 18:54:59 +0000641 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000642 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000643 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000645 // Catch the i64 constants that end up here. Note: The backend doesn't
646 // attempt to legalize the constant (it's useless because DAGCombiner
647 // will insert 64-bit constants and we can't stop it).
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000648 return SelectI64Constant(N, OpVT, N->getDebugLoc());
Scott Michel94bd57e2009-01-15 04:41:47 +0000649 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 && OpVT == MVT::i64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000651 SDValue Op0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000652 EVT Op0VT = Op0.getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +0000653 EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
654 Op0VT, (128 / Op0VT.getSizeInBits()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000655 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +0000656 OpVT, (128 / OpVT.getSizeInBits()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000657 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000658
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 switch (Op0VT.getSimpleVT().SimpleTy) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000660 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000661 report_fatal_error("CellSPU Select: Unhandled zero/any extend EVT");
Scott Michel94bd57e2009-01-15 04:41:47 +0000662 /*NOTREACHED*/
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 case MVT::i32:
664 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
665 CurDAG->getConstant(0x80808080, MVT::i32),
666 CurDAG->getConstant(0x00010203, MVT::i32),
667 CurDAG->getConstant(0x80808080, MVT::i32),
668 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000669 break;
670
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 case MVT::i16:
672 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
673 CurDAG->getConstant(0x80808080, MVT::i32),
674 CurDAG->getConstant(0x80800203, MVT::i32),
675 CurDAG->getConstant(0x80808080, MVT::i32),
676 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000677 break;
678
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 case MVT::i8:
680 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
681 CurDAG->getConstant(0x80808080, MVT::i32),
682 CurDAG->getConstant(0x80808003, MVT::i32),
683 CurDAG->getConstant(0x80808080, MVT::i32),
684 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000685 break;
Scott Michel58c58182008-01-17 20:38:41 +0000686 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000687
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000688 SDNode *shufMaskLoad = emitBuildVector(shufMask.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000689
Chris Lattnera8e76142010-02-23 05:30:43 +0000690 HandleSDNode PromoteScalar(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl,
691 Op0VecVT, Op0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000692
Chris Lattnera8e76142010-02-23 05:30:43 +0000693 SDValue PromScalar;
694 if (SDNode *N = SelectCode(PromoteScalar.getValue().getNode()))
695 PromScalar = SDValue(N, 0);
696 else
697 PromScalar = PromoteScalar.getValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000698
Scott Michel94bd57e2009-01-15 04:41:47 +0000699 SDValue zextShuffle =
Dale Johannesened2eee62009-02-06 01:31:28 +0000700 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000701 PromScalar, PromScalar,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000702 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000703
Chris Lattnera8e76142010-02-23 05:30:43 +0000704 HandleSDNode Dummy2(zextShuffle);
705 if (SDNode *N = SelectCode(Dummy2.getValue().getNode()))
706 zextShuffle = SDValue(N, 0);
707 else
708 zextShuffle = Dummy2.getValue();
709 HandleSDNode Dummy(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
710 zextShuffle));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000711
Chris Lattnera8e76142010-02-23 05:30:43 +0000712 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
713 SelectCode(Dummy.getValue().getNode());
714 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000716 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000717 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000718
Chris Lattnera8e76142010-02-23 05:30:43 +0000719 HandleSDNode Dummy(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
720 N->getOperand(0), N->getOperand(1),
721 SDValue(CGLoad, 0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000722
Chris Lattnera8e76142010-02-23 05:30:43 +0000723 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
724 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
725 return N;
726 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000728 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000729 emitBuildVector(getBorrowGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000730
Chris Lattnera8e76142010-02-23 05:30:43 +0000731 HandleSDNode Dummy(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
732 N->getOperand(0), N->getOperand(1),
733 SDValue(CGLoad, 0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000734
Chris Lattnera8e76142010-02-23 05:30:43 +0000735 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
736 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
737 return N;
738 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000740 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000741 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000742
Chris Lattnera8e76142010-02-23 05:30:43 +0000743 HandleSDNode Dummy(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
744 N->getOperand(0), N->getOperand(1),
745 SDValue(CGLoad, 0)));
746 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
747 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
748 return N;
749 return Dummy.getValue().getNode();
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000750 } else if (Opc == ISD::TRUNCATE) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000751 SDValue Op0 = N->getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000752 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 && OpVT == MVT::i32
754 && Op0.getValueType() == MVT::i64) {
Scott Michel9de57a92009-01-26 22:33:37 +0000755 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
756 //
757 // Take advantage of the fact that the upper 32 bits are in the
758 // i32 preferred slot and avoid shuffle gymnastics:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000759 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
760 if (CN != 0) {
761 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000762
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000763 if (shift_amt >= 32) {
764 SDNode *hi32 =
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000765 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
766 Op0.getOperand(0), getRC(MVT::i32));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000767
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000768 shift_amt -= 32;
769 if (shift_amt > 0) {
770 // Take care of the additional shift, if present:
Owen Anderson825b72b2009-08-11 20:47:22 +0000771 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000772 unsigned Opc = SPU::ROTMAIr32_i32;
Scott Michel9de57a92009-01-26 22:33:37 +0000773
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000774 if (Op0.getOpcode() == ISD::SRL)
775 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000776
Dan Gohman602b0c82009-09-25 18:54:59 +0000777 hi32 = CurDAG->getMachineNode(Opc, dl, OpVT, SDValue(hi32, 0),
778 shift);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000779 }
780
781 return hi32;
782 }
783 }
784 }
Scott Michel02d711b2008-12-30 23:28:25 +0000785 } else if (Opc == ISD::SHL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000786 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000787 return SelectSHLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000788 } else if (Opc == ISD::SRL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000789 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000790 return SelectSRLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000791 } else if (Opc == ISD::SRA) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000792 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000793 return SelectSRAi64(N, OpVT);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000794 } else if (Opc == ISD::FNEG
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000796 DebugLoc dl = N->getDebugLoc();
Scott Michel7ea02ff2009-03-17 01:15:45 +0000797 // Check if the pattern is a special form of DFNMS:
798 // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000799 SDValue Op0 = N->getOperand(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000800 if (Op0.getOpcode() == ISD::FSUB) {
801 SDValue Op00 = Op0.getOperand(0);
802 if (Op00.getOpcode() == ISD::FMUL) {
803 unsigned Opc = SPU::DFNMSf64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 if (OpVT == MVT::v2f64)
Scott Michel7ea02ff2009-03-17 01:15:45 +0000805 Opc = SPU::DFNMSv2f64;
806
Dan Gohman602b0c82009-09-25 18:54:59 +0000807 return CurDAG->getMachineNode(Opc, dl, OpVT,
808 Op00.getOperand(0),
809 Op00.getOperand(1),
810 Op0.getOperand(1));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000811 }
812 }
813
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000815 SDNode *signMask = 0;
Scott Michela82d3f72009-03-17 16:45:16 +0000816 unsigned Opc = SPU::XORfneg64;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000817
Owen Anderson825b72b2009-08-11 20:47:22 +0000818 if (OpVT == MVT::f64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000819 signMask = SelectI64Constant(negConst.getNode(), MVT::i64, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 } else if (OpVT == MVT::v2f64) {
Scott Michela82d3f72009-03-17 16:45:16 +0000821 Opc = SPU::XORfnegvec;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000822 signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000823 MVT::v2i64,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000824 negConst, negConst).getNode());
Scott Michel7ea02ff2009-03-17 01:15:45 +0000825 }
826
Dan Gohman602b0c82009-09-25 18:54:59 +0000827 return CurDAG->getMachineNode(Opc, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000828 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000829 } else if (Opc == ISD::FABS) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 if (OpVT == MVT::f64) {
831 SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
Dan Gohman602b0c82009-09-25 18:54:59 +0000832 return CurDAG->getMachineNode(SPU::ANDfabs64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000833 N->getOperand(0), SDValue(signMask, 0));
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 } else if (OpVT == MVT::v2f64) {
835 SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
836 SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000837 absConst, absConst);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000838 SDNode *signMask = emitBuildVector(absVec.getNode());
Dan Gohman602b0c82009-09-25 18:54:59 +0000839 return CurDAG->getMachineNode(SPU::ANDfabsvec, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000840 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000841 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000842 } else if (Opc == SPUISD::LDRESULT) {
843 // Custom select instructions for LDRESULT
Owen Andersone50ed302009-08-10 22:56:29 +0000844 EVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000845 SDValue Arg = N->getOperand(0);
846 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000847 SDNode *Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000848
Kalle Raiskila82581352010-10-01 09:20:01 +0000849 Result = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VT,
850 MVT::Other, Arg,
851 getRC( VT.getSimpleVT()), Chain);
Scott Michel266bc8f2007-12-04 22:23:35 +0000852 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000853
Scott Michel053c1da2008-01-29 02:16:57 +0000854 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000855 // Look at the operands: SelectCode() will catch the cases that aren't
856 // specifically handled here.
857 //
858 // SPUInstrInfo catches the following patterns:
859 // (SPUindirect (SPUhi ...), (SPUlo ...))
860 // (SPUindirect $sp, imm)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000861 EVT VT = N->getValueType(0);
Scott Michelf0569be2008-12-27 04:51:36 +0000862 SDValue Op0 = N->getOperand(0);
863 SDValue Op1 = N->getOperand(1);
864 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000865
Scott Michelf0569be2008-12-27 04:51:36 +0000866 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
867 || (Op0.getOpcode() == ISD::Register
868 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
869 && RN->getReg() != SPU::R1))) {
870 NewOpc = SPU::Ar32;
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000871 Ops[1] = Op1;
Scott Michel58c58182008-01-17 20:38:41 +0000872 if (Op1.getOpcode() == ISD::Constant) {
873 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000874 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000875 if (isInt<10>(CN->getSExtValue())) {
876 NewOpc = SPU::AIr32;
877 Ops[1] = Op1;
878 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000879 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILr32, dl,
880 N->getValueType(0),
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000881 Op1),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000882 0);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000883 }
Scott Michel58c58182008-01-17 20:38:41 +0000884 }
Scott Michelf0569be2008-12-27 04:51:36 +0000885 Ops[0] = Op0;
Scott Michelf0569be2008-12-27 04:51:36 +0000886 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000887 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000888 }
Scott Michel02d711b2008-12-30 23:28:25 +0000889
Scott Michel58c58182008-01-17 20:38:41 +0000890 if (n_ops > 0) {
891 if (N->hasOneUse())
892 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
893 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000894 return CurDAG->getMachineNode(NewOpc, dl, OpVT, Ops, n_ops);
Scott Michel58c58182008-01-17 20:38:41 +0000895 } else
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000896 return SelectCode(N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000897}
898
Scott Michel02d711b2008-12-30 23:28:25 +0000899/*!
900 * Emit the instruction sequence for i64 left shifts. The basic algorithm
901 * is to fill the bottom two word slots with zeros so that zeros are shifted
902 * in as the entire quadword is shifted left.
903 *
904 * \note This code could also be used to implement v2i64 shl.
905 *
906 * @param Op The shl operand
907 * @param OpVT Op's machine value value type (doesn't need to be passed, but
908 * makes life easier.)
909 * @return The SDNode with the entire instruction sequence
910 */
911SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000912SPUDAGToDAGISel::SelectSHLi64(SDNode *N, EVT OpVT) {
913 SDValue Op0 = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000914 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +0000915 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000916 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000917 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000918 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
919 SDValue SelMaskVal;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000920 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000921
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000922 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
923 Op0, getRC(MVT::v2i64) );
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
Dan Gohman602b0c82009-09-25 18:54:59 +0000925 SelMask = CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
926 ZeroFill = CurDAG->getMachineNode(SPU::ILv2i64, dl, VecVT,
927 CurDAG->getTargetConstant(0, OpVT));
928 VecOp0 = CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
929 SDValue(ZeroFill, 0),
930 SDValue(VecOp0, 0),
931 SDValue(SelMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000932
933 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
934 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
935 unsigned bits = unsigned(CN->getZExtValue()) & 7;
936
937 if (bytes > 0) {
938 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000939 CurDAG->getMachineNode(SPU::SHLQBYIv2i64, dl, VecVT,
940 SDValue(VecOp0, 0),
941 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000942 }
943
944 if (bits > 0) {
945 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000946 CurDAG->getMachineNode(SPU::SHLQBIIv2i64, dl, VecVT,
947 SDValue((Shift != 0 ? Shift : VecOp0), 0),
948 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000949 }
950 } else {
951 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +0000952 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
953 ShiftAmt,
954 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000955 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +0000956 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
957 ShiftAmt,
958 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000959 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000960 CurDAG->getMachineNode(SPU::SHLQBYv2i64, dl, VecVT,
961 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000962 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000963 CurDAG->getMachineNode(SPU::SHLQBIv2i64, dl, VecVT,
964 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000965 }
966
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000967 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000968 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +0000969}
970
971/*!
972 * Emit the instruction sequence for i64 logical right shifts.
973 *
974 * @param Op The shl operand
975 * @param OpVT Op's machine value value type (doesn't need to be passed, but
976 * makes life easier.)
977 * @return The SDNode with the entire instruction sequence
978 */
979SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000980SPUDAGToDAGISel::SelectSRLi64(SDNode *N, EVT OpVT) {
981 SDValue Op0 = N->getOperand(0);
Owen Anderson23b9b192009-08-12 00:36:31 +0000982 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
983 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000984 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000985 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000986 SDNode *VecOp0, *Shift = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000987 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000988
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000989 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
990 Op0, getRC(MVT::v2i64) );
Scott Michel02d711b2008-12-30 23:28:25 +0000991
992 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
993 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
994 unsigned bits = unsigned(CN->getZExtValue()) & 7;
995
996 if (bytes > 0) {
997 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000998 CurDAG->getMachineNode(SPU::ROTQMBYIv2i64, dl, VecVT,
999 SDValue(VecOp0, 0),
1000 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001001 }
1002
1003 if (bits > 0) {
1004 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001005 CurDAG->getMachineNode(SPU::ROTQMBIIv2i64, dl, VecVT,
1006 SDValue((Shift != 0 ? Shift : VecOp0), 0),
1007 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001008 }
1009 } else {
1010 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +00001011 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1012 ShiftAmt,
1013 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001014 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +00001015 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1016 ShiftAmt,
1017 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001018
1019 // Ensure that the shift amounts are negated!
Dan Gohman602b0c82009-09-25 18:54:59 +00001020 Bytes = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1021 SDValue(Bytes, 0),
1022 CurDAG->getTargetConstant(0, ShiftAmtVT));
1023
1024 Bits = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1025 SDValue(Bits, 0),
Scott Michel02d711b2008-12-30 23:28:25 +00001026 CurDAG->getTargetConstant(0, ShiftAmtVT));
1027
Scott Michel02d711b2008-12-30 23:28:25 +00001028 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001029 CurDAG->getMachineNode(SPU::ROTQMBYv2i64, dl, VecVT,
1030 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001031 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001032 CurDAG->getMachineNode(SPU::ROTQMBIv2i64, dl, VecVT,
1033 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001034 }
1035
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001036 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001037 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001038}
1039
1040/*!
1041 * Emit the instruction sequence for i64 arithmetic right shifts.
1042 *
1043 * @param Op The shl operand
1044 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1045 * makes life easier.)
1046 * @return The SDNode with the entire instruction sequence
1047 */
1048SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001049SPUDAGToDAGISel::SelectSRAi64(SDNode *N, EVT OpVT) {
Scott Michel02d711b2008-12-30 23:28:25 +00001050 // Promote Op0 to vector
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001051 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +00001052 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001053 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001054 EVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001055 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +00001056
1057 SDNode *VecOp0 =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001058 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001059 VecVT, N->getOperand(0), getRC(MVT::v2i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001060
1061 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1062 SDNode *SignRot =
Dan Gohman602b0c82009-09-25 18:54:59 +00001063 CurDAG->getMachineNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
1064 SDValue(VecOp0, 0), SignRotAmt);
Scott Michel02d711b2008-12-30 23:28:25 +00001065 SDNode *UpperHalfSign =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001066 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001067 MVT::i32, SDValue(SignRot, 0), getRC(MVT::i32));
Scott Michel02d711b2008-12-30 23:28:25 +00001068
1069 SDNode *UpperHalfSignMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001070 CurDAG->getMachineNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001071 SDNode *UpperLowerMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001072 CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT,
1073 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
Scott Michel02d711b2008-12-30 23:28:25 +00001074 SDNode *UpperLowerSelect =
Dan Gohman602b0c82009-09-25 18:54:59 +00001075 CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
1076 SDValue(UpperHalfSignMask, 0),
1077 SDValue(VecOp0, 0),
1078 SDValue(UpperLowerMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001079
1080 SDNode *Shift = 0;
1081
1082 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1083 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1084 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1085
1086 if (bytes > 0) {
1087 bytes = 31 - bytes;
1088 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001089 CurDAG->getMachineNode(SPU::ROTQBYIv2i64, dl, VecVT,
1090 SDValue(UpperLowerSelect, 0),
1091 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001092 }
1093
1094 if (bits > 0) {
1095 bits = 8 - bits;
1096 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001097 CurDAG->getMachineNode(SPU::ROTQBIIv2i64, dl, VecVT,
1098 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1099 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001100 }
1101 } else {
1102 SDNode *NegShift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001103 CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1104 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001105
1106 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001107 CurDAG->getMachineNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
1108 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001109 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001110 CurDAG->getMachineNode(SPU::ROTQBIv2i64, dl, VecVT,
1111 SDValue(Shift, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001112 }
1113
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001114 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001115 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001116}
1117
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001118/*!
1119 Do the necessary magic necessary to load a i64 constant
1120 */
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001121SDNode *SPUDAGToDAGISel::SelectI64Constant(SDNode *N, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001122 DebugLoc dl) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001123 ConstantSDNode *CN = cast<ConstantSDNode>(N);
Scott Michel7ea02ff2009-03-17 01:15:45 +00001124 return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
1125}
1126
Owen Andersone50ed302009-08-10 22:56:29 +00001127SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001128 DebugLoc dl) {
Owen Anderson23b9b192009-08-12 00:36:31 +00001129 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001130 SDValue i64vec =
Scott Michel7ea02ff2009-03-17 01:15:45 +00001131 SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001132
1133 // Here's where it gets interesting, because we have to parse out the
1134 // subtree handed back in i64vec:
1135
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001136 if (i64vec.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001137 // The degenerate case where the upper and lower bits in the splat are
1138 // identical:
1139 SDValue Op0 = i64vec.getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001140
Scott Michel9de57a92009-01-26 22:33:37 +00001141 ReplaceUses(i64vec, Op0);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001142 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1143 SDValue(emitBuildVector(Op0.getNode()), 0),
1144 getRC(MVT::i64));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001145 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1146 SDValue lhs = i64vec.getOperand(0);
1147 SDValue rhs = i64vec.getOperand(1);
1148 SDValue shufmask = i64vec.getOperand(2);
1149
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001150 if (lhs.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001151 ReplaceUses(lhs, lhs.getOperand(0));
1152 lhs = lhs.getOperand(0);
1153 }
1154
1155 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1156 ? lhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001157 : emitBuildVector(lhs.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001158
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001159 if (rhs.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001160 ReplaceUses(rhs, rhs.getOperand(0));
1161 rhs = rhs.getOperand(0);
1162 }
1163
1164 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1165 ? rhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001166 : emitBuildVector(rhs.getNode()));
Scott Michel9de57a92009-01-26 22:33:37 +00001167
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001168 if (shufmask.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001169 ReplaceUses(shufmask, shufmask.getOperand(0));
1170 shufmask = shufmask.getOperand(0);
1171 }
1172
1173 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1174 ? shufmask.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001175 : emitBuildVector(shufmask.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001176
Chris Lattnera8e76142010-02-23 05:30:43 +00001177 SDValue shufNode =
1178 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001179 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
Chris Lattnera8e76142010-02-23 05:30:43 +00001180 SDValue(shufMaskNode, 0));
1181 HandleSDNode Dummy(shufNode);
1182 SDNode *SN = SelectCode(Dummy.getValue().getNode());
1183 if (SN == 0) SN = Dummy.getValue().getNode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001184
1185 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001186 OpVT, SDValue(SN, 0), getRC(MVT::i64));
Scott Michel7ea02ff2009-03-17 01:15:45 +00001187 } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001188 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1189 SDValue(emitBuildVector(i64vec.getNode()), 0),
1190 getRC(MVT::i64));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001191 } else {
Chris Lattner75361b62010-04-07 22:58:41 +00001192 report_fatal_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
Torok Edwindac237e2009-07-08 20:53:28 +00001193 "condition");
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001194 }
1195}
1196
Scott Michel02d711b2008-12-30 23:28:25 +00001197/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001198/// SPU-specific DAG, ready for instruction scheduling.
1199///
1200FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1201 return new SPUDAGToDAGISel(TM);
1202}