blob: 38a13d1874ce92a7e8b6d1e6068639c3d50c7a86 [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"
18#include "SPUFrameInfo.h"
Scott Michel203b2d62008-04-30 00:30:08 +000019#include "SPURegisterNames.h"
Scott Michel94bd57e2009-01-15 04:41:47 +000020#include "SPUTargetMachine.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000021#include "llvm/CodeGen/MachineConstantPool.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineFunction.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
Scott Michel94bd57e2009-01-15 04:41:47 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000027#include "llvm/Target/TargetOptions.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/Constants.h"
30#include "llvm/GlobalValue.h"
31#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000032#include "llvm/LLVMContext.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000033#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000035#include "llvm/Support/MathExtras.h"
36#include "llvm/Support/Compiler.h"
Torok Edwindac237e2009-07-08 20:53:28 +000037#include "llvm/Support/raw_ostream.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000038
39using namespace llvm;
40
41namespace {
42 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
43 bool
Scott Michel266bc8f2007-12-04 22:23:35 +000044 isI32IntS10Immediate(ConstantSDNode *CN)
45 {
Benjamin Kramer7e09deb2010-03-29 19:07:58 +000046 return isInt<10>(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000047 }
48
Scott Michel504c3692007-12-17 22:32:34 +000049 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
50 bool
51 isI32IntU10Immediate(ConstantSDNode *CN)
52 {
Benjamin Kramer34247a02010-03-29 21:13:41 +000053 return isUInt<10>(CN->getSExtValue());
Scott Michel504c3692007-12-17 22:32:34 +000054 }
55
Scott Michel266bc8f2007-12-04 22:23:35 +000056 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
57 bool
58 isI16IntS10Immediate(ConstantSDNode *CN)
59 {
Benjamin Kramer7e09deb2010-03-29 19:07:58 +000060 return isInt<10>(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000061 }
62
Scott Michelec2a08f2007-12-15 00:38:50 +000063 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
64 bool
65 isI16IntU10Immediate(ConstantSDNode *CN)
66 {
Benjamin Kramer34247a02010-03-29 21:13:41 +000067 return isUInt<10>((short) CN->getZExtValue());
Scott Michelec2a08f2007-12-15 00:38:50 +000068 }
69
Scott Michel266bc8f2007-12-04 22:23:35 +000070 //! ConstantSDNode predicate for signed 16-bit values
71 /*!
72 \arg CN The constant SelectionDAG node holding the value
73 \arg Imm The returned 16-bit value, if returning true
74
75 This predicate tests the value in \a CN to see whether it can be
76 represented as a 16-bit, sign-extended quantity. Returns true if
77 this is the case.
78 */
79 bool
80 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
81 {
Owen Andersone50ed302009-08-10 22:56:29 +000082 EVT vt = CN->getValueType(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000083 Imm = (short) CN->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000084 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +000085 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +000086 } else if (vt == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000087 int32_t i_val = (int32_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +000088 short s_val = (short) i_val;
89 return i_val == s_val;
90 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000091 int64_t i_val = (int64_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +000092 short s_val = (short) i_val;
93 return i_val == s_val;
94 }
95
96 return false;
97 }
98
Scott Michel266bc8f2007-12-04 22:23:35 +000099 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
100 static bool
101 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
102 {
Owen Andersone50ed302009-08-10 22:56:29 +0000103 EVT vt = FPN->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000105 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000106 int sval = (int) ((val << 16) >> 16);
107 Imm = (short) val;
108 return val == sval;
109 }
110
111 return false;
112 }
113
Scott Michel7ea02ff2009-03-17 01:15:45 +0000114 //! Generate the carry-generate shuffle mask.
115 SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
116 SmallVector<SDValue, 16 > ShufBytes;
Dan Gohman844731a2008-05-13 00:00:25 +0000117
Scott Michel7ea02ff2009-03-17 01:15:45 +0000118 // Create the shuffle mask for "rotating" the borrow up one register slot
119 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
121 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
122 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
123 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +0000124
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000126 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000127 }
Scott Michel02d711b2008-12-30 23:28:25 +0000128
Scott Michel7ea02ff2009-03-17 01:15:45 +0000129 //! Generate the borrow-generate shuffle mask
130 SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
131 SmallVector<SDValue, 16 > ShufBytes;
132
133 // Create the shuffle mask for "rotating" the borrow up one register slot
134 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
136 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
137 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
138 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000139
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000141 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000142 }
143
Scott Michel7ea02ff2009-03-17 01:15:45 +0000144 //===------------------------------------------------------------------===//
145 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
146 /// instructions for SelectionDAG operations.
147 ///
148 class SPUDAGToDAGISel :
149 public SelectionDAGISel
150 {
Dan Gohmand858e902010-04-17 15:26:15 +0000151 const SPUTargetMachine &TM;
152 const SPUTargetLowering &SPUtli;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000153 unsigned GlobalBaseReg;
Scott Michel02d711b2008-12-30 23:28:25 +0000154
Scott Michel7ea02ff2009-03-17 01:15:45 +0000155 public:
156 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
157 SelectionDAGISel(tm),
158 TM(tm),
159 SPUtli(*tm.getTargetLowering())
160 { }
161
Dan Gohmanad2afc22009-07-31 18:16:33 +0000162 virtual bool runOnMachineFunction(MachineFunction &MF) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000163 // Make sure we re-emit a set of the global base reg if necessary
164 GlobalBaseReg = 0;
Dan Gohmanad2afc22009-07-31 18:16:33 +0000165 SelectionDAGISel::runOnMachineFunction(MF);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000166 return true;
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000167 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000168
Scott Michel7ea02ff2009-03-17 01:15:45 +0000169 /// getI32Imm - Return a target constant with the specified value, of type
170 /// i32.
171 inline SDValue getI32Imm(uint32_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 return CurDAG->getTargetConstant(Imm, MVT::i32);
Scott Michel94bd57e2009-01-15 04:41:47 +0000173 }
174
Scott Michel7ea02ff2009-03-17 01:15:45 +0000175 /// getSmallIPtrImm - Return a target constant of pointer type.
176 inline SDValue getSmallIPtrImm(unsigned Imm) {
177 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Chris Lattner17aa6802010-09-04 18:12:00 +0000178 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000179
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000180 SDNode *emitBuildVector(SDNode *bvNode) {
181 EVT vecVT = bvNode->getValueType(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000182 DebugLoc dl = bvNode->getDebugLoc();
183
184 // Check to see if this vector can be represented as a CellSPU immediate
185 // constant by invoking all of the instruction selection predicates:
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 if (((vecVT == MVT::v8i16) &&
187 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
188 ((vecVT == MVT::v4i32) &&
189 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
190 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
191 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
Scott Michel7ea02ff2009-03-17 01:15:45 +0000192 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 ((vecVT == MVT::v2i64) &&
194 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
195 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
Chris Lattnera8e76142010-02-23 05:30:43 +0000196 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)))) {
197 HandleSDNode Dummy(SDValue(bvNode, 0));
198 if (SDNode *N = Select(bvNode))
199 return N;
200 return Dummy.getValue().getNode();
201 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000202
203 // No, need to emit a constant pool spill:
204 std::vector<Constant*> CV;
205
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000206 for (size_t i = 0; i < bvNode->getNumOperands(); ++i) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000207 ConstantSDNode *V = cast<ConstantSDNode > (bvNode->getOperand(i));
Chris Lattnera8e76142010-02-23 05:30:43 +0000208 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000209 }
210
Dan Gohman46510a72010-04-15 01:51:59 +0000211 const Constant *CP = ConstantVector::get(CV);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000212 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
213 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
214 SDValue CGPoolOffset =
Dan Gohmand858e902010-04-17 15:26:15 +0000215 SPU::LowerConstantPool(CPIdx, *CurDAG, TM);
Chris Lattnera8e76142010-02-23 05:30:43 +0000216
217 HandleSDNode Dummy(CurDAG->getLoad(vecVT, dl,
218 CurDAG->getEntryNode(), CGPoolOffset,
Chris Lattnere8639032010-09-21 06:22:23 +0000219 MachinePointerInfo::getConstantPool(),
Chris Lattnera8e76142010-02-23 05:30:43 +0000220 false, false, Alignment));
221 CurDAG->ReplaceAllUsesWith(SDValue(bvNode, 0), Dummy.getValue());
222 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
223 return N;
224 return Dummy.getValue().getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000225 }
Scott Michel02d711b2008-12-30 23:28:25 +0000226
Scott Michel7ea02ff2009-03-17 01:15:45 +0000227 /// Select - Convert the specified operand from a target-independent to a
228 /// target-specific node if it hasn't already been changed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000229 SDNode *Select(SDNode *N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000230
Scott Michel7ea02ff2009-03-17 01:15:45 +0000231 //! Emit the instruction sequence for i64 shl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000232 SDNode *SelectSHLi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000233
Scott Michel7ea02ff2009-03-17 01:15:45 +0000234 //! Emit the instruction sequence for i64 srl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000235 SDNode *SelectSRLi64(SDNode *N, EVT OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000236
Scott Michel7ea02ff2009-03-17 01:15:45 +0000237 //! Emit the instruction sequence for i64 sra
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000238 SDNode *SelectSRAi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000239
Scott Michel7ea02ff2009-03-17 01:15:45 +0000240 //! Emit the necessary sequence for loading i64 constants:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000241 SDNode *SelectI64Constant(SDNode *N, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000242
243 //! Alternate instruction emit sequence for loading i64 constants
Owen Andersone50ed302009-08-10 22:56:29 +0000244 SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000245
246 //! Returns true if the address N is an A-form (local store) address
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000247 bool SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000248 SDValue &Index);
249
250 //! D-form address predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000251 bool SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000252 SDValue &Index);
253
254 /// Alternate D-form address using i7 offset predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000255 bool SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000256 SDValue &Base);
257
258 /// D-form address selection workhorse
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000259 bool DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000260 SDValue &Base, int minOffset, int maxOffset);
261
262 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000263 bool SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000264 SDValue &Index);
265
266 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
267 /// inline asm expressions.
268 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
269 char ConstraintCode,
270 std::vector<SDValue> &OutOps) {
271 SDValue Op0, Op1;
272 switch (ConstraintCode) {
273 default: return true;
274 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000275 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
276 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1))
277 SelectXFormAddr(Op.getNode(), Op, Op0, Op1);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000278 break;
279 case 'o': // offsetable
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000280 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
281 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1)) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000282 Op0 = Op;
283 Op1 = getSmallIPtrImm(0);
284 }
285 break;
286 case 'v': // not offsetable
287#if 1
Torok Edwinc23197a2009-07-14 16:55:14 +0000288 llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
Scott Michel7ea02ff2009-03-17 01:15:45 +0000289#else
290 SelectAddrIdxOnly(Op, Op, Op0, Op1);
291#endif
292 break;
293 }
294
295 OutOps.push_back(Op0);
296 OutOps.push_back(Op1);
297 return false;
298 }
299
Scott Michel7ea02ff2009-03-17 01:15:45 +0000300 virtual const char *getPassName() const {
301 return "Cell SPU DAG->DAG Pattern Instruction Selection";
302 }
303
304 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
305 /// this target when scheduling the DAG.
306 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
307 const TargetInstrInfo *II = TM.getInstrInfo();
308 assert(II && "No InstrInfo?");
309 return new SPUHazardRecognizer(*II);
310 }
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000311
312 private:
313 SDValue getRC( MVT );
Scott Michel7ea02ff2009-03-17 01:15:45 +0000314
315 // Include the pieces autogenerated from the target description.
Scott Michel266bc8f2007-12-04 22:23:35 +0000316#include "SPUGenDAGISel.inc"
Scott Michel7ea02ff2009-03-17 01:15:45 +0000317 };
Dan Gohman844731a2008-05-13 00:00:25 +0000318}
319
Scott Michel266bc8f2007-12-04 22:23:35 +0000320/*!
Scott Michel9de57a92009-01-26 22:33:37 +0000321 \arg Op The ISD instruction operand
Scott Michel266bc8f2007-12-04 22:23:35 +0000322 \arg N The address to be tested
323 \arg Base The base address
324 \arg Index The base address index
325 */
326bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000327SPUDAGToDAGISel::SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000328 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000329 // These match the addr256k operand type:
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 EVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000331 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000332
333 switch (N.getOpcode()) {
334 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000335 case ISD::ConstantPool:
336 case ISD::GlobalAddress:
Chris Lattner75361b62010-04-07 22:58:41 +0000337 report_fatal_error("SPU SelectAFormAddr: Constant/Pool/Global not lowered.");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000338 /*NOTREACHED*/
339
Scott Michel053c1da2008-01-29 02:16:57 +0000340 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000341 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000342 case ISD::TargetJumpTable:
Chris Lattner75361b62010-04-07 22:58:41 +0000343 report_fatal_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
Torok Edwindac237e2009-07-08 20:53:28 +0000344 "not wrapped as A-form address.");
Scott Michel053c1da2008-01-29 02:16:57 +0000345 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000346
Scott Michel02d711b2008-12-30 23:28:25 +0000347 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000348 // Just load from memory if there's only a single use of the location,
349 // otherwise, this will get handled below with D-form offset addresses
350 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000351 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000352 switch (Op0.getOpcode()) {
353 case ISD::TargetConstantPool:
354 case ISD::TargetJumpTable:
355 Base = Op0;
356 Index = Zero;
357 return true;
358
359 case ISD::TargetGlobalAddress: {
360 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
Dan Gohman46510a72010-04-15 01:51:59 +0000361 const GlobalValue *GV = GSDN->getGlobal();
Scott Michel053c1da2008-01-29 02:16:57 +0000362 if (GV->getAlignment() == 16) {
363 Base = Op0;
364 Index = Zero;
365 return true;
366 }
367 break;
368 }
369 }
370 }
371 break;
372 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000373 return false;
374}
375
Scott Michel02d711b2008-12-30 23:28:25 +0000376bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000377SPUDAGToDAGISel::SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Dan Gohman475871a2008-07-27 21:46:04 +0000378 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000379 const int minDForm2Offset = -(1 << 7);
380 const int maxDForm2Offset = (1 << 7) - 1;
381 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
382 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000383}
384
Scott Michel266bc8f2007-12-04 22:23:35 +0000385/*!
386 \arg Op The ISD instruction (ignored)
387 \arg N The address to be tested
388 \arg Base Base address register/pointer
389 \arg Index Base address index
390
391 Examine the input address by a base register plus a signed 10-bit
392 displacement, [r+I10] (D-form address).
393
394 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000395 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000396*/
397bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000398SPUDAGToDAGISel::SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000399 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000400 return DFormAddressPredicate(Op, N, Base, Index,
Scott Michel9c0c6b22008-11-21 02:56:16 +0000401 SPUFrameInfo::minFrameOffset(),
402 SPUFrameInfo::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000403}
404
405bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000406SPUDAGToDAGISel::DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000407 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000408 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000409 unsigned Opc = N.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +0000410 EVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000411
Scott Michel053c1da2008-01-29 02:16:57 +0000412 if (Opc == ISD::FrameIndex) {
413 // Stack frame index must be less than 512 (divided by 16):
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000414 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(N);
Scott Michel203b2d62008-04-30 00:30:08 +0000415 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000416 DEBUG(errs() << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000417 << FI << "\n");
418 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000419 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000420 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000421 return true;
422 }
423 } else if (Opc == ISD::ADD) {
424 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000425 const SDValue Op0 = N.getOperand(0);
426 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000427
Scott Michel053c1da2008-01-29 02:16:57 +0000428 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
429 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
430 Base = CurDAG->getTargetConstant(0, PtrTy);
431 Index = N;
432 return true;
433 } else if (Op1.getOpcode() == ISD::Constant
434 || Op1.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000435 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000436 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000437
Scott Michel053c1da2008-01-29 02:16:57 +0000438 if (Op0.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000439 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op0);
Scott Michel203b2d62008-04-30 00:30:08 +0000440 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000441 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000442 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000443
Scott Michel203b2d62008-04-30 00:30:08 +0000444 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000445 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000446 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000447 return true;
448 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000449 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000450 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000451 Index = Op0;
452 return true;
453 }
454 } else if (Op0.getOpcode() == ISD::Constant
455 || Op0.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000456 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000457 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000458
459 if (Op1.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000460 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op1);
Scott Michel203b2d62008-04-30 00:30:08 +0000461 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000462 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000463 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000464
Scott Michel203b2d62008-04-30 00:30:08 +0000465 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000466 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000467 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000468 return true;
469 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000470 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000471 Base = CurDAG->getTargetConstant(offset, PtrTy);
472 Index = Op1;
473 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000474 }
Scott Michel053c1da2008-01-29 02:16:57 +0000475 }
476 } else if (Opc == SPUISD::IndirectAddr) {
477 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000478 const SDValue Op0 = N.getOperand(0);
479 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000480
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000481 if (Op0.getOpcode() == SPUISD::Hi
482 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000483 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000484 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000485 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000486 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000487 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
488 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000489 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000490
491 if (isa<ConstantSDNode>(Op1)) {
492 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000493 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000494 idxOp = Op0;
495 } else if (isa<ConstantSDNode>(Op0)) {
496 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000497 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000498 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000499 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000500
501 if (offset >= minOffset && offset <= maxOffset) {
502 Base = CurDAG->getTargetConstant(offset, PtrTy);
503 Index = idxOp;
504 return true;
505 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000506 }
Scott Michel053c1da2008-01-29 02:16:57 +0000507 } else if (Opc == SPUISD::AFormAddr) {
508 Base = CurDAG->getTargetConstant(0, N.getValueType());
509 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000510 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000511 } else if (Opc == SPUISD::LDRESULT) {
512 Base = CurDAG->getTargetConstant(0, N.getValueType());
513 Index = N;
514 return true;
Kalle Raiskilac6166c62010-06-09 08:29:41 +0000515 } else if (Opc == ISD::Register
516 ||Opc == ISD::CopyFromReg
Kalle Raiskilabc2697c2010-08-04 13:59:48 +0000517 ||Opc == ISD::UNDEF
518 ||Opc == ISD::Constant) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000519 unsigned OpOpc = Op->getOpcode();
Scott Michel9c0c6b22008-11-21 02:56:16 +0000520
521 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
522 // Direct load/store without getelementptr
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000523 SDValue Offs;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000524
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000525 Offs = ((OpOpc == ISD::STORE) ? Op->getOperand(3) : Op->getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000526
527 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
528 if (Offs.getOpcode() == ISD::UNDEF)
529 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
530
531 Base = Offs;
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000532 Index = N;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000533 return true;
534 }
Scott Michelaedc6372008-12-10 00:15:19 +0000535 } else {
536 /* If otherwise unadorned, default to D-form address with 0 offset: */
537 if (Opc == ISD::CopyFromReg) {
Scott Michel19c10e62009-01-26 03:37:41 +0000538 Index = N.getOperand(1);
Scott Michelaedc6372008-12-10 00:15:19 +0000539 } else {
Scott Michel19c10e62009-01-26 03:37:41 +0000540 Index = N;
Scott Michelaedc6372008-12-10 00:15:19 +0000541 }
542
543 Base = CurDAG->getTargetConstant(0, Index.getValueType());
544 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000545 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000546 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000547
Scott Michel266bc8f2007-12-04 22:23:35 +0000548 return false;
549}
550
551/*!
552 \arg Op The ISD instruction operand
553 \arg N The address operand
554 \arg Base The base pointer operand
555 \arg Index The offset/index operand
556
Scott Michel9c0c6b22008-11-21 02:56:16 +0000557 If the address \a N can be expressed as an A-form or D-form address, returns
558 false. Otherwise, creates two operands, Base and Index that will become the
559 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000560*/
561bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000562SPUDAGToDAGISel::SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000563 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000564 if (!SelectAFormAddr(Op, N, Base, Index)
565 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000566 // If the address is neither A-form or D-form, punt and use an X-form
567 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000568 Base = N.getOperand(1);
569 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000570 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000571 }
572
573 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000574}
575
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000576/*!
577 Utility function to use with COPY_TO_REGCLASS instructions. Returns a SDValue
578 to be used as the last parameter of a
579CurDAG->getMachineNode(COPY_TO_REGCLASS,..., ) function call
580 \arg VT the value type for which we want a register class
581*/
582SDValue SPUDAGToDAGISel::getRC( MVT VT ) {
583 switch( VT.SimpleTy ) {
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000584 case MVT::i8:
585 return CurDAG->getTargetConstant(SPU::R8CRegClass.getID(), MVT::i32);
586 break;
587 case MVT::i16:
588 return CurDAG->getTargetConstant(SPU::R16CRegClass.getID(), MVT::i32);
589 break;
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000590 case MVT::i32:
591 return CurDAG->getTargetConstant(SPU::R32CRegClass.getID(), MVT::i32);
592 break;
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000593 case MVT::f32:
594 return CurDAG->getTargetConstant(SPU::R32FPRegClass.getID(), MVT::i32);
595 break;
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000596 case MVT::i64:
597 return CurDAG->getTargetConstant(SPU::R64CRegClass.getID(), MVT::i32);
598 break;
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000599 case MVT::v16i8:
600 case MVT::v8i16:
601 case MVT::v4i32:
602 case MVT::v4f32:
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000603 case MVT::v2i64:
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000604 case MVT::v2f64:
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000605 return CurDAG->getTargetConstant(SPU::VECREGRegClass.getID(), MVT::i32);
606 break;
607 default:
608 assert( false && "add a new case here" );
609 }
610 return SDValue();
611}
612
Scott Michel266bc8f2007-12-04 22:23:35 +0000613//! Convert the operand from a target-independent to a target-specific node
614/*!
615 */
616SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000617SPUDAGToDAGISel::Select(SDNode *N) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000618 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000619 int n_ops = -1;
620 unsigned NewOpc;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000621 EVT OpVT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000622 SDValue Ops[8];
Dale Johannesened2eee62009-02-06 01:31:28 +0000623 DebugLoc dl = N->getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +0000624
Chris Lattnera8e76142010-02-23 05:30:43 +0000625 if (N->isMachineOpcode())
Scott Michel266bc8f2007-12-04 22:23:35 +0000626 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000627
628 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000629 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000630 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
631 SDValue Imm0 = CurDAG->getTargetConstant(0, N->getValueType(0));
Scott Michel266bc8f2007-12-04 22:23:35 +0000632
Scott Michel02d711b2008-12-30 23:28:25 +0000633 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000634 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000635 Ops[0] = TFI;
636 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000637 n_ops = 2;
638 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000639 NewOpc = SPU::Ar32;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000640 Ops[0] = CurDAG->getRegister(SPU::R1, N->getValueType(0));
Dan Gohman602b0c82009-09-25 18:54:59 +0000641 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILAr32, dl,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000642 N->getValueType(0), TFI, Imm0),
Dan Gohman602b0c82009-09-25 18:54:59 +0000643 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000644 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000645 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000647 // Catch the i64 constants that end up here. Note: The backend doesn't
648 // attempt to legalize the constant (it's useless because DAGCombiner
649 // will insert 64-bit constants and we can't stop it).
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000650 return SelectI64Constant(N, OpVT, N->getDebugLoc());
Scott Michel94bd57e2009-01-15 04:41:47 +0000651 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 && OpVT == MVT::i64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000653 SDValue Op0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000654 EVT Op0VT = Op0.getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +0000655 EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
656 Op0VT, (128 / Op0VT.getSizeInBits()));
657 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(),
658 OpVT, (128 / OpVT.getSizeInBits()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000659 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 switch (Op0VT.getSimpleVT().SimpleTy) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000662 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000663 report_fatal_error("CellSPU Select: Unhandled zero/any extend EVT");
Scott Michel94bd57e2009-01-15 04:41:47 +0000664 /*NOTREACHED*/
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 case MVT::i32:
666 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
667 CurDAG->getConstant(0x80808080, MVT::i32),
668 CurDAG->getConstant(0x00010203, MVT::i32),
669 CurDAG->getConstant(0x80808080, MVT::i32),
670 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000671 break;
672
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 case MVT::i16:
674 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
675 CurDAG->getConstant(0x80808080, MVT::i32),
676 CurDAG->getConstant(0x80800203, MVT::i32),
677 CurDAG->getConstant(0x80808080, MVT::i32),
678 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000679 break;
680
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 case MVT::i8:
682 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
683 CurDAG->getConstant(0x80808080, MVT::i32),
684 CurDAG->getConstant(0x80808003, MVT::i32),
685 CurDAG->getConstant(0x80808080, MVT::i32),
686 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000687 break;
Scott Michel58c58182008-01-17 20:38:41 +0000688 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000689
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000690 SDNode *shufMaskLoad = emitBuildVector(shufMask.getNode());
Chris Lattnera8e76142010-02-23 05:30:43 +0000691
692 HandleSDNode PromoteScalar(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl,
693 Op0VecVT, Op0));
694
695 SDValue PromScalar;
696 if (SDNode *N = SelectCode(PromoteScalar.getValue().getNode()))
697 PromScalar = SDValue(N, 0);
698 else
699 PromScalar = PromoteScalar.getValue();
700
Scott Michel94bd57e2009-01-15 04:41:47 +0000701 SDValue zextShuffle =
Dale Johannesened2eee62009-02-06 01:31:28 +0000702 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Chris Lattnera8e76142010-02-23 05:30:43 +0000703 PromScalar, PromScalar,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000704 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000705
Chris Lattnera8e76142010-02-23 05:30:43 +0000706 HandleSDNode Dummy2(zextShuffle);
707 if (SDNode *N = SelectCode(Dummy2.getValue().getNode()))
708 zextShuffle = SDValue(N, 0);
709 else
710 zextShuffle = Dummy2.getValue();
711 HandleSDNode Dummy(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
712 zextShuffle));
713
714 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
715 SelectCode(Dummy.getValue().getNode());
716 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000718 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000719 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000720
Chris Lattnera8e76142010-02-23 05:30:43 +0000721 HandleSDNode Dummy(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
722 N->getOperand(0), N->getOperand(1),
723 SDValue(CGLoad, 0)));
724
725 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
726 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
727 return N;
728 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000729 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000730 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000731 emitBuildVector(getBorrowGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000732
Chris Lattnera8e76142010-02-23 05:30:43 +0000733 HandleSDNode Dummy(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
734 N->getOperand(0), N->getOperand(1),
735 SDValue(CGLoad, 0)));
736
737 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
738 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
739 return N;
740 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000742 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000743 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000744
Chris Lattnera8e76142010-02-23 05:30:43 +0000745 HandleSDNode Dummy(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
746 N->getOperand(0), N->getOperand(1),
747 SDValue(CGLoad, 0)));
748 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
749 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
750 return N;
751 return Dummy.getValue().getNode();
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000752 } else if (Opc == ISD::TRUNCATE) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000753 SDValue Op0 = N->getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000754 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 && OpVT == MVT::i32
756 && Op0.getValueType() == MVT::i64) {
Scott Michel9de57a92009-01-26 22:33:37 +0000757 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
758 //
759 // Take advantage of the fact that the upper 32 bits are in the
760 // i32 preferred slot and avoid shuffle gymnastics:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000761 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
762 if (CN != 0) {
763 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000764
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000765 if (shift_amt >= 32) {
766 SDNode *hi32 =
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000767 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
768 Op0.getOperand(0), getRC(MVT::i32));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000769
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000770 shift_amt -= 32;
771 if (shift_amt > 0) {
772 // Take care of the additional shift, if present:
Owen Anderson825b72b2009-08-11 20:47:22 +0000773 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000774 unsigned Opc = SPU::ROTMAIr32_i32;
Scott Michel9de57a92009-01-26 22:33:37 +0000775
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000776 if (Op0.getOpcode() == ISD::SRL)
777 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000778
Dan Gohman602b0c82009-09-25 18:54:59 +0000779 hi32 = CurDAG->getMachineNode(Opc, dl, OpVT, SDValue(hi32, 0),
780 shift);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000781 }
782
783 return hi32;
784 }
785 }
786 }
Scott Michel02d711b2008-12-30 23:28:25 +0000787 } else if (Opc == ISD::SHL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000788 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000789 return SelectSHLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000790 } else if (Opc == ISD::SRL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000791 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000792 return SelectSRLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000793 } else if (Opc == ISD::SRA) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000794 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000795 return SelectSRAi64(N, OpVT);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000796 } else if (Opc == ISD::FNEG
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000798 DebugLoc dl = N->getDebugLoc();
Scott Michel7ea02ff2009-03-17 01:15:45 +0000799 // Check if the pattern is a special form of DFNMS:
800 // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000801 SDValue Op0 = N->getOperand(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000802 if (Op0.getOpcode() == ISD::FSUB) {
803 SDValue Op00 = Op0.getOperand(0);
804 if (Op00.getOpcode() == ISD::FMUL) {
805 unsigned Opc = SPU::DFNMSf64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 if (OpVT == MVT::v2f64)
Scott Michel7ea02ff2009-03-17 01:15:45 +0000807 Opc = SPU::DFNMSv2f64;
808
Dan Gohman602b0c82009-09-25 18:54:59 +0000809 return CurDAG->getMachineNode(Opc, dl, OpVT,
810 Op00.getOperand(0),
811 Op00.getOperand(1),
812 Op0.getOperand(1));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000813 }
814 }
815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000817 SDNode *signMask = 0;
Scott Michela82d3f72009-03-17 16:45:16 +0000818 unsigned Opc = SPU::XORfneg64;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000819
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 if (OpVT == MVT::f64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000821 signMask = SelectI64Constant(negConst.getNode(), MVT::i64, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +0000822 } else if (OpVT == MVT::v2f64) {
Scott Michela82d3f72009-03-17 16:45:16 +0000823 Opc = SPU::XORfnegvec;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000824 signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 MVT::v2i64,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000826 negConst, negConst).getNode());
Scott Michel7ea02ff2009-03-17 01:15:45 +0000827 }
828
Dan Gohman602b0c82009-09-25 18:54:59 +0000829 return CurDAG->getMachineNode(Opc, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000830 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000831 } else if (Opc == ISD::FABS) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 if (OpVT == MVT::f64) {
833 SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
Dan Gohman602b0c82009-09-25 18:54:59 +0000834 return CurDAG->getMachineNode(SPU::ANDfabs64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000835 N->getOperand(0), SDValue(signMask, 0));
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 } else if (OpVT == MVT::v2f64) {
837 SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
838 SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000839 absConst, absConst);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000840 SDNode *signMask = emitBuildVector(absVec.getNode());
Dan Gohman602b0c82009-09-25 18:54:59 +0000841 return CurDAG->getMachineNode(SPU::ANDfabsvec, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000842 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000843 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000844 } else if (Opc == SPUISD::LDRESULT) {
845 // Custom select instructions for LDRESULT
Owen Andersone50ed302009-08-10 22:56:29 +0000846 EVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000847 SDValue Arg = N->getOperand(0);
848 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000849 SDNode *Result;
Kalle Raiskila82581352010-10-01 09:20:01 +0000850
851 Result = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VT,
852 MVT::Other, Arg,
853 getRC( VT.getSimpleVT()), Chain);
Scott Michel266bc8f2007-12-04 22:23:35 +0000854 return Result;
Kalle Raiskila82581352010-10-01 09:20:01 +0000855
Scott Michel053c1da2008-01-29 02:16:57 +0000856 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000857 // Look at the operands: SelectCode() will catch the cases that aren't
858 // specifically handled here.
859 //
860 // SPUInstrInfo catches the following patterns:
861 // (SPUindirect (SPUhi ...), (SPUlo ...))
862 // (SPUindirect $sp, imm)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000863 EVT VT = N->getValueType(0);
Scott Michelf0569be2008-12-27 04:51:36 +0000864 SDValue Op0 = N->getOperand(0);
865 SDValue Op1 = N->getOperand(1);
866 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000867
Scott Michelf0569be2008-12-27 04:51:36 +0000868 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
869 || (Op0.getOpcode() == ISD::Register
870 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
871 && RN->getReg() != SPU::R1))) {
872 NewOpc = SPU::Ar32;
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000873 Ops[1] = Op1;
Scott Michel58c58182008-01-17 20:38:41 +0000874 if (Op1.getOpcode() == ISD::Constant) {
875 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000876 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000877 if (isInt<10>(CN->getSExtValue())) {
878 NewOpc = SPU::AIr32;
879 Ops[1] = Op1;
880 } else {
881 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILr32, dl,
882 N->getValueType(0),
883 Op1),
884 0);
885 }
Scott Michel58c58182008-01-17 20:38:41 +0000886 }
Scott Michelf0569be2008-12-27 04:51:36 +0000887 Ops[0] = Op0;
Scott Michelf0569be2008-12-27 04:51:36 +0000888 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000889 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000890 }
Scott Michel02d711b2008-12-30 23:28:25 +0000891
Scott Michel58c58182008-01-17 20:38:41 +0000892 if (n_ops > 0) {
893 if (N->hasOneUse())
894 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
895 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000896 return CurDAG->getMachineNode(NewOpc, dl, OpVT, Ops, n_ops);
Scott Michel58c58182008-01-17 20:38:41 +0000897 } else
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000898 return SelectCode(N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000899}
900
Scott Michel02d711b2008-12-30 23:28:25 +0000901/*!
902 * Emit the instruction sequence for i64 left shifts. The basic algorithm
903 * is to fill the bottom two word slots with zeros so that zeros are shifted
904 * in as the entire quadword is shifted left.
905 *
906 * \note This code could also be used to implement v2i64 shl.
907 *
908 * @param Op The shl operand
909 * @param OpVT Op's machine value value type (doesn't need to be passed, but
910 * makes life easier.)
911 * @return The SDNode with the entire instruction sequence
912 */
913SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000914SPUDAGToDAGISel::SelectSHLi64(SDNode *N, EVT OpVT) {
915 SDValue Op0 = N->getOperand(0);
Owen Anderson23b9b192009-08-12 00:36:31 +0000916 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
917 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000918 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000919 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000920 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
921 SDValue SelMaskVal;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000922 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000923
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000924 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
925 Op0, getRC(MVT::v2i64) );
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
Dan Gohman602b0c82009-09-25 18:54:59 +0000927 SelMask = CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
928 ZeroFill = CurDAG->getMachineNode(SPU::ILv2i64, dl, VecVT,
929 CurDAG->getTargetConstant(0, OpVT));
930 VecOp0 = CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
931 SDValue(ZeroFill, 0),
932 SDValue(VecOp0, 0),
933 SDValue(SelMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000934
935 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
936 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
937 unsigned bits = unsigned(CN->getZExtValue()) & 7;
938
939 if (bytes > 0) {
940 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000941 CurDAG->getMachineNode(SPU::SHLQBYIv2i64, dl, VecVT,
942 SDValue(VecOp0, 0),
943 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000944 }
945
946 if (bits > 0) {
947 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000948 CurDAG->getMachineNode(SPU::SHLQBIIv2i64, dl, VecVT,
949 SDValue((Shift != 0 ? Shift : VecOp0), 0),
950 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000951 }
952 } else {
953 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +0000954 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
955 ShiftAmt,
956 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000957 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +0000958 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
959 ShiftAmt,
960 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000961 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000962 CurDAG->getMachineNode(SPU::SHLQBYv2i64, dl, VecVT,
963 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000964 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000965 CurDAG->getMachineNode(SPU::SHLQBIv2i64, dl, VecVT,
966 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000967 }
968
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000969 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
970 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +0000971}
972
973/*!
974 * Emit the instruction sequence for i64 logical right shifts.
975 *
976 * @param Op The shl operand
977 * @param OpVT Op's machine value value type (doesn't need to be passed, but
978 * makes life easier.)
979 * @return The SDNode with the entire instruction sequence
980 */
981SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000982SPUDAGToDAGISel::SelectSRLi64(SDNode *N, EVT OpVT) {
983 SDValue Op0 = N->getOperand(0);
Owen Anderson23b9b192009-08-12 00:36:31 +0000984 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
985 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000986 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000987 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000988 SDNode *VecOp0, *Shift = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000989 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000990
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000991 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
992 Op0, getRC(MVT::v2i64) );
Scott Michel02d711b2008-12-30 23:28:25 +0000993
994 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
995 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
996 unsigned bits = unsigned(CN->getZExtValue()) & 7;
997
998 if (bytes > 0) {
999 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001000 CurDAG->getMachineNode(SPU::ROTQMBYIv2i64, dl, VecVT,
1001 SDValue(VecOp0, 0),
1002 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001003 }
1004
1005 if (bits > 0) {
1006 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001007 CurDAG->getMachineNode(SPU::ROTQMBIIv2i64, dl, VecVT,
1008 SDValue((Shift != 0 ? Shift : VecOp0), 0),
1009 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001010 }
1011 } else {
1012 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +00001013 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1014 ShiftAmt,
1015 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001016 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +00001017 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1018 ShiftAmt,
1019 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001020
1021 // Ensure that the shift amounts are negated!
Dan Gohman602b0c82009-09-25 18:54:59 +00001022 Bytes = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1023 SDValue(Bytes, 0),
1024 CurDAG->getTargetConstant(0, ShiftAmtVT));
1025
1026 Bits = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1027 SDValue(Bits, 0),
Scott Michel02d711b2008-12-30 23:28:25 +00001028 CurDAG->getTargetConstant(0, ShiftAmtVT));
1029
Scott Michel02d711b2008-12-30 23:28:25 +00001030 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001031 CurDAG->getMachineNode(SPU::ROTQMBYv2i64, dl, VecVT,
1032 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001033 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001034 CurDAG->getMachineNode(SPU::ROTQMBIv2i64, dl, VecVT,
1035 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001036 }
1037
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001038 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1039 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001040}
1041
1042/*!
1043 * Emit the instruction sequence for i64 arithmetic right shifts.
1044 *
1045 * @param Op The shl operand
1046 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1047 * makes life easier.)
1048 * @return The SDNode with the entire instruction sequence
1049 */
1050SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001051SPUDAGToDAGISel::SelectSRAi64(SDNode *N, EVT OpVT) {
Scott Michel02d711b2008-12-30 23:28:25 +00001052 // Promote Op0 to vector
Owen Anderson23b9b192009-08-12 00:36:31 +00001053 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
1054 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001055 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001056 EVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001057 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +00001058
1059 SDNode *VecOp0 =
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001060 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1061 VecVT, N->getOperand(0), getRC(MVT::v2i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001062
1063 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1064 SDNode *SignRot =
Dan Gohman602b0c82009-09-25 18:54:59 +00001065 CurDAG->getMachineNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
1066 SDValue(VecOp0, 0), SignRotAmt);
Scott Michel02d711b2008-12-30 23:28:25 +00001067 SDNode *UpperHalfSign =
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001068 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1069 MVT::i32, SDValue(SignRot, 0), getRC(MVT::i32));
Scott Michel02d711b2008-12-30 23:28:25 +00001070
1071 SDNode *UpperHalfSignMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001072 CurDAG->getMachineNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001073 SDNode *UpperLowerMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001074 CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT,
1075 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
Scott Michel02d711b2008-12-30 23:28:25 +00001076 SDNode *UpperLowerSelect =
Dan Gohman602b0c82009-09-25 18:54:59 +00001077 CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
1078 SDValue(UpperHalfSignMask, 0),
1079 SDValue(VecOp0, 0),
1080 SDValue(UpperLowerMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001081
1082 SDNode *Shift = 0;
1083
1084 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1085 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1086 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1087
1088 if (bytes > 0) {
1089 bytes = 31 - bytes;
1090 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001091 CurDAG->getMachineNode(SPU::ROTQBYIv2i64, dl, VecVT,
1092 SDValue(UpperLowerSelect, 0),
1093 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001094 }
1095
1096 if (bits > 0) {
1097 bits = 8 - bits;
1098 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001099 CurDAG->getMachineNode(SPU::ROTQBIIv2i64, dl, VecVT,
1100 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1101 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001102 }
1103 } else {
1104 SDNode *NegShift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001105 CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1106 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001107
1108 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001109 CurDAG->getMachineNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
1110 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001111 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001112 CurDAG->getMachineNode(SPU::ROTQBIv2i64, dl, VecVT,
1113 SDValue(Shift, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001114 }
1115
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001116 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1117 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001118}
1119
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001120/*!
1121 Do the necessary magic necessary to load a i64 constant
1122 */
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001123SDNode *SPUDAGToDAGISel::SelectI64Constant(SDNode *N, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001124 DebugLoc dl) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001125 ConstantSDNode *CN = cast<ConstantSDNode>(N);
Scott Michel7ea02ff2009-03-17 01:15:45 +00001126 return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
1127}
1128
Owen Andersone50ed302009-08-10 22:56:29 +00001129SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001130 DebugLoc dl) {
Owen Anderson23b9b192009-08-12 00:36:31 +00001131 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001132 SDValue i64vec =
Scott Michel7ea02ff2009-03-17 01:15:45 +00001133 SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001134
1135 // Here's where it gets interesting, because we have to parse out the
1136 // subtree handed back in i64vec:
1137
1138 if (i64vec.getOpcode() == ISD::BIT_CONVERT) {
1139 // The degenerate case where the upper and lower bits in the splat are
1140 // identical:
1141 SDValue Op0 = i64vec.getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001142
Scott Michel9de57a92009-01-26 22:33:37 +00001143 ReplaceUses(i64vec, Op0);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001144 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1145 SDValue(emitBuildVector(Op0.getNode()), 0),
1146 getRC(MVT::i64));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001147 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1148 SDValue lhs = i64vec.getOperand(0);
1149 SDValue rhs = i64vec.getOperand(1);
1150 SDValue shufmask = i64vec.getOperand(2);
1151
1152 if (lhs.getOpcode() == ISD::BIT_CONVERT) {
1153 ReplaceUses(lhs, lhs.getOperand(0));
1154 lhs = lhs.getOperand(0);
1155 }
1156
1157 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1158 ? lhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001159 : emitBuildVector(lhs.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001160
1161 if (rhs.getOpcode() == ISD::BIT_CONVERT) {
1162 ReplaceUses(rhs, rhs.getOperand(0));
1163 rhs = rhs.getOperand(0);
1164 }
1165
1166 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1167 ? rhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001168 : emitBuildVector(rhs.getNode()));
Scott Michel9de57a92009-01-26 22:33:37 +00001169
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001170 if (shufmask.getOpcode() == ISD::BIT_CONVERT) {
1171 ReplaceUses(shufmask, shufmask.getOperand(0));
1172 shufmask = shufmask.getOperand(0);
1173 }
1174
1175 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1176 ? shufmask.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001177 : emitBuildVector(shufmask.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001178
Chris Lattnera8e76142010-02-23 05:30:43 +00001179 SDValue shufNode =
1180 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001181 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
Chris Lattnera8e76142010-02-23 05:30:43 +00001182 SDValue(shufMaskNode, 0));
1183 HandleSDNode Dummy(shufNode);
1184 SDNode *SN = SelectCode(Dummy.getValue().getNode());
1185 if (SN == 0) SN = Dummy.getValue().getNode();
1186
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001187 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1188 OpVT, SDValue(SN, 0), getRC(MVT::i64));
Scott Michel7ea02ff2009-03-17 01:15:45 +00001189 } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001190 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1191 SDValue(emitBuildVector(i64vec.getNode()), 0),
1192 getRC(MVT::i64));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001193 } else {
Chris Lattner75361b62010-04-07 22:58:41 +00001194 report_fatal_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
Torok Edwindac237e2009-07-08 20:53:28 +00001195 "condition");
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001196 }
1197}
1198
Scott Michel02d711b2008-12-30 23:28:25 +00001199/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001200/// SPU-specific DAG, ready for instruction scheduling.
1201///
1202FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1203 return new SPUDAGToDAGISel(TM);
1204}