blob: c27caeae7d45cd9dd3419d27f826264ce68f0229 [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 }
Scott Michel266bc8f2007-12-04 22:23:35 +000093 }
94
Scott Michel266bc8f2007-12-04 22:23:35 +000095 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
96 static bool
97 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
98 {
Owen Andersone50ed302009-08-10 22:56:29 +000099 EVT vt = FPN->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000100 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000101 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000102 int sval = (int) ((val << 16) >> 16);
103 Imm = (short) val;
104 return val == sval;
105 }
106
107 return false;
108 }
109
Scott Michel7ea02ff2009-03-17 01:15:45 +0000110 //! Generate the carry-generate shuffle mask.
111 SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
112 SmallVector<SDValue, 16 > ShufBytes;
Dan Gohman844731a2008-05-13 00:00:25 +0000113
Scott Michel7ea02ff2009-03-17 01:15:45 +0000114 // Create the shuffle mask for "rotating" the borrow up one register slot
115 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
117 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
118 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
119 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +0000120
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000122 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000123 }
Scott Michel02d711b2008-12-30 23:28:25 +0000124
Scott Michel7ea02ff2009-03-17 01:15:45 +0000125 //! Generate the borrow-generate shuffle mask
126 SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
127 SmallVector<SDValue, 16 > ShufBytes;
128
129 // Create the shuffle mask for "rotating" the borrow up one register slot
130 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
132 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
133 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
134 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000135
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000137 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000138 }
139
Scott Michel7ea02ff2009-03-17 01:15:45 +0000140 //===------------------------------------------------------------------===//
141 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
142 /// instructions for SelectionDAG operations.
143 ///
144 class SPUDAGToDAGISel :
145 public SelectionDAGISel
146 {
Dan Gohmand858e902010-04-17 15:26:15 +0000147 const SPUTargetMachine &TM;
148 const SPUTargetLowering &SPUtli;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000149 unsigned GlobalBaseReg;
Scott Michel02d711b2008-12-30 23:28:25 +0000150
Scott Michel7ea02ff2009-03-17 01:15:45 +0000151 public:
152 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
153 SelectionDAGISel(tm),
154 TM(tm),
155 SPUtli(*tm.getTargetLowering())
156 { }
157
Dan Gohmanad2afc22009-07-31 18:16:33 +0000158 virtual bool runOnMachineFunction(MachineFunction &MF) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000159 // Make sure we re-emit a set of the global base reg if necessary
160 GlobalBaseReg = 0;
Dan Gohmanad2afc22009-07-31 18:16:33 +0000161 SelectionDAGISel::runOnMachineFunction(MF);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000162 return true;
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000163 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000164
Scott Michel7ea02ff2009-03-17 01:15:45 +0000165 /// getI32Imm - Return a target constant with the specified value, of type
166 /// i32.
167 inline SDValue getI32Imm(uint32_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 return CurDAG->getTargetConstant(Imm, MVT::i32);
Scott Michel94bd57e2009-01-15 04:41:47 +0000169 }
170
Scott Michel7ea02ff2009-03-17 01:15:45 +0000171 /// getSmallIPtrImm - Return a target constant of pointer type.
172 inline SDValue getSmallIPtrImm(unsigned Imm) {
173 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Chris Lattner17aa6802010-09-04 18:12:00 +0000174 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000175
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000176 SDNode *emitBuildVector(SDNode *bvNode) {
177 EVT vecVT = bvNode->getValueType(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000178 DebugLoc dl = bvNode->getDebugLoc();
179
180 // Check to see if this vector can be represented as a CellSPU immediate
181 // constant by invoking all of the instruction selection predicates:
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 if (((vecVT == MVT::v8i16) &&
183 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
184 ((vecVT == MVT::v4i32) &&
185 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
186 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
187 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
Scott Michel7ea02ff2009-03-17 01:15:45 +0000188 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 ((vecVT == MVT::v2i64) &&
190 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
191 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
Chris Lattnera8e76142010-02-23 05:30:43 +0000192 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)))) {
193 HandleSDNode Dummy(SDValue(bvNode, 0));
194 if (SDNode *N = Select(bvNode))
195 return N;
196 return Dummy.getValue().getNode();
197 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000198
199 // No, need to emit a constant pool spill:
200 std::vector<Constant*> CV;
201
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000202 for (size_t i = 0; i < bvNode->getNumOperands(); ++i) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000203 ConstantSDNode *V = cast<ConstantSDNode > (bvNode->getOperand(i));
Chris Lattnera8e76142010-02-23 05:30:43 +0000204 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000205 }
206
Dan Gohman46510a72010-04-15 01:51:59 +0000207 const Constant *CP = ConstantVector::get(CV);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000208 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
209 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
210 SDValue CGPoolOffset =
Dan Gohmand858e902010-04-17 15:26:15 +0000211 SPU::LowerConstantPool(CPIdx, *CurDAG, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000212
Chris Lattnera8e76142010-02-23 05:30:43 +0000213 HandleSDNode Dummy(CurDAG->getLoad(vecVT, dl,
214 CurDAG->getEntryNode(), CGPoolOffset,
Chris Lattnere8639032010-09-21 06:22:23 +0000215 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000216 false, false, false, Alignment));
Chris Lattnera8e76142010-02-23 05:30:43 +0000217 CurDAG->ReplaceAllUsesWith(SDValue(bvNode, 0), Dummy.getValue());
218 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
219 return N;
220 return Dummy.getValue().getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000221 }
Scott Michel02d711b2008-12-30 23:28:25 +0000222
Scott Michel7ea02ff2009-03-17 01:15:45 +0000223 /// Select - Convert the specified operand from a target-independent to a
224 /// target-specific node if it hasn't already been changed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000225 SDNode *Select(SDNode *N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000226
Scott Michel7ea02ff2009-03-17 01:15:45 +0000227 //! Emit the instruction sequence for i64 shl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000228 SDNode *SelectSHLi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000229
Scott Michel7ea02ff2009-03-17 01:15:45 +0000230 //! Emit the instruction sequence for i64 srl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000231 SDNode *SelectSRLi64(SDNode *N, EVT OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000232
Scott Michel7ea02ff2009-03-17 01:15:45 +0000233 //! Emit the instruction sequence for i64 sra
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000234 SDNode *SelectSRAi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000235
Scott Michel7ea02ff2009-03-17 01:15:45 +0000236 //! Emit the necessary sequence for loading i64 constants:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000237 SDNode *SelectI64Constant(SDNode *N, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000238
239 //! Alternate instruction emit sequence for loading i64 constants
Owen Andersone50ed302009-08-10 22:56:29 +0000240 SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000241
242 //! Returns true if the address N is an A-form (local store) address
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000243 bool SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000244 SDValue &Index);
245
246 //! D-form address predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000247 bool SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000248 SDValue &Index);
249
250 /// Alternate D-form address using i7 offset predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000251 bool SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000252 SDValue &Base);
253
254 /// D-form address selection workhorse
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000255 bool DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000256 SDValue &Base, int minOffset, int maxOffset);
257
258 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000259 bool SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000260 SDValue &Index);
261
262 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
263 /// inline asm expressions.
264 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
265 char ConstraintCode,
266 std::vector<SDValue> &OutOps) {
267 SDValue Op0, Op1;
268 switch (ConstraintCode) {
269 default: return true;
270 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000271 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
272 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1))
273 SelectXFormAddr(Op.getNode(), Op, Op0, Op1);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000274 break;
275 case 'o': // offsetable
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000276 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
277 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1)) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000278 Op0 = Op;
279 Op1 = getSmallIPtrImm(0);
280 }
281 break;
282 case 'v': // not offsetable
283#if 1
Torok Edwinc23197a2009-07-14 16:55:14 +0000284 llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
Scott Michel7ea02ff2009-03-17 01:15:45 +0000285#else
286 SelectAddrIdxOnly(Op, Op, Op0, Op1);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000287 break;
David Blaikie4d6ccb52012-01-20 21:51:11 +0000288#endif
Scott Michel7ea02ff2009-03-17 01:15:45 +0000289 }
290
291 OutOps.push_back(Op0);
292 OutOps.push_back(Op1);
293 return false;
294 }
295
Scott Michel7ea02ff2009-03-17 01:15:45 +0000296 virtual const char *getPassName() const {
297 return "Cell SPU DAG->DAG Pattern Instruction Selection";
298 }
299
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000300 private:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000301 SDValue getRC( MVT );
Scott Michel7ea02ff2009-03-17 01:15:45 +0000302
303 // Include the pieces autogenerated from the target description.
Scott Michel266bc8f2007-12-04 22:23:35 +0000304#include "SPUGenDAGISel.inc"
Scott Michel7ea02ff2009-03-17 01:15:45 +0000305 };
Dan Gohman844731a2008-05-13 00:00:25 +0000306}
307
Scott Michel266bc8f2007-12-04 22:23:35 +0000308/*!
Scott Michel9de57a92009-01-26 22:33:37 +0000309 \arg Op The ISD instruction operand
Scott Michel266bc8f2007-12-04 22:23:35 +0000310 \arg N The address to be tested
311 \arg Base The base address
312 \arg Index The base address index
313 */
314bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000315SPUDAGToDAGISel::SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000316 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000317 // These match the addr256k operand type:
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 EVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000319 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000320 int64_t val;
Scott Michel266bc8f2007-12-04 22:23:35 +0000321
322 switch (N.getOpcode()) {
323 case ISD::Constant:
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000324 val = dyn_cast<ConstantSDNode>(N.getNode())->getSExtValue();
325 Base = CurDAG->getTargetConstant( val , MVT::i32);
326 Index = Zero;
David Blaikie4d6ccb52012-01-20 21:51:11 +0000327 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000328 case ISD::ConstantPool:
329 case ISD::GlobalAddress:
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000330 report_fatal_error("SPU SelectAFormAddr: Pool/Global not lowered.");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000331 /*NOTREACHED*/
332
Scott Michel053c1da2008-01-29 02:16:57 +0000333 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000334 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000335 case ISD::TargetJumpTable:
Chris Lattner75361b62010-04-07 22:58:41 +0000336 report_fatal_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
Torok Edwindac237e2009-07-08 20:53:28 +0000337 "not wrapped as A-form address.");
Scott Michel053c1da2008-01-29 02:16:57 +0000338 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000339
Scott Michel02d711b2008-12-30 23:28:25 +0000340 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000341 // Just load from memory if there's only a single use of the location,
342 // otherwise, this will get handled below with D-form offset addresses
343 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000344 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000345 switch (Op0.getOpcode()) {
346 case ISD::TargetConstantPool:
347 case ISD::TargetJumpTable:
348 Base = Op0;
349 Index = Zero;
350 return true;
351
352 case ISD::TargetGlobalAddress: {
353 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
Dan Gohman46510a72010-04-15 01:51:59 +0000354 const GlobalValue *GV = GSDN->getGlobal();
Scott Michel053c1da2008-01-29 02:16:57 +0000355 if (GV->getAlignment() == 16) {
356 Base = Op0;
357 Index = Zero;
358 return true;
359 }
360 break;
361 }
362 }
363 }
364 break;
365 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000366 return false;
367}
368
Scott Michel02d711b2008-12-30 23:28:25 +0000369bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000370SPUDAGToDAGISel::SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Dan Gohman475871a2008-07-27 21:46:04 +0000371 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000372 const int minDForm2Offset = -(1 << 7);
373 const int maxDForm2Offset = (1 << 7) - 1;
374 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
375 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000376}
377
Scott Michel266bc8f2007-12-04 22:23:35 +0000378/*!
379 \arg Op The ISD instruction (ignored)
380 \arg N The address to be tested
381 \arg Base Base address register/pointer
382 \arg Index Base address index
383
384 Examine the input address by a base register plus a signed 10-bit
385 displacement, [r+I10] (D-form address).
386
387 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000388 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000389*/
390bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000391SPUDAGToDAGISel::SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000392 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000393 return DFormAddressPredicate(Op, N, Base, Index,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000394 SPUFrameLowering::minFrameOffset(),
395 SPUFrameLowering::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000396}
397
398bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000399SPUDAGToDAGISel::DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000400 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000401 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000402 unsigned Opc = N.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +0000403 EVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000404
Scott Michel053c1da2008-01-29 02:16:57 +0000405 if (Opc == ISD::FrameIndex) {
406 // Stack frame index must be less than 512 (divided by 16):
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000407 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(N);
Scott Michel203b2d62008-04-30 00:30:08 +0000408 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000409 DEBUG(errs() << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000410 << FI << "\n");
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000411 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000412 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000413 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000414 return true;
415 }
416 } else if (Opc == ISD::ADD) {
417 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000418 const SDValue Op0 = N.getOperand(0);
419 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000420
Scott Michel053c1da2008-01-29 02:16:57 +0000421 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
422 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
423 Base = CurDAG->getTargetConstant(0, PtrTy);
424 Index = N;
425 return true;
426 } else if (Op1.getOpcode() == ISD::Constant
427 || Op1.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000428 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000429 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000430
Scott Michel053c1da2008-01-29 02:16:57 +0000431 if (Op0.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000432 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op0);
Scott Michel203b2d62008-04-30 00:30:08 +0000433 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000434 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000435 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000436
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000437 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000438 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000439 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000440 return true;
441 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000442 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000443 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000444 Index = Op0;
445 return true;
446 }
447 } else if (Op0.getOpcode() == ISD::Constant
448 || Op0.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000449 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000450 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000451
452 if (Op1.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000453 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op1);
Scott Michel203b2d62008-04-30 00:30:08 +0000454 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000455 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000456 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000457
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000458 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000459 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000460 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000461 return true;
462 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000463 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000464 Base = CurDAG->getTargetConstant(offset, PtrTy);
465 Index = Op1;
466 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000467 }
Scott Michel053c1da2008-01-29 02:16:57 +0000468 }
469 } else if (Opc == SPUISD::IndirectAddr) {
470 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000471 const SDValue Op0 = N.getOperand(0);
472 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000473
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000474 if (Op0.getOpcode() == SPUISD::Hi
475 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000476 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000477 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000478 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000479 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000480 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
481 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000482 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000483
484 if (isa<ConstantSDNode>(Op1)) {
485 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000486 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000487 idxOp = Op0;
488 } else if (isa<ConstantSDNode>(Op0)) {
489 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000490 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000491 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000492 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000493
494 if (offset >= minOffset && offset <= maxOffset) {
495 Base = CurDAG->getTargetConstant(offset, PtrTy);
496 Index = idxOp;
497 return true;
498 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000499 }
Scott Michel053c1da2008-01-29 02:16:57 +0000500 } else if (Opc == SPUISD::AFormAddr) {
501 Base = CurDAG->getTargetConstant(0, N.getValueType());
502 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000503 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000504 } else if (Opc == SPUISD::LDRESULT) {
505 Base = CurDAG->getTargetConstant(0, N.getValueType());
506 Index = N;
507 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000508 } else if (Opc == ISD::Register
509 ||Opc == ISD::CopyFromReg
Kalle Raiskilabc2697c2010-08-04 13:59:48 +0000510 ||Opc == ISD::UNDEF
511 ||Opc == ISD::Constant) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000512 unsigned OpOpc = Op->getOpcode();
Scott Michel9c0c6b22008-11-21 02:56:16 +0000513
514 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
515 // Direct load/store without getelementptr
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000516 SDValue Offs;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000517
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000518 Offs = ((OpOpc == ISD::STORE) ? Op->getOperand(3) : Op->getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000519
520 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
521 if (Offs.getOpcode() == ISD::UNDEF)
522 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
523
524 Base = Offs;
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000525 Index = N;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000526 return true;
527 }
Scott Michelaedc6372008-12-10 00:15:19 +0000528 } else {
529 /* If otherwise unadorned, default to D-form address with 0 offset: */
530 if (Opc == ISD::CopyFromReg) {
Scott Michel19c10e62009-01-26 03:37:41 +0000531 Index = N.getOperand(1);
Scott Michelaedc6372008-12-10 00:15:19 +0000532 } else {
Scott Michel19c10e62009-01-26 03:37:41 +0000533 Index = N;
Scott Michelaedc6372008-12-10 00:15:19 +0000534 }
535
536 Base = CurDAG->getTargetConstant(0, Index.getValueType());
537 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000538 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000539 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000540
Scott Michel266bc8f2007-12-04 22:23:35 +0000541 return false;
542}
543
544/*!
545 \arg Op The ISD instruction operand
546 \arg N The address operand
547 \arg Base The base pointer operand
548 \arg Index The offset/index operand
549
Scott Michel9c0c6b22008-11-21 02:56:16 +0000550 If the address \a N can be expressed as an A-form or D-form address, returns
551 false. Otherwise, creates two operands, Base and Index that will become the
552 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000553*/
554bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000555SPUDAGToDAGISel::SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000556 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000557 if (!SelectAFormAddr(Op, N, Base, Index)
558 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000559 // If the address is neither A-form or D-form, punt and use an X-form
560 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000561 Base = N.getOperand(1);
562 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000563 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000564 }
565
566 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000567}
568
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000569/*!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000570 Utility function to use with COPY_TO_REGCLASS instructions. Returns a SDValue
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000571 to be used as the last parameter of a
572CurDAG->getMachineNode(COPY_TO_REGCLASS,..., ) function call
573 \arg VT the value type for which we want a register class
574*/
575SDValue SPUDAGToDAGISel::getRC( MVT VT ) {
576 switch( VT.SimpleTy ) {
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000577 case MVT::i8:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000578 return CurDAG->getTargetConstant(SPU::R8CRegClass.getID(), MVT::i32);
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000579 case MVT::i16:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000580 return CurDAG->getTargetConstant(SPU::R16CRegClass.getID(), MVT::i32);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000581 case MVT::i32:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000582 return CurDAG->getTargetConstant(SPU::R32CRegClass.getID(), MVT::i32);
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000583 case MVT::f32:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000584 return CurDAG->getTargetConstant(SPU::R32FPRegClass.getID(), MVT::i32);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000585 case MVT::i64:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000586 return CurDAG->getTargetConstant(SPU::R64CRegClass.getID(), MVT::i32);
Kalle Raiskila11edd0c2010-11-29 09:36:26 +0000587 case MVT::i128:
588 return CurDAG->getTargetConstant(SPU::GPRCRegClass.getID(), MVT::i32);
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000589 case MVT::v16i8:
590 case MVT::v8i16:
591 case MVT::v4i32:
592 case MVT::v4f32:
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000593 case MVT::v2i64:
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000594 case MVT::v2f64:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000595 return CurDAG->getTargetConstant(SPU::VECREGRegClass.getID(), MVT::i32);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000596 default:
597 assert( false && "add a new case here" );
David Blaikie4d6ccb52012-01-20 21:51:11 +0000598 return SDValue();
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000599 }
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000600}
601
Scott Michel266bc8f2007-12-04 22:23:35 +0000602//! Convert the operand from a target-independent to a target-specific node
603/*!
604 */
605SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000606SPUDAGToDAGISel::Select(SDNode *N) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000607 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000608 int n_ops = -1;
Ted Kremenek584520e2011-01-23 17:05:06 +0000609 unsigned NewOpc = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000610 EVT OpVT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000611 SDValue Ops[8];
Dale Johannesened2eee62009-02-06 01:31:28 +0000612 DebugLoc dl = N->getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +0000613
Chris Lattnera8e76142010-02-23 05:30:43 +0000614 if (N->isMachineOpcode())
Scott Michel266bc8f2007-12-04 22:23:35 +0000615 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000616
617 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000618 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000619 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
620 SDValue Imm0 = CurDAG->getTargetConstant(0, N->getValueType(0));
Scott Michel266bc8f2007-12-04 22:23:35 +0000621
Scott Michel02d711b2008-12-30 23:28:25 +0000622 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000623 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000624 Ops[0] = TFI;
625 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000626 n_ops = 2;
627 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000628 NewOpc = SPU::Ar32;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000629 Ops[0] = CurDAG->getRegister(SPU::R1, N->getValueType(0));
Dan Gohman602b0c82009-09-25 18:54:59 +0000630 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILAr32, dl,
Kalle Raiskila7d170972010-12-09 16:17:31 +0000631 N->getValueType(0), TFI),
Dan Gohman602b0c82009-09-25 18:54:59 +0000632 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000633 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000634 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000636 // Catch the i64 constants that end up here. Note: The backend doesn't
637 // attempt to legalize the constant (it's useless because DAGCombiner
638 // will insert 64-bit constants and we can't stop it).
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000639 return SelectI64Constant(N, OpVT, N->getDebugLoc());
Scott Michel94bd57e2009-01-15 04:41:47 +0000640 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 && OpVT == MVT::i64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000642 SDValue Op0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000643 EVT Op0VT = Op0.getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +0000644 EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
645 Op0VT, (128 / Op0VT.getSizeInBits()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000646 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +0000647 OpVT, (128 / OpVT.getSizeInBits()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000648 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000649
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 switch (Op0VT.getSimpleVT().SimpleTy) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000651 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000652 report_fatal_error("CellSPU Select: Unhandled zero/any extend EVT");
Scott Michel94bd57e2009-01-15 04:41:47 +0000653 /*NOTREACHED*/
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 case MVT::i32:
655 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
656 CurDAG->getConstant(0x80808080, MVT::i32),
657 CurDAG->getConstant(0x00010203, MVT::i32),
658 CurDAG->getConstant(0x80808080, MVT::i32),
659 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000660 break;
661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 case MVT::i16:
663 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
664 CurDAG->getConstant(0x80808080, MVT::i32),
665 CurDAG->getConstant(0x80800203, MVT::i32),
666 CurDAG->getConstant(0x80808080, MVT::i32),
667 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000668 break;
669
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 case MVT::i8:
671 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
672 CurDAG->getConstant(0x80808080, MVT::i32),
673 CurDAG->getConstant(0x80808003, MVT::i32),
674 CurDAG->getConstant(0x80808080, MVT::i32),
675 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000676 break;
Scott Michel58c58182008-01-17 20:38:41 +0000677 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000678
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000679 SDNode *shufMaskLoad = emitBuildVector(shufMask.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000680
Chris Lattnera8e76142010-02-23 05:30:43 +0000681 HandleSDNode PromoteScalar(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl,
682 Op0VecVT, Op0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000683
Chris Lattnera8e76142010-02-23 05:30:43 +0000684 SDValue PromScalar;
685 if (SDNode *N = SelectCode(PromoteScalar.getValue().getNode()))
686 PromScalar = SDValue(N, 0);
687 else
688 PromScalar = PromoteScalar.getValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000689
Scott Michel94bd57e2009-01-15 04:41:47 +0000690 SDValue zextShuffle =
Dale Johannesened2eee62009-02-06 01:31:28 +0000691 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000692 PromScalar, PromScalar,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000693 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000694
Chris Lattnera8e76142010-02-23 05:30:43 +0000695 HandleSDNode Dummy2(zextShuffle);
696 if (SDNode *N = SelectCode(Dummy2.getValue().getNode()))
697 zextShuffle = SDValue(N, 0);
698 else
699 zextShuffle = Dummy2.getValue();
700 HandleSDNode Dummy(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
701 zextShuffle));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000702
Chris Lattnera8e76142010-02-23 05:30:43 +0000703 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
704 SelectCode(Dummy.getValue().getNode());
705 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000707 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000708 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000709
Chris Lattnera8e76142010-02-23 05:30:43 +0000710 HandleSDNode Dummy(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
711 N->getOperand(0), N->getOperand(1),
712 SDValue(CGLoad, 0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000713
Chris Lattnera8e76142010-02-23 05:30:43 +0000714 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
715 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
716 return N;
717 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000719 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000720 emitBuildVector(getBorrowGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000721
Chris Lattnera8e76142010-02-23 05:30:43 +0000722 HandleSDNode Dummy(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
723 N->getOperand(0), N->getOperand(1),
724 SDValue(CGLoad, 0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000725
Chris Lattnera8e76142010-02-23 05:30:43 +0000726 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
727 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
728 return N;
729 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000731 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000732 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000733
Chris Lattnera8e76142010-02-23 05:30:43 +0000734 HandleSDNode Dummy(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
735 N->getOperand(0), N->getOperand(1),
736 SDValue(CGLoad, 0)));
737 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
738 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
739 return N;
740 return Dummy.getValue().getNode();
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000741 } else if (Opc == ISD::TRUNCATE) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000742 SDValue Op0 = N->getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000743 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 && OpVT == MVT::i32
745 && Op0.getValueType() == MVT::i64) {
Scott Michel9de57a92009-01-26 22:33:37 +0000746 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
747 //
748 // Take advantage of the fact that the upper 32 bits are in the
749 // i32 preferred slot and avoid shuffle gymnastics:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000750 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
751 if (CN != 0) {
752 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000753
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000754 if (shift_amt >= 32) {
755 SDNode *hi32 =
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000756 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
757 Op0.getOperand(0), getRC(MVT::i32));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000758
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000759 shift_amt -= 32;
760 if (shift_amt > 0) {
761 // Take care of the additional shift, if present:
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000763 unsigned Opc = SPU::ROTMAIr32_i32;
Scott Michel9de57a92009-01-26 22:33:37 +0000764
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000765 if (Op0.getOpcode() == ISD::SRL)
766 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000767
Dan Gohman602b0c82009-09-25 18:54:59 +0000768 hi32 = CurDAG->getMachineNode(Opc, dl, OpVT, SDValue(hi32, 0),
769 shift);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000770 }
771
772 return hi32;
773 }
774 }
775 }
Scott Michel02d711b2008-12-30 23:28:25 +0000776 } else if (Opc == ISD::SHL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000777 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000778 return SelectSHLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000779 } else if (Opc == ISD::SRL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000780 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000781 return SelectSRLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000782 } else if (Opc == ISD::SRA) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000783 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000784 return SelectSRAi64(N, OpVT);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000785 } else if (Opc == ISD::FNEG
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000787 DebugLoc dl = N->getDebugLoc();
Scott Michel7ea02ff2009-03-17 01:15:45 +0000788 // Check if the pattern is a special form of DFNMS:
789 // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000790 SDValue Op0 = N->getOperand(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000791 if (Op0.getOpcode() == ISD::FSUB) {
792 SDValue Op00 = Op0.getOperand(0);
793 if (Op00.getOpcode() == ISD::FMUL) {
794 unsigned Opc = SPU::DFNMSf64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 if (OpVT == MVT::v2f64)
Scott Michel7ea02ff2009-03-17 01:15:45 +0000796 Opc = SPU::DFNMSv2f64;
797
Dan Gohman602b0c82009-09-25 18:54:59 +0000798 return CurDAG->getMachineNode(Opc, dl, OpVT,
799 Op00.getOperand(0),
800 Op00.getOperand(1),
801 Op0.getOperand(1));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000802 }
803 }
804
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000806 SDNode *signMask = 0;
Scott Michela82d3f72009-03-17 16:45:16 +0000807 unsigned Opc = SPU::XORfneg64;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000808
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 if (OpVT == MVT::f64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000810 signMask = SelectI64Constant(negConst.getNode(), MVT::i64, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 } else if (OpVT == MVT::v2f64) {
Scott Michela82d3f72009-03-17 16:45:16 +0000812 Opc = SPU::XORfnegvec;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000813 signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 MVT::v2i64,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000815 negConst, negConst).getNode());
Scott Michel7ea02ff2009-03-17 01:15:45 +0000816 }
817
Dan Gohman602b0c82009-09-25 18:54:59 +0000818 return CurDAG->getMachineNode(Opc, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000819 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000820 } else if (Opc == ISD::FABS) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 if (OpVT == MVT::f64) {
822 SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
Dan Gohman602b0c82009-09-25 18:54:59 +0000823 return CurDAG->getMachineNode(SPU::ANDfabs64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000824 N->getOperand(0), SDValue(signMask, 0));
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 } else if (OpVT == MVT::v2f64) {
826 SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
827 SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000828 absConst, absConst);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000829 SDNode *signMask = emitBuildVector(absVec.getNode());
Dan Gohman602b0c82009-09-25 18:54:59 +0000830 return CurDAG->getMachineNode(SPU::ANDfabsvec, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000831 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000832 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000833 } else if (Opc == SPUISD::LDRESULT) {
834 // Custom select instructions for LDRESULT
Owen Andersone50ed302009-08-10 22:56:29 +0000835 EVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000836 SDValue Arg = N->getOperand(0);
837 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000838 SDNode *Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000839
Kalle Raiskila82581352010-10-01 09:20:01 +0000840 Result = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VT,
841 MVT::Other, Arg,
842 getRC( VT.getSimpleVT()), Chain);
Scott Michel266bc8f2007-12-04 22:23:35 +0000843 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000844
Scott Michel053c1da2008-01-29 02:16:57 +0000845 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000846 // Look at the operands: SelectCode() will catch the cases that aren't
847 // specifically handled here.
848 //
849 // SPUInstrInfo catches the following patterns:
850 // (SPUindirect (SPUhi ...), (SPUlo ...))
851 // (SPUindirect $sp, imm)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000852 EVT VT = N->getValueType(0);
Scott Michelf0569be2008-12-27 04:51:36 +0000853 SDValue Op0 = N->getOperand(0);
854 SDValue Op1 = N->getOperand(1);
855 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000856
Scott Michelf0569be2008-12-27 04:51:36 +0000857 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
858 || (Op0.getOpcode() == ISD::Register
859 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
860 && RN->getReg() != SPU::R1))) {
861 NewOpc = SPU::Ar32;
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000862 Ops[1] = Op1;
Scott Michel58c58182008-01-17 20:38:41 +0000863 if (Op1.getOpcode() == ISD::Constant) {
864 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000865 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000866 if (isInt<10>(CN->getSExtValue())) {
867 NewOpc = SPU::AIr32;
868 Ops[1] = Op1;
869 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000870 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILr32, dl,
871 N->getValueType(0),
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000872 Op1),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000873 0);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000874 }
Scott Michel58c58182008-01-17 20:38:41 +0000875 }
Scott Michelf0569be2008-12-27 04:51:36 +0000876 Ops[0] = Op0;
Scott Michelf0569be2008-12-27 04:51:36 +0000877 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000878 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000879 }
Scott Michel02d711b2008-12-30 23:28:25 +0000880
Scott Michel58c58182008-01-17 20:38:41 +0000881 if (n_ops > 0) {
882 if (N->hasOneUse())
883 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
884 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000885 return CurDAG->getMachineNode(NewOpc, dl, OpVT, Ops, n_ops);
Scott Michel58c58182008-01-17 20:38:41 +0000886 } else
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000887 return SelectCode(N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000888}
889
Scott Michel02d711b2008-12-30 23:28:25 +0000890/*!
891 * Emit the instruction sequence for i64 left shifts. The basic algorithm
892 * is to fill the bottom two word slots with zeros so that zeros are shifted
893 * in as the entire quadword is shifted left.
894 *
895 * \note This code could also be used to implement v2i64 shl.
896 *
897 * @param Op The shl operand
898 * @param OpVT Op's machine value value type (doesn't need to be passed, but
899 * makes life easier.)
900 * @return The SDNode with the entire instruction sequence
901 */
902SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000903SPUDAGToDAGISel::SelectSHLi64(SDNode *N, EVT OpVT) {
904 SDValue Op0 = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000905 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +0000906 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000907 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000908 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000909 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
910 SDValue SelMaskVal;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000911 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000912
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000913 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
914 Op0, getRC(MVT::v2i64) );
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
Dan Gohman602b0c82009-09-25 18:54:59 +0000916 SelMask = CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
917 ZeroFill = CurDAG->getMachineNode(SPU::ILv2i64, dl, VecVT,
918 CurDAG->getTargetConstant(0, OpVT));
919 VecOp0 = CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
920 SDValue(ZeroFill, 0),
921 SDValue(VecOp0, 0),
922 SDValue(SelMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000923
924 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
925 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
926 unsigned bits = unsigned(CN->getZExtValue()) & 7;
927
928 if (bytes > 0) {
929 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000930 CurDAG->getMachineNode(SPU::SHLQBYIv2i64, dl, VecVT,
931 SDValue(VecOp0, 0),
932 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000933 }
934
935 if (bits > 0) {
936 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000937 CurDAG->getMachineNode(SPU::SHLQBIIv2i64, dl, VecVT,
938 SDValue((Shift != 0 ? Shift : VecOp0), 0),
939 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000940 }
941 } else {
942 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +0000943 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
944 ShiftAmt,
945 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000946 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +0000947 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
948 ShiftAmt,
949 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000950 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000951 CurDAG->getMachineNode(SPU::SHLQBYv2i64, dl, VecVT,
952 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000953 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000954 CurDAG->getMachineNode(SPU::SHLQBIv2i64, dl, VecVT,
955 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000956 }
957
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000958 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000959 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +0000960}
961
962/*!
963 * Emit the instruction sequence for i64 logical right shifts.
964 *
965 * @param Op The shl operand
966 * @param OpVT Op's machine value value type (doesn't need to be passed, but
967 * makes life easier.)
968 * @return The SDNode with the entire instruction sequence
969 */
970SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000971SPUDAGToDAGISel::SelectSRLi64(SDNode *N, EVT OpVT) {
972 SDValue Op0 = N->getOperand(0);
Owen Anderson23b9b192009-08-12 00:36:31 +0000973 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
974 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000975 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000976 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000977 SDNode *VecOp0, *Shift = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000978 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000979
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000980 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
981 Op0, getRC(MVT::v2i64) );
Scott Michel02d711b2008-12-30 23:28:25 +0000982
983 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
984 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
985 unsigned bits = unsigned(CN->getZExtValue()) & 7;
986
987 if (bytes > 0) {
988 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000989 CurDAG->getMachineNode(SPU::ROTQMBYIv2i64, dl, VecVT,
990 SDValue(VecOp0, 0),
991 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000992 }
993
994 if (bits > 0) {
995 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000996 CurDAG->getMachineNode(SPU::ROTQMBIIv2i64, dl, VecVT,
997 SDValue((Shift != 0 ? Shift : VecOp0), 0),
998 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000999 }
1000 } else {
1001 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +00001002 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1003 ShiftAmt,
1004 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001005 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +00001006 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1007 ShiftAmt,
1008 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001009
1010 // Ensure that the shift amounts are negated!
Dan Gohman602b0c82009-09-25 18:54:59 +00001011 Bytes = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1012 SDValue(Bytes, 0),
1013 CurDAG->getTargetConstant(0, ShiftAmtVT));
1014
1015 Bits = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1016 SDValue(Bits, 0),
Scott Michel02d711b2008-12-30 23:28:25 +00001017 CurDAG->getTargetConstant(0, ShiftAmtVT));
1018
Scott Michel02d711b2008-12-30 23:28:25 +00001019 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001020 CurDAG->getMachineNode(SPU::ROTQMBYv2i64, dl, VecVT,
1021 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001022 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001023 CurDAG->getMachineNode(SPU::ROTQMBIv2i64, dl, VecVT,
1024 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001025 }
1026
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001027 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001028 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001029}
1030
1031/*!
1032 * Emit the instruction sequence for i64 arithmetic right shifts.
1033 *
1034 * @param Op The shl operand
1035 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1036 * makes life easier.)
1037 * @return The SDNode with the entire instruction sequence
1038 */
1039SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001040SPUDAGToDAGISel::SelectSRAi64(SDNode *N, EVT OpVT) {
Scott Michel02d711b2008-12-30 23:28:25 +00001041 // Promote Op0 to vector
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001042 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +00001043 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001044 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001045 EVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001046 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +00001047
1048 SDNode *VecOp0 =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001049 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001050 VecVT, N->getOperand(0), getRC(MVT::v2i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001051
1052 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1053 SDNode *SignRot =
Dan Gohman602b0c82009-09-25 18:54:59 +00001054 CurDAG->getMachineNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
1055 SDValue(VecOp0, 0), SignRotAmt);
Scott Michel02d711b2008-12-30 23:28:25 +00001056 SDNode *UpperHalfSign =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001057 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001058 MVT::i32, SDValue(SignRot, 0), getRC(MVT::i32));
Scott Michel02d711b2008-12-30 23:28:25 +00001059
1060 SDNode *UpperHalfSignMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001061 CurDAG->getMachineNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001062 SDNode *UpperLowerMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001063 CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT,
1064 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
Scott Michel02d711b2008-12-30 23:28:25 +00001065 SDNode *UpperLowerSelect =
Dan Gohman602b0c82009-09-25 18:54:59 +00001066 CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
1067 SDValue(UpperHalfSignMask, 0),
1068 SDValue(VecOp0, 0),
1069 SDValue(UpperLowerMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001070
1071 SDNode *Shift = 0;
1072
1073 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1074 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1075 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1076
1077 if (bytes > 0) {
1078 bytes = 31 - bytes;
1079 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001080 CurDAG->getMachineNode(SPU::ROTQBYIv2i64, dl, VecVT,
1081 SDValue(UpperLowerSelect, 0),
1082 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001083 }
1084
1085 if (bits > 0) {
1086 bits = 8 - bits;
1087 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001088 CurDAG->getMachineNode(SPU::ROTQBIIv2i64, dl, VecVT,
1089 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1090 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001091 }
1092 } else {
1093 SDNode *NegShift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001094 CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1095 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001096
1097 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001098 CurDAG->getMachineNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
1099 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001100 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001101 CurDAG->getMachineNode(SPU::ROTQBIv2i64, dl, VecVT,
1102 SDValue(Shift, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001103 }
1104
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001105 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001106 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001107}
1108
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001109/*!
1110 Do the necessary magic necessary to load a i64 constant
1111 */
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001112SDNode *SPUDAGToDAGISel::SelectI64Constant(SDNode *N, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001113 DebugLoc dl) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001114 ConstantSDNode *CN = cast<ConstantSDNode>(N);
Scott Michel7ea02ff2009-03-17 01:15:45 +00001115 return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
1116}
1117
Owen Andersone50ed302009-08-10 22:56:29 +00001118SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001119 DebugLoc dl) {
Owen Anderson23b9b192009-08-12 00:36:31 +00001120 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001121 SDValue i64vec =
Scott Michel7ea02ff2009-03-17 01:15:45 +00001122 SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001123
1124 // Here's where it gets interesting, because we have to parse out the
1125 // subtree handed back in i64vec:
1126
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001127 if (i64vec.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001128 // The degenerate case where the upper and lower bits in the splat are
1129 // identical:
1130 SDValue Op0 = i64vec.getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001131
Scott Michel9de57a92009-01-26 22:33:37 +00001132 ReplaceUses(i64vec, Op0);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001133 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1134 SDValue(emitBuildVector(Op0.getNode()), 0),
1135 getRC(MVT::i64));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001136 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1137 SDValue lhs = i64vec.getOperand(0);
1138 SDValue rhs = i64vec.getOperand(1);
1139 SDValue shufmask = i64vec.getOperand(2);
1140
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001141 if (lhs.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001142 ReplaceUses(lhs, lhs.getOperand(0));
1143 lhs = lhs.getOperand(0);
1144 }
1145
1146 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1147 ? lhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001148 : emitBuildVector(lhs.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001149
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001150 if (rhs.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001151 ReplaceUses(rhs, rhs.getOperand(0));
1152 rhs = rhs.getOperand(0);
1153 }
1154
1155 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1156 ? rhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001157 : emitBuildVector(rhs.getNode()));
Scott Michel9de57a92009-01-26 22:33:37 +00001158
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001159 if (shufmask.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001160 ReplaceUses(shufmask, shufmask.getOperand(0));
1161 shufmask = shufmask.getOperand(0);
1162 }
1163
1164 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1165 ? shufmask.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001166 : emitBuildVector(shufmask.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001167
Chris Lattnera8e76142010-02-23 05:30:43 +00001168 SDValue shufNode =
1169 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001170 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
Chris Lattnera8e76142010-02-23 05:30:43 +00001171 SDValue(shufMaskNode, 0));
1172 HandleSDNode Dummy(shufNode);
1173 SDNode *SN = SelectCode(Dummy.getValue().getNode());
1174 if (SN == 0) SN = Dummy.getValue().getNode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001175
1176 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001177 OpVT, SDValue(SN, 0), getRC(MVT::i64));
Scott Michel7ea02ff2009-03-17 01:15:45 +00001178 } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001179 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1180 SDValue(emitBuildVector(i64vec.getNode()), 0),
1181 getRC(MVT::i64));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001182 } else {
Chris Lattner75361b62010-04-07 22:58:41 +00001183 report_fatal_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
Torok Edwindac237e2009-07-08 20:53:28 +00001184 "condition");
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001185 }
1186}
1187
Scott Michel02d711b2008-12-30 23:28:25 +00001188/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001189/// SPU-specific DAG, ready for instruction scheduling.
1190///
1191FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1192 return new SPUDAGToDAGISel(TM);
1193}