blob: 371c25b1e83e32fb6f1663fa7d0aa8fac912d282 [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
114 //===------------------------------------------------------------------===//
Owen Andersone50ed302009-08-10 22:56:29 +0000115 //! EVT to "useful stuff" mapping structure:
Scott Michel266bc8f2007-12-04 22:23:35 +0000116
117 struct valtype_map_s {
Owen Andersone50ed302009-08-10 22:56:29 +0000118 EVT VT;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000119 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
Scott Michela59d4692008-02-23 18:41:37 +0000120 bool ldresult_imm; /// LDRESULT instruction requires immediate?
Scott Michelf0569be2008-12-27 04:51:36 +0000121 unsigned lrinst; /// LR instruction
Scott Michel266bc8f2007-12-04 22:23:35 +0000122 };
123
124 const valtype_map_s valtype_map[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 { MVT::i8, SPU::ORBIr8, true, SPU::LRr8 },
126 { MVT::i16, SPU::ORHIr16, true, SPU::LRr16 },
127 { MVT::i32, SPU::ORIr32, true, SPU::LRr32 },
128 { MVT::i64, SPU::ORr64, false, SPU::LRr64 },
129 { MVT::f32, SPU::ORf32, false, SPU::LRf32 },
130 { MVT::f64, SPU::ORf64, false, SPU::LRf64 },
Scott Michel58c58182008-01-17 20:38:41 +0000131 // vector types... (sigh!)
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 { MVT::v16i8, 0, false, SPU::LRv16i8 },
133 { MVT::v8i16, 0, false, SPU::LRv8i16 },
134 { MVT::v4i32, 0, false, SPU::LRv4i32 },
135 { MVT::v2i64, 0, false, SPU::LRv2i64 },
136 { MVT::v4f32, 0, false, SPU::LRv4f32 },
137 { MVT::v2f64, 0, false, SPU::LRv2f64 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000138 };
139
140 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
141
Owen Andersone50ed302009-08-10 22:56:29 +0000142 const valtype_map_s *getValueTypeMapEntry(EVT VT)
Scott Michel266bc8f2007-12-04 22:23:35 +0000143 {
144 const valtype_map_s *retval = 0;
145 for (size_t i = 0; i < n_valtype_map; ++i) {
146 if (valtype_map[i].VT == VT) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000147 retval = valtype_map + i;
148 break;
Scott Michel266bc8f2007-12-04 22:23:35 +0000149 }
150 }
151
152
153#ifndef NDEBUG
154 if (retval == 0) {
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000155 report_fatal_error("SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns"
156 "NULL for " + Twine(VT.getEVTString()));
Scott Michel266bc8f2007-12-04 22:23:35 +0000157 }
158#endif
159
160 return retval;
161 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000162
Scott Michel7ea02ff2009-03-17 01:15:45 +0000163 //! Generate the carry-generate shuffle mask.
164 SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
165 SmallVector<SDValue, 16 > ShufBytes;
Dan Gohman844731a2008-05-13 00:00:25 +0000166
Scott Michel7ea02ff2009-03-17 01:15:45 +0000167 // Create the shuffle mask for "rotating" the borrow up one register slot
168 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
170 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
171 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
172 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +0000173
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000175 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000176 }
Scott Michel02d711b2008-12-30 23:28:25 +0000177
Scott Michel7ea02ff2009-03-17 01:15:45 +0000178 //! Generate the borrow-generate shuffle mask
179 SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
180 SmallVector<SDValue, 16 > ShufBytes;
181
182 // Create the shuffle mask for "rotating" the borrow up one register slot
183 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
185 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
186 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
187 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000188
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000190 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000191 }
192
Scott Michel7ea02ff2009-03-17 01:15:45 +0000193 //===------------------------------------------------------------------===//
194 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
195 /// instructions for SelectionDAG operations.
196 ///
197 class SPUDAGToDAGISel :
198 public SelectionDAGISel
199 {
Dan Gohmand858e902010-04-17 15:26:15 +0000200 const SPUTargetMachine &TM;
201 const SPUTargetLowering &SPUtli;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000202 unsigned GlobalBaseReg;
Scott Michel02d711b2008-12-30 23:28:25 +0000203
Scott Michel7ea02ff2009-03-17 01:15:45 +0000204 public:
205 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
206 SelectionDAGISel(tm),
207 TM(tm),
208 SPUtli(*tm.getTargetLowering())
209 { }
210
Dan Gohmanad2afc22009-07-31 18:16:33 +0000211 virtual bool runOnMachineFunction(MachineFunction &MF) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000212 // Make sure we re-emit a set of the global base reg if necessary
213 GlobalBaseReg = 0;
Dan Gohmanad2afc22009-07-31 18:16:33 +0000214 SelectionDAGISel::runOnMachineFunction(MF);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000215 return true;
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000216 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000217
Scott Michel7ea02ff2009-03-17 01:15:45 +0000218 /// getI32Imm - Return a target constant with the specified value, of type
219 /// i32.
220 inline SDValue getI32Imm(uint32_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 return CurDAG->getTargetConstant(Imm, MVT::i32);
Scott Michel94bd57e2009-01-15 04:41:47 +0000222 }
223
Scott Michel7ea02ff2009-03-17 01:15:45 +0000224 /// getSmallIPtrImm - Return a target constant of pointer type.
225 inline SDValue getSmallIPtrImm(unsigned Imm) {
226 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Chris Lattner17aa6802010-09-04 18:12:00 +0000227 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000228
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000229 SDNode *emitBuildVector(SDNode *bvNode) {
230 EVT vecVT = bvNode->getValueType(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000231 DebugLoc dl = bvNode->getDebugLoc();
232
233 // Check to see if this vector can be represented as a CellSPU immediate
234 // constant by invoking all of the instruction selection predicates:
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 if (((vecVT == MVT::v8i16) &&
236 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
237 ((vecVT == MVT::v4i32) &&
238 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
239 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
240 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
Scott Michel7ea02ff2009-03-17 01:15:45 +0000241 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 ((vecVT == MVT::v2i64) &&
243 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
244 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
Chris Lattnera8e76142010-02-23 05:30:43 +0000245 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)))) {
246 HandleSDNode Dummy(SDValue(bvNode, 0));
247 if (SDNode *N = Select(bvNode))
248 return N;
249 return Dummy.getValue().getNode();
250 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000251
252 // No, need to emit a constant pool spill:
253 std::vector<Constant*> CV;
254
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000255 for (size_t i = 0; i < bvNode->getNumOperands(); ++i) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000256 ConstantSDNode *V = cast<ConstantSDNode > (bvNode->getOperand(i));
Chris Lattnera8e76142010-02-23 05:30:43 +0000257 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000258 }
259
Dan Gohman46510a72010-04-15 01:51:59 +0000260 const Constant *CP = ConstantVector::get(CV);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000261 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
262 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
263 SDValue CGPoolOffset =
Dan Gohmand858e902010-04-17 15:26:15 +0000264 SPU::LowerConstantPool(CPIdx, *CurDAG, TM);
Chris Lattnera8e76142010-02-23 05:30:43 +0000265
266 HandleSDNode Dummy(CurDAG->getLoad(vecVT, dl,
267 CurDAG->getEntryNode(), CGPoolOffset,
268 PseudoSourceValue::getConstantPool(),0,
269 false, false, Alignment));
270 CurDAG->ReplaceAllUsesWith(SDValue(bvNode, 0), Dummy.getValue());
271 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
272 return N;
273 return Dummy.getValue().getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000274 }
Scott Michel02d711b2008-12-30 23:28:25 +0000275
Scott Michel7ea02ff2009-03-17 01:15:45 +0000276 /// Select - Convert the specified operand from a target-independent to a
277 /// target-specific node if it hasn't already been changed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000278 SDNode *Select(SDNode *N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000279
Scott Michel7ea02ff2009-03-17 01:15:45 +0000280 //! Emit the instruction sequence for i64 shl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000281 SDNode *SelectSHLi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000282
Scott Michel7ea02ff2009-03-17 01:15:45 +0000283 //! Emit the instruction sequence for i64 srl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000284 SDNode *SelectSRLi64(SDNode *N, EVT OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000285
Scott Michel7ea02ff2009-03-17 01:15:45 +0000286 //! Emit the instruction sequence for i64 sra
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000287 SDNode *SelectSRAi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000288
Scott Michel7ea02ff2009-03-17 01:15:45 +0000289 //! Emit the necessary sequence for loading i64 constants:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000290 SDNode *SelectI64Constant(SDNode *N, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000291
292 //! Alternate instruction emit sequence for loading i64 constants
Owen Andersone50ed302009-08-10 22:56:29 +0000293 SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000294
295 //! Returns true if the address N is an A-form (local store) address
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000296 bool SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000297 SDValue &Index);
298
299 //! D-form address predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000300 bool SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000301 SDValue &Index);
302
303 /// Alternate D-form address using i7 offset predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000304 bool SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000305 SDValue &Base);
306
307 /// D-form address selection workhorse
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000308 bool DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000309 SDValue &Base, int minOffset, int maxOffset);
310
311 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000312 bool SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000313 SDValue &Index);
314
315 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
316 /// inline asm expressions.
317 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
318 char ConstraintCode,
319 std::vector<SDValue> &OutOps) {
320 SDValue Op0, Op1;
321 switch (ConstraintCode) {
322 default: return true;
323 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000324 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
325 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1))
326 SelectXFormAddr(Op.getNode(), Op, Op0, Op1);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000327 break;
328 case 'o': // offsetable
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000329 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
330 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1)) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000331 Op0 = Op;
332 Op1 = getSmallIPtrImm(0);
333 }
334 break;
335 case 'v': // not offsetable
336#if 1
Torok Edwinc23197a2009-07-14 16:55:14 +0000337 llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
Scott Michel7ea02ff2009-03-17 01:15:45 +0000338#else
339 SelectAddrIdxOnly(Op, Op, Op0, Op1);
340#endif
341 break;
342 }
343
344 OutOps.push_back(Op0);
345 OutOps.push_back(Op1);
346 return false;
347 }
348
Scott Michel7ea02ff2009-03-17 01:15:45 +0000349 virtual const char *getPassName() const {
350 return "Cell SPU DAG->DAG Pattern Instruction Selection";
351 }
352
353 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
354 /// this target when scheduling the DAG.
355 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
356 const TargetInstrInfo *II = TM.getInstrInfo();
357 assert(II && "No InstrInfo?");
358 return new SPUHazardRecognizer(*II);
359 }
360
361 // Include the pieces autogenerated from the target description.
Scott Michel266bc8f2007-12-04 22:23:35 +0000362#include "SPUGenDAGISel.inc"
Scott Michel7ea02ff2009-03-17 01:15:45 +0000363 };
Dan Gohman844731a2008-05-13 00:00:25 +0000364}
365
Scott Michel266bc8f2007-12-04 22:23:35 +0000366/*!
Scott Michel9de57a92009-01-26 22:33:37 +0000367 \arg Op The ISD instruction operand
Scott Michel266bc8f2007-12-04 22:23:35 +0000368 \arg N The address to be tested
369 \arg Base The base address
370 \arg Index The base address index
371 */
372bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000373SPUDAGToDAGISel::SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000374 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000375 // These match the addr256k operand type:
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 EVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000377 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000378
379 switch (N.getOpcode()) {
380 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000381 case ISD::ConstantPool:
382 case ISD::GlobalAddress:
Chris Lattner75361b62010-04-07 22:58:41 +0000383 report_fatal_error("SPU SelectAFormAddr: Constant/Pool/Global not lowered.");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000384 /*NOTREACHED*/
385
Scott Michel053c1da2008-01-29 02:16:57 +0000386 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000387 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000388 case ISD::TargetJumpTable:
Chris Lattner75361b62010-04-07 22:58:41 +0000389 report_fatal_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
Torok Edwindac237e2009-07-08 20:53:28 +0000390 "not wrapped as A-form address.");
Scott Michel053c1da2008-01-29 02:16:57 +0000391 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000392
Scott Michel02d711b2008-12-30 23:28:25 +0000393 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000394 // Just load from memory if there's only a single use of the location,
395 // otherwise, this will get handled below with D-form offset addresses
396 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000397 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000398 switch (Op0.getOpcode()) {
399 case ISD::TargetConstantPool:
400 case ISD::TargetJumpTable:
401 Base = Op0;
402 Index = Zero;
403 return true;
404
405 case ISD::TargetGlobalAddress: {
406 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
Dan Gohman46510a72010-04-15 01:51:59 +0000407 const GlobalValue *GV = GSDN->getGlobal();
Scott Michel053c1da2008-01-29 02:16:57 +0000408 if (GV->getAlignment() == 16) {
409 Base = Op0;
410 Index = Zero;
411 return true;
412 }
413 break;
414 }
415 }
416 }
417 break;
418 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000419 return false;
420}
421
Scott Michel02d711b2008-12-30 23:28:25 +0000422bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000423SPUDAGToDAGISel::SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Dan Gohman475871a2008-07-27 21:46:04 +0000424 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000425 const int minDForm2Offset = -(1 << 7);
426 const int maxDForm2Offset = (1 << 7) - 1;
427 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
428 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000429}
430
Scott Michel266bc8f2007-12-04 22:23:35 +0000431/*!
432 \arg Op The ISD instruction (ignored)
433 \arg N The address to be tested
434 \arg Base Base address register/pointer
435 \arg Index Base address index
436
437 Examine the input address by a base register plus a signed 10-bit
438 displacement, [r+I10] (D-form address).
439
440 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000441 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000442*/
443bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000444SPUDAGToDAGISel::SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000445 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000446 return DFormAddressPredicate(Op, N, Base, Index,
Scott Michel9c0c6b22008-11-21 02:56:16 +0000447 SPUFrameInfo::minFrameOffset(),
448 SPUFrameInfo::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000449}
450
451bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000452SPUDAGToDAGISel::DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000453 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000454 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000455 unsigned Opc = N.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +0000456 EVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000457
Scott Michel053c1da2008-01-29 02:16:57 +0000458 if (Opc == ISD::FrameIndex) {
459 // Stack frame index must be less than 512 (divided by 16):
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000460 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(N);
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::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000463 << FI << "\n");
464 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000465 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000466 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000467 return true;
468 }
469 } else if (Opc == ISD::ADD) {
470 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000471 const SDValue Op0 = N.getOperand(0);
472 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000473
Scott Michel053c1da2008-01-29 02:16:57 +0000474 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
475 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
476 Base = CurDAG->getTargetConstant(0, PtrTy);
477 Index = N;
478 return true;
479 } else if (Op1.getOpcode() == ISD::Constant
480 || Op1.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000481 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000482 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000483
Scott Michel053c1da2008-01-29 02:16:57 +0000484 if (Op0.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000485 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op0);
Scott Michel203b2d62008-04-30 00:30:08 +0000486 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000487 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000488 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000489
Scott Michel203b2d62008-04-30 00:30:08 +0000490 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000491 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000492 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000493 return true;
494 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000495 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000496 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000497 Index = Op0;
498 return true;
499 }
500 } else if (Op0.getOpcode() == ISD::Constant
501 || Op0.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000502 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000503 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000504
505 if (Op1.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000506 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op1);
Scott Michel203b2d62008-04-30 00:30:08 +0000507 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000508 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000509 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000510
Scott Michel203b2d62008-04-30 00:30:08 +0000511 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000512 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000513 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000514 return true;
515 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000516 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000517 Base = CurDAG->getTargetConstant(offset, PtrTy);
518 Index = Op1;
519 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000520 }
Scott Michel053c1da2008-01-29 02:16:57 +0000521 }
522 } else if (Opc == SPUISD::IndirectAddr) {
523 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000524 const SDValue Op0 = N.getOperand(0);
525 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000526
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000527 if (Op0.getOpcode() == SPUISD::Hi
528 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000529 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000530 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000531 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000532 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000533 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
534 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000535 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000536
537 if (isa<ConstantSDNode>(Op1)) {
538 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000539 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000540 idxOp = Op0;
541 } else if (isa<ConstantSDNode>(Op0)) {
542 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000543 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000544 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000545 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000546
547 if (offset >= minOffset && offset <= maxOffset) {
548 Base = CurDAG->getTargetConstant(offset, PtrTy);
549 Index = idxOp;
550 return true;
551 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000552 }
Scott Michel053c1da2008-01-29 02:16:57 +0000553 } else if (Opc == SPUISD::AFormAddr) {
554 Base = CurDAG->getTargetConstant(0, N.getValueType());
555 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000556 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000557 } else if (Opc == SPUISD::LDRESULT) {
558 Base = CurDAG->getTargetConstant(0, N.getValueType());
559 Index = N;
560 return true;
Kalle Raiskilac6166c62010-06-09 08:29:41 +0000561 } else if (Opc == ISD::Register
562 ||Opc == ISD::CopyFromReg
Kalle Raiskilabc2697c2010-08-04 13:59:48 +0000563 ||Opc == ISD::UNDEF
564 ||Opc == ISD::Constant) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000565 unsigned OpOpc = Op->getOpcode();
Scott Michel9c0c6b22008-11-21 02:56:16 +0000566
567 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
568 // Direct load/store without getelementptr
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000569 SDValue Offs;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000570
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000571 Offs = ((OpOpc == ISD::STORE) ? Op->getOperand(3) : Op->getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000572
573 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
574 if (Offs.getOpcode() == ISD::UNDEF)
575 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
576
577 Base = Offs;
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000578 Index = N;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000579 return true;
580 }
Scott Michelaedc6372008-12-10 00:15:19 +0000581 } else {
582 /* If otherwise unadorned, default to D-form address with 0 offset: */
583 if (Opc == ISD::CopyFromReg) {
Scott Michel19c10e62009-01-26 03:37:41 +0000584 Index = N.getOperand(1);
Scott Michelaedc6372008-12-10 00:15:19 +0000585 } else {
Scott Michel19c10e62009-01-26 03:37:41 +0000586 Index = N;
Scott Michelaedc6372008-12-10 00:15:19 +0000587 }
588
589 Base = CurDAG->getTargetConstant(0, Index.getValueType());
590 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000591 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000592 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000593
Scott Michel266bc8f2007-12-04 22:23:35 +0000594 return false;
595}
596
597/*!
598 \arg Op The ISD instruction operand
599 \arg N The address operand
600 \arg Base The base pointer operand
601 \arg Index The offset/index operand
602
Scott Michel9c0c6b22008-11-21 02:56:16 +0000603 If the address \a N can be expressed as an A-form or D-form address, returns
604 false. Otherwise, creates two operands, Base and Index that will become the
605 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000606*/
607bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000608SPUDAGToDAGISel::SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000609 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000610 if (!SelectAFormAddr(Op, N, Base, Index)
611 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000612 // If the address is neither A-form or D-form, punt and use an X-form
613 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000614 Base = N.getOperand(1);
615 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000616 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000617 }
618
619 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000620}
621
Scott Michel266bc8f2007-12-04 22:23:35 +0000622//! Convert the operand from a target-independent to a target-specific node
623/*!
624 */
625SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000626SPUDAGToDAGISel::Select(SDNode *N) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000627 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000628 int n_ops = -1;
629 unsigned NewOpc;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000630 EVT OpVT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000631 SDValue Ops[8];
Dale Johannesened2eee62009-02-06 01:31:28 +0000632 DebugLoc dl = N->getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +0000633
Chris Lattnera8e76142010-02-23 05:30:43 +0000634 if (N->isMachineOpcode())
Scott Michel266bc8f2007-12-04 22:23:35 +0000635 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000636
637 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000638 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000639 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
640 SDValue Imm0 = CurDAG->getTargetConstant(0, N->getValueType(0));
Scott Michel266bc8f2007-12-04 22:23:35 +0000641
Scott Michel02d711b2008-12-30 23:28:25 +0000642 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000643 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000644 Ops[0] = TFI;
645 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000646 n_ops = 2;
647 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000648 NewOpc = SPU::Ar32;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000649 Ops[0] = CurDAG->getRegister(SPU::R1, N->getValueType(0));
Dan Gohman602b0c82009-09-25 18:54:59 +0000650 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILAr32, dl,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000651 N->getValueType(0), TFI, Imm0),
Dan Gohman602b0c82009-09-25 18:54:59 +0000652 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000653 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000654 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000656 // Catch the i64 constants that end up here. Note: The backend doesn't
657 // attempt to legalize the constant (it's useless because DAGCombiner
658 // will insert 64-bit constants and we can't stop it).
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000659 return SelectI64Constant(N, OpVT, N->getDebugLoc());
Scott Michel94bd57e2009-01-15 04:41:47 +0000660 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 && OpVT == MVT::i64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000662 SDValue Op0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000663 EVT Op0VT = Op0.getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +0000664 EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
665 Op0VT, (128 / Op0VT.getSizeInBits()));
666 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(),
667 OpVT, (128 / OpVT.getSizeInBits()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000668 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000669
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 switch (Op0VT.getSimpleVT().SimpleTy) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000671 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000672 report_fatal_error("CellSPU Select: Unhandled zero/any extend EVT");
Scott Michel94bd57e2009-01-15 04:41:47 +0000673 /*NOTREACHED*/
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 case MVT::i32:
675 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
676 CurDAG->getConstant(0x80808080, MVT::i32),
677 CurDAG->getConstant(0x00010203, MVT::i32),
678 CurDAG->getConstant(0x80808080, MVT::i32),
679 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000680 break;
681
Owen Anderson825b72b2009-08-11 20:47:22 +0000682 case MVT::i16:
683 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
684 CurDAG->getConstant(0x80808080, MVT::i32),
685 CurDAG->getConstant(0x80800203, MVT::i32),
686 CurDAG->getConstant(0x80808080, MVT::i32),
687 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000688 break;
689
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 case MVT::i8:
691 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
692 CurDAG->getConstant(0x80808080, MVT::i32),
693 CurDAG->getConstant(0x80808003, MVT::i32),
694 CurDAG->getConstant(0x80808080, MVT::i32),
695 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000696 break;
Scott Michel58c58182008-01-17 20:38:41 +0000697 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000698
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000699 SDNode *shufMaskLoad = emitBuildVector(shufMask.getNode());
Chris Lattnera8e76142010-02-23 05:30:43 +0000700
701 HandleSDNode PromoteScalar(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl,
702 Op0VecVT, Op0));
703
704 SDValue PromScalar;
705 if (SDNode *N = SelectCode(PromoteScalar.getValue().getNode()))
706 PromScalar = SDValue(N, 0);
707 else
708 PromScalar = PromoteScalar.getValue();
709
Scott Michel94bd57e2009-01-15 04:41:47 +0000710 SDValue zextShuffle =
Dale Johannesened2eee62009-02-06 01:31:28 +0000711 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Chris Lattnera8e76142010-02-23 05:30:43 +0000712 PromScalar, PromScalar,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000713 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000714
Chris Lattnera8e76142010-02-23 05:30:43 +0000715 HandleSDNode Dummy2(zextShuffle);
716 if (SDNode *N = SelectCode(Dummy2.getValue().getNode()))
717 zextShuffle = SDValue(N, 0);
718 else
719 zextShuffle = Dummy2.getValue();
720 HandleSDNode Dummy(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
721 zextShuffle));
722
723 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
724 SelectCode(Dummy.getValue().getNode());
725 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000727 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000728 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000729
Chris Lattnera8e76142010-02-23 05:30:43 +0000730 HandleSDNode Dummy(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
731 N->getOperand(0), N->getOperand(1),
732 SDValue(CGLoad, 0)));
733
734 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
735 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
736 return N;
737 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000738 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000739 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000740 emitBuildVector(getBorrowGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000741
Chris Lattnera8e76142010-02-23 05:30:43 +0000742 HandleSDNode Dummy(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
743 N->getOperand(0), N->getOperand(1),
744 SDValue(CGLoad, 0)));
745
746 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
747 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
748 return N;
749 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000751 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000752 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000753
Chris Lattnera8e76142010-02-23 05:30:43 +0000754 HandleSDNode Dummy(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
755 N->getOperand(0), N->getOperand(1),
756 SDValue(CGLoad, 0)));
757 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
758 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
759 return N;
760 return Dummy.getValue().getNode();
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000761 } else if (Opc == ISD::TRUNCATE) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000762 SDValue Op0 = N->getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000763 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 && OpVT == MVT::i32
765 && Op0.getValueType() == MVT::i64) {
Scott Michel9de57a92009-01-26 22:33:37 +0000766 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
767 //
768 // Take advantage of the fact that the upper 32 bits are in the
769 // i32 preferred slot and avoid shuffle gymnastics:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000770 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
771 if (CN != 0) {
772 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000773
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000774 if (shift_amt >= 32) {
775 SDNode *hi32 =
Dan Gohman602b0c82009-09-25 18:54:59 +0000776 CurDAG->getMachineNode(SPU::ORr32_r64, dl, OpVT,
777 Op0.getOperand(0));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000778
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000779 shift_amt -= 32;
780 if (shift_amt > 0) {
781 // Take care of the additional shift, if present:
Owen Anderson825b72b2009-08-11 20:47:22 +0000782 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000783 unsigned Opc = SPU::ROTMAIr32_i32;
Scott Michel9de57a92009-01-26 22:33:37 +0000784
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000785 if (Op0.getOpcode() == ISD::SRL)
786 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000787
Dan Gohman602b0c82009-09-25 18:54:59 +0000788 hi32 = CurDAG->getMachineNode(Opc, dl, OpVT, SDValue(hi32, 0),
789 shift);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000790 }
791
792 return hi32;
793 }
794 }
795 }
Scott Michel02d711b2008-12-30 23:28:25 +0000796 } else if (Opc == ISD::SHL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000797 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000798 return SelectSHLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000799 } else if (Opc == ISD::SRL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000800 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000801 return SelectSRLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000802 } else if (Opc == ISD::SRA) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000803 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000804 return SelectSRAi64(N, OpVT);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000805 } else if (Opc == ISD::FNEG
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000807 DebugLoc dl = N->getDebugLoc();
Scott Michel7ea02ff2009-03-17 01:15:45 +0000808 // Check if the pattern is a special form of DFNMS:
809 // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000810 SDValue Op0 = N->getOperand(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000811 if (Op0.getOpcode() == ISD::FSUB) {
812 SDValue Op00 = Op0.getOperand(0);
813 if (Op00.getOpcode() == ISD::FMUL) {
814 unsigned Opc = SPU::DFNMSf64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 if (OpVT == MVT::v2f64)
Scott Michel7ea02ff2009-03-17 01:15:45 +0000816 Opc = SPU::DFNMSv2f64;
817
Dan Gohman602b0c82009-09-25 18:54:59 +0000818 return CurDAG->getMachineNode(Opc, dl, OpVT,
819 Op00.getOperand(0),
820 Op00.getOperand(1),
821 Op0.getOperand(1));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000822 }
823 }
824
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000826 SDNode *signMask = 0;
Scott Michela82d3f72009-03-17 16:45:16 +0000827 unsigned Opc = SPU::XORfneg64;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 if (OpVT == MVT::f64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000830 signMask = SelectI64Constant(negConst.getNode(), MVT::i64, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +0000831 } else if (OpVT == MVT::v2f64) {
Scott Michela82d3f72009-03-17 16:45:16 +0000832 Opc = SPU::XORfnegvec;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000833 signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 MVT::v2i64,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000835 negConst, negConst).getNode());
Scott Michel7ea02ff2009-03-17 01:15:45 +0000836 }
837
Dan Gohman602b0c82009-09-25 18:54:59 +0000838 return CurDAG->getMachineNode(Opc, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000839 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000840 } else if (Opc == ISD::FABS) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 if (OpVT == MVT::f64) {
842 SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
Dan Gohman602b0c82009-09-25 18:54:59 +0000843 return CurDAG->getMachineNode(SPU::ANDfabs64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000844 N->getOperand(0), SDValue(signMask, 0));
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 } else if (OpVT == MVT::v2f64) {
846 SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
847 SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000848 absConst, absConst);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000849 SDNode *signMask = emitBuildVector(absVec.getNode());
Dan Gohman602b0c82009-09-25 18:54:59 +0000850 return CurDAG->getMachineNode(SPU::ANDfabsvec, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000851 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000852 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000853 } else if (Opc == SPUISD::LDRESULT) {
854 // Custom select instructions for LDRESULT
Owen Andersone50ed302009-08-10 22:56:29 +0000855 EVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000856 SDValue Arg = N->getOperand(0);
857 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000858 SDNode *Result;
Scott Michela59d4692008-02-23 18:41:37 +0000859 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
860
861 if (vtm->ldresult_ins == 0) {
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000862 report_fatal_error("LDRESULT for unsupported type: " +
863 Twine(VT.getEVTString()));
Scott Michela59d4692008-02-23 18:41:37 +0000864 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000865
Scott Michela59d4692008-02-23 18:41:37 +0000866 Opc = vtm->ldresult_ins;
867 if (vtm->ldresult_imm) {
Dan Gohman475871a2008-07-27 21:46:04 +0000868 SDValue Zero = CurDAG->getTargetConstant(0, VT);
Scott Michel86c041f2007-12-20 00:44:13 +0000869
Dan Gohman602b0c82009-09-25 18:54:59 +0000870 Result = CurDAG->getMachineNode(Opc, dl, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000871 } else {
Dan Gohman602b0c82009-09-25 18:54:59 +0000872 Result = CurDAG->getMachineNode(Opc, dl, VT, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000873 }
874
Scott Michel266bc8f2007-12-04 22:23:35 +0000875 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000876 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000877 // Look at the operands: SelectCode() will catch the cases that aren't
878 // specifically handled here.
879 //
880 // SPUInstrInfo catches the following patterns:
881 // (SPUindirect (SPUhi ...), (SPUlo ...))
882 // (SPUindirect $sp, imm)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000883 EVT VT = N->getValueType(0);
Scott Michelf0569be2008-12-27 04:51:36 +0000884 SDValue Op0 = N->getOperand(0);
885 SDValue Op1 = N->getOperand(1);
886 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000887
Scott Michelf0569be2008-12-27 04:51:36 +0000888 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
889 || (Op0.getOpcode() == ISD::Register
890 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
891 && RN->getReg() != SPU::R1))) {
892 NewOpc = SPU::Ar32;
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000893 Ops[1] = Op1;
Scott Michel58c58182008-01-17 20:38:41 +0000894 if (Op1.getOpcode() == ISD::Constant) {
895 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000896 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000897 if (isInt<10>(CN->getSExtValue())) {
898 NewOpc = SPU::AIr32;
899 Ops[1] = Op1;
900 } else {
901 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILr32, dl,
902 N->getValueType(0),
903 Op1),
904 0);
905 }
Scott Michel58c58182008-01-17 20:38:41 +0000906 }
Scott Michelf0569be2008-12-27 04:51:36 +0000907 Ops[0] = Op0;
Scott Michelf0569be2008-12-27 04:51:36 +0000908 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000909 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000910 }
Scott Michel02d711b2008-12-30 23:28:25 +0000911
Scott Michel58c58182008-01-17 20:38:41 +0000912 if (n_ops > 0) {
913 if (N->hasOneUse())
914 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
915 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000916 return CurDAG->getMachineNode(NewOpc, dl, OpVT, Ops, n_ops);
Scott Michel58c58182008-01-17 20:38:41 +0000917 } else
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000918 return SelectCode(N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000919}
920
Scott Michel02d711b2008-12-30 23:28:25 +0000921/*!
922 * Emit the instruction sequence for i64 left shifts. The basic algorithm
923 * is to fill the bottom two word slots with zeros so that zeros are shifted
924 * in as the entire quadword is shifted left.
925 *
926 * \note This code could also be used to implement v2i64 shl.
927 *
928 * @param Op The shl operand
929 * @param OpVT Op's machine value value type (doesn't need to be passed, but
930 * makes life easier.)
931 * @return The SDNode with the entire instruction sequence
932 */
933SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000934SPUDAGToDAGISel::SelectSHLi64(SDNode *N, EVT OpVT) {
935 SDValue Op0 = N->getOperand(0);
Owen Anderson23b9b192009-08-12 00:36:31 +0000936 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
937 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000938 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000939 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000940 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
941 SDValue SelMaskVal;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000942 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000943
Dan Gohman602b0c82009-09-25 18:54:59 +0000944 VecOp0 = CurDAG->getMachineNode(SPU::ORv2i64_i64, dl, VecVT, Op0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000945 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
Dan Gohman602b0c82009-09-25 18:54:59 +0000946 SelMask = CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
947 ZeroFill = CurDAG->getMachineNode(SPU::ILv2i64, dl, VecVT,
948 CurDAG->getTargetConstant(0, OpVT));
949 VecOp0 = CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
950 SDValue(ZeroFill, 0),
951 SDValue(VecOp0, 0),
952 SDValue(SelMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000953
954 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
955 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
956 unsigned bits = unsigned(CN->getZExtValue()) & 7;
957
958 if (bytes > 0) {
959 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000960 CurDAG->getMachineNode(SPU::SHLQBYIv2i64, dl, VecVT,
961 SDValue(VecOp0, 0),
962 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000963 }
964
965 if (bits > 0) {
966 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000967 CurDAG->getMachineNode(SPU::SHLQBIIv2i64, dl, VecVT,
968 SDValue((Shift != 0 ? Shift : VecOp0), 0),
969 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000970 }
971 } else {
972 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +0000973 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
974 ShiftAmt,
975 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000976 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +0000977 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
978 ShiftAmt,
979 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +0000980 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000981 CurDAG->getMachineNode(SPU::SHLQBYv2i64, dl, VecVT,
982 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000983 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +0000984 CurDAG->getMachineNode(SPU::SHLQBIv2i64, dl, VecVT,
985 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000986 }
987
Dan Gohman602b0c82009-09-25 18:54:59 +0000988 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000989}
990
991/*!
992 * Emit the instruction sequence for i64 logical right shifts.
993 *
994 * @param Op The shl operand
995 * @param OpVT Op's machine value value type (doesn't need to be passed, but
996 * makes life easier.)
997 * @return The SDNode with the entire instruction sequence
998 */
999SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001000SPUDAGToDAGISel::SelectSRLi64(SDNode *N, EVT OpVT) {
1001 SDValue Op0 = N->getOperand(0);
Owen Anderson23b9b192009-08-12 00:36:31 +00001002 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
1003 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001004 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001005 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +00001006 SDNode *VecOp0, *Shift = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001007 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +00001008
Dan Gohman602b0c82009-09-25 18:54:59 +00001009 VecOp0 = CurDAG->getMachineNode(SPU::ORv2i64_i64, dl, VecVT, Op0);
Scott Michel02d711b2008-12-30 23:28:25 +00001010
1011 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1012 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1013 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1014
1015 if (bytes > 0) {
1016 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001017 CurDAG->getMachineNode(SPU::ROTQMBYIv2i64, dl, VecVT,
1018 SDValue(VecOp0, 0),
1019 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001020 }
1021
1022 if (bits > 0) {
1023 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001024 CurDAG->getMachineNode(SPU::ROTQMBIIv2i64, dl, VecVT,
1025 SDValue((Shift != 0 ? Shift : VecOp0), 0),
1026 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001027 }
1028 } else {
1029 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +00001030 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1031 ShiftAmt,
1032 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001033 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +00001034 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1035 ShiftAmt,
1036 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001037
1038 // Ensure that the shift amounts are negated!
Dan Gohman602b0c82009-09-25 18:54:59 +00001039 Bytes = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1040 SDValue(Bytes, 0),
1041 CurDAG->getTargetConstant(0, ShiftAmtVT));
1042
1043 Bits = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1044 SDValue(Bits, 0),
Scott Michel02d711b2008-12-30 23:28:25 +00001045 CurDAG->getTargetConstant(0, ShiftAmtVT));
1046
Scott Michel02d711b2008-12-30 23:28:25 +00001047 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001048 CurDAG->getMachineNode(SPU::ROTQMBYv2i64, dl, VecVT,
1049 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001050 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001051 CurDAG->getMachineNode(SPU::ROTQMBIv2i64, dl, VecVT,
1052 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001053 }
1054
Dan Gohman602b0c82009-09-25 18:54:59 +00001055 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001056}
1057
1058/*!
1059 * Emit the instruction sequence for i64 arithmetic right shifts.
1060 *
1061 * @param Op The shl operand
1062 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1063 * makes life easier.)
1064 * @return The SDNode with the entire instruction sequence
1065 */
1066SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001067SPUDAGToDAGISel::SelectSRAi64(SDNode *N, EVT OpVT) {
Scott Michel02d711b2008-12-30 23:28:25 +00001068 // Promote Op0 to vector
Owen Anderson23b9b192009-08-12 00:36:31 +00001069 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
1070 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001071 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001072 EVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001073 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +00001074
1075 SDNode *VecOp0 =
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001076 CurDAG->getMachineNode(SPU::ORv2i64_i64, dl, VecVT, N->getOperand(0));
Scott Michel02d711b2008-12-30 23:28:25 +00001077
1078 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1079 SDNode *SignRot =
Dan Gohman602b0c82009-09-25 18:54:59 +00001080 CurDAG->getMachineNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
1081 SDValue(VecOp0, 0), SignRotAmt);
Scott Michel02d711b2008-12-30 23:28:25 +00001082 SDNode *UpperHalfSign =
Dan Gohman602b0c82009-09-25 18:54:59 +00001083 CurDAG->getMachineNode(SPU::ORi32_v4i32, dl, MVT::i32, SDValue(SignRot, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001084
1085 SDNode *UpperHalfSignMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001086 CurDAG->getMachineNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001087 SDNode *UpperLowerMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001088 CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT,
1089 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
Scott Michel02d711b2008-12-30 23:28:25 +00001090 SDNode *UpperLowerSelect =
Dan Gohman602b0c82009-09-25 18:54:59 +00001091 CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
1092 SDValue(UpperHalfSignMask, 0),
1093 SDValue(VecOp0, 0),
1094 SDValue(UpperLowerMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001095
1096 SDNode *Shift = 0;
1097
1098 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1099 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1100 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1101
1102 if (bytes > 0) {
1103 bytes = 31 - bytes;
1104 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001105 CurDAG->getMachineNode(SPU::ROTQBYIv2i64, dl, VecVT,
1106 SDValue(UpperLowerSelect, 0),
1107 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001108 }
1109
1110 if (bits > 0) {
1111 bits = 8 - bits;
1112 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001113 CurDAG->getMachineNode(SPU::ROTQBIIv2i64, dl, VecVT,
1114 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1115 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001116 }
1117 } else {
1118 SDNode *NegShift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001119 CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1120 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001121
1122 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001123 CurDAG->getMachineNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
1124 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001125 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001126 CurDAG->getMachineNode(SPU::ROTQBIv2i64, dl, VecVT,
1127 SDValue(Shift, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001128 }
1129
Dan Gohman602b0c82009-09-25 18:54:59 +00001130 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001131}
1132
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001133/*!
1134 Do the necessary magic necessary to load a i64 constant
1135 */
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001136SDNode *SPUDAGToDAGISel::SelectI64Constant(SDNode *N, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001137 DebugLoc dl) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001138 ConstantSDNode *CN = cast<ConstantSDNode>(N);
Scott Michel7ea02ff2009-03-17 01:15:45 +00001139 return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
1140}
1141
Owen Andersone50ed302009-08-10 22:56:29 +00001142SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001143 DebugLoc dl) {
Owen Anderson23b9b192009-08-12 00:36:31 +00001144 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001145 SDValue i64vec =
Scott Michel7ea02ff2009-03-17 01:15:45 +00001146 SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001147
1148 // Here's where it gets interesting, because we have to parse out the
1149 // subtree handed back in i64vec:
1150
1151 if (i64vec.getOpcode() == ISD::BIT_CONVERT) {
1152 // The degenerate case where the upper and lower bits in the splat are
1153 // identical:
1154 SDValue Op0 = i64vec.getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001155
Scott Michel9de57a92009-01-26 22:33:37 +00001156 ReplaceUses(i64vec, Op0);
Dan Gohman602b0c82009-09-25 18:54:59 +00001157 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001158 SDValue(emitBuildVector(Op0.getNode()), 0));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001159 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1160 SDValue lhs = i64vec.getOperand(0);
1161 SDValue rhs = i64vec.getOperand(1);
1162 SDValue shufmask = i64vec.getOperand(2);
1163
1164 if (lhs.getOpcode() == ISD::BIT_CONVERT) {
1165 ReplaceUses(lhs, lhs.getOperand(0));
1166 lhs = lhs.getOperand(0);
1167 }
1168
1169 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1170 ? lhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001171 : emitBuildVector(lhs.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001172
1173 if (rhs.getOpcode() == ISD::BIT_CONVERT) {
1174 ReplaceUses(rhs, rhs.getOperand(0));
1175 rhs = rhs.getOperand(0);
1176 }
1177
1178 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1179 ? rhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001180 : emitBuildVector(rhs.getNode()));
Scott Michel9de57a92009-01-26 22:33:37 +00001181
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001182 if (shufmask.getOpcode() == ISD::BIT_CONVERT) {
1183 ReplaceUses(shufmask, shufmask.getOperand(0));
1184 shufmask = shufmask.getOperand(0);
1185 }
1186
1187 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1188 ? shufmask.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001189 : emitBuildVector(shufmask.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001190
Chris Lattnera8e76142010-02-23 05:30:43 +00001191 SDValue shufNode =
1192 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001193 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
Chris Lattnera8e76142010-02-23 05:30:43 +00001194 SDValue(shufMaskNode, 0));
1195 HandleSDNode Dummy(shufNode);
1196 SDNode *SN = SelectCode(Dummy.getValue().getNode());
1197 if (SN == 0) SN = Dummy.getValue().getNode();
1198
1199 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(SN, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +00001200 } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman602b0c82009-09-25 18:54:59 +00001201 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001202 SDValue(emitBuildVector(i64vec.getNode()), 0));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001203 } else {
Chris Lattner75361b62010-04-07 22:58:41 +00001204 report_fatal_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
Torok Edwindac237e2009-07-08 20:53:28 +00001205 "condition");
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001206 }
1207}
1208
Scott Michel02d711b2008-12-30 23:28:25 +00001209/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001210/// SPU-specific DAG, ready for instruction scheduling.
1211///
1212FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1213 return new SPUDAGToDAGISel(TM);
1214}