blob: 425371d3e11d4f1c0fbbc0f9827f8f618579b6ca [file] [log] [blame]
Chris Lattner4ee451d2007-12-29 20:36:04 +00001//===-- SPUISelDAGToDAG.cpp - CellSPU pattern matching inst selector ------===//
Scott Michel266bc8f2007-12-04 22:23:35 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Scott Michel266bc8f2007-12-04 22:23:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for the Cell SPU,
11// converting from a legalized dag to a SPU-target dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SPU.h"
16#include "SPUTargetMachine.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000017#include "SPUHazardRecognizers.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000018#include "SPUFrameLowering.h"
Scott Michel94bd57e2009-01-15 04:41:47 +000019#include "SPUTargetMachine.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000020#include "llvm/CodeGen/MachineConstantPool.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineFunction.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000023#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/Target/TargetOptions.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/Constants.h"
28#include "llvm/GlobalValue.h"
29#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000031#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000032#include "llvm/Support/ErrorHandling.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000033#include "llvm/Support/MathExtras.h"
34#include "llvm/Support/Compiler.h"
Torok Edwindac237e2009-07-08 20:53:28 +000035#include "llvm/Support/raw_ostream.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000036
37using namespace llvm;
38
39namespace {
40 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
41 bool
Scott Michel266bc8f2007-12-04 22:23:35 +000042 isI32IntS10Immediate(ConstantSDNode *CN)
43 {
Benjamin Kramer7e09deb2010-03-29 19:07:58 +000044 return isInt<10>(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000045 }
46
Scott Michel504c3692007-12-17 22:32:34 +000047 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
48 bool
49 isI32IntU10Immediate(ConstantSDNode *CN)
50 {
Benjamin Kramer34247a02010-03-29 21:13:41 +000051 return isUInt<10>(CN->getSExtValue());
Scott Michel504c3692007-12-17 22:32:34 +000052 }
53
Scott Michel266bc8f2007-12-04 22:23:35 +000054 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
55 bool
56 isI16IntS10Immediate(ConstantSDNode *CN)
57 {
Benjamin Kramer7e09deb2010-03-29 19:07:58 +000058 return isInt<10>(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000059 }
60
Scott Michelec2a08f2007-12-15 00:38:50 +000061 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
62 bool
63 isI16IntU10Immediate(ConstantSDNode *CN)
64 {
Benjamin Kramer34247a02010-03-29 21:13:41 +000065 return isUInt<10>((short) CN->getZExtValue());
Scott Michelec2a08f2007-12-15 00:38:50 +000066 }
67
Scott Michel266bc8f2007-12-04 22:23:35 +000068 //! ConstantSDNode predicate for signed 16-bit values
69 /*!
70 \arg CN The constant SelectionDAG node holding the value
71 \arg Imm The returned 16-bit value, if returning true
72
73 This predicate tests the value in \a CN to see whether it can be
74 represented as a 16-bit, sign-extended quantity. Returns true if
75 this is the case.
76 */
77 bool
78 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
79 {
Owen Andersone50ed302009-08-10 22:56:29 +000080 EVT vt = CN->getValueType(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000081 Imm = (short) CN->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000082 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +000083 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +000084 } else if (vt == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000085 int32_t i_val = (int32_t) CN->getZExtValue();
Richard Smith1144af32012-08-24 23:29:28 +000086 return i_val == SignExtend32<16>(i_val);
Scott Michel266bc8f2007-12-04 22:23:35 +000087 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000088 int64_t i_val = (int64_t) CN->getZExtValue();
Richard Smith1144af32012-08-24 23:29:28 +000089 return i_val == SignExtend64<16>(i_val);
Scott Michel266bc8f2007-12-04 22:23:35 +000090 }
Scott Michel266bc8f2007-12-04 22:23:35 +000091 }
92
Scott Michel266bc8f2007-12-04 22:23:35 +000093 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
94 static bool
95 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
96 {
Owen Andersone50ed302009-08-10 22:56:29 +000097 EVT vt = FPN->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000098 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +000099 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Richard Smith1144af32012-08-24 23:29:28 +0000100 if (val == SignExtend32<16>(val)) {
101 Imm = (short) val;
102 return true;
103 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000104 }
105
106 return false;
107 }
108
Scott Michel7ea02ff2009-03-17 01:15:45 +0000109 //! Generate the carry-generate shuffle mask.
110 SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
111 SmallVector<SDValue, 16 > ShufBytes;
Dan Gohman844731a2008-05-13 00:00:25 +0000112
Scott Michel7ea02ff2009-03-17 01:15:45 +0000113 // Create the shuffle mask for "rotating" the borrow up one register slot
114 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
116 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
117 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
118 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +0000119
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000121 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000122 }
Scott Michel02d711b2008-12-30 23:28:25 +0000123
Scott Michel7ea02ff2009-03-17 01:15:45 +0000124 //! Generate the borrow-generate shuffle mask
125 SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
126 SmallVector<SDValue, 16 > ShufBytes;
127
128 // Create the shuffle mask for "rotating" the borrow up one register slot
129 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
131 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
132 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
133 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000134
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000136 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000137 }
138
Scott Michel7ea02ff2009-03-17 01:15:45 +0000139 //===------------------------------------------------------------------===//
140 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
141 /// instructions for SelectionDAG operations.
142 ///
143 class SPUDAGToDAGISel :
144 public SelectionDAGISel
145 {
Dan Gohmand858e902010-04-17 15:26:15 +0000146 const SPUTargetMachine &TM;
147 const SPUTargetLowering &SPUtli;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000148 unsigned GlobalBaseReg;
Scott Michel02d711b2008-12-30 23:28:25 +0000149
Scott Michel7ea02ff2009-03-17 01:15:45 +0000150 public:
151 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
152 SelectionDAGISel(tm),
153 TM(tm),
154 SPUtli(*tm.getTargetLowering())
155 { }
156
Dan Gohmanad2afc22009-07-31 18:16:33 +0000157 virtual bool runOnMachineFunction(MachineFunction &MF) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000158 // Make sure we re-emit a set of the global base reg if necessary
159 GlobalBaseReg = 0;
Dan Gohmanad2afc22009-07-31 18:16:33 +0000160 SelectionDAGISel::runOnMachineFunction(MF);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000161 return true;
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000162 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000163
Scott Michel7ea02ff2009-03-17 01:15:45 +0000164 /// getI32Imm - Return a target constant with the specified value, of type
165 /// i32.
166 inline SDValue getI32Imm(uint32_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 return CurDAG->getTargetConstant(Imm, MVT::i32);
Scott Michel94bd57e2009-01-15 04:41:47 +0000168 }
169
Scott Michel7ea02ff2009-03-17 01:15:45 +0000170 /// getSmallIPtrImm - Return a target constant of pointer type.
171 inline SDValue getSmallIPtrImm(unsigned Imm) {
172 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Chris Lattner17aa6802010-09-04 18:12:00 +0000173 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000174
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000175 SDNode *emitBuildVector(SDNode *bvNode) {
176 EVT vecVT = bvNode->getValueType(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000177 DebugLoc dl = bvNode->getDebugLoc();
178
179 // Check to see if this vector can be represented as a CellSPU immediate
180 // constant by invoking all of the instruction selection predicates:
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 if (((vecVT == MVT::v8i16) &&
182 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
183 ((vecVT == MVT::v4i32) &&
184 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
185 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
186 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
Scott Michel7ea02ff2009-03-17 01:15:45 +0000187 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 ((vecVT == MVT::v2i64) &&
189 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
190 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
Chris Lattnera8e76142010-02-23 05:30:43 +0000191 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)))) {
192 HandleSDNode Dummy(SDValue(bvNode, 0));
193 if (SDNode *N = Select(bvNode))
194 return N;
195 return Dummy.getValue().getNode();
196 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000197
198 // No, need to emit a constant pool spill:
199 std::vector<Constant*> CV;
200
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000201 for (size_t i = 0; i < bvNode->getNumOperands(); ++i) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000202 ConstantSDNode *V = cast<ConstantSDNode > (bvNode->getOperand(i));
Chris Lattnera8e76142010-02-23 05:30:43 +0000203 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000204 }
205
Dan Gohman46510a72010-04-15 01:51:59 +0000206 const Constant *CP = ConstantVector::get(CV);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000207 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
208 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
209 SDValue CGPoolOffset =
Dan Gohmand858e902010-04-17 15:26:15 +0000210 SPU::LowerConstantPool(CPIdx, *CurDAG, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000211
Chris Lattnera8e76142010-02-23 05:30:43 +0000212 HandleSDNode Dummy(CurDAG->getLoad(vecVT, dl,
213 CurDAG->getEntryNode(), CGPoolOffset,
Chris Lattnere8639032010-09-21 06:22:23 +0000214 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000215 false, false, false, Alignment));
Chris Lattnera8e76142010-02-23 05:30:43 +0000216 CurDAG->ReplaceAllUsesWith(SDValue(bvNode, 0), Dummy.getValue());
217 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
218 return N;
219 return Dummy.getValue().getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000220 }
Scott Michel02d711b2008-12-30 23:28:25 +0000221
Scott Michel7ea02ff2009-03-17 01:15:45 +0000222 /// Select - Convert the specified operand from a target-independent to a
223 /// target-specific node if it hasn't already been changed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000224 SDNode *Select(SDNode *N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000225
Scott Michel7ea02ff2009-03-17 01:15:45 +0000226 //! Emit the instruction sequence for i64 shl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000227 SDNode *SelectSHLi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000228
Scott Michel7ea02ff2009-03-17 01:15:45 +0000229 //! Emit the instruction sequence for i64 srl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000230 SDNode *SelectSRLi64(SDNode *N, EVT OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000231
Scott Michel7ea02ff2009-03-17 01:15:45 +0000232 //! Emit the instruction sequence for i64 sra
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000233 SDNode *SelectSRAi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000234
Scott Michel7ea02ff2009-03-17 01:15:45 +0000235 //! Emit the necessary sequence for loading i64 constants:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000236 SDNode *SelectI64Constant(SDNode *N, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000237
238 //! Alternate instruction emit sequence for loading i64 constants
Owen Andersone50ed302009-08-10 22:56:29 +0000239 SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000240
241 //! Returns true if the address N is an A-form (local store) address
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000242 bool SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000243 SDValue &Index);
244
245 //! D-form address predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000246 bool SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000247 SDValue &Index);
248
249 /// Alternate D-form address using i7 offset predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000250 bool SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000251 SDValue &Base);
252
253 /// D-form address selection workhorse
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000254 bool DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000255 SDValue &Base, int minOffset, int maxOffset);
256
257 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000258 bool SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000259 SDValue &Index);
260
261 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
262 /// inline asm expressions.
263 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
264 char ConstraintCode,
265 std::vector<SDValue> &OutOps) {
266 SDValue Op0, Op1;
267 switch (ConstraintCode) {
268 default: return true;
269 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000270 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
271 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1))
272 SelectXFormAddr(Op.getNode(), Op, Op0, Op1);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000273 break;
274 case 'o': // offsetable
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000275 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
276 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1)) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000277 Op0 = Op;
278 Op1 = getSmallIPtrImm(0);
279 }
280 break;
281 case 'v': // not offsetable
282#if 1
Torok Edwinc23197a2009-07-14 16:55:14 +0000283 llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
Scott Michel7ea02ff2009-03-17 01:15:45 +0000284#else
285 SelectAddrIdxOnly(Op, Op, Op0, Op1);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000286 break;
David Blaikie4d6ccb52012-01-20 21:51:11 +0000287#endif
Scott Michel7ea02ff2009-03-17 01:15:45 +0000288 }
289
290 OutOps.push_back(Op0);
291 OutOps.push_back(Op1);
292 return false;
293 }
294
Scott Michel7ea02ff2009-03-17 01:15:45 +0000295 virtual const char *getPassName() const {
296 return "Cell SPU DAG->DAG Pattern Instruction Selection";
297 }
298
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000299 private:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000300 SDValue getRC( MVT );
Scott Michel7ea02ff2009-03-17 01:15:45 +0000301
302 // Include the pieces autogenerated from the target description.
Scott Michel266bc8f2007-12-04 22:23:35 +0000303#include "SPUGenDAGISel.inc"
Scott Michel7ea02ff2009-03-17 01:15:45 +0000304 };
Dan Gohman844731a2008-05-13 00:00:25 +0000305}
306
Scott Michel266bc8f2007-12-04 22:23:35 +0000307/*!
Scott Michel9de57a92009-01-26 22:33:37 +0000308 \arg Op The ISD instruction operand
Scott Michel266bc8f2007-12-04 22:23:35 +0000309 \arg N The address to be tested
310 \arg Base The base address
311 \arg Index The base address index
312 */
313bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000314SPUDAGToDAGISel::SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000315 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000316 // These match the addr256k operand type:
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 EVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000318 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000319 int64_t val;
Scott Michel266bc8f2007-12-04 22:23:35 +0000320
321 switch (N.getOpcode()) {
322 case ISD::Constant:
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000323 val = dyn_cast<ConstantSDNode>(N.getNode())->getSExtValue();
324 Base = CurDAG->getTargetConstant( val , MVT::i32);
325 Index = Zero;
David Blaikie4d6ccb52012-01-20 21:51:11 +0000326 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000327 case ISD::ConstantPool:
328 case ISD::GlobalAddress:
Kalle Raiskila7f5de8b2011-03-04 12:00:11 +0000329 report_fatal_error("SPU SelectAFormAddr: Pool/Global not lowered.");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000330 /*NOTREACHED*/
331
Scott Michel053c1da2008-01-29 02:16:57 +0000332 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000333 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000334 case ISD::TargetJumpTable:
Chris Lattner75361b62010-04-07 22:58:41 +0000335 report_fatal_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
Torok Edwindac237e2009-07-08 20:53:28 +0000336 "not wrapped as A-form address.");
Scott Michel053c1da2008-01-29 02:16:57 +0000337 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000338
Scott Michel02d711b2008-12-30 23:28:25 +0000339 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000340 // Just load from memory if there's only a single use of the location,
341 // otherwise, this will get handled below with D-form offset addresses
342 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000343 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000344 switch (Op0.getOpcode()) {
345 case ISD::TargetConstantPool:
346 case ISD::TargetJumpTable:
347 Base = Op0;
348 Index = Zero;
349 return true;
350
351 case ISD::TargetGlobalAddress: {
352 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
Dan Gohman46510a72010-04-15 01:51:59 +0000353 const GlobalValue *GV = GSDN->getGlobal();
Scott Michel053c1da2008-01-29 02:16:57 +0000354 if (GV->getAlignment() == 16) {
355 Base = Op0;
356 Index = Zero;
357 return true;
358 }
359 break;
360 }
361 }
362 }
363 break;
364 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000365 return false;
366}
367
Scott Michel02d711b2008-12-30 23:28:25 +0000368bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000369SPUDAGToDAGISel::SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Dan Gohman475871a2008-07-27 21:46:04 +0000370 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000371 const int minDForm2Offset = -(1 << 7);
372 const int maxDForm2Offset = (1 << 7) - 1;
373 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
374 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000375}
376
Scott Michel266bc8f2007-12-04 22:23:35 +0000377/*!
378 \arg Op The ISD instruction (ignored)
379 \arg N The address to be tested
380 \arg Base Base address register/pointer
381 \arg Index Base address index
382
383 Examine the input address by a base register plus a signed 10-bit
384 displacement, [r+I10] (D-form address).
385
386 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000387 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000388*/
389bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000390SPUDAGToDAGISel::SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000391 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000392 return DFormAddressPredicate(Op, N, Base, Index,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000393 SPUFrameLowering::minFrameOffset(),
394 SPUFrameLowering::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000395}
396
397bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000398SPUDAGToDAGISel::DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000399 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000400 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000401 unsigned Opc = N.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +0000402 EVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000403
Scott Michel053c1da2008-01-29 02:16:57 +0000404 if (Opc == ISD::FrameIndex) {
405 // Stack frame index must be less than 512 (divided by 16):
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000406 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(N);
Scott Michel203b2d62008-04-30 00:30:08 +0000407 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000408 DEBUG(errs() << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000409 << FI << "\n");
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000410 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000411 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000412 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000413 return true;
414 }
415 } else if (Opc == ISD::ADD) {
416 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000417 const SDValue Op0 = N.getOperand(0);
418 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000419
Scott Michel053c1da2008-01-29 02:16:57 +0000420 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
421 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
422 Base = CurDAG->getTargetConstant(0, PtrTy);
423 Index = N;
424 return true;
425 } else if (Op1.getOpcode() == ISD::Constant
426 || Op1.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000427 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000428 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000429
Scott Michel053c1da2008-01-29 02:16:57 +0000430 if (Op0.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000431 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op0);
Scott Michel203b2d62008-04-30 00:30:08 +0000432 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000433 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000434 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000435
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000436 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000437 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000438 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000439 return true;
440 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000441 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000442 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000443 Index = Op0;
444 return true;
445 }
446 } else if (Op0.getOpcode() == ISD::Constant
447 || Op0.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000448 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000449 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000450
451 if (Op1.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000452 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op1);
Scott Michel203b2d62008-04-30 00:30:08 +0000453 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000454 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000455 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000456
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000457 if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000458 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000459 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000460 return true;
461 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000462 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000463 Base = CurDAG->getTargetConstant(offset, PtrTy);
464 Index = Op1;
465 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000466 }
Scott Michel053c1da2008-01-29 02:16:57 +0000467 }
468 } else if (Opc == SPUISD::IndirectAddr) {
469 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000470 const SDValue Op0 = N.getOperand(0);
471 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000472
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000473 if (Op0.getOpcode() == SPUISD::Hi
474 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000475 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000476 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000477 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000478 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000479 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
480 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000481 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000482
483 if (isa<ConstantSDNode>(Op1)) {
484 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000485 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000486 idxOp = Op0;
487 } else if (isa<ConstantSDNode>(Op0)) {
488 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000489 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000490 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000491 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000492
493 if (offset >= minOffset && offset <= maxOffset) {
494 Base = CurDAG->getTargetConstant(offset, PtrTy);
495 Index = idxOp;
496 return true;
497 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000498 }
Scott Michel053c1da2008-01-29 02:16:57 +0000499 } else if (Opc == SPUISD::AFormAddr) {
500 Base = CurDAG->getTargetConstant(0, N.getValueType());
501 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000502 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000503 } else if (Opc == SPUISD::LDRESULT) {
504 Base = CurDAG->getTargetConstant(0, N.getValueType());
505 Index = N;
506 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000507 } else if (Opc == ISD::Register
508 ||Opc == ISD::CopyFromReg
Kalle Raiskilabc2697c2010-08-04 13:59:48 +0000509 ||Opc == ISD::UNDEF
510 ||Opc == ISD::Constant) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000511 unsigned OpOpc = Op->getOpcode();
Scott Michel9c0c6b22008-11-21 02:56:16 +0000512
513 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
514 // Direct load/store without getelementptr
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000515 SDValue Offs;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000516
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000517 Offs = ((OpOpc == ISD::STORE) ? Op->getOperand(3) : Op->getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000518
519 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
520 if (Offs.getOpcode() == ISD::UNDEF)
521 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
522
523 Base = Offs;
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000524 Index = N;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000525 return true;
526 }
Scott Michelaedc6372008-12-10 00:15:19 +0000527 } else {
528 /* If otherwise unadorned, default to D-form address with 0 offset: */
529 if (Opc == ISD::CopyFromReg) {
Scott Michel19c10e62009-01-26 03:37:41 +0000530 Index = N.getOperand(1);
Scott Michelaedc6372008-12-10 00:15:19 +0000531 } else {
Scott Michel19c10e62009-01-26 03:37:41 +0000532 Index = N;
Scott Michelaedc6372008-12-10 00:15:19 +0000533 }
534
535 Base = CurDAG->getTargetConstant(0, Index.getValueType());
536 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000537 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000538 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000539
Scott Michel266bc8f2007-12-04 22:23:35 +0000540 return false;
541}
542
543/*!
544 \arg Op The ISD instruction operand
545 \arg N The address operand
546 \arg Base The base pointer operand
547 \arg Index The offset/index operand
548
Scott Michel9c0c6b22008-11-21 02:56:16 +0000549 If the address \a N can be expressed as an A-form or D-form address, returns
550 false. Otherwise, creates two operands, Base and Index that will become the
551 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000552*/
553bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000554SPUDAGToDAGISel::SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000555 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000556 if (!SelectAFormAddr(Op, N, Base, Index)
557 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000558 // If the address is neither A-form or D-form, punt and use an X-form
559 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000560 Base = N.getOperand(1);
561 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000562 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000563 }
564
565 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000566}
567
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000568/*!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000569 Utility function to use with COPY_TO_REGCLASS instructions. Returns a SDValue
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000570 to be used as the last parameter of a
571CurDAG->getMachineNode(COPY_TO_REGCLASS,..., ) function call
572 \arg VT the value type for which we want a register class
573*/
574SDValue SPUDAGToDAGISel::getRC( MVT VT ) {
575 switch( VT.SimpleTy ) {
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000576 case MVT::i8:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000577 return CurDAG->getTargetConstant(SPU::R8CRegClass.getID(), MVT::i32);
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000578 case MVT::i16:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000579 return CurDAG->getTargetConstant(SPU::R16CRegClass.getID(), MVT::i32);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000580 case MVT::i32:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000581 return CurDAG->getTargetConstant(SPU::R32CRegClass.getID(), MVT::i32);
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000582 case MVT::f32:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000583 return CurDAG->getTargetConstant(SPU::R32FPRegClass.getID(), MVT::i32);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000584 case MVT::i64:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000585 return CurDAG->getTargetConstant(SPU::R64CRegClass.getID(), MVT::i32);
Kalle Raiskila11edd0c2010-11-29 09:36:26 +0000586 case MVT::i128:
587 return CurDAG->getTargetConstant(SPU::GPRCRegClass.getID(), MVT::i32);
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000588 case MVT::v16i8:
589 case MVT::v8i16:
590 case MVT::v4i32:
591 case MVT::v4f32:
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000592 case MVT::v2i64:
Kalle Raiskila218c98c2010-10-07 16:32:42 +0000593 case MVT::v2f64:
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000594 return CurDAG->getTargetConstant(SPU::VECREGRegClass.getID(), MVT::i32);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000595 default:
596 assert( false && "add a new case here" );
David Blaikie4d6ccb52012-01-20 21:51:11 +0000597 return SDValue();
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000598 }
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000599}
600
Scott Michel266bc8f2007-12-04 22:23:35 +0000601//! Convert the operand from a target-independent to a target-specific node
602/*!
603 */
604SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000605SPUDAGToDAGISel::Select(SDNode *N) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000606 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000607 int n_ops = -1;
Ted Kremenek584520e2011-01-23 17:05:06 +0000608 unsigned NewOpc = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000609 EVT OpVT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000610 SDValue Ops[8];
Dale Johannesened2eee62009-02-06 01:31:28 +0000611 DebugLoc dl = N->getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +0000612
Chris Lattnera8e76142010-02-23 05:30:43 +0000613 if (N->isMachineOpcode())
Scott Michel266bc8f2007-12-04 22:23:35 +0000614 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000615
616 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000617 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000618 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
619 SDValue Imm0 = CurDAG->getTargetConstant(0, N->getValueType(0));
Scott Michel266bc8f2007-12-04 22:23:35 +0000620
Scott Michel02d711b2008-12-30 23:28:25 +0000621 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000622 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000623 Ops[0] = TFI;
624 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000625 n_ops = 2;
626 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000627 NewOpc = SPU::Ar32;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000628 Ops[0] = CurDAG->getRegister(SPU::R1, N->getValueType(0));
Dan Gohman602b0c82009-09-25 18:54:59 +0000629 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILAr32, dl,
Kalle Raiskila7d170972010-12-09 16:17:31 +0000630 N->getValueType(0), TFI),
Dan Gohman602b0c82009-09-25 18:54:59 +0000631 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000632 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000633 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000635 // Catch the i64 constants that end up here. Note: The backend doesn't
636 // attempt to legalize the constant (it's useless because DAGCombiner
637 // will insert 64-bit constants and we can't stop it).
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000638 return SelectI64Constant(N, OpVT, N->getDebugLoc());
Scott Michel94bd57e2009-01-15 04:41:47 +0000639 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 && OpVT == MVT::i64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000641 SDValue Op0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000642 EVT Op0VT = Op0.getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +0000643 EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
644 Op0VT, (128 / Op0VT.getSizeInBits()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000645 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +0000646 OpVT, (128 / OpVT.getSizeInBits()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000647 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000648
Owen Anderson825b72b2009-08-11 20:47:22 +0000649 switch (Op0VT.getSimpleVT().SimpleTy) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000650 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000651 report_fatal_error("CellSPU Select: Unhandled zero/any extend EVT");
Scott Michel94bd57e2009-01-15 04:41:47 +0000652 /*NOTREACHED*/
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 case MVT::i32:
654 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
655 CurDAG->getConstant(0x80808080, MVT::i32),
656 CurDAG->getConstant(0x00010203, MVT::i32),
657 CurDAG->getConstant(0x80808080, MVT::i32),
658 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000659 break;
660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 case MVT::i16:
662 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
663 CurDAG->getConstant(0x80808080, MVT::i32),
664 CurDAG->getConstant(0x80800203, MVT::i32),
665 CurDAG->getConstant(0x80808080, MVT::i32),
666 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000667 break;
668
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 case MVT::i8:
670 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
671 CurDAG->getConstant(0x80808080, MVT::i32),
672 CurDAG->getConstant(0x80808003, MVT::i32),
673 CurDAG->getConstant(0x80808080, MVT::i32),
674 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000675 break;
Scott Michel58c58182008-01-17 20:38:41 +0000676 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000677
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000678 SDNode *shufMaskLoad = emitBuildVector(shufMask.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000679
Chris Lattnera8e76142010-02-23 05:30:43 +0000680 HandleSDNode PromoteScalar(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl,
681 Op0VecVT, Op0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000682
Chris Lattnera8e76142010-02-23 05:30:43 +0000683 SDValue PromScalar;
684 if (SDNode *N = SelectCode(PromoteScalar.getValue().getNode()))
685 PromScalar = SDValue(N, 0);
686 else
687 PromScalar = PromoteScalar.getValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000688
Scott Michel94bd57e2009-01-15 04:41:47 +0000689 SDValue zextShuffle =
Dale Johannesened2eee62009-02-06 01:31:28 +0000690 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000691 PromScalar, PromScalar,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000692 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000693
Chris Lattnera8e76142010-02-23 05:30:43 +0000694 HandleSDNode Dummy2(zextShuffle);
695 if (SDNode *N = SelectCode(Dummy2.getValue().getNode()))
696 zextShuffle = SDValue(N, 0);
697 else
698 zextShuffle = Dummy2.getValue();
699 HandleSDNode Dummy(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
700 zextShuffle));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000701
Chris Lattnera8e76142010-02-23 05:30:43 +0000702 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
703 SelectCode(Dummy.getValue().getNode());
704 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000706 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000707 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000708
Chris Lattnera8e76142010-02-23 05:30:43 +0000709 HandleSDNode Dummy(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
710 N->getOperand(0), N->getOperand(1),
711 SDValue(CGLoad, 0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000712
Chris Lattnera8e76142010-02-23 05:30:43 +0000713 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
714 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
715 return N;
716 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 } else if (Opc == ISD::SUB && (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(getBorrowGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000720
Chris Lattnera8e76142010-02-23 05:30:43 +0000721 HandleSDNode Dummy(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
722 N->getOperand(0), N->getOperand(1),
723 SDValue(CGLoad, 0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000724
Chris Lattnera8e76142010-02-23 05:30:43 +0000725 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::MUL && (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(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000732
Chris Lattnera8e76142010-02-23 05:30:43 +0000733 HandleSDNode Dummy(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
734 N->getOperand(0), N->getOperand(1),
735 SDValue(CGLoad, 0)));
736 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
737 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
738 return N;
739 return Dummy.getValue().getNode();
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000740 } else if (Opc == ISD::TRUNCATE) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000741 SDValue Op0 = N->getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000742 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 && OpVT == MVT::i32
744 && Op0.getValueType() == MVT::i64) {
Scott Michel9de57a92009-01-26 22:33:37 +0000745 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
746 //
747 // Take advantage of the fact that the upper 32 bits are in the
748 // i32 preferred slot and avoid shuffle gymnastics:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000749 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
750 if (CN != 0) {
751 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000752
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000753 if (shift_amt >= 32) {
754 SDNode *hi32 =
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000755 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
756 Op0.getOperand(0), getRC(MVT::i32));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000757
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000758 shift_amt -= 32;
759 if (shift_amt > 0) {
760 // Take care of the additional shift, if present:
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000762 unsigned Opc = SPU::ROTMAIr32_i32;
Scott Michel9de57a92009-01-26 22:33:37 +0000763
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000764 if (Op0.getOpcode() == ISD::SRL)
765 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000766
Dan Gohman602b0c82009-09-25 18:54:59 +0000767 hi32 = CurDAG->getMachineNode(Opc, dl, OpVT, SDValue(hi32, 0),
768 shift);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000769 }
770
771 return hi32;
772 }
773 }
774 }
Scott Michel02d711b2008-12-30 23:28:25 +0000775 } else if (Opc == ISD::SHL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000776 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000777 return SelectSHLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000778 } else if (Opc == ISD::SRL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000779 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000780 return SelectSRLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000781 } else if (Opc == ISD::SRA) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000782 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000783 return SelectSRAi64(N, OpVT);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000784 } else if (Opc == ISD::FNEG
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000786 DebugLoc dl = N->getDebugLoc();
Scott Michel7ea02ff2009-03-17 01:15:45 +0000787 // Check if the pattern is a special form of DFNMS:
788 // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000789 SDValue Op0 = N->getOperand(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000790 if (Op0.getOpcode() == ISD::FSUB) {
791 SDValue Op00 = Op0.getOperand(0);
792 if (Op00.getOpcode() == ISD::FMUL) {
793 unsigned Opc = SPU::DFNMSf64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 if (OpVT == MVT::v2f64)
Scott Michel7ea02ff2009-03-17 01:15:45 +0000795 Opc = SPU::DFNMSv2f64;
796
Dan Gohman602b0c82009-09-25 18:54:59 +0000797 return CurDAG->getMachineNode(Opc, dl, OpVT,
798 Op00.getOperand(0),
799 Op00.getOperand(1),
800 Op0.getOperand(1));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000801 }
802 }
803
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000805 SDNode *signMask = 0;
Scott Michela82d3f72009-03-17 16:45:16 +0000806 unsigned Opc = SPU::XORfneg64;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000807
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 if (OpVT == MVT::f64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000809 signMask = SelectI64Constant(negConst.getNode(), MVT::i64, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 } else if (OpVT == MVT::v2f64) {
Scott Michela82d3f72009-03-17 16:45:16 +0000811 Opc = SPU::XORfnegvec;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000812 signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 MVT::v2i64,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000814 negConst, negConst).getNode());
Scott Michel7ea02ff2009-03-17 01:15:45 +0000815 }
816
Dan Gohman602b0c82009-09-25 18:54:59 +0000817 return CurDAG->getMachineNode(Opc, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000818 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000819 } else if (Opc == ISD::FABS) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 if (OpVT == MVT::f64) {
821 SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
Dan Gohman602b0c82009-09-25 18:54:59 +0000822 return CurDAG->getMachineNode(SPU::ANDfabs64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000823 N->getOperand(0), SDValue(signMask, 0));
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 } else if (OpVT == MVT::v2f64) {
825 SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
826 SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000827 absConst, absConst);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000828 SDNode *signMask = emitBuildVector(absVec.getNode());
Dan Gohman602b0c82009-09-25 18:54:59 +0000829 return CurDAG->getMachineNode(SPU::ANDfabsvec, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000830 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000831 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000832 } else if (Opc == SPUISD::LDRESULT) {
833 // Custom select instructions for LDRESULT
Owen Andersone50ed302009-08-10 22:56:29 +0000834 EVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000835 SDValue Arg = N->getOperand(0);
836 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000837 SDNode *Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000838
Kalle Raiskila82581352010-10-01 09:20:01 +0000839 Result = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VT,
840 MVT::Other, Arg,
841 getRC( VT.getSimpleVT()), Chain);
Scott Michel266bc8f2007-12-04 22:23:35 +0000842 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000843
Scott Michel053c1da2008-01-29 02:16:57 +0000844 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000845 // Look at the operands: SelectCode() will catch the cases that aren't
846 // specifically handled here.
847 //
848 // SPUInstrInfo catches the following patterns:
849 // (SPUindirect (SPUhi ...), (SPUlo ...))
850 // (SPUindirect $sp, imm)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000851 EVT VT = N->getValueType(0);
Scott Michelf0569be2008-12-27 04:51:36 +0000852 SDValue Op0 = N->getOperand(0);
853 SDValue Op1 = N->getOperand(1);
854 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000855
Scott Michelf0569be2008-12-27 04:51:36 +0000856 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
857 || (Op0.getOpcode() == ISD::Register
858 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
859 && RN->getReg() != SPU::R1))) {
860 NewOpc = SPU::Ar32;
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000861 Ops[1] = Op1;
Scott Michel58c58182008-01-17 20:38:41 +0000862 if (Op1.getOpcode() == ISD::Constant) {
863 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000864 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000865 if (isInt<10>(CN->getSExtValue())) {
866 NewOpc = SPU::AIr32;
867 Ops[1] = Op1;
868 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000869 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILr32, dl,
870 N->getValueType(0),
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000871 Op1),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000872 0);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000873 }
Scott Michel58c58182008-01-17 20:38:41 +0000874 }
Scott Michelf0569be2008-12-27 04:51:36 +0000875 Ops[0] = Op0;
Scott Michelf0569be2008-12-27 04:51:36 +0000876 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000877 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000878 }
Scott Michel02d711b2008-12-30 23:28:25 +0000879
Scott Michel58c58182008-01-17 20:38:41 +0000880 if (n_ops > 0) {
881 if (N->hasOneUse())
882 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
883 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000884 return CurDAG->getMachineNode(NewOpc, dl, OpVT, Ops, n_ops);
Scott Michel58c58182008-01-17 20:38:41 +0000885 } else
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000886 return SelectCode(N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000887}
888
Scott Michel02d711b2008-12-30 23:28:25 +0000889/*!
890 * Emit the instruction sequence for i64 left shifts. The basic algorithm
891 * is to fill the bottom two word slots with zeros so that zeros are shifted
892 * in as the entire quadword is shifted left.
893 *
894 * \note This code could also be used to implement v2i64 shl.
895 *
896 * @param Op The shl operand
897 * @param OpVT Op's machine value value type (doesn't need to be passed, but
898 * makes life easier.)
899 * @return The SDNode with the entire instruction sequence
900 */
901SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000902SPUDAGToDAGISel::SelectSHLi64(SDNode *N, EVT OpVT) {
903 SDValue Op0 = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000904 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +0000905 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000906 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000907 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000908 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
909 SDValue SelMaskVal;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000910 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000911
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000912 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
913 Op0, getRC(MVT::v2i64) );
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
Dan Gohman602b0c82009-09-25 18:54:59 +0000915 SelMask = CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
916 ZeroFill = CurDAG->getMachineNode(SPU::ILv2i64, dl, VecVT,
917 CurDAG->getTargetConstant(0, OpVT));
918 VecOp0 = CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
919 SDValue(ZeroFill, 0),
920 SDValue(VecOp0, 0),
921 SDValue(SelMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000922
923 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
924 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
925 unsigned bits = unsigned(CN->getZExtValue()) & 7;
926
927 if (bytes > 0) {
928 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000929 CurDAG->getMachineNode(SPU::SHLQBYIv2i64, dl, VecVT,
930 SDValue(VecOp0, 0),
931 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000932 }
933
934 if (bits > 0) {
935 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000936 CurDAG->getMachineNode(SPU::SHLQBIIv2i64, dl, VecVT,
937 SDValue((Shift != 0 ? Shift : VecOp0), 0),
938 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000939 }
940 } else {
941 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +0000942 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
943 ShiftAmt,
944 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000945 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +0000946 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
947 ShiftAmt,
948 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000949 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000950 CurDAG->getMachineNode(SPU::SHLQBYv2i64, dl, VecVT,
951 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000952 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000953 CurDAG->getMachineNode(SPU::SHLQBIv2i64, dl, VecVT,
954 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000955 }
956
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000957 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000958 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +0000959}
960
961/*!
962 * Emit the instruction sequence for i64 logical right shifts.
963 *
964 * @param Op The shl operand
965 * @param OpVT Op's machine value value type (doesn't need to be passed, but
966 * makes life easier.)
967 * @return The SDNode with the entire instruction sequence
968 */
969SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000970SPUDAGToDAGISel::SelectSRLi64(SDNode *N, EVT OpVT) {
971 SDValue Op0 = N->getOperand(0);
Owen Anderson23b9b192009-08-12 00:36:31 +0000972 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
973 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000974 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000975 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000976 SDNode *VecOp0, *Shift = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000977 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000978
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +0000979 VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
980 Op0, getRC(MVT::v2i64) );
Scott Michel02d711b2008-12-30 23:28:25 +0000981
982 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
983 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
984 unsigned bits = unsigned(CN->getZExtValue()) & 7;
985
986 if (bytes > 0) {
987 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000988 CurDAG->getMachineNode(SPU::ROTQMBYIv2i64, dl, VecVT,
989 SDValue(VecOp0, 0),
990 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000991 }
992
993 if (bits > 0) {
994 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000995 CurDAG->getMachineNode(SPU::ROTQMBIIv2i64, dl, VecVT,
996 SDValue((Shift != 0 ? Shift : VecOp0), 0),
997 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000998 }
999 } else {
1000 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +00001001 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1002 ShiftAmt,
1003 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001004 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +00001005 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1006 ShiftAmt,
1007 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001008
1009 // Ensure that the shift amounts are negated!
Dan Gohman602b0c82009-09-25 18:54:59 +00001010 Bytes = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1011 SDValue(Bytes, 0),
1012 CurDAG->getTargetConstant(0, ShiftAmtVT));
1013
1014 Bits = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1015 SDValue(Bits, 0),
Scott Michel02d711b2008-12-30 23:28:25 +00001016 CurDAG->getTargetConstant(0, ShiftAmtVT));
1017
Scott Michel02d711b2008-12-30 23:28:25 +00001018 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001019 CurDAG->getMachineNode(SPU::ROTQMBYv2i64, dl, VecVT,
1020 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001021 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001022 CurDAG->getMachineNode(SPU::ROTQMBIv2i64, dl, VecVT,
1023 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001024 }
1025
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001026 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001027 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001028}
1029
1030/*!
1031 * Emit the instruction sequence for i64 arithmetic right shifts.
1032 *
1033 * @param Op The shl operand
1034 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1035 * makes life easier.)
1036 * @return The SDNode with the entire instruction sequence
1037 */
1038SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001039SPUDAGToDAGISel::SelectSRAi64(SDNode *N, EVT OpVT) {
Scott Michel02d711b2008-12-30 23:28:25 +00001040 // Promote Op0 to vector
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001041 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
Owen Anderson23b9b192009-08-12 00:36:31 +00001042 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001043 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001044 EVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001045 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +00001046
1047 SDNode *VecOp0 =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001048 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001049 VecVT, N->getOperand(0), getRC(MVT::v2i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001050
1051 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1052 SDNode *SignRot =
Dan Gohman602b0c82009-09-25 18:54:59 +00001053 CurDAG->getMachineNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
1054 SDValue(VecOp0, 0), SignRotAmt);
Scott Michel02d711b2008-12-30 23:28:25 +00001055 SDNode *UpperHalfSign =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001056 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001057 MVT::i32, SDValue(SignRot, 0), getRC(MVT::i32));
Scott Michel02d711b2008-12-30 23:28:25 +00001058
1059 SDNode *UpperHalfSignMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001060 CurDAG->getMachineNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001061 SDNode *UpperLowerMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001062 CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT,
1063 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
Scott Michel02d711b2008-12-30 23:28:25 +00001064 SDNode *UpperLowerSelect =
Dan Gohman602b0c82009-09-25 18:54:59 +00001065 CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
1066 SDValue(UpperHalfSignMask, 0),
1067 SDValue(VecOp0, 0),
1068 SDValue(UpperLowerMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001069
1070 SDNode *Shift = 0;
1071
1072 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1073 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1074 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1075
1076 if (bytes > 0) {
1077 bytes = 31 - bytes;
1078 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001079 CurDAG->getMachineNode(SPU::ROTQBYIv2i64, dl, VecVT,
1080 SDValue(UpperLowerSelect, 0),
1081 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001082 }
1083
1084 if (bits > 0) {
1085 bits = 8 - bits;
1086 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001087 CurDAG->getMachineNode(SPU::ROTQBIIv2i64, dl, VecVT,
1088 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1089 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001090 }
1091 } else {
1092 SDNode *NegShift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001093 CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1094 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001095
1096 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001097 CurDAG->getMachineNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
1098 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001099 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001100 CurDAG->getMachineNode(SPU::ROTQBIv2i64, dl, VecVT,
1101 SDValue(Shift, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001102 }
1103
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001104 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001105 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
Scott Michel02d711b2008-12-30 23:28:25 +00001106}
1107
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001108/*!
1109 Do the necessary magic necessary to load a i64 constant
1110 */
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001111SDNode *SPUDAGToDAGISel::SelectI64Constant(SDNode *N, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001112 DebugLoc dl) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001113 ConstantSDNode *CN = cast<ConstantSDNode>(N);
Scott Michel7ea02ff2009-03-17 01:15:45 +00001114 return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
1115}
1116
Owen Andersone50ed302009-08-10 22:56:29 +00001117SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001118 DebugLoc dl) {
Owen Anderson23b9b192009-08-12 00:36:31 +00001119 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001120 SDValue i64vec =
Scott Michel7ea02ff2009-03-17 01:15:45 +00001121 SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001122
1123 // Here's where it gets interesting, because we have to parse out the
1124 // subtree handed back in i64vec:
1125
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001126 if (i64vec.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001127 // The degenerate case where the upper and lower bits in the splat are
1128 // identical:
1129 SDValue Op0 = i64vec.getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001130
Scott Michel9de57a92009-01-26 22:33:37 +00001131 ReplaceUses(i64vec, Op0);
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001132 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1133 SDValue(emitBuildVector(Op0.getNode()), 0),
1134 getRC(MVT::i64));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001135 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1136 SDValue lhs = i64vec.getOperand(0);
1137 SDValue rhs = i64vec.getOperand(1);
1138 SDValue shufmask = i64vec.getOperand(2);
1139
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001140 if (lhs.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001141 ReplaceUses(lhs, lhs.getOperand(0));
1142 lhs = lhs.getOperand(0);
1143 }
1144
1145 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1146 ? lhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001147 : emitBuildVector(lhs.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001148
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001149 if (rhs.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001150 ReplaceUses(rhs, rhs.getOperand(0));
1151 rhs = rhs.getOperand(0);
1152 }
1153
1154 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1155 ? rhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001156 : emitBuildVector(rhs.getNode()));
Scott Michel9de57a92009-01-26 22:33:37 +00001157
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001158 if (shufmask.getOpcode() == ISD::BITCAST) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001159 ReplaceUses(shufmask, shufmask.getOperand(0));
1160 shufmask = shufmask.getOperand(0);
1161 }
1162
1163 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1164 ? shufmask.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001165 : emitBuildVector(shufmask.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001166
Chris Lattnera8e76142010-02-23 05:30:43 +00001167 SDValue shufNode =
1168 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001169 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
Chris Lattnera8e76142010-02-23 05:30:43 +00001170 SDValue(shufMaskNode, 0));
1171 HandleSDNode Dummy(shufNode);
1172 SDNode *SN = SelectCode(Dummy.getValue().getNode());
1173 if (SN == 0) SN = Dummy.getValue().getNode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001174
1175 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001176 OpVT, SDValue(SN, 0), getRC(MVT::i64));
Scott Michel7ea02ff2009-03-17 01:15:45 +00001177 } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
Kalle Raiskila1cd1b0b2010-09-16 12:29:33 +00001178 return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1179 SDValue(emitBuildVector(i64vec.getNode()), 0),
1180 getRC(MVT::i64));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001181 } else {
Chris Lattner75361b62010-04-07 22:58:41 +00001182 report_fatal_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
Torok Edwindac237e2009-07-08 20:53:28 +00001183 "condition");
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001184 }
1185}
1186
Scott Michel02d711b2008-12-30 23:28:25 +00001187/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001188/// SPU-specific DAG, ready for instruction scheduling.
1189///
1190FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1191 return new SPUDAGToDAGISel(TM);
1192}