blob: a297d036f03e55b128925984d261bc3ae055a51f [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"
Scott Michel94bd57e2009-01-15 04:41:47 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000026#include "llvm/Target/TargetOptions.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/Constants.h"
29#include "llvm/GlobalValue.h"
30#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000031#include "llvm/LLVMContext.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000032#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000034#include "llvm/Support/MathExtras.h"
35#include "llvm/Support/Compiler.h"
Torok Edwindac237e2009-07-08 20:53:28 +000036#include "llvm/Support/raw_ostream.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000037
38using namespace llvm;
39
40namespace {
41 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
42 bool
Scott Michel266bc8f2007-12-04 22:23:35 +000043 isI32IntS10Immediate(ConstantSDNode *CN)
44 {
Benjamin Kramer7e09deb2010-03-29 19:07:58 +000045 return isInt<10>(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000046 }
47
Scott Michel504c3692007-12-17 22:32:34 +000048 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
49 bool
50 isI32IntU10Immediate(ConstantSDNode *CN)
51 {
Benjamin Kramer34247a02010-03-29 21:13:41 +000052 return isUInt<10>(CN->getSExtValue());
Scott Michel504c3692007-12-17 22:32:34 +000053 }
54
Scott Michel266bc8f2007-12-04 22:23:35 +000055 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
56 bool
57 isI16IntS10Immediate(ConstantSDNode *CN)
58 {
Benjamin Kramer7e09deb2010-03-29 19:07:58 +000059 return isInt<10>(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000060 }
61
Scott Michelec2a08f2007-12-15 00:38:50 +000062 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
63 bool
64 isI16IntU10Immediate(ConstantSDNode *CN)
65 {
Benjamin Kramer34247a02010-03-29 21:13:41 +000066 return isUInt<10>((short) CN->getZExtValue());
Scott Michelec2a08f2007-12-15 00:38:50 +000067 }
68
Scott Michel266bc8f2007-12-04 22:23:35 +000069 //! ConstantSDNode predicate for signed 16-bit values
70 /*!
71 \arg CN The constant SelectionDAG node holding the value
72 \arg Imm The returned 16-bit value, if returning true
73
74 This predicate tests the value in \a CN to see whether it can be
75 represented as a 16-bit, sign-extended quantity. Returns true if
76 this is the case.
77 */
78 bool
79 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
80 {
Owen Andersone50ed302009-08-10 22:56:29 +000081 EVT vt = CN->getValueType(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000082 Imm = (short) CN->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000083 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +000084 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +000085 } else if (vt == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000086 int32_t i_val = (int32_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +000087 short s_val = (short) i_val;
88 return i_val == s_val;
89 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000090 int64_t i_val = (int64_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +000091 short s_val = (short) i_val;
92 return i_val == s_val;
93 }
94
95 return false;
96 }
97
Scott Michel266bc8f2007-12-04 22:23:35 +000098 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
99 static bool
100 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
101 {
Owen Andersone50ed302009-08-10 22:56:29 +0000102 EVT vt = FPN->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000104 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000105 int sval = (int) ((val << 16) >> 16);
106 Imm = (short) val;
107 return val == sval;
108 }
109
110 return false;
111 }
112
Scott Michel7ea02ff2009-03-17 01:15:45 +0000113 //! Generate the carry-generate shuffle mask.
114 SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
115 SmallVector<SDValue, 16 > ShufBytes;
Dan Gohman844731a2008-05-13 00:00:25 +0000116
Scott Michel7ea02ff2009-03-17 01:15:45 +0000117 // Create the shuffle mask for "rotating" the borrow up one register slot
118 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
120 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
121 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
122 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +0000123
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000125 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000126 }
Scott Michel02d711b2008-12-30 23:28:25 +0000127
Scott Michel7ea02ff2009-03-17 01:15:45 +0000128 //! Generate the borrow-generate shuffle mask
129 SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
130 SmallVector<SDValue, 16 > ShufBytes;
131
132 // Create the shuffle mask for "rotating" the borrow up one register slot
133 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
135 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
136 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
137 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000138
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000140 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000141 }
142
Scott Michel7ea02ff2009-03-17 01:15:45 +0000143 //===------------------------------------------------------------------===//
144 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
145 /// instructions for SelectionDAG operations.
146 ///
147 class SPUDAGToDAGISel :
148 public SelectionDAGISel
149 {
Dan Gohmand858e902010-04-17 15:26:15 +0000150 const SPUTargetMachine &TM;
151 const SPUTargetLowering &SPUtli;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000152 unsigned GlobalBaseReg;
Scott Michel02d711b2008-12-30 23:28:25 +0000153
Scott Michel7ea02ff2009-03-17 01:15:45 +0000154 public:
155 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
156 SelectionDAGISel(tm),
157 TM(tm),
158 SPUtli(*tm.getTargetLowering())
159 { }
160
Dan Gohmanad2afc22009-07-31 18:16:33 +0000161 virtual bool runOnMachineFunction(MachineFunction &MF) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000162 // Make sure we re-emit a set of the global base reg if necessary
163 GlobalBaseReg = 0;
Dan Gohmanad2afc22009-07-31 18:16:33 +0000164 SelectionDAGISel::runOnMachineFunction(MF);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000165 return true;
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000166 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000167
Scott Michel7ea02ff2009-03-17 01:15:45 +0000168 /// getI32Imm - Return a target constant with the specified value, of type
169 /// i32.
170 inline SDValue getI32Imm(uint32_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 return CurDAG->getTargetConstant(Imm, MVT::i32);
Scott Michel94bd57e2009-01-15 04:41:47 +0000172 }
173
Scott Michel7ea02ff2009-03-17 01:15:45 +0000174 /// getSmallIPtrImm - Return a target constant of pointer type.
175 inline SDValue getSmallIPtrImm(unsigned Imm) {
176 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Chris Lattner17aa6802010-09-04 18:12:00 +0000177 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000178
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000179 SDNode *emitBuildVector(SDNode *bvNode) {
180 EVT vecVT = bvNode->getValueType(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000181 DebugLoc dl = bvNode->getDebugLoc();
182
183 // Check to see if this vector can be represented as a CellSPU immediate
184 // constant by invoking all of the instruction selection predicates:
Owen Anderson825b72b2009-08-11 20:47:22 +0000185 if (((vecVT == MVT::v8i16) &&
186 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
187 ((vecVT == MVT::v4i32) &&
188 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
189 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
190 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
Scott Michel7ea02ff2009-03-17 01:15:45 +0000191 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 ((vecVT == MVT::v2i64) &&
193 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
194 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
Chris Lattnera8e76142010-02-23 05:30:43 +0000195 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)))) {
196 HandleSDNode Dummy(SDValue(bvNode, 0));
197 if (SDNode *N = Select(bvNode))
198 return N;
199 return Dummy.getValue().getNode();
200 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000201
202 // No, need to emit a constant pool spill:
203 std::vector<Constant*> CV;
204
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000205 for (size_t i = 0; i < bvNode->getNumOperands(); ++i) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000206 ConstantSDNode *V = cast<ConstantSDNode > (bvNode->getOperand(i));
Chris Lattnera8e76142010-02-23 05:30:43 +0000207 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000208 }
209
Dan Gohman46510a72010-04-15 01:51:59 +0000210 const Constant *CP = ConstantVector::get(CV);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000211 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
212 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
213 SDValue CGPoolOffset =
Dan Gohmand858e902010-04-17 15:26:15 +0000214 SPU::LowerConstantPool(CPIdx, *CurDAG, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000215
Chris Lattnera8e76142010-02-23 05:30:43 +0000216 HandleSDNode Dummy(CurDAG->getLoad(vecVT, dl,
217 CurDAG->getEntryNode(), CGPoolOffset,
Chris Lattnere8639032010-09-21 06:22:23 +0000218 MachinePointerInfo::getConstantPool(),
Chris Lattnera8e76142010-02-23 05:30:43 +0000219 false, false, Alignment));
220 CurDAG->ReplaceAllUsesWith(SDValue(bvNode, 0), Dummy.getValue());
221 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
222 return N;
223 return Dummy.getValue().getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000224 }
Scott Michel02d711b2008-12-30 23:28:25 +0000225
Scott Michel7ea02ff2009-03-17 01:15:45 +0000226 /// Select - Convert the specified operand from a target-independent to a
227 /// target-specific node if it hasn't already been changed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000228 SDNode *Select(SDNode *N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000229
Scott Michel7ea02ff2009-03-17 01:15:45 +0000230 //! Emit the instruction sequence for i64 shl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000231 SDNode *SelectSHLi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000232
Scott Michel7ea02ff2009-03-17 01:15:45 +0000233 //! Emit the instruction sequence for i64 srl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000234 SDNode *SelectSRLi64(SDNode *N, EVT OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000235
Scott Michel7ea02ff2009-03-17 01:15:45 +0000236 //! Emit the instruction sequence for i64 sra
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000237 SDNode *SelectSRAi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000238
Scott Michel7ea02ff2009-03-17 01:15:45 +0000239 //! Emit the necessary sequence for loading i64 constants:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000240 SDNode *SelectI64Constant(SDNode *N, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000241
242 //! Alternate instruction emit sequence for loading i64 constants
Owen Andersone50ed302009-08-10 22:56:29 +0000243 SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000244
245 //! Returns true if the address N is an A-form (local store) address
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000246 bool SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000247 SDValue &Index);
248
249 //! D-form address predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000250 bool SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000251 SDValue &Index);
252
253 /// Alternate D-form address using i7 offset predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000254 bool SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000255 SDValue &Base);
256
257 /// D-form address selection workhorse
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000258 bool DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000259 SDValue &Base, int minOffset, int maxOffset);
260
261 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000262 bool SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000263 SDValue &Index);
264
265 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
266 /// inline asm expressions.
267 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
268 char ConstraintCode,
269 std::vector<SDValue> &OutOps) {
270 SDValue Op0, Op1;
271 switch (ConstraintCode) {
272 default: return true;
273 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000274 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
275 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1))
276 SelectXFormAddr(Op.getNode(), Op, Op0, Op1);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000277 break;
278 case 'o': // offsetable
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000279 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
280 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1)) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000281 Op0 = Op;
282 Op1 = getSmallIPtrImm(0);
283 }
284 break;
285 case 'v': // not offsetable
286#if 1
Torok Edwinc23197a2009-07-14 16:55:14 +0000287 llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
Scott Michel7ea02ff2009-03-17 01:15:45 +0000288#else
289 SelectAddrIdxOnly(Op, Op, Op0, Op1);
290#endif
291 break;
292 }
293
294 OutOps.push_back(Op0);
295 OutOps.push_back(Op1);
296 return false;
297 }
298
Scott Michel7ea02ff2009-03-17 01:15:45 +0000299 virtual const char *getPassName() const {
300 return "Cell SPU DAG->DAG Pattern Instruction Selection";
301 }
302
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000303 private:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000304 SDValue getRC( MVT );
Scott Michel7ea02ff2009-03-17 01:15:45 +0000305
306 // Include the pieces autogenerated from the target description.
Scott Michel266bc8f2007-12-04 22:23:35 +0000307#include "SPUGenDAGISel.inc"
Scott Michel7ea02ff2009-03-17 01:15:45 +0000308 };
Dan Gohman844731a2008-05-13 00:00:25 +0000309}
310
Scott Michel266bc8f2007-12-04 22:23:35 +0000311/*!
Scott Michel9de57a92009-01-26 22:33:37 +0000312 \arg Op The ISD instruction operand
Scott Michel266bc8f2007-12-04 22:23:35 +0000313 \arg N The address to be tested
314 \arg Base The base address
315 \arg Index The base address index
316 */
317bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000318SPUDAGToDAGISel::SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000319 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000320 // These match the addr256k operand type:
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 EVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000322 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000323 int64_t val;
Scott Michel266bc8f2007-12-04 22:23:35 +0000324
325 switch (N.getOpcode()) {
326 case ISD::Constant:
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000327 val = dyn_cast<ConstantSDNode>(N.getNode())->getSExtValue();
328 Base = CurDAG->getTargetConstant( val , MVT::i32);
329 Index = Zero;
330 return true; break;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000331 case ISD::ConstantPool:
332 case ISD::GlobalAddress:
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000333 report_fatal_error("SPU SelectAFormAddr: Pool/Global not lowered.");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000334 /*NOTREACHED*/
335
Scott Michel053c1da2008-01-29 02:16:57 +0000336 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000337 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000338 case ISD::TargetJumpTable:
Chris Lattner75361b62010-04-07 22:58:41 +0000339 report_fatal_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
Torok Edwindac237e2009-07-08 20:53:28 +0000340 "not wrapped as A-form address.");
Scott Michel053c1da2008-01-29 02:16:57 +0000341 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000342
Scott Michel02d711b2008-12-30 23:28:25 +0000343 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000344 // Just load from memory if there's only a single use of the location,
345 // otherwise, this will get handled below with D-form offset addresses
346 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000347 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000348 switch (Op0.getOpcode()) {
349 case ISD::TargetConstantPool:
350 case ISD::TargetJumpTable:
351 Base = Op0;
352 Index = Zero;
353 return true;
354
355 case ISD::TargetGlobalAddress: {
356 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
Dan Gohman46510a72010-04-15 01:51:59 +0000357 const GlobalValue *GV = GSDN->getGlobal();
Scott Michel053c1da2008-01-29 02:16:57 +0000358 if (GV->getAlignment() == 16) {
359 Base = Op0;
360 Index = Zero;
361 return true;
362 }
363 break;
364 }
365 }
366 }
367 break;
368 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000369 return false;
370}
371
Scott Michel02d711b2008-12-30 23:28:25 +0000372bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000373SPUDAGToDAGISel::SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Dan Gohman475871a2008-07-27 21:46:04 +0000374 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000375 const int minDForm2Offset = -(1 << 7);
376 const int maxDForm2Offset = (1 << 7) - 1;
377 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
378 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000379}
380
Scott Michel266bc8f2007-12-04 22:23:35 +0000381/*!
382 \arg Op The ISD instruction (ignored)
383 \arg N The address to be tested
384 \arg Base Base address register/pointer
385 \arg Index Base address index
386
387 Examine the input address by a base register plus a signed 10-bit
388 displacement, [r+I10] (D-form address).
389
390 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000391 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000392*/
393bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000394SPUDAGToDAGISel::SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000395 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000396 return DFormAddressPredicate(Op, N, Base, Index,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000397 SPUFrameLowering::minFrameOffset(),
398 SPUFrameLowering::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000399}
400
401bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000402SPUDAGToDAGISel::DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000403 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000404 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000405 unsigned Opc = N.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +0000406 EVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000407
Scott Michel053c1da2008-01-29 02:16:57 +0000408 if (Opc == ISD::FrameIndex) {
409 // Stack frame index must be less than 512 (divided by 16):
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000410 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(N);
Scott Michel203b2d62008-04-30 00:30:08 +0000411 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000412 DEBUG(errs() << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000413 << FI << "\n");
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000414 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000415 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000416 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000417 return true;
418 }
419 } else if (Opc == ISD::ADD) {
420 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000421 const SDValue Op0 = N.getOperand(0);
422 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000423
Scott Michel053c1da2008-01-29 02:16:57 +0000424 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
425 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
426 Base = CurDAG->getTargetConstant(0, PtrTy);
427 Index = N;
428 return true;
429 } else if (Op1.getOpcode() == ISD::Constant
430 || Op1.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000431 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000432 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000433
Scott Michel053c1da2008-01-29 02:16:57 +0000434 if (Op0.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000435 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op0);
Scott Michel203b2d62008-04-30 00:30:08 +0000436 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000437 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000438 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000439
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000440 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000441 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000442 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000443 return true;
444 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000445 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000446 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000447 Index = Op0;
448 return true;
449 }
450 } else if (Op0.getOpcode() == ISD::Constant
451 || Op0.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000452 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000453 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000454
455 if (Op1.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000456 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op1);
Scott Michel203b2d62008-04-30 00:30:08 +0000457 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000458 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000459 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000460
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000461 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000462 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000463 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000464 return true;
465 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000466 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000467 Base = CurDAG->getTargetConstant(offset, PtrTy);
468 Index = Op1;
469 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000470 }
Scott Michel053c1da2008-01-29 02:16:57 +0000471 }
472 } else if (Opc == SPUISD::IndirectAddr) {
473 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000474 const SDValue Op0 = N.getOperand(0);
475 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000476
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000477 if (Op0.getOpcode() == SPUISD::Hi
478 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000479 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000480 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000481 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000482 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000483 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
484 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000485 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000486
487 if (isa<ConstantSDNode>(Op1)) {
488 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000489 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000490 idxOp = Op0;
491 } else if (isa<ConstantSDNode>(Op0)) {
492 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000493 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000494 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000495 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000496
497 if (offset >= minOffset && offset <= maxOffset) {
498 Base = CurDAG->getTargetConstant(offset, PtrTy);
499 Index = idxOp;
500 return true;
501 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000502 }
Scott Michel053c1da2008-01-29 02:16:57 +0000503 } else if (Opc == SPUISD::AFormAddr) {
504 Base = CurDAG->getTargetConstant(0, N.getValueType());
505 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000506 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000507 } else if (Opc == SPUISD::LDRESULT) {
508 Base = CurDAG->getTargetConstant(0, N.getValueType());
509 Index = N;
510 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000511 } else if (Opc == ISD::Register
512 ||Opc == ISD::CopyFromReg
Kalle Raiskilabc2697c2010-08-04 13:59:48 +0000513 ||Opc == ISD::UNDEF
514 ||Opc == ISD::Constant) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000515 unsigned OpOpc = Op->getOpcode();
Scott Michel9c0c6b22008-11-21 02:56:16 +0000516
517 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
518 // Direct load/store without getelementptr
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000519 SDValue Offs;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000520
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000521 Offs = ((OpOpc == ISD::STORE) ? Op->getOperand(3) : Op->getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000522
523 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
524 if (Offs.getOpcode() == ISD::UNDEF)
525 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
526
527 Base = Offs;
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000528 Index = N;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000529 return true;
530 }
Scott Michelaedc6372008-12-10 00:15:19 +0000531 } else {
532 /* If otherwise unadorned, default to D-form address with 0 offset: */
533 if (Opc == ISD::CopyFromReg) {
Scott Michel19c10e62009-01-26 03:37:41 +0000534 Index = N.getOperand(1);
Scott Michelaedc6372008-12-10 00:15:19 +0000535 } else {
Scott Michel19c10e62009-01-26 03:37:41 +0000536 Index = N;
Scott Michelaedc6372008-12-10 00:15:19 +0000537 }
538
539 Base = CurDAG->getTargetConstant(0, Index.getValueType());
540 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000541 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000542 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000543
Scott Michel266bc8f2007-12-04 22:23:35 +0000544 return false;
545}
546
547/*!
548 \arg Op The ISD instruction operand
549 \arg N The address operand
550 \arg Base The base pointer operand
551 \arg Index The offset/index operand
552
Scott Michel9c0c6b22008-11-21 02:56:16 +0000553 If the address \a N can be expressed as an A-form or D-form address, returns
554 false. Otherwise, creates two operands, Base and Index that will become the
555 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000556*/
557bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000558SPUDAGToDAGISel::SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000559 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000560 if (!SelectAFormAddr(Op, N, Base, Index)
561 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000562 // If the address is neither A-form or D-form, punt and use an X-form
563 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000564 Base = N.getOperand(1);
565 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000566 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000567 }
568
569 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000570}
571
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000572/*!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000573 Utility function to use with COPY_TO_REGCLASS instructions. Returns a SDValue
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000574 to be used as the last parameter of a
575CurDAG->getMachineNode(COPY_TO_REGCLASS,..., ) function call
576 \arg VT the value type for which we want a register class
577*/
578SDValue SPUDAGToDAGISel::getRC( MVT VT ) {
579 switch( VT.SimpleTy ) {
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000580 case MVT::i8:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000581 return CurDAG->getTargetConstant(SPU::R8CRegClass.getID(), MVT::i32);
582 break;
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000583 case MVT::i16:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000584 return CurDAG->getTargetConstant(SPU::R16CRegClass.getID(), MVT::i32);
585 break;
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000586 case MVT::i32:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000587 return CurDAG->getTargetConstant(SPU::R32CRegClass.getID(), MVT::i32);
588 break;
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000589 case MVT::f32:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000590 return CurDAG->getTargetConstant(SPU::R32FPRegClass.getID(), MVT::i32);
591 break;
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000592 case MVT::i64:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000593 return CurDAG->getTargetConstant(SPU::R64CRegClass.getID(), MVT::i32);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000594 break;
Kalle Raiskila11edd0c2010-11-29 09:36:26 +0000595 case MVT::i128:
596 return CurDAG->getTargetConstant(SPU::GPRCRegClass.getID(), MVT::i32);
597 break;
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000598 case MVT::v16i8:
599 case MVT::v8i16:
600 case MVT::v4i32:
601 case MVT::v4f32:
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000602 case MVT::v2i64:
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000603 case MVT::v2f64:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000604 return CurDAG->getTargetConstant(SPU::VECREGRegClass.getID(), MVT::i32);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000605 break;
606 default:
607 assert( false && "add a new case here" );
608 }
609 return SDValue();
610}
611
Scott Michel266bc8f2007-12-04 22:23:35 +0000612//! Convert the operand from a target-independent to a target-specific node
613/*!
614 */
615SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000616SPUDAGToDAGISel::Select(SDNode *N) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000617 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000618 int n_ops = -1;
Ted Kremenek584520e2011-01-23 17:05:06 +0000619 unsigned NewOpc = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000620 EVT OpVT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000621 SDValue Ops[8];
Dale Johannesened2eee62009-02-06 01:31:28 +0000622 DebugLoc dl = N->getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +0000623
Chris Lattnera8e76142010-02-23 05:30:43 +0000624 if (N->isMachineOpcode())
Scott Michel266bc8f2007-12-04 22:23:35 +0000625 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000626
627 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000628 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000629 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
630 SDValue Imm0 = CurDAG->getTargetConstant(0, N->getValueType(0));
Scott Michel266bc8f2007-12-04 22:23:35 +0000631
Scott Michel02d711b2008-12-30 23:28:25 +0000632 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000633 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000634 Ops[0] = TFI;
635 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000636 n_ops = 2;
637 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000638 NewOpc = SPU::Ar32;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000639 Ops[0] = CurDAG->getRegister(SPU::R1, N->getValueType(0));
Dan Gohman602b0c82009-09-25 18:54:59 +0000640 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILAr32, dl,
Kalle Raiskila7d170972010-12-09 16:17:31 +0000641 N->getValueType(0), TFI),
Dan Gohman602b0c82009-09-25 18:54:59 +0000642 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000643 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000644 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000646 // Catch the i64 constants that end up here. Note: The backend doesn't
647 // attempt to legalize the constant (it's useless because DAGCombiner
648 // will insert 64-bit constants and we can't stop it).
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000649 return SelectI64Constant(N, OpVT, N->getDebugLoc());
Scott Michel94bd57e2009-01-15 04:41:47 +0000650 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 && OpVT == MVT::i64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000652 SDValue Op0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000653 EVT Op0VT = Op0.getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +0000654 EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
655 Op0VT, (128 / Op0VT.getSizeInBits()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000656 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +0000657 OpVT, (128 / OpVT.getSizeInBits()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000658 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000659
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 switch (Op0VT.getSimpleVT().SimpleTy) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000661 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000662 report_fatal_error("CellSPU Select: Unhandled zero/any extend EVT");
Scott Michel94bd57e2009-01-15 04:41:47 +0000663 /*NOTREACHED*/
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 case MVT::i32:
665 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
666 CurDAG->getConstant(0x80808080, MVT::i32),
667 CurDAG->getConstant(0x00010203, MVT::i32),
668 CurDAG->getConstant(0x80808080, MVT::i32),
669 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000670 break;
671
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 case MVT::i16:
673 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
674 CurDAG->getConstant(0x80808080, MVT::i32),
675 CurDAG->getConstant(0x80800203, MVT::i32),
676 CurDAG->getConstant(0x80808080, MVT::i32),
677 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000678 break;
679
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 case MVT::i8:
681 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
682 CurDAG->getConstant(0x80808080, MVT::i32),
683 CurDAG->getConstant(0x80808003, MVT::i32),
684 CurDAG->getConstant(0x80808080, MVT::i32),
685 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000686 break;
Scott Michel58c58182008-01-17 20:38:41 +0000687 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000688
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000689 SDNode *shufMaskLoad = emitBuildVector(shufMask.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000690
Chris Lattnera8e76142010-02-23 05:30:43 +0000691 HandleSDNode PromoteScalar(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl,
692 Op0VecVT, Op0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000693
Chris Lattnera8e76142010-02-23 05:30:43 +0000694 SDValue PromScalar;
695 if (SDNode *N = SelectCode(PromoteScalar.getValue().getNode()))
696 PromScalar = SDValue(N, 0);
697 else
698 PromScalar = PromoteScalar.getValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000699
Scott Michel94bd57e2009-01-15 04:41:47 +0000700 SDValue zextShuffle =
Dale Johannesened2eee62009-02-06 01:31:28 +0000701 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000702 PromScalar, PromScalar,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000703 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000704
Chris Lattnera8e76142010-02-23 05:30:43 +0000705 HandleSDNode Dummy2(zextShuffle);
706 if (SDNode *N = SelectCode(Dummy2.getValue().getNode()))
707 zextShuffle = SDValue(N, 0);
708 else
709 zextShuffle = Dummy2.getValue();
710 HandleSDNode Dummy(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
711 zextShuffle));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000712
Chris Lattnera8e76142010-02-23 05:30:43 +0000713 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
714 SelectCode(Dummy.getValue().getNode());
715 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000717 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000718 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000719
Chris Lattnera8e76142010-02-23 05:30:43 +0000720 HandleSDNode Dummy(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
721 N->getOperand(0), N->getOperand(1),
722 SDValue(CGLoad, 0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000723
Chris Lattnera8e76142010-02-23 05:30:43 +0000724 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
725 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
726 return N;
727 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000729 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000730 emitBuildVector(getBorrowGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000731
Chris Lattnera8e76142010-02-23 05:30:43 +0000732 HandleSDNode Dummy(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
733 N->getOperand(0), N->getOperand(1),
734 SDValue(CGLoad, 0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000735
Chris Lattnera8e76142010-02-23 05:30:43 +0000736 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
737 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
738 return N;
739 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000741 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000742 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000743
Chris Lattnera8e76142010-02-23 05:30:43 +0000744 HandleSDNode Dummy(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
745 N->getOperand(0), N->getOperand(1),
746 SDValue(CGLoad, 0)));
747 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
748 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
749 return N;
750 return Dummy.getValue().getNode();
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000751 } else if (Opc == ISD::TRUNCATE) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000752 SDValue Op0 = N->getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000753 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 && OpVT == MVT::i32
755 && Op0.getValueType() == MVT::i64) {
Scott Michel9de57a92009-01-26 22:33:37 +0000756 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
757 //
758 // Take advantage of the fact that the upper 32 bits are in the
759 // i32 preferred slot and avoid shuffle gymnastics:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000760 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
761 if (CN != 0) {
762 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000763
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000764 if (shift_amt >= 32) {
765 SDNode *hi32 =
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000766 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
767 Op0.getOperand(0), getRC(MVT::i32));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000768
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000769 shift_amt -= 32;
770 if (shift_amt > 0) {
771 // Take care of the additional shift, if present:
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000773 unsigned Opc = SPU::ROTMAIr32_i32;
Scott Michel9de57a92009-01-26 22:33:37 +0000774
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000775 if (Op0.getOpcode() == ISD::SRL)
776 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000777
Dan Gohman602b0c82009-09-25 18:54:59 +0000778 hi32 = CurDAG->getMachineNode(Opc, dl, OpVT, SDValue(hi32, 0),
779 shift);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000780 }
781
782 return hi32;
783 }
784 }
785 }
Scott Michel02d711b2008-12-30 23:28:25 +0000786 } else if (Opc == ISD::SHL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000787 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000788 return SelectSHLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000789 } else if (Opc == ISD::SRL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000790 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000791 return SelectSRLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000792 } else if (Opc == ISD::SRA) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000793 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000794 return SelectSRAi64(N, OpVT);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000795 } else if (Opc == ISD::FNEG
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000797 DebugLoc dl = N->getDebugLoc();
Scott Michel7ea02ff2009-03-17 01:15:45 +0000798 // Check if the pattern is a special form of DFNMS:
799 // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000800 SDValue Op0 = N->getOperand(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000801 if (Op0.getOpcode() == ISD::FSUB) {
802 SDValue Op00 = Op0.getOperand(0);
803 if (Op00.getOpcode() == ISD::FMUL) {
804 unsigned Opc = SPU::DFNMSf64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 if (OpVT == MVT::v2f64)
Scott Michel7ea02ff2009-03-17 01:15:45 +0000806 Opc = SPU::DFNMSv2f64;
807
Dan Gohman602b0c82009-09-25 18:54:59 +0000808 return CurDAG->getMachineNode(Opc, dl, OpVT,
809 Op00.getOperand(0),
810 Op00.getOperand(1),
811 Op0.getOperand(1));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000812 }
813 }
814
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000816 SDNode *signMask = 0;
Scott Michela82d3f72009-03-17 16:45:16 +0000817 unsigned Opc = SPU::XORfneg64;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000818
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 if (OpVT == MVT::f64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000820 signMask = SelectI64Constant(negConst.getNode(), MVT::i64, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 } else if (OpVT == MVT::v2f64) {
Scott Michela82d3f72009-03-17 16:45:16 +0000822 Opc = SPU::XORfnegvec;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000823 signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 MVT::v2i64,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000825 negConst, negConst).getNode());
Scott Michel7ea02ff2009-03-17 01:15:45 +0000826 }
827
Dan Gohman602b0c82009-09-25 18:54:59 +0000828 return CurDAG->getMachineNode(Opc, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000829 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000830 } else if (Opc == ISD::FABS) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000831 if (OpVT == MVT::f64) {
832 SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
Dan Gohman602b0c82009-09-25 18:54:59 +0000833 return CurDAG->getMachineNode(SPU::ANDfabs64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000834 N->getOperand(0), SDValue(signMask, 0));
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 } else if (OpVT == MVT::v2f64) {
836 SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
837 SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000838 absConst, absConst);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000839 SDNode *signMask = emitBuildVector(absVec.getNode());
Dan Gohman602b0c82009-09-25 18:54:59 +0000840 return CurDAG->getMachineNode(SPU::ANDfabsvec, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000841 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000842 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000843 } else if (Opc == SPUISD::LDRESULT) {
844 // Custom select instructions for LDRESULT
Owen Andersone50ed302009-08-10 22:56:29 +0000845 EVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000846 SDValue Arg = N->getOperand(0);
847 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000848 SDNode *Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000849
Kalle Raiskila82581352010-10-01 09:20:01 +0000850 Result = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VT,
851 MVT::Other, Arg,
852 getRC( VT.getSimpleVT()), Chain);
Scott Michel266bc8f2007-12-04 22:23:35 +0000853 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000854
Scott Michel053c1da2008-01-29 02:16:57 +0000855 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000856 // Look at the operands: SelectCode() will catch the cases that aren't
857 // specifically handled here.
858 //
859 // SPUInstrInfo catches the following patterns:
860 // (SPUindirect (SPUhi ...), (SPUlo ...))
861 // (SPUindirect $sp, imm)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000862 EVT VT = N->getValueType(0);
Scott Michelf0569be2008-12-27 04:51:36 +0000863 SDValue Op0 = N->getOperand(0);
864 SDValue Op1 = N->getOperand(1);
865 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000866
Scott Michelf0569be2008-12-27 04:51:36 +0000867 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
868 || (Op0.getOpcode() == ISD::Register
869 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
870 && RN->getReg() != SPU::R1))) {
871 NewOpc = SPU::Ar32;
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000872 Ops[1] = Op1;
Scott Michel58c58182008-01-17 20:38:41 +0000873 if (Op1.getOpcode() == ISD::Constant) {
874 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000875 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000876 if (isInt<10>(CN->getSExtValue())) {
877 NewOpc = SPU::AIr32;
878 Ops[1] = Op1;
879 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000880 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILr32, dl,
881 N->getValueType(0),
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000882 Op1),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000883 0);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000884 }
Scott Michel58c58182008-01-17 20:38:41 +0000885 }
Scott Michelf0569be2008-12-27 04:51:36 +0000886 Ops[0] = Op0;
Scott Michelf0569be2008-12-27 04:51:36 +0000887 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000888 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000889 }
Scott Michel02d711b2008-12-30 23:28:25 +0000890
Scott Michel58c58182008-01-17 20:38:41 +0000891 if (n_ops > 0) {
892 if (N->hasOneUse())
893 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
894 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000895 return CurDAG->getMachineNode(NewOpc, dl, OpVT, Ops, n_ops);
Scott Michel58c58182008-01-17 20:38:41 +0000896 } else
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000897 return SelectCode(N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000898}
899
Scott Michel02d711b2008-12-30 23:28:25 +0000900/*!
901 * Emit the instruction sequence for i64 left shifts. The basic algorithm
902 * is to fill the bottom two word slots with zeros so that zeros are shifted
903 * in as the entire quadword is shifted left.
904 *
905 * \note This code could also be used to implement v2i64 shl.
906 *
907 * @param Op The shl operand
908 * @param OpVT Op's machine value value type (doesn't need to be passed, but
909 * makes life easier.)
910 * @return The SDNode with the entire instruction sequence
911 */
912SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000913SPUDAGToDAGISel::SelectSHLi64(SDNode *N, EVT OpVT) {
914 SDValue Op0 = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000915 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +0000916 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000917 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000918 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000919 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
920 SDValue SelMaskVal;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000921 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000922
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000923 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
924 Op0, getRC(MVT::v2i64) );
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
Dan Gohman602b0c82009-09-25 18:54:59 +0000926 SelMask = CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
927 ZeroFill = CurDAG->getMachineNode(SPU::ILv2i64, dl, VecVT,
928 CurDAG->getTargetConstant(0, OpVT));
929 VecOp0 = CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
930 SDValue(ZeroFill, 0),
931 SDValue(VecOp0, 0),
932 SDValue(SelMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000933
934 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
935 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
936 unsigned bits = unsigned(CN->getZExtValue()) & 7;
937
938 if (bytes > 0) {
939 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000940 CurDAG->getMachineNode(SPU::SHLQBYIv2i64, dl, VecVT,
941 SDValue(VecOp0, 0),
942 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000943 }
944
945 if (bits > 0) {
946 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000947 CurDAG->getMachineNode(SPU::SHLQBIIv2i64, dl, VecVT,
948 SDValue((Shift != 0 ? Shift : VecOp0), 0),
949 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000950 }
951 } else {
952 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +0000953 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
954 ShiftAmt,
955 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000956 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +0000957 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
958 ShiftAmt,
959 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000960 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000961 CurDAG->getMachineNode(SPU::SHLQBYv2i64, dl, VecVT,
962 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000963 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000964 CurDAG->getMachineNode(SPU::SHLQBIv2i64, dl, VecVT,
965 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000966 }
967
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000968 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000969 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +0000970}
971
972/*!
973 * Emit the instruction sequence for i64 logical right shifts.
974 *
975 * @param Op The shl operand
976 * @param OpVT Op's machine value value type (doesn't need to be passed, but
977 * makes life easier.)
978 * @return The SDNode with the entire instruction sequence
979 */
980SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000981SPUDAGToDAGISel::SelectSRLi64(SDNode *N, EVT OpVT) {
982 SDValue Op0 = N->getOperand(0);
Owen Anderson23b9b192009-08-12 00:36:31 +0000983 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
984 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000985 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000986 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000987 SDNode *VecOp0, *Shift = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000988 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000989
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000990 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
991 Op0, getRC(MVT::v2i64) );
Scott Michel02d711b2008-12-30 23:28:25 +0000992
993 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
994 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
995 unsigned bits = unsigned(CN->getZExtValue()) & 7;
996
997 if (bytes > 0) {
998 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000999 CurDAG->getMachineNode(SPU::ROTQMBYIv2i64, dl, VecVT,
1000 SDValue(VecOp0, 0),
1001 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001002 }
1003
1004 if (bits > 0) {
1005 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001006 CurDAG->getMachineNode(SPU::ROTQMBIIv2i64, dl, VecVT,
1007 SDValue((Shift != 0 ? Shift : VecOp0), 0),
1008 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001009 }
1010 } else {
1011 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +00001012 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1013 ShiftAmt,
1014 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001015 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +00001016 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1017 ShiftAmt,
1018 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001019
1020 // Ensure that the shift amounts are negated!
Dan Gohman602b0c82009-09-25 18:54:59 +00001021 Bytes = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1022 SDValue(Bytes, 0),
1023 CurDAG->getTargetConstant(0, ShiftAmtVT));
1024
1025 Bits = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1026 SDValue(Bits, 0),
Scott Michel02d711b2008-12-30 23:28:25 +00001027 CurDAG->getTargetConstant(0, ShiftAmtVT));
1028
Scott Michel02d711b2008-12-30 23:28:25 +00001029 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001030 CurDAG->getMachineNode(SPU::ROTQMBYv2i64, dl, VecVT,
1031 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001032 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001033 CurDAG->getMachineNode(SPU::ROTQMBIv2i64, dl, VecVT,
1034 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001035 }
1036
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001037 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001038 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001039}
1040
1041/*!
1042 * Emit the instruction sequence for i64 arithmetic right shifts.
1043 *
1044 * @param Op The shl operand
1045 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1046 * makes life easier.)
1047 * @return The SDNode with the entire instruction sequence
1048 */
1049SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001050SPUDAGToDAGISel::SelectSRAi64(SDNode *N, EVT OpVT) {
Scott Michel02d711b2008-12-30 23:28:25 +00001051 // Promote Op0 to vector
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001052 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +00001053 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001054 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001055 EVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001056 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +00001057
1058 SDNode *VecOp0 =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001059 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001060 VecVT, N->getOperand(0), getRC(MVT::v2i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001061
1062 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1063 SDNode *SignRot =
Dan Gohman602b0c82009-09-25 18:54:59 +00001064 CurDAG->getMachineNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
1065 SDValue(VecOp0, 0), SignRotAmt);
Scott Michel02d711b2008-12-30 23:28:25 +00001066 SDNode *UpperHalfSign =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001067 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001068 MVT::i32, SDValue(SignRot, 0), getRC(MVT::i32));
Scott Michel02d711b2008-12-30 23:28:25 +00001069
1070 SDNode *UpperHalfSignMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001071 CurDAG->getMachineNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001072 SDNode *UpperLowerMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001073 CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT,
1074 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
Scott Michel02d711b2008-12-30 23:28:25 +00001075 SDNode *UpperLowerSelect =
Dan Gohman602b0c82009-09-25 18:54:59 +00001076 CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
1077 SDValue(UpperHalfSignMask, 0),
1078 SDValue(VecOp0, 0),
1079 SDValue(UpperLowerMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001080
1081 SDNode *Shift = 0;
1082
1083 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1084 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1085 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1086
1087 if (bytes > 0) {
1088 bytes = 31 - bytes;
1089 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001090 CurDAG->getMachineNode(SPU::ROTQBYIv2i64, dl, VecVT,
1091 SDValue(UpperLowerSelect, 0),
1092 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001093 }
1094
1095 if (bits > 0) {
1096 bits = 8 - bits;
1097 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001098 CurDAG->getMachineNode(SPU::ROTQBIIv2i64, dl, VecVT,
1099 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1100 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001101 }
1102 } else {
1103 SDNode *NegShift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001104 CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1105 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001106
1107 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001108 CurDAG->getMachineNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
1109 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001110 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001111 CurDAG->getMachineNode(SPU::ROTQBIv2i64, dl, VecVT,
1112 SDValue(Shift, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001113 }
1114
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001115 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001116 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001117}
1118
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001119/*!
1120 Do the necessary magic necessary to load a i64 constant
1121 */
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001122SDNode *SPUDAGToDAGISel::SelectI64Constant(SDNode *N, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001123 DebugLoc dl) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001124 ConstantSDNode *CN = cast<ConstantSDNode>(N);
Scott Michel7ea02ff2009-03-17 01:15:45 +00001125 return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
1126}
1127
Owen Andersone50ed302009-08-10 22:56:29 +00001128SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001129 DebugLoc dl) {
Owen Anderson23b9b192009-08-12 00:36:31 +00001130 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001131 SDValue i64vec =
Scott Michel7ea02ff2009-03-17 01:15:45 +00001132 SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001133
1134 // Here's where it gets interesting, because we have to parse out the
1135 // subtree handed back in i64vec:
1136
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001137 if (i64vec.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001138 // The degenerate case where the upper and lower bits in the splat are
1139 // identical:
1140 SDValue Op0 = i64vec.getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001141
Scott Michel9de57a92009-01-26 22:33:37 +00001142 ReplaceUses(i64vec, Op0);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001143 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1144 SDValue(emitBuildVector(Op0.getNode()), 0),
1145 getRC(MVT::i64));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001146 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1147 SDValue lhs = i64vec.getOperand(0);
1148 SDValue rhs = i64vec.getOperand(1);
1149 SDValue shufmask = i64vec.getOperand(2);
1150
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001151 if (lhs.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001152 ReplaceUses(lhs, lhs.getOperand(0));
1153 lhs = lhs.getOperand(0);
1154 }
1155
1156 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1157 ? lhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001158 : emitBuildVector(lhs.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001159
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001160 if (rhs.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001161 ReplaceUses(rhs, rhs.getOperand(0));
1162 rhs = rhs.getOperand(0);
1163 }
1164
1165 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1166 ? rhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001167 : emitBuildVector(rhs.getNode()));
Scott Michel9de57a92009-01-26 22:33:37 +00001168
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001169 if (shufmask.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001170 ReplaceUses(shufmask, shufmask.getOperand(0));
1171 shufmask = shufmask.getOperand(0);
1172 }
1173
1174 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1175 ? shufmask.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001176 : emitBuildVector(shufmask.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001177
Chris Lattnera8e76142010-02-23 05:30:43 +00001178 SDValue shufNode =
1179 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001180 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
Chris Lattnera8e76142010-02-23 05:30:43 +00001181 SDValue(shufMaskNode, 0));
1182 HandleSDNode Dummy(shufNode);
1183 SDNode *SN = SelectCode(Dummy.getValue().getNode());
1184 if (SN == 0) SN = Dummy.getValue().getNode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001185
1186 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001187 OpVT, SDValue(SN, 0), getRC(MVT::i64));
Scott Michel7ea02ff2009-03-17 01:15:45 +00001188 } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001189 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1190 SDValue(emitBuildVector(i64vec.getNode()), 0),
1191 getRC(MVT::i64));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001192 } else {
Chris Lattner75361b62010-04-07 22:58:41 +00001193 report_fatal_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
Torok Edwindac237e2009-07-08 20:53:28 +00001194 "condition");
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001195 }
1196}
1197
Scott Michel02d711b2008-12-30 23:28:25 +00001198/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001199/// SPU-specific DAG, ready for instruction scheduling.
1200///
1201FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1202 return new SPUDAGToDAGISel(TM);
1203}